diff rest_tool_advanced.py @ 9:80bf0039c0dc draft

Uploaded
author bernhardlutz
date Fri, 02 May 2014 18:16:36 -0400
parents 3c1e862e8cd6
children
line wrap: on
line diff
--- a/rest_tool_advanced.py	Fri May 02 05:40:28 2014 -0400
+++ b/rest_tool_advanced.py	Fri May 02 18:16:36 2014 -0400
@@ -7,11 +7,13 @@
 
 #dicitionary for the output format
 
-dict_output={"cids" :"txt", "aids" : "txt", "sids" : "txt", "description": "xml", "summary" : "xml", "record" : "csv", "classification": "xml", "targets" : "txt", "xrefs" : "txt", "synonyms" : "txt", "property": "csv" }
+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" }
 
 #alles andere ist xml
 check_for_id_type=["cids", "aids", "sids"]
 
+post_id_types=["inchi", "sdf", "smiles"]
+
 id_dict={"compound": "cid", "assay": "aid", "substance" : "sid" }
 
 def getListString(args):
@@ -19,31 +21,43 @@
         #build comma list
         list_string=",".join(getListFromFile(open(args.id_value,"r")))
     else:
-        list_string=args.id_value
+        print (args.id_value)
+        list_string=args.id_value.strip().replace("__cr____cn__", ",")
     return list_string
 
-
+    
 def main(args):
-
     url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/"
     url+=args.id_type+"/"
-    if args.id_type ==id_dict[args.type]:
-        url+=getListString(args)+"/"
-    else:
-        url+=args.id_value+"/"
+    # check if we are post then skip this part otherwise put the ids in the url
+    if not args.id_type in post_id_types:
+        if args.id_type ==id_dict[args.type]:
+            url+=getListString(args)+"/"
+        else:
+            url+=args.id_value+"/"
+
     url+=args.operation+"/"
     if args.operation == "target" or args.operation == "property" or args.operation == "xrefs":
         url+=args.operation_value+"/"
     
-    url+=dict_output[args.operation]
-    if args.operation in check_for_id_type:
-            url+="?"+args.operation+"_type="+args.ids_operation_type
+    if args.operation == "xrefs":
+        if "," in args.operation_value:
+            url+="xml"
+        else:
+            url+="txt"
+    else:
+        url+=dict_output[args.operation]
+    if args.operation in check_for_id_type and args.id_type not in post_id_types:
+            url+="?%s_type=%s" % (args.operation, args.ids_operation_type)
     print(url)
-    readfile.store_result(url, args.outfile)
-    
-    
-    
-    
+    if args.id_type in post_id_types:
+        postfile=open(args.id_value,"r")
+        post_value=postfile.read()
+        post_dict={args.id_type : post_value}
+        print(post_dict)
+        readfile.store_result_post(url, post_dict, args.outfile)
+    else:
+        readfile.store_result_get(url, args.outfile)
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
     parser.add_argument('--type', type=str, required=True,