Mercurial > repos > bernhardlutz > rest_tool
changeset 9:80bf0039c0dc draft
Uploaded
author | bernhardlutz |
---|---|
date | Fri, 02 May 2014 18:16:36 -0400 |
parents | 3c1e862e8cd6 |
children | a76d64d2ed44 |
files | readfile.py rest_tool.xml rest_tool_advanced.py rest_tool_macro_advanced.xml rest_tool_macros.xml rest_tool_search.py test-data/empty-file test-data/postfile tool_dependencies.xml |
diffstat | 8 files changed, 130 insertions(+), 65 deletions(-) [+] |
line wrap: on
line diff
--- a/readfile.py Fri May 02 05:40:28 2014 -0400 +++ b/readfile.py Fri May 02 18:16:36 2014 -0400 @@ -17,7 +17,16 @@ else: return connection.read().rstrip() -def store_result(url, outfile): +def store_result_get(url, outfile): data=getresult(url) outfile.write(data) outfile.close() + +def store_result_post(url, post, outfile): + data = urllib.urlencode(post) + headers={"Content-Type" : "application/x-www-form-urlencoded"} + req = urllib2.Request(url, data, headers) + response = urllib2.urlopen(req) + the_page = response.read() + outfile.write(the_page) + outfile.close()
--- a/rest_tool.xml Fri May 02 05:40:28 2014 -0400 +++ b/rest_tool.xml Fri May 02 18:16:36 2014 -0400 @@ -68,7 +68,7 @@ --id-type-ff $choose_action.input_type.namespace_id.field_or_file.field_or_file_select --id-value $choose_action.input_type.namespace_id.field_or_file.id_value #elif $choose_action.input_type.namespace_id.namespace_id_select == 'target': - --id-value $choose_action.input_type.namespace_id.target_select + --id-value $choose_action.input_type.namespace_id.target_select"/"$choose_action.input_type.namespace_id.target_value #elif $choose_action.input_type.namespace_id.namespace_id_select == 'type': --id-value $choose_action.input_type.namespace_id.type_select #elif $choose_action.input_type.namespace_id.namespace_id_select == 'activity': @@ -81,6 +81,10 @@ --operation-value $choose_action.input_type.operation_assay.operation_assay_select.property_assay #elif $choose_action.input_type.operation_assay.operation_assay_select == 'target': --operation-value $choose_action.input_type.operation_assay.operation_assay_select.target_assay + #elif $choose_action.input_type.operation_assay.operation_assay_select == 'xrefs': + --operation-value $choose_action.input_type.operation_assay.xref_operation + #elif $choose_action.input_type.operation_assay.operation_assay_select == 'doseresponse': + --sid-filter $choose_action.input_type.operation_assay.doseresponse_sids #elif $choose_action.input_type.operation_assay.operation_assay_select == 'aids' or $choose_action.input_type.operation_assay.operation_assay_select == 'cids' or $choose_action.input_type.operation_assay.operation_assay_select == 'sids': --ids-operation-type $choose_action.input_type.operation_assay.ids_type_assay #end if @@ -97,7 +101,7 @@ #if $choose_action.input_type.operation_compound.operation_compound_select == 'property': --operation-value $choose_action.input_type.operation_compound.property_compound #elif $choose_action.input_type.operation_compound.operation_compound_select == 'xrefs': - --operation-value $choose_action.input_type.operation_compound.xref_compound + --operation-value $choose_action.input_type.operation_compound.xref_operation #elif $choose_action.input_type.operation_compound.operation_compound_select == 'aids' or $choose_action.input_type.operation_compound.operation_compound_select == 'cids' or $choose_action.input_type.operation_compound.operation_compound_select == 'sids': --ids-operation-type $choose_action.input_type.operation_compound.ids_type_compound #end if @@ -114,7 +118,7 @@ #if $choose_action.input_type.operation_substance.operation_substance_select == 'property': --operation-value $choose_action.input_type.operation_substance.operation_substance_select.property_substance #elif $choose_action.input_type.operation_substance.operation_substance_select == 'xref': - --operation-value $choose_action.input_type.operation_substance.xref_substance + --operation-value $choose_action.input_type.operation_substance.xref_operation #elif $choose_action.input_type.operation_substance.operation_substance_select == 'aids' or $choose_action.input_type.operation_substance.operation_substance_select == 'cids' or $choose_action.input_type.operation_substance.operation_substance_select == 'sids': --ids-operation-type $choose_action.input_type.operation_substance.ids_type_substance #end if @@ -188,9 +192,78 @@ </conditional> </inputs> <outputs> - <data format="csv" name="output" /> + <data format="tabular" name="output"> + <change_format> + <when input="choose_action.input_type.operation_assay.operation_assay_select" value="summary" format="xml" /> + <when input="choose_action.input_type.operation_assay.operation_assay_select" value="description" format="xml" /> + <when input="choose_action.input_type.operation_assay.operation_assay_select" value="classification" format="xml" /> + <when input="choose_action.input_type.operation_substance.operation_substance_select" value="summary" format="xml" /> + <when input="choose_action.input_type.operation_substance.operation_substance_select" value="description" format="xml" /> + <when input="choose_action.input_type.operation_substance.operation_substance_select" value="classification" format="xml" /> + <when input="choose_action.input_type.operation_compound.operation_compound_select" value="summary" format="xml" /> + <when input="choose_action.input_type.operation_compound.operation_compound_select" value="description" format="xml" /> + <when input="choose_action.input_type.operation_compound.operation_compound_select" value="classification" format="xml" /> + </change_format> + </data> </outputs> <tests> + <test> + <output name="output"> + <assert_contents> + <has_n_columns n="1" /> + <has_line_matching expression="[0-9]+" /> + </assert_contents> + </output> + <param name="choose_action.action" value="advanced" /> + <param name="choose_action.input_type.input_type_select" value="assay" /> + <param name="choose_action.input_type.namespace_id.namespace_id_select" value="target" /> + <param name="choose_action.input_type.namespace_id.target_select" value="genesymbol" /> + <param name="choose_action.input_type.namespace_id.target_value" value="usp2" /> + <param name="choose_action.input_type.operation_assay.operation_assay_select" value="aids" /> + </test> + <test> + <output name="output2"> + <assert_contents> + <has_n_columns n="1" /> + <has_line_matching expression="[0-9]+" /> + </assert_contents> + </output> + <param name="choose_action.action" value="advanced" /> + <param name="choose_action.input_type.input_type_select" value="substance" /> + <param name="choose_action.input_type.namespace_id.namespace_id_select" value="xref" /> + <param name="choose_action.input_type.namespace_id.xref_select" value="PatentID" /> + <param name="choose_action.input_type.namespace_id.xref_value" value="EP0711162A1" /> + <param name="choose_action.input_type.operation_substance.operation_substance_select_select" value="cids" /> + </test> + <!-- post test --> + <test> + <output name="output3"> + <assert_contents> + <has_n_columns n="1" /> + <has_line_matching expression="[0-9]+" /> + </assert_contents> + </output> + <param name="choose_action.action" value="advanced" /> + <param name="choose_action.input_type.input_type_select" value="compound" /> + <param name="choose_action.input_type.namespace_id.namespace_id_select" value="inchi" /> + <param name="choose_action.input_type.namespace_id.field_or_file.field_or_file_select" value="file" /> + <param name="choose_action.input_type.namespace_id.field_or_file.id_value" value="postfile" /> + <param name="choose_action.input_type.operation_substance.operation_substance_select_select" value="cids" /> + </test> + <test> + <output name="output4"> + <assert_contents> + <has_n_columns n="1" /> + <has_line_matching expression="[0-9]+" /> + </assert_contents> + </output> + <param name="choose_action.action" value="compounds_for_assay" /> + <param name="choose_action.input_type.input_type_select" value="compound" /> + <param name="choose_action.input_type.namespace_id.namespace_id_select" value="inchi" /> + <param name="choose_action.input_type.namespace_id.field_or_file.field_or_file_select" value="file" /> + <param name="choose_action.input_type.namespace_id.field_or_file.id_value" value="postfile" /> + <param name="choose_action.input_type.operation_substance.operation_substance_select_select" value="cids" /> + </test> </tests> <help> **What it does**
--- 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,
--- a/rest_tool_macro_advanced.xml Fri May 02 05:40:28 2014 -0400 +++ b/rest_tool_macro_advanced.xml Fri May 02 18:16:36 2014 -0400 @@ -33,6 +33,8 @@ <option value="geneid">geneid</option> <option value="genesymbol">genesymbol</option> </param> + <param name="target_value" multiple="false" type="text" label="Enter target" /> + </when> <when value="activity"> <!-- known activity: ic50, ec50, ac50, gi50 -->
--- a/rest_tool_macros.xml Fri May 02 05:40:28 2014 -0400 +++ b/rest_tool_macros.xml Fri May 02 18:16:36 2014 -0400 @@ -155,7 +155,7 @@ <expand macro="compound_properties" /> </when> <when value="xrefs"> - <param name="xref_compound" label="Choose Xrefs" multiple="true" type="select"> + <param name="xref_operation" label="Choose Xrefs" multiple="true" type="select"> <expand macro="xref_options" /> </param> </when> @@ -184,11 +184,17 @@ <option value="classification">Classification</option> <option value="xrefs">Xrefs</option> <option value="description">Discription</option> + <option value="doseresponse">Doseresponse</option> <option value="summary">Summary</option> <option value="targets">Targets</option> </param> <when value="xrefs"> - <param name="property_assay" label="Specify Property" type="text" /> + <param name="xref_operation" label="Choose Xrefs" multiple="true" type="select"> + <expand macro="xref_options" /> + </param> + </when> + <when value="doseresponse"> + <param name="doseresponse_sids" label="Filter by SIDs (comma separated)" type="text" /> </when> <when value="targets"> @@ -226,7 +232,7 @@ </param> <when value="xrefs"> - <param name="xref_substance" label="Choose Xrefs" multiple="true" type="select"> + <param name="xref_operation" label="Choose Xrefs" multiple="true" type="select"> <expand macro="xref_options" /> </param> </when>
--- a/rest_tool_search.py Fri May 02 05:40:28 2014 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -#!/usr/bin/env python - -import sys, os -import argparse -import readfile - -def main(args): - #search for acitivity or target - url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/name/"+args.name - if args.type == "assay": - url+="/aids" - elif args.type == "compound": - url+="/cids" - else: - url+="/sids" - url+="/txt" - #print("url: "+url) - data=readfile.getresult(url) - args.outfile.write(data) - args.outfile.close() - - -if __name__ == "__main__": - parser = argparse.ArgumentParser() - parser.add_argument('--name', type=str, required=True, - help="Enter the name") - parser.add_argument('--type', type=str, required=True, - help="What you want to search for") - parser.add_argument('--outfile', type=argparse.FileType('w'), required=True, - help="Specify output file") - if len(sys.argv) < 2: - print "Too few arguments..." - parser.print_help() - exit(1) - args = parser.parse_args() - main( args )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/postfile Fri May 02 18:16:36 2014 -0400 @@ -0,0 +1,2 @@ +InChI=1S/C3H8/c1-3-2/h3H2,1-2H3 +
--- a/tool_dependencies.xml Fri May 02 05:40:28 2014 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -<tool_dependency> - <set_environment version="1.0"> - <environment_variable name="REST_TOOL_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable> - </set_environment> -</tool_dependency>