annotate toolRPViz.py @ 27:b30e3e5ee8f8 draft

planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
author pablocarb
date Fri, 02 Aug 2019 05:17:33 -0400
parents cb029043c1d6
children d854b21ada0c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
1 #!/usr/bin/env python3
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
2 # -*- coding: utf-8 -*-
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
3 """
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
4 Created on Mar 19
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
5
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
6 @author: Pablo Carbonell
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
7 @description: Query RPViz: pathway visualizer.
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
8
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
9 """
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
10 import requests
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
11 import argparse
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
12 import csv
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
13 import os
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
14 import json
27
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
15 import tarfile
19
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
16
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
17 def arguments():
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
18 parser = argparse.ArgumentParser(description='toolRPViz: Pathway visualizer. Pablo Carbonell, SYNBIOCHEM, 2019')
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
19 parser.add_argument('infile',
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
20 help='Pathways in SBML format.')
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
21 parser.add_argument('outfile',
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
22 help='HTML visualizer file.')
27
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
23 parser.add_argument('--input_format', default='sbml',
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
24 help='Input format: sbml|json')
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
25 parser.add_argument('--choice', default="5",
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
26 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')
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
27 parser.add_argument('--selenzyme_table',
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
28 default="N",
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
29 help='Do you want to display the selenzyme information ? Y/N')
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
30 parser.add_argument('--outfolder',
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
31 help='Location of additional files')
19
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
32 parser.add_argument('-server', default='http://rpviz.synbiochem.co.uk/REST',
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
33 help='RPViz server.')
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
34 return parser
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
35
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
36 def testApp(url):
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
37 r = requests.get( url )
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
38 res = json.loads( r.content.decode('utf-8') )
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
39 print( res )
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
40
27
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
41 def pathwayUpload( arg ):
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
42 files = { 'file': open(arg.infile, 'rb' ) }
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
43 data = {'selenzyme_table': arg.selenzyme_table, 'input_format': arg.input_format}
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
44 print('Sending query to '+arg.server)
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
45 r=requests.post( arg.server+'/Query',files=files,data=data )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
46 if arg.outfolder is None:
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
47 arg.outfolder = os.path.dirname( arg.outfile )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
48 if not os.path.exists(outfolder):
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
49 os.mkdir(arg.outfolder)
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
50 outtar = os.path.join( arg.outfolder, 'out.tar' )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
51 open(outtar,'wb').write( r.content )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
52 print( 'Response successfully received' )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
53 tar = tarfile.open(outtar)
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
54 tar.extractall(path=arg.outfolder)
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
55 html = os.path.join( arg.outfolder, 'index.html' )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
56 shutil.cp( html, arg.outfile )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
57 print( 'Files extracted' )
19
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
58
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
59
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
60 if __name__ == "__main__":
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
61 parser = arguments()
dbc40b306014 planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5-dirty
pablocarb
parents:
diff changeset
62 arg = parser.parse_args()
27
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
63 assert os.path.exists( arg.infile )
b30e3e5ee8f8 planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05
pablocarb
parents: 21
diff changeset
64 pathwayUpload( arg )