-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pipeline.py
220 lines (176 loc) · 6.98 KB
/
run_pipeline.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import sys
import getopt
import numpy
os.environ["WORK_DIR_PEP"] = os.getcwd() + '/Peptide/'
os.environ["WORK_DIR_PRO"] = os.getcwd() + '/Protein/'
os.environ["WORK_DIR_CLS"] = os.getcwd() + '/Classifier/'
from Classifier import classifier
from Peptide import run_peptide
from Protein import run_protein
from datetime import datetime
import logging
logging.basicConfig(filename='pipeline.log', format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
logging.captureWarnings(True)
def usage():
'''
Details on how to use pipeline.
'''
print "\n\n python2.7 run_pipeline.py -i [PWM data] -o [Output file/dir]" +\
" -p [p-value] -d [Domain data file]\n\n"
print " Options:\n -i Path to PWM file or directory\n" +\
" -o Path to output file or directory\n" +\
" -p P-value threshold for PWM scanning (default: 1e-05)\n" +\
" -d Path to file containing domain information\n"
def command_line_arguments(argv):
'''
Process the command line arguments. Returns options variable.
'''
options = {'output': None, 'pwm': None, 'domain': None, 'p-value': 1e-05}
try:
opts, args = getopt.getopt(argv, "i:o:p:d:")
except getopt.GetoptError:
usage()
sys.exit()
if len(opts) == 0:
usage()
sys.exit()
for opt, arg in opts:
if opt in ("-h"):
usage()
sys.exit()
elif opt in ("-i"):
if os.path.isfile(arg) or os.path.isdir(arg):
options['pwm'] = arg
else:
print '\nPWM input file found\n'
elif opt in ("-o"):
options['output'] = arg
elif opt in ("-p"):
try:
pval = float(arg)
except:
print '\nInvalid p-value. Using default 1e-05.\n'
else:
options['p-value'] = pval
elif opt in ("-d"):
if os.path.isfile(arg):
options['domain'] = arg
else:
print '\nDomain sequence file not found. Skipping Structure \
Score.\n'
if not options['pwm']:
usage()
sys.exit()
return options
def write_file(path, cel_pred, pro_set, detail=True):
'''
'''
write_file = open(path, 'w')
if detail:
write_file.write('Domain\tPeptide\tStart\tStop\tSequence\tPeptide Score\tPeptide Count\tProtein Score\tProtein Count\tScore\n')
for idx, (com, pep, pot, npe, npr) in enumerate(cel_pred):
write_file.write(pro_set[idx][0] + '\t' + pro_set[idx][1] + '\t'
+ pro_set[idx][2] + '\t' + pro_set[idx][3] + '\t'
+ pro_set[idx][4] + '\t'
+ str(round(pep['positive'], 2)) + '\t'
+ str(npe) + '\t'
+ str(round(pot['positive'], 2)) + '\t'
+ str(npr) + '\t'
+ str(round(com['positive'], 2)) + '\n')
else:
write_file.write('Domain\tPeptide\tScore\n')
for idx, val in enumerate(cel_pred):
write_file.write(pro_set[idx][0] + '\t' + pro_set[idx][1] + '\t'
+ str(round(val['positive'], 2)) + '\n')
write_file.close()
def write_std(cel_pred, pro_set, detail=False):
'''
'''
print 'Domain\tPeptide\tScore\n'
for idx, val in enumerate(cel_pred):
print pro_set[idx][0] + '\t' + pro_set[idx][1] + '\t' +\
str(round(val['positive'], 2))
def peptide_module(pwm, domain, pval, data):
'''
blah blah
'''
# data = run_peptide.setup_peptide()
gnom = run_peptide.process_genome_file()
fnam = os.path.basename(pwm)
if domain:
seq_data = dict(numpy.loadtxt(domain, delimiter='\t', dtype='S'))
if fnam not in seq_data:
print '\nSkipping Structure Score. Domain sequence'\
+ '%s not found.\n' % fnam
structure = None
else:
if data[3]:
structure = (seq_data[fnam], data[3])
else:
structure = None
else:
structure = None
print '\nDomain sequence file not found. Skipping Structure Score.\n'
peptide, pep_set = run_peptide.run_pwm(pwm, gnom, pval, data[0], data[1],
data[2], structure)
return peptide, pep_set
def protein_module(pep_set, data):
'''
'''
protein, pro_set = run_protein.run_features(pep_set, (data[0][0]["C"], data[0][1]),
(data[0][0]["P"], data[0][1]), (data[0][0]["F"], data[0][1]), data[1], data[2])
return protein, pro_set
def pwm_runner(pwm, domain, pval, output, gen_data, cel_data):
'''
'''
peptide, pep_set = peptide_module(pwm, domain, pval, gen_data)
protein, pro_set = protein_module(pep_set, cel_data)
assert (pep_set == pro_set).all(), 'Problem.. gen cel not same'
gen_clas, cel_clas = classifier.get_classifier()
pred = []
for idx, (case1, case2) in enumerate(zip(peptide, protein)):
num_pep = 4 - list(case1).count(None)
num_pot = 5 - list(case2).count(None)
post1 = gen_clas.test(case1[1:])
post2 = cel_clas.test(case2)
post = classifier.bayes(post1, post2)
pred.append((post, post1, post2, num_pep, num_pot))
if output:
write_file(output, pred, pro_set)
else:
write_std(pred, pro_set)
def process_options(options):
'''
'''
if os.path.isfile(options['pwm']):
print '\nProcessing PWM: %s\n' % (os.path.basename(options['pwm']))
if options['output'] and os.path.isdir(options['output']):
out_path = options['output'] + '/' + options['pwm'] + '_' +\
str(options['p-value'])
else:
out_path = options['output']
gen_data = run_peptide.setup_peptide()
cel_data = run_protein.setup_protein()
pwm_runner(options['pwm'], options['domain'], options['p-value'],
out_path, gen_data, cel_data)
elif os.path.isdir(options['pwm']):
if not options['output'] or not os.path.isdir(options['output']):
print '\nOutput dirctory not present. Creating output directory.'
outdir = str(datetime.now()).replace(' ', '_').replace(':', '-') +\
'.out'
os.makedirs(outdir)
else:
outdir = options['output']
gen_data = run_peptide.setup_peptide()
cel_data = run_protein.setup_protein()
for pwm_file in os.listdir(options['pwm']):
print '\nProcessing PWM: %s\n' % (pwm_file)
pwm_path = options['pwm'] + '/' + pwm_file
out_path = outdir + '/' + pwm_file + '_' + str(options['p-value'])
pwm_runner(pwm_path, options['domain'], options['p-value'],
out_path, gen_data, cel_data)
if __name__ == '__main__':
options = command_line_arguments(sys.argv[1:])
process_options(options)