Mercurial > repos > bernhardlutz > rest_tool
comparison rest_tool_comp_for_assay.py @ 2:d3f9c70919dc draft
Uploaded
author | bernhardlutz |
---|---|
date | Mon, 24 Mar 2014 14:00:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:954a489c1606 | 2:d3f9c70919dc |
---|---|
1 #!/usr/bin/env python | |
2 # Aufruf convert_graph.py --aid list of ids --aid-from-file file | |
3 | |
4 import sys, os | |
5 import networkx as nx | |
6 import argparse | |
7 import urllib2, urllib, httplib | |
8 | |
9 #supported graph_types | |
10 #output_types = ["tsv", "csv", "png", "json", "txt", "xml", "sdf", "asnt", "asnb", "jsonp"] | |
11 | |
12 | |
13 def getresult(url): | |
14 try: | |
15 connection = urllib2.urlopen(url) | |
16 except urllib2.HTTPError, e: | |
17 return "" | |
18 else: | |
19 return connection.read().rstrip() | |
20 | |
21 | |
22 def getresultPost(url, aidList): | |
23 values={'aids': ",".join(aidList)} | |
24 data=urllib.urlencode(values) | |
25 print(data) | |
26 | |
27 headers = {"Content-type": "application/x-www-form-urlencoded"} | |
28 conn = httplib.HTTPConnection("pubchem.ncbi.nlm.nih.gov") | |
29 conn.request("POST", "/rest/pug/assay/aid/aids/csv", data, headers) | |
30 response = conn.getresponse() | |
31 conn.close() | |
32 return response.read() | |
33 | |
34 | |
35 | |
36 #get the cids for bioassay aid | |
37 def getCompoundList(aid): | |
38 url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/assay/aid/"+aid+"/cids/txt" | |
39 data=getresult(url) | |
40 return data | |
41 | |
42 def main(args): | |
43 cids=getCompoundList(args.aid) | |
44 args.outfile.write(cids) | |
45 args.outfile.close() | |
46 if __name__ == "__main__": | |
47 parser = argparse.ArgumentParser() | |
48 parser.add_argument('--aid', type=str, | |
49 help="AIDs of the BioAssay") | |
50 parser.add_argument('--aid-from-file', type=argparse.FileType('r'), | |
51 help="Specify a file with a list of aids, one per line") | |
52 parser.add_argument('--outfile', type=argparse.FileType('w'), | |
53 help="Specify output file") | |
54 if len(sys.argv) < 2: | |
55 print "Too few arguments..." | |
56 parser.print_help() | |
57 exit(1) | |
58 args = parser.parse_args() | |
59 main( args ) |