Mercurial > repos > bernhardlutz > rest_tool
comparison rest_tool_advanced.py @ 9:80bf0039c0dc draft
Uploaded
author | bernhardlutz |
---|---|
date | Fri, 02 May 2014 18:16:36 -0400 |
parents | 3c1e862e8cd6 |
children |
comparison
equal
deleted
inserted
replaced
8:3c1e862e8cd6 | 9:80bf0039c0dc |
---|---|
5 | 5 |
6 import readfile | 6 import readfile |
7 | 7 |
8 #dicitionary for the output format | 8 #dicitionary for the output format |
9 | 9 |
10 dict_output={"cids" :"txt", "aids" : "txt", "sids" : "txt", "description": "xml", "summary" : "xml", "record" : "csv", "classification": "xml", "targets" : "txt", "xrefs" : "txt", "synonyms" : "txt", "property": "csv" } | 10 dict_output={"cids" :"txt", "aids" : "txt", "sids" : "txt", "description": "xml", "summary" : "xml", "record" : "csv", "classification": "xml", "targets" : "txt", "xrefs" : "txt", "synonyms" : "txt", "property": "csv", "doseresponse" : "csv" } |
11 | 11 |
12 #alles andere ist xml | 12 #alles andere ist xml |
13 check_for_id_type=["cids", "aids", "sids"] | 13 check_for_id_type=["cids", "aids", "sids"] |
14 | |
15 post_id_types=["inchi", "sdf", "smiles"] | |
14 | 16 |
15 id_dict={"compound": "cid", "assay": "aid", "substance" : "sid" } | 17 id_dict={"compound": "cid", "assay": "aid", "substance" : "sid" } |
16 | 18 |
17 def getListString(args): | 19 def getListString(args): |
18 if args.id_type_ff == "file": | 20 if args.id_type_ff == "file": |
19 #build comma list | 21 #build comma list |
20 list_string=",".join(getListFromFile(open(args.id_value,"r"))) | 22 list_string=",".join(getListFromFile(open(args.id_value,"r"))) |
21 else: | 23 else: |
22 list_string=args.id_value | 24 print (args.id_value) |
25 list_string=args.id_value.strip().replace("__cr____cn__", ",") | |
23 return list_string | 26 return list_string |
24 | 27 |
25 | 28 |
26 def main(args): | 29 def main(args): |
27 | |
28 url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/" | 30 url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/" |
29 url+=args.id_type+"/" | 31 url+=args.id_type+"/" |
30 if args.id_type ==id_dict[args.type]: | 32 # check if we are post then skip this part otherwise put the ids in the url |
31 url+=getListString(args)+"/" | 33 if not args.id_type in post_id_types: |
32 else: | 34 if args.id_type ==id_dict[args.type]: |
33 url+=args.id_value+"/" | 35 url+=getListString(args)+"/" |
36 else: | |
37 url+=args.id_value+"/" | |
38 | |
34 url+=args.operation+"/" | 39 url+=args.operation+"/" |
35 if args.operation == "target" or args.operation == "property" or args.operation == "xrefs": | 40 if args.operation == "target" or args.operation == "property" or args.operation == "xrefs": |
36 url+=args.operation_value+"/" | 41 url+=args.operation_value+"/" |
37 | 42 |
38 url+=dict_output[args.operation] | 43 if args.operation == "xrefs": |
39 if args.operation in check_for_id_type: | 44 if "," in args.operation_value: |
40 url+="?"+args.operation+"_type="+args.ids_operation_type | 45 url+="xml" |
46 else: | |
47 url+="txt" | |
48 else: | |
49 url+=dict_output[args.operation] | |
50 if args.operation in check_for_id_type and args.id_type not in post_id_types: | |
51 url+="?%s_type=%s" % (args.operation, args.ids_operation_type) | |
41 print(url) | 52 print(url) |
42 readfile.store_result(url, args.outfile) | 53 if args.id_type in post_id_types: |
43 | 54 postfile=open(args.id_value,"r") |
44 | 55 post_value=postfile.read() |
45 | 56 post_dict={args.id_type : post_value} |
46 | 57 print(post_dict) |
58 readfile.store_result_post(url, post_dict, args.outfile) | |
59 else: | |
60 readfile.store_result_get(url, args.outfile) | |
47 if __name__ == "__main__": | 61 if __name__ == "__main__": |
48 parser = argparse.ArgumentParser() | 62 parser = argparse.ArgumentParser() |
49 parser.add_argument('--type', type=str, required=True, | 63 parser.add_argument('--type', type=str, required=True, |
50 help="That you want BioAssay Compund ...") | 64 help="That you want BioAssay Compund ...") |
51 parser.add_argument('--id-type', type=str, | 65 parser.add_argument('--id-type', type=str, |