comparison rest_tool.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 --type type --operation op --id id --outformat format --outfile outfile
3
4 import sys, os
5 import networkx as nx
6 import argparse
7 import urllib2
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 def main(args):
21 url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/"
22 if args.type == "assay":
23 url+="aid/"
24 elif args.type == "compound":
25 url+="cid/"
26 url+=args.id+"/"+args.operation+"/"+args.outformat
27 print(url)
28 print(args.type)
29 data=getresult(url)
30 file=args.outfile
31 file.write(data)
32 file.close()
33
34 if __name__ == "__main__":
35 parser = argparse.ArgumentParser()
36 parser.add_argument('--type', type=str,
37 help="That you want BioAssay Compund ...")
38 parser.add_argument('--id', type=str,
39 help="Specify the ID")
40 parser.add_argument('--operation', type=str,
41 help="Specify the operation")
42 parser.add_argument('--property-value', type=str,
43 help="Specify the property")
44 parser.add_argument('--outformat', type=str,
45 help="Specify the format of the output", choices = output_types)
46 parser.add_argument('--outfile', type=argparse.FileType('w'),
47 help="Specify one output file")
48 if len(sys.argv) < 8:
49 print "Too few arguments..."
50 parser.print_help()
51 exit(1)
52 args = parser.parse_args()
53 main( args )