diff toolRPViz.py @ 27:b30e3e5ee8f8 draft

planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
author pablocarb
date Fri, 02 Aug 2019 05:17:33 -0400
parents cb029043c1d6
children d854b21ada0c
line wrap: on
line diff
--- a/toolRPViz.py	Fri Jul 05 17:29:50 2019 -0400
+++ b/toolRPViz.py	Fri Aug 02 05:17:33 2019 -0400
@@ -12,6 +12,7 @@
 import csv
 import os
 import json
+import tarfile
 
 def arguments():
     parser = argparse.ArgumentParser(description='toolRPViz: Pathway visualizer. Pablo Carbonell, SYNBIOCHEM, 2019')
@@ -19,6 +20,15 @@
                         help='Pathways in SBML format.')
     parser.add_argument('outfile', 
                         help='HTML visualizer file.')
+    parser.add_argument('--input_format', default='sbml',
+                        help='Input format: sbml|json')
+    parser.add_argument('--choice', default="5",
+                        help='What kind of output do you want ? \n 1/Single HTML file \n 2/Separated HTML files \n 3/View directly in Cytoscape \n 4/Generate a file readable in Cytoscape \n 5/Tar file')
+    parser.add_argument('--selenzyme_table',
+                        default="N",
+                        help='Do you want to display the selenzyme information ? Y/N')
+    parser.add_argument('--outfolder', 
+                        help='Location of additional files')
     parser.add_argument('-server', default='http://rpviz.synbiochem.co.uk/REST',
                         help='RPViz server.')
     return parser
@@ -28,18 +38,27 @@
     res = json.loads( r.content.decode('utf-8') )
     print( res )
     
-def pathwayUpload(infile, outfile, url):
-    files = { 'file': open(infile, 'rb' ) }
-    r = requests.post( os.path.join(url, 'Query' ), files=files )
-    res = json.loads( r.content.decode('utf-8') )
-    html = res['data']['html']
-    with open(outfile, 'w') as h:
-        h.write(html)
-    print( 'Success!')
+def pathwayUpload( arg ):
+    files = { 'file': open(arg.infile, 'rb' ) }
+    data = {'selenzyme_table': arg.selenzyme_table, 'input_format': arg.input_format}
+    print('Sending query to '+arg.server)
+    r=requests.post( arg.server+'/Query',files=files,data=data )
+    if arg.outfolder is None:
+        arg.outfolder = os.path.dirname( arg.outfile )
+    if not os.path.exists(outfolder):
+        os.mkdir(arg.outfolder)
+    outtar = os.path.join( arg.outfolder, 'out.tar' )
+    open(outtar,'wb').write( r.content )
+    print( 'Response successfully received' )
+    tar = tarfile.open(outtar)
+    tar.extractall(path=arg.outfolder)
+    html = os.path.join( arg.outfolder, 'index.html' )
+    shutil.cp( html, arg.outfile )  
+    print( 'Files extracted' )
 
 
 if __name__ == "__main__":
     parser = arguments()
     arg = parser.parse_args()
-    assert os.path.exists(arg.infile)
-    pathwayUpload( arg.infile, arg.outfile, arg.server )
+    assert os.path.exists( arg.infile )
+    pathwayUpload( arg )