-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapRPreader.py
89 lines (76 loc) · 2.9 KB
/
wrapRPreader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
#from io import BytesIO
#from contextlib import closing
#import time
#import tarfile
import libsbml
import argparse
import sys #exit using sys exit if any error is encountered
import os
import sys
import io
import zipfile
sys.path.insert(0, '/home/src/')
import rpFBA
## Function that wraps the SBML files into a zip file to be passed to the next galaxy node
#
'''
def writerpSBMLtar(rpsbml_paths, outTar):
with tarfile.open(outTar, 'w:xz') as tf:
for rpsbml_name in rpsbml_paths:
data = libsbml.writeSBMLToString(rpsbml_paths[rpsbml_name].document).encode('utf-8')
fiOut = BytesIO(data)
info = tarfile.TarInfo(rpsbml_name)
info.size = len(data)
tf.addfile(tarinfo=info, fileobj=fiOut)
'''
def writerpSBMLzip(rpsbml_paths, outZip):
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, mode="a", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zip_file:
for rpsbml_name in rpsbml_paths:
data = libsbml.writeSBMLToString(rpsbml_paths[rpsbml_name].document).encode('utf-8')
data = io.BytesIO(bytes(data))
zip_file.writestr(rpsbml_name, data.getvalue())
with open(outZip, 'wb') as f:
f.write(zip_buffer.getvalue())
'''
def writerpSBMLzip(rpsbml_paths, outZip):
zf = ZipFile(in_memory, mode="w")
for rpsbml_name in rpsbml_paths:
data = libsbml.writeSBMLToString(rpsbml_paths[rpsbml_name].document).encode('utf-8')
in_memory = BytesIO(data)
zf.writestr(data, file_data)
#Close the zip file
zf.close()
#Go to beginning
in_memory.seek(0)
#read the data
data = in_memory.read()
#You can save it to disk
with open(outZip,'wb') as out:
out.write(data)
'''
if __name__ == "__main__":
parser = argparse.ArgumentParser('Python wrapper to read the output of rp2paths into SBML files including their cofactors')
parser.add_argument('-rp2paths_compounds', type=str)
parser.add_argument('-rp2paths_outPaths', type=str)
parser.add_argument('-rp2paths_scope', type=str)
#parser.add_argument('-model_compartments', type=str)
#parser.add_argument('-model_sink', type=str)
#parser.add_argument('-outSBMLtar', type=str)
parser.add_argument('-outSBMLzip', type=str)
params = parser.parse_args()
#parse the files into the reader
rpreader = rpFBA.rpReader()
rpreader.compounds(params.rp2paths_compounds)
rpreader.transformation(params.rp2paths_scope)
#rpreader.compartments(params.model_compartments)
#rpreader.chemicals(params.model_sink)
rpreader.outPaths(params.rp2paths_outPaths)
#add the cofactors to the parsed
rpcofactors = rpFBA.rpCofactors(rpreader)
rpcofactors.pathsToSBML()
#package the rpsbml's for the next pathway
#writerpSBMLtar(rpcofactors.sbml_paths, params.outSBMLtar)
writerpSBMLzip(rpcofactors.sbml_paths, params.outSBMLzip)
exit(0)