<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Our list of targets
Step1:  Make the API call with our list of targets to find the associations. Set facets to true.
Step2:  Print out all the json returned just for reference
Step3:  The therapeutic area facets look interesting - lets iterate through these and display
Step4:  Sort by target count and then disease count
Step5:  Using the python tabulate library to render a pretty table of our extracted therapeutic areas.
Note
Step6:  Lets just consider the first 5 top therapeutic areas
Step7:  Now for each of those identify the top 5 diseases. Unfortunately we don't get the disease names in the facets, just the codes. Is this is the right approach then an API change???
<ASSISTANT_TASK:>
Python Code:
targets = ['ENSG00000069696', 'ENSG00000144285']
targets_string = ', '.join('"{0}"'.format(t) for t in targets)
Explanation: Our list of targets
End of explanation
url = 'https://www.targetvalidation.org/api/latest/public/association/filter'
headers = {"Accept": "application/json"}
# There may be an easier way of building these parameters...
data = "{\"target\":[" + targets_string + "], \"facets\":true}"
response = requests.post(url, headers=headers, data=data)
output = response.json()
Explanation: Make the API call with our list of targets to find the associations. Set facets to true.
End of explanation
#print json.dumps(output, indent=2)
Explanation: Print out all the json returned just for reference
End of explanation
therapeuticareas = []
for bucket in output['facets']['therapeutic_area']['buckets']:
    therapeuticareas.append({
            'target_count' : bucket['unique_target_count']['value'], 
            'disease_count' : bucket['unique_disease_count']['value'],
            'therapeutic_area' : bucket['label'],
            'key' : bucket['key']
        })
Explanation: The therapeutic area facets look interesting - lets iterate through these and display
End of explanation
therapeuticareas = sorted(therapeuticareas, key=lambda k: (k['target_count'],k['disease_count']), reverse=True) 
Explanation: Sort by target count and then disease count
End of explanation
print tabulate(therapeuticareas, headers="keys", tablefmt="grid")
Explanation: Using the python tabulate library to render a pretty table of our extracted therapeutic areas.
Note: You may need to run pip install tabulate in your python environment
End of explanation
therapeuticareas = therapeuticareas[:5]
print tabulate(therapeuticareas, headers="keys", tablefmt="grid")
Explanation: Lets just consider the first 5 top therapeutic areas
End of explanation
for therapeuticarea in therapeuticareas:
    print "Therapeutic area: " + therapeuticarea['therapeutic_area']
    data = "{\"target\":[" + targets_string + "], \"facets\":true, \"therapeutic_area\":[\"" + therapeuticarea['key'] + "\"]}"
    response = requests.post(url, headers=headers, data=data)
    output = response.json()
    
    diseases = []
    for bucket in output['facets']['disease']['buckets']:
        diseases.append({
            'target_count' : bucket['unique_target_count']['value'], 
            'doc_count' : bucket['doc_count'],
            'key' : bucket['key']
        })
    
    # Sort and take top 5
    diseases = sorted(diseases, key=lambda k: (k['target_count'],k['doc_count']), reverse=True) 
    diseases = diseases[:5]
    
    print tabulate(diseases, headers="keys", tablefmt="grid")
    print ""
Explanation: Now for each of those identify the top 5 diseases. Unfortunately we don't get the disease names in the facets, just the codes. Is this is the right approach then an API change???
End of explanation
<END_TASK>