annotate rest_tool_advanced.py @ 10:a76d64d2ed44 draft default tip

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