8
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import sys, os
|
|
4 import argparse
|
|
5
|
|
6 import readfile
|
|
7
|
|
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", "doseresponse" : "csv" }
|
8
|
11
|
|
12 #alles andere ist xml
|
|
13 check_for_id_type=["cids", "aids", "sids"]
|
|
14
|
9
|
15 post_id_types=["inchi", "sdf", "smiles"]
|
|
16
|
8
|
17 id_dict={"compound": "cid", "assay": "aid", "substance" : "sid" }
|
|
18
|
|
19 def getListString(args):
|
|
20 if args.id_type_ff == "file":
|
|
21 #build comma list
|
|
22 list_string=",".join(getListFromFile(open(args.id_value,"r")))
|
|
23 else:
|
9
|
24 print (args.id_value)
|
|
25 list_string=args.id_value.strip().replace("__cr____cn__", ",")
|
8
|
26 return list_string
|
|
27
|
9
|
28
|
8
|
29 def main(args):
|
|
30 url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/"
|
|
31 url+=args.id_type+"/"
|
9
|
32 # check if we are post then skip this part otherwise put the ids in the url
|
|
33 if not args.id_type in post_id_types:
|
|
34 if args.id_type ==id_dict[args.type]:
|
|
35 url+=getListString(args)+"/"
|
|
36 else:
|
|
37 url+=args.id_value+"/"
|
|
38
|
8
|
39 url+=args.operation+"/"
|
|
40 if args.operation == "target" or args.operation == "property" or args.operation == "xrefs":
|
|
41 url+=args.operation_value+"/"
|
|
42
|
9
|
43 if args.operation == "xrefs":
|
|
44 if "," in args.operation_value:
|
|
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)
|
8
|
52 print(url)
|
9
|
53 if args.id_type in post_id_types:
|
|
54 postfile=open(args.id_value,"r")
|
|
55 post_value=postfile.read()
|
|
56 post_dict={args.id_type : post_value}
|
|
57 print(post_dict)
|
|
58 readfile.store_result_post(url, post_dict, args.outfile)
|
|
59 else:
|
|
60 readfile.store_result_get(url, args.outfile)
|
8
|
61 if __name__ == "__main__":
|
|
62 parser = argparse.ArgumentParser()
|
|
63 parser.add_argument('--type', type=str, required=True,
|
|
64 help="That you want BioAssay Compund ...")
|
|
65 parser.add_argument('--id-type', type=str,
|
|
66 help="Specify the ID type")
|
|
67 parser.add_argument('--operation', type=str, required=True,
|
|
68 help="Specify the operation")
|
|
69 parser.add_argument('--operation-value', dest="operation_value", type=str, required=False,
|
|
70 help="Specify the additional operation value")
|
|
71 parser.add_argument('--xref-operation-value', dest="xref_operation_value", type=str, required=False,
|
|
72 help="Specify the xref operation ")
|
|
73 parser.add_argument('--ids-operation-type', dest="ids_operation_type", type=str, required=False,
|
|
74 help="all inactive ...")
|
|
75 parser.add_argument('--xref', dest="xref", type=str,
|
|
76 help="use xref to identify the searched thing")
|
|
77 parser.add_argument('--xref-value', dest="xref_value", type=str,
|
|
78 help="Specify the xref")
|
|
79 parser.add_argument('--property-value', dest="property_value", type=str,
|
|
80 help="Specify the property value")
|
|
81 parser.add_argument('--id-type-ff', dest="id_type_ff", type=str,
|
|
82 help="file or field")
|
|
83 parser.add_argument('--id-value', dest="id_value", type=str, required=True,
|
|
84 help="Specify the id")
|
|
85 parser.add_argument('--outfile', type=argparse.FileType('w'), required=True,
|
|
86 help="Specify the output file")
|
|
87
|
|
88
|
|
89 if len(sys.argv) < 8:
|
|
90 print "Too few arguments..."
|
|
91 parser.print_help()
|
|
92 exit(1)
|
|
93 args = parser.parse_args()
|
|
94 main( args )
|