-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyKnime.py
More file actions
125 lines (114 loc) · 5.7 KB
/
pyKnime.py
File metadata and controls
125 lines (114 loc) · 5.7 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
from __future__ import print_function
import subprocess
import argparse
import os
import sys
import csv
import glob
import shutil
# local paths
#KPATH = '/home/mdulac/knime_3.6.1/knime'
#RP_WORK_PATH = '/home/mdulac/Downloads/RetroPath2.0_docker_test/RetroPath2.0/RetroPath2.0.knwf'
#docker paths
KPATH = '/usr/local/knime/knime'
RP_WORK_PATH = '/home/src/RetroPath2.0.knwf'
#Debug function
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def main(dmin, dmax, maxSteps, sourcefile, sinkfile, rulesfile, topx, mwmax_source, mwmax_cof, outDir):
try:
#construct the knime command
knime_command = [KPATH,
'-nosplash',
'-nosave',
'-reset',
'--launcher.suppressErrors',
'-application',
'org.knime.product.KNIME_BATCH_APPLICATION',
'-workflowFile='+RP_WORK_PATH,
'-workflow.variable=input.dmin,"'+str(dmin)+'",int',
'-workflow.variable=input.dmax,"'+str(dmax)+'",int',
'-workflow.variable=input.max-steps,"'+str(maxSteps)+'",int',
'-workflow.variable=input.sourcefile,"'+str(sourcefile)+'",String',
'-workflow.variable=input.sinkfile,"'+str(sinkfile)+'",String',
'-workflow.variable=input.rulesfile,"'+str(rulesfile)+'",String',
'-workflow.variable=output.topx,"'+str(topx)+'",int',
'-workflow.variable=output.mwmax-source,"'+str(mwmax_source)+'",int',
'-workflow.variable=output.mwmax-cof,"'+str(mwmax_cof)+'",int',
'-workflow.variable=output.dir,"'+str(outDir)+'",String',
'-workflow.variable=output.solutionfile,"results.csv",String',
'-workflow.variable=output.sourceinsinkfile,"source-in-sink.csv",String']
#we make the knime call and supress the output since its has warning and errors and Galaxy doesn't like that
#FNULL = open(os.devnull, 'w')
#exit_code = subprocess.call(knime_command, stdout=FNULL, stderr=subprocess.STDOUT)
exit_code = subprocess.call(knime_command, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
#TODO: make sure that the sourceinsink file is empty
#with open(outDir+'/stdout.txt','wb') as out, open(outDir+'/stderr.txt','wb') as err:
# exit_code = subprocess.call(knime_command, stdout=out, stderr=err)
#FNULL = open(os.devnull, 'w')
#exit_code = subprocess.call(knime_command, stdout=sys.stderr, stderr=FNULL)
except OSError as e:
eprint('Running the RetroPath2.0 Knime program produced an OSError')
eprint(e)
return 2
#in the docker it returns 4 but everything is well generated
#if exit_code==4:
# return 0
return exit_code
#function that takes the .dat input of a file, opens to be read by python and then writes it to a file to be csv
def readCopyFile(inputFile, outDir):
outputFile = outDir+'/'+inputFile.split('/')[-1].replace('.dat', '')+'.csv'
#outputFile = inputFile.split('/')[-1].replace('.dat', '')+'.csv'
with open(outputFile, 'w') as outF:
outCSV = csv.writer(outF, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
with open(inputFile, 'r') as inF:
inCSV = csv.reader(inF, delimiter=',', quotechar='"')
for row in inCSV:
outCSV.writerow(row)
return outputFile, inputFile.split('/')[-1].replace('.dat', '')+'.csv'
if __name__ == "__main__":
#### WARNING: as it stands one can only have a single source molecule
parser = argparse.ArgumentParser('Python wrapper for the KNIME workflow')
parser.add_argument('-source', type=str)
parser.add_argument('-sink', type=str)
parser.add_argument('-rules', type=str)
parser.add_argument('-dmin', type=int)
parser.add_argument('-dmax', type=int)
parser.add_argument('-maxSteps', type=int)
#parser.add_argument('-results', type=str)
#parser.add_argument('-sourceinsink', type=str)
parser.add_argument('-scopeCSV', type=str)
parser.add_argument('-topx', type=int)
parser.add_argument('-mwmax_source', type=int)
parser.add_argument('-mwmax_cof', type=int)
#parser.add_argument('-outDir', type=str)
params = parser.parse_args()
#### Create symlink with the input files that are .dat to the KNIME accepted .csv format
# Does not work with DOCKER
# Trying to read and copy the input file to the docker since we cannot add a symlink to it
outDir = '/home/src/data'
cp_source, fname_source = readCopyFile(params.source, outDir)
cp_sink, fname_sink = readCopyFile(params.sink, outDir)
cp_rules, fname_rules = readCopyFile(params.rules, outDir)
#### TODO: make sure that you test that there is only a single source molecule....
exit_code = main(params.dmin, params.dmax, params.maxSteps, cp_source, cp_sink, cp_rules, params.topx, params.mwmax_source, params.mwmax_cof, outDir)
csvScope = glob.glob(outDir+'/*_scope.csv')
###### parse the source-in-sink and return an error if its full
#shutil.copy2(outDir+'/source-in-sink.csv', params.sourceinsink)
count = 0
with open(outDir+'/source-in-sink.csv') as f:
reader = csv.reader(f, delimiter=',', quotechar='"')
for i in reader:
count += 1
if count>1:
eprint('ERROR: Source has been found in the sink')
exit(1)
#TODO: have another error if there are no results in the scope file
try:
shutil.copy2(csvScope[0], params.scopeCSV)
except IndexError:
eprint('ERROR: RetroPath2.0 has not found any results')
exit(1)
#shutil.copy2(outDir+'/results.csv', params.results)
exit(exit_code)