changeset 2:d3f9c70919dc draft

Uploaded
author bernhardlutz
date Mon, 24 Mar 2014 14:00:06 -0400
parents 954a489c1606
children 0bbb107a2cf2
files rest_tool.py rest_tool.xml rest_tool_comp_for_assay.py tool_dependencies.xml
diffstat 4 files changed, 202 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rest_tool.py	Mon Mar 24 14:00:06 2014 -0400
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# Aufruf convert_graph.py --type type --operation op --id id --outformat format --outfile outfile
+
+import sys, os
+import networkx as nx
+import argparse
+import urllib2
+
+#supported graph_types
+output_types = ["tsv", "csv", "png", "json", "txt", "xml", "sdf", "asnt", "asnb", "jsonp"]
+
+
+def getresult(url):
+    try:
+        connection = urllib2.urlopen(url)
+    except urllib2.HTTPError, e:
+        return ""
+    else:
+        return connection.read().rstrip()
+def main(args):
+    url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/"
+    if args.type == "assay":
+        url+="aid/"
+    elif args.type == "compound":
+        url+="cid/"
+    url+=args.id+"/"+args.operation+"/"+args.outformat
+    print(url)
+    print(args.type)
+    data=getresult(url)
+    file=args.outfile
+    file.write(data)
+    file.close()
+    
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--type', type=str,
+        help="That you want BioAssay Compund ...")
+    parser.add_argument('--id', type=str,
+        help="Specify the ID")
+    parser.add_argument('--operation', type=str,
+        help="Specify the operation")
+    parser.add_argument('--property-value', type=str,
+        help="Specify the property")
+    parser.add_argument('--outformat', type=str,
+        help="Specify the format of the output", choices = output_types)
+    parser.add_argument('--outfile', type=argparse.FileType('w'),
+        help="Specify one output file")
+    if len(sys.argv) < 8:
+        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/rest_tool.xml	Mon Mar 24 14:00:06 2014 -0400
@@ -0,0 +1,85 @@
+<tool id="rest_tool" name="Fetch Data from pubchem" version="0.1.0">
+    <description>Fetch pubchem data</description>
+    <version_command>echo "0.1.0"</version_command>
+    <requirements>
+        <requirement type="set_environment">REST_TOOL_SCRIPT_PATH</requirement>
+    </requirements>
+    <command interpreter="python">
+        #if $choose_action.action == 'specific_data':
+        rest_tool.py 
+            --id $choose_action.id 
+            --type $choose_action.input_type 
+            --outformat $choose_action.output_format 
+            
+            --operation $choose_action.operation_property.operation
+            #if $choose_action.operation_property.operation == 'property':
+                --property-value $choose_action.operation_property.property
+            #end if
+            
+            --outfile $output
+        #elif $choose_action.action == 'compounds_for_assay':
+            rest_tool_comp_for_assay.py --aid $choose_action.aid --outfile $output
+        #end if
+    </command>
+
+    <inputs>
+        <conditional name="choose_action">
+            <param name="action" multiple="false" type="select" label="Choose action">
+                <option value="specific_data">Get Data for specific Assay/Compound/Substance</option>
+                <option value="compounds_for_assay">Get all compound IDs to given BioAssays</option>
+                <option value="all_assay_with_compounds">Get BioAssay IDs with Compound IDs</option>
+            </param>
+            <when value="specific_data">
+                <param name="input_type" multiple="false" type="select" label="Input Type">
+                    <option value="assay">BioAssay</option>
+                    <option value="compound">Compound</option>
+                    <option value="substance">Substance</option>
+                </param>
+                <param format="txt" name="id" type="text" label="Specify the ID" />
+                <conditional name="operation_property">
+                    <param name="operation" type="select" multiple="false" label="Operation">
+                    <option value="synonyms">Synonyms</option>
+                    <option value="cids">CIDs</option>
+                    <option value="property">Property</option>
+                    <option value="xrefs">XRefs</option>
+                    <option value="description">Discription</option>
+                    <option value="summary">Summary</option>
+                    </param>
+                    <when value="property">
+                        <param name="property" label="Specify Property" type="text" />
+                    </when>
+                </conditional>
+                <param name="output_format" type="select" label="Output Format">
+                <option value="tsv">tsv</option>
+                <option value="csv">csv</option>
+                <option value="txt">txt</option>
+                <option value="png">png</option>
+                <option value="json">json</option>
+                <option value="xml">xml</option>
+                </param>
+            </when>
+            <when value="compounds_for_assay">
+                <param format="txt" name="aid" type="text" label="Specify the ID of BioAssay" />
+            </when>
+        
+        </conditional>
+    </inputs>
+    <outputs>
+        <data format="csv" name="output">
+            <change_format>
+                <when input="output_format" value="tsv" format="tsv" />
+                <when input="output_format" value="png" format="png" />
+                <when input="output_format" value="csv" format="csv" />
+                <when input="output_format" value="txt" format="txt" />
+                <when input="output_format" value="json" format="json" />
+            </change_format>
+        </data>
+    </outputs>
+    <tests>
+    </tests>
+    <help>
+**What it does**
+
+This tool fetches data from pubchem
+    </help>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rest_tool_comp_for_assay.py	Mon Mar 24 14:00:06 2014 -0400
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# Aufruf convert_graph.py --aid list of ids --aid-from-file file
+
+import sys, os
+import networkx as nx
+import argparse
+import urllib2, urllib, httplib
+
+#supported graph_types
+#output_types = ["tsv", "csv", "png", "json", "txt", "xml", "sdf", "asnt", "asnb", "jsonp"]
+
+
+def getresult(url):
+    try:
+        connection = urllib2.urlopen(url)
+    except urllib2.HTTPError, e:
+        return ""
+    else:
+        return connection.read().rstrip()
+        
+        
+def getresultPost(url, aidList):
+    values={'aids': ",".join(aidList)}
+    data=urllib.urlencode(values)
+    print(data)
+    
+    headers = {"Content-type": "application/x-www-form-urlencoded"}
+    conn = httplib.HTTPConnection("pubchem.ncbi.nlm.nih.gov")
+    conn.request("POST", "/rest/pug/assay/aid/aids/csv", data, headers)
+    response = conn.getresponse()
+    conn.close()
+    return response.read()
+
+
+        
+#get the cids for bioassay aid
+def getCompoundList(aid):
+    url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/assay/aid/"+aid+"/cids/txt"
+    data=getresult(url)
+    return data
+        
+def main(args):
+    cids=getCompoundList(args.aid)
+    args.outfile.write(cids)
+    args.outfile.close()
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--aid', type=str,
+        help="AIDs of the BioAssay")
+    parser.add_argument('--aid-from-file', type=argparse.FileType('r'),
+        help="Specify a file with a list of aids, one per line")
+    parser.add_argument('--outfile', type=argparse.FileType('w'),
+        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/tool_dependencies.xml	Mon Mar 24 14:00:06 2014 -0400
@@ -0,0 +1,5 @@
+<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>