-
Notifications
You must be signed in to change notification settings - Fork 18
/
structureCheck.py
381 lines (345 loc) · 18.4 KB
/
structureCheck.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
# --------------------------------------------------------------------------
# OligoMiner
# structureCheck.py
#
# (c) 2017 Molecular Systems Lab
#
# Wyss Institute for Biologically-Inspired Engineering
# Harvard University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# --------------------------------------------------------------------------
# Specific script name.
scriptName = 'structureCheck'
# Specify script version.
Version = '1.7'
# Import module for handling input arguments.
import argparse
# Import os module.
import os
# Import timeit module and record start time. This provides a rough estimate of
# the wall clock time it takes to run the script.
import timeit
# Import numpy module.
import numpy as np
# Import subprocess module.
import subprocess
from subprocess import PIPE, Popen
# Import Biopython modules.
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
class StructureChecker:
def __init__(self, inputFile, formConc, saltConc, NUPACKmat, threshVal,
Temp, IDval, reportVal, debugVal, metaVal, tempDir,
outNameVal, startTime):
self.inputFile = inputFile
self.formConc = formConc
self.saltConc = saltConc
self.NUPACKmat = NUPACKmat
self.threshVal = threshVal
self.Temp = Temp
self.IDval = IDval
self.reportVal = reportVal
self.debugVal = debugVal
self.metaVal = metaVal
self.tempDir = tempDir
self.outNameVal = outNameVal
self.startTime = startTime
# Calculate T to use with NUPACK.
self.CorrTemp = 0.65 * self.formConc + self.Temp
# Calculate salt to use with NUPACK.
self.CorrSalt = self.saltConc * 0.001
# Determine the stem of the input filename.
self.fileName = str(self.inputFile).split('.')[0]
def prob_check(self, seq1, struct1):
"""Runs the NUPACK 'prob' command."""
randomInt = np.random.random_integers(0, 1000000)
NUPACK_input = open('%s_%s_%0.0f_%d_prob_temp.in'
% (self.fileName, self.IDval, self.Temp, randomInt),
'w')
NUPACK_input.write('%s\n%s\n' % (seq1, struct1))
NUPACK_input.close()
p = Popen(['prob', '-T', str(self.CorrTemp), '-sodium',
str(self.CorrSalt), '-material', str(self.NUPACKmat),
'%s_%s_%0.0f_%d_prob_temp' \
% (self.fileName, self.IDval,self.Temp, randomInt),
'>%s_%s_%0.0f_%d_temp_prob.txt' \
% (self.fileName, self.IDval, self.Temp, randomInt)],
stderr=PIPE, stdout=PIPE, bufsize=1)
stdout_lines = p.stdout.readlines()
prob_val = None
for i in range(len(stdout_lines)):
if stdout_lines[i] == '% Probability:\n':
prob_val = float(stdout_lines[i + 1])
if prob_val == None:
print '***********************************************************'
print 'NUPACK ERROR: could not run prob command with these inputs.'
print '***********************************************************'
os.remove('%s_%s_%0.0f_%d_prob_temp.in' \
% (self.fileName, self.IDval, self.Temp, randomInt))
return prob_val
def run(self):
"""Runs the structureChecker with the given parameters."""
# Create a randomized directory to hold temp files.
dirName = '%s_%d' \
% (self.fileName, np.random.random_integers(0, 1000000))
if not os.path.exists('%s/%s' % (self.tempDir, dirName)):
os.makedirs('%s/%s' % (self.tempDir, dirName))
# Open input file for reading.
with open(self.inputFile, 'r') as f:
file_read = [line.strip() for line in f]
# Create list to hold output.
outList = []
probList = []
# Make list to hold Report info if desired.
if self.reportVal is True:
reportList = []
# Iterate through probe file checking for predicted secondary structure.
for i in range(0, len(file_read), 1):
probeSeq = file_read[i].split('\t')[3]
struct_in = '.' * len(probeSeq)
p = self.prob_check(probeSeq, struct_in)
probList.append(p)
if p >= self.threshVal:
outList.append(file_read[i])
if self.reportVal is True:
reportList.append('Candidate probe at %s:%s-%s is '
'predicted to have a linear structure '
'with p=%0.4f > %0.4f at %dC in %d mM '
'Na+ and %d%% formamide, added to '
'output' \
% (file_read[i].split('\t')[0],
file_read[i].split('\t')[1],
file_read[i].split('\t')[2],
p, self.threshVal, self.Temp,
self.saltConc, self.formConc))
if self.debugVal is True:
print('Candidate probe at %s:%s-%s is predicted to have a '
'linear structure with p=%0.4f > %0.4f at %dC in %d '
'mM Na+ and %d%% formamide, added to output' \
% (file_read[i].split('\t')[0],
file_read[i].split('\t')[1],
file_read[i].split('\t')[2],
p, self.threshVal, self.Temp,
self.saltConc, self.formConc))
else:
if self.reportVal is True:
reportList.append('Candidate probe at %s:%s-%s is '
'predicted to have a linear structure '
'with p=%0.4f < %0.4f %dC in %d mM Na+ '
'and %d%% formamide, filtered from '
'output' \
% (file_read[i].split('\t')[0],
file_read[i].split('\t')[1],
file_read[i].split('\t')[2],
p, self.threshVal, self.Temp,
self.saltConc, self.formConc))
if self.debugVal is True:
print('Candidate probe at %s:%s-%s is predicted to have a '
'linear structure with p=%0.4f < %0.4f %dC in %d mM '
'Na+ and %d%% formamide, filtered from output' \
% (file_read[i].split('\t')[0],
file_read[i].split('\t')[1],
file_read[i].split('\t')[2],
p, self.threshVal, self.Temp,
self.saltConc, self.formConc))
# Remove the temporary directory.
os.removedirs('%s/%s' % (self.tempDir, dirName))
# Determine the name of the output file.
if self.outNameVal is None:
if self.IDval is None:
outName = '%s_sC' % self.fileName
else:
outName = '%s_%s_sC' % (self.fileName, self.IDval)
else:
outName = self.outNameVal
# Create the output file.
output = open('%s.bed' % outName, 'w')
# Write the output file.
output.write('\n'.join(outList))
output.close()
# Print info about the results to terminal.
candsNum = len(file_read)
cleanNum = len(outList)
if 'rna' in self.NUPACKmat:
print '********************************************************************************'
print 'NUPACK WARNING: No salt corrections available for RNA. Using 1 M Na and 0 M Mg.'
print '********************************************************************************'
print('structureCheck predicted that %d of %d / %0.4f%% candidate '
'probes are predicted to have a linear structure with p>%0.4f at '
'%dC in %d mM Na+ and %d%% formamide' \
% (cleanNum, candsNum, float(cleanNum) / float(candsNum) * 100,
self.threshVal, self.Temp, self.saltConc, self.formConc))
# Write meta information to a .txt file if desired.
if self.metaVal is True:
metaText = open('%s_outputClean_meta.txt' % outName, 'w')
metaText.write('%s\t%f\t%s\t%0.2f\t%d\t%d' \
% (self.inputFile,
timeit.default_timer() - self.startTime,
Version, self.Temp, cleanNum, candsNum))
metaText.close()
# If desired, create report file and tabulate stats.
if self.reportVal is True:
a = np.asarray(probList)
bin1 = ((0 < a) & (a <= .0001)).sum()
bin2 = ((.0001 < a) & (a <= .001)).sum()
bin3 = ((.001 < a) & (a <= .01)).sum()
bin4 = ((.01 < a) & (a <= .1)).sum()
bin5 = ((.1 < a) & (a <= 1)).sum()
reportOut = open('%s_structureCheck_log.txt' % outName, 'w')
reportList.insert(0, 'Results produced by %s %s' \
% (scriptName, Version))
reportList.insert(1, '-' * 100)
reportList.insert(2, 'structureCheck predicted that %d of %d / '
'%0.4f%% candidate probes are predicted to '
'have a linear structure with p>%0.4f at %dC '
'in %d mM Na+ and %d%% formamide' \
% (cleanNum, candsNum,
float(cleanNum) / float(candsNum) * 100,
self.threshVal, self.Temp, self.saltConc,
self.formConc))
if len(a) == 0:
reportList.insert(3, '-' * 100)
else:
reportList.insert(3, '%d of %d / %0.4f%% of probes have a '
'predicted to have linear structures with '
'0 < prob. <= .0001' \
% (bin1, len(a),
100 * float(bin1) / float(len(a))))
reportList.insert(4, '%d of %d / %0.4f%% of probes have a '
'predicted to have linear structures with '
'.0001 < prob. <= .001'
% (bin2, len(a),
100 * float(bin2) / float(len(a))))
reportList.insert(5, '%d of %d / %0.4f%% of probes have a '
'predicted to have linear structures with '
'.001 < prob. <= .01'
% (bin3, len(a),
100 * float(bin3) / float(len(a))))
reportList.insert(6, '%d of %d / %0.4f%% of probes have a '
'predicted to have linear structures with '
'.01 < prob. <= .1'
% (bin4, len(a),
100 * float(bin4) / float(len(a))))
reportList.insert(7, '%d of %d / %0.4f%% of probes have a '
'predicted to have linear structures with '
'.1 < prob. <= 1' \
% (bin5, len(a),
100 * float(bin5) / float(len(a))))
reportList.insert(8, '-' * 100)
reportOut.write('\n'.join(reportList))
reportOut.close()
def runStructureChecker(inputFile, formConc, saltConc, NUPACKmat, threshVal,
Temp, IDval, reportVal, debugVal, metaVal, tempDir,
outNameVal, startTime):
"""Creates and runs an instance of a StructureChecker, which scans probes
and evaluates their structures using NUPACK."""
sc = StructureChecker(inputFile, formConc, saltConc, NUPACKmat, threshVal,
Temp, IDval, reportVal, debugVal, metaVal, tempDir,
outNameVal, startTime)
sc.run()
def main():
"""Uses NUPACK to check structures of a given probe set according to
specified conditions."""
startTime = timeit.default_timer()
# Allow user to input parameters on command line.
userInput = argparse.ArgumentParser(description=\
'%s version %s. Requires a .bed file containing probe sequences in the '
'fourth tab-separated column. Also requires NUPACK to be installed and '
'in your path. Returns a .bed file in the same format as the input '
'file filtered of probes predicted to have more thermodynamically '
'stable secondary structures or homodimer structure than the desired '
'probe-target duplex by NUPACK' % (scriptName, Version))
requiredNamed = userInput.add_argument_group('required arguments')
requiredNamed.add_argument('-f', '--file', action='store', required=True,
help='The .bed file to check probes in')
userInput.add_argument('-F', '--formamide', action='store', default=50,
type=float,
help='The percent formamide being used, default is '
'50')
userInput.add_argument('-s', '--salt', action='store', default=390,
type=float,
help='The mM Na+ concentration, default is 390. '
'NOTE: NUPACK\'s allowable range is 50-1100 mM')
userInput.add_argument('-m', '--material', action='store',
default='dna1998', type=str,
help='The NUPACK material setting, default is '
'dna1998')
userInput.add_argument('-t', '--threshold', action='store', type=float,
default=0.1,
help='The probability threshold for a probe having '
'a linear structure to use for filtering, '
'default=0.1')
userInput.add_argument('-T', '--hybTemp', action='store', default=47,
type=float,
help='The temperature at which you want to '
'hybridize your probes')
userInput.add_argument('-I', '--ID', action='store', type=str, default=None,
help='Specify an ID to be associated with temporary '
'file names & the default output name. Can be '
'useful for parallelization. Null by default. '
'Will not be automatically included in output '
'file name if -o is flagged')
userInput.add_argument('-R', '--Report', action='store_true', default=False,
help='Write a Report file detailing the results of '
'the secondary structure filtering. Off by '
'default. Note, selecting this option will '
'slow the script.')
userInput.add_argument('-D', '--Debug', action='store_true', default=False,
help='The same as -Report, but prints info to '
'terminal instead of writing a log file. Off '
'by default')
userInput.add_argument('-M', '--Meta', action='store_true', default=False,
help='Write a text file containing meta '
'information. Off by default. Reports input '
'file <tab> estimated runtime <tab> '
'structureCheck version ]<tab> hybridization '
'temperature used <tab> number of input '
'probes passing secondary structure filter '
'<tab> number of input probes')
userInput.add_argument('-d', '--temp', action='store', type=str,
default='/tmp',
help='the directory where temporary files will be a '
'written, default="/tmp"')
userInput.add_argument('-o', '--output', action='store', default=None,
type=str,
help='Specify the name prefix of the output file')
# Import user-specified command line values.
args = userInput.parse_args()
inputFile = args.file
formConc = args.formamide
saltConc = args.salt
NUPACKmat = args.material
threshVal = args.threshold
Temp = args.hybTemp
IDval = args.ID
reportVal = args.Report
debugVal = args.Debug
metaVal = args.Meta
tempDir = args.temp
outNameVal = args.output
# Run the structure checker.
runStructureChecker(inputFile, formConc, saltConc, NUPACKmat, threshVal,
Temp, IDval, reportVal, debugVal, metaVal, tempDir,
outNameVal, startTime)
# Print wall-clock runtime to terminal.
print 'Program took %f seconds' % (timeit.default_timer() - startTime)
if __name__ == '__main__':
main()