forked from umerijaz/nanopore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchopSEQ.py
executable file
·300 lines (237 loc) · 14.9 KB
/
chopSEQ.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
#!/usr/bin/python
# ***************************************************************
# Name: chopSEQ.py
# Purpose: Fixes the problems in consensus sequences produced by INC-SEQ by
# a) Rearranging the sequences to start from forward primer
# b) Remove tandom repeats from the resulting rearrangement
# c) Corrects the orientation and also colors the primers and tandem repeats in verbosity mode -v
# d) Apply read length thresholds
# Dependencies:
# Biopython is installed
# EMBOSS etandem is in path
# conconcert.futures
# psutil
# Version: 0.3.3 (2020-05-29)
# History: Fixed the bugs associated with right overhang and included thresholds for final read lengths
# Added dynamic tandem threshold and fixed a few bugs
# Authors: Umer Zeeshan Ijaz ([email protected]), Young Sam Eugene ([email protected])`
# http://userweb.eng.gla.ac.uk/umer.ijaz
# Created: 2017-06-28
# License: Copyright (c) 2017 Environmental'Omics Lab, University of Glasgow, UK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Modifed: 2020-05-29
# **************************************************************/
from getopt import getopt, GetoptError
from sys import argv, exit
from numpy import mean
from subprocess import Popen,PIPE,STDOUT
from math import floor
from Bio.SeqIO import parse
from Bio.pairwise2 import align
from Bio.Seq import Seq
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
from psutil import cpu_count
#from time import perf_counter
def usage():
print ('Usage:')
print("\tYour total amount of cpu: %d\n\tHalf the amount of cpu is %d"%(cpu_count(),cpu_count(logical=False)))
print ('\tpython chopSEQ.py -i <input_file> -f <forward_primer> -r <reverse_primer> -p <number of process> -t > <filtered_fasta_file> ')
print ('''
Other options are:
-l (--minimum_length) After processing, ignore reads below this length threshold
-m (--maximum_length) After processing, ignore reads above this length threshold
-v Verbosity switch to visualise primers across the length of the reads
-t (--threading) this allow for multithreading at maximum threads
-p (--processing) This allow for the user to input the number of process the user want
to use default is a single processor being used
''')
def run_prog(prog):
p = Popen(prog, shell=True, stdout=PIPE, stderr=STDOUT)
returned_list=[]
for line in iter(p.stdout.readline, b''):
returned_list.append(line.rstrip())
retval = p.wait()
return returned_list
def process_etandem_record(full_sequence,verbosity,tandem_min_repeat,tandem_max_repeat,tandem_threshold,tandem_mismatch,tandem_identity_threshold):
cmd_line="etandem -sequence=asis:" + full_sequence + " -minrepeat=" + str(tandem_min_repeat) + " -maxrepeat=" + str(tandem_max_repeat) + " -threshold=" + str(tandem_threshold) + " -mismatch=" + str(tandem_mismatch) + " -stdout -auto -rformat excel"
etandem_output=run_prog(cmd_line)
corrected_sequence=''
if(len(etandem_output)>1):
record=etandem_output[1].split("\t")
t_match_left_position=int(record[1])
t_match_right_position=int(record[2])
t_match_count=int(record[6])
t_match_score=int(record[3])
t_match_length=int(record[5])
t_match_ident=float(record[7])
t_match_consensus=record[8]
t_left_string=full_sequence[0:(t_match_left_position-1)]
t_tandom_repeat_1=full_sequence[(t_match_left_position-1):(t_match_left_position+t_match_length-1)]
t_tandom_repeat_2=full_sequence[(t_match_left_position+t_match_length-1):t_match_right_position]
t_right_string=full_sequence[t_match_right_position:]
#Calculate allowed ident threshold
#As the tandem repeat becomes longer, there is a high probability of incorporating the errors
#For this purpose we divide the range from tandem_min_repeat to tandem_max_repeat in 80 parts
#and then subtract the number of parts from the tandem_identity_threshold
allowed_ident_threshold=(tandem_identity_threshold - floor( t_match_length / (tandem_max_repeat-tandem_min_repeat) / 80.0))
if verbosity:
if t_match_ident>=allowed_ident_threshold:
print ("Step 2: Removing tandom repeats (identity >= "+str(allowed_ident_threshold)+ ") in correctly arranged sequence" + "[length="+str(t_match_length)+";score=" + str(t_match_score) + ";ident=" + str(t_match_ident) + ";count=" + str(t_match_count)+ ";consensus="+t_match_consensus+ "]")
print(t_left_string+'\x1b[6;30;44m'+t_tandom_repeat_1+'\x1b[0m'+ '\x1b[6;30;45m' + t_tandom_repeat_2+'\x1b[0m'+t_right_string)
if t_match_ident>=allowed_ident_threshold:
corrected_sequence=t_left_string+t_tandom_repeat_1+t_right_string
else:
corrected_sequence=full_sequence
else:
corrected_sequence=full_sequence
return corrected_sequence
def main(arguv):
input_file=''
forward_primer=''
reverse_primer=''
# *Default parameters *************
# *********************************/
verbosity = 0
processing = 1
threading = 0
#Used in Step 3:
minimum_read_length_threshold = 0
maximum_read_length_threshold = 100000
#this code is used to put options in the arguments in the proper place I'm having to spilit up the options in this case becuse they do not read from commandline properly
try:
opts,args=getopt(arguv,"hi:f:r:vl:m:p:t",["input_file","forward_primer","reverse_primer","minimum_length","maximum_length","multiprocessing","multithreading"])
except GetoptError:
usage()
exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
exit()
elif opt == '-v':
verbosity=1
elif opt in ("-i","--input_file"):
input_file=arg
elif opt in ("-f","--forward_primer"):
forward_primer=arg
elif opt in ("-r","--reverse_primer"):
reverse_primer=arg
elif opt in ("-l","--minimum_length"):
minimum_read_length_threshold=int(arg)
elif opt in ("-m","--maximum_length"):
maximum_read_length_threshold=int(arg)
elif opt in ("-p","--processing"):
processing = int(arg)
elif opt in ("-t","--threading"):
threading = 1
if input_file=='' or forward_primer=='' or reverse_primer=='' or processing <=0 or processing > cpu_count():
usage()
exit(2)
#Some of the references we used in writing the code
#Reference: http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc85
#Reference: https://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-pyton
#this allow the multiprocessor call and allow for the ability
if processing == cpu_count():
with ProcessPoolExecutor() as executor:
results =[executor.submit(process_seq_records, seq_record, forward_primer, reverse_primer, verbosity, minimum_read_length_threshold, maximum_read_length_threshold) for seq_record in parse(input_file,"fasta")]
for resultant in as_completed(results):
print(resultant.result())
elif processing and threading == 0:
with ProcessPoolExecutor(max_workers=processing) as executor:
results =[executor.submit(process_seq_records, seq_record, forward_primer, reverse_primer, verbosity, minimum_read_length_threshold, maximum_read_length_threshold) for seq_record in parse(input_file,"fasta")]
for resultant in as_completed(results):
print(resultant.result())
elif threading:
with ThreadPoolExecutor() as executor:
results = [executor.submit(process_seq_records,seq_record,forward_primer,reverse_primer,verbosity,minimum_read_length_threshold,maximum_read_length_threshold) for seq_record in parse(input_file,"fasta")]
for resultant in as_completed(results):
print(resultant.result())
def process_seq_records(seq_record,forward_primer,reverse_primer,verbosity,minimum_read_length_threshold,maximum_read_length_threshold):
#used in step1
primer_match_score=5
primer_mismatch_score=-4
primer_open_gap_score=-2
primer_extend_gap_score=-2
#Used in Step 2:
tandem_min_repeat=10
tandem_max_repeat=350
tandem_threshold=10
tandem_mismatch=5
tandem_identity_threshold=85
if verbosity:
print('\x1b[6;37;40m' + str(seq_record.id) + '\x1b[0m')
forward_orientation_forward_primer_alignment=align.localms(str(seq_record.seq),forward_primer,primer_match_score, primer_mismatch_score, primer_open_gap_score, primer_extend_gap_score, one_alignment_only=1)
forward_orientation_reverse_primer_alignment=align.localms(str(seq_record.seq),str(Seq(reverse_primer).reverse_complement()),primer_match_score,primer_mismatch_score, primer_open_gap_score, primer_extend_gap_score, one_alignment_only=1)
reverse_orientation_forward_primer_alignment=align.localms(str(seq_record.seq),reverse_primer,primer_match_score,primer_mismatch_score, primer_open_gap_score, primer_extend_gap_score, one_alignment_only=1)
reverse_orientation_reverse_primer_alignment=align.localms(str(seq_record.seq),str(Seq(forward_primer).reverse_complement()),primer_match_score,primer_mismatch_score, primer_open_gap_score, primer_extend_gap_score, one_alignment_only=1)
#Find the correct orientation based on mean scores of primers matching
if mean([forward_orientation_forward_primer_alignment[0][2],forward_orientation_reverse_primer_alignment[0][2]]) > mean([reverse_orientation_forward_primer_alignment[0][2],reverse_orientation_reverse_primer_alignment[0][2]]):
f_match_left_position=forward_orientation_forward_primer_alignment[0][3]
f_match_right_position=forward_orientation_forward_primer_alignment[0][4]
f_left_string=str(forward_orientation_forward_primer_alignment[0][0][0:(f_match_left_position)])
f_match_string=str(forward_orientation_forward_primer_alignment[0][0][f_match_left_position:f_match_right_position])
f_right_string=str(forward_orientation_forward_primer_alignment[0][0][(f_match_right_position):])
if verbosity:
print ("Step 1: Match primers and correctly rearrange the sequence")
print ('Forward Primer Match:')
print(f_left_string + '\x1b[6;30;42m' + f_match_string + '\x1b[0m' + f_right_string)
r_match_left_position=forward_orientation_reverse_primer_alignment[0][3]
r_match_right_position=forward_orientation_reverse_primer_alignment[0][4]
r_left_string=str(forward_orientation_reverse_primer_alignment[0][0][0:(r_match_left_position)])
r_match_string=str(forward_orientation_reverse_primer_alignment[0][0][r_match_left_position:r_match_right_position])
r_right_string=str(forward_orientation_reverse_primer_alignment[0][0][(r_match_right_position):])
if verbosity:
print ('Reverse Primer Match:')
print(r_left_string + '\x1b[6;30;43m' + r_match_string + '\x1b[0m' + r_right_string)
#See if there is a right-over hang after the reverse primer and remove it
ind_r=f_right_string.find(r_match_string.replace("-",""))
if (ind_r > -1):
f_right_string=f_right_string[0:ind_r]
full_sequence=f_match_string + f_right_string + f_left_string
full_sequence=full_sequence.replace("-","")
full_sequence=process_etandem_record(full_sequence,verbosity,tandem_min_repeat,tandem_max_repeat,tandem_threshold,tandem_mismatch,tandem_identity_threshold)
if not verbosity:
if ((len(full_sequence)>=minimum_read_length_threshold) and (len(full_sequence)<=maximum_read_length_threshold)):
return ">" + str(seq_record.id) + "_corrected_length=" + str(len(full_sequence)) + "\n" + str(full_sequence)
else:
f_match_left_position=reverse_orientation_forward_primer_alignment[0][3]
f_match_right_position=reverse_orientation_forward_primer_alignment[0][4]
f_left_string=str(reverse_orientation_forward_primer_alignment[0][0][0:(f_match_left_position)])
f_match_string=str(reverse_orientation_forward_primer_alignment[0][0][f_match_left_position:f_match_right_position])
f_right_string=str(reverse_orientation_forward_primer_alignment[0][0][(f_match_right_position):])
if verbosity:
print ("Step 1: Match primers and correctly rearrange the sequence")
print ('Reverse Primer Match (Reverse Orientation):')
print(f_left_string + '\x1b[6;30;42m' + f_match_string + '\x1b[0m' + f_right_string)
r_match_left_position=reverse_orientation_reverse_primer_alignment[0][3]
r_match_right_position=reverse_orientation_reverse_primer_alignment[0][4]
r_left_string=str(reverse_orientation_reverse_primer_alignment[0][0][0:(r_match_left_position)])
r_match_string=str(reverse_orientation_reverse_primer_alignment[0][0][r_match_left_position:r_match_right_position])
r_right_string=str(reverse_orientation_reverse_primer_alignment[0][0][(r_match_right_position):])
#See if there is a right-over hang after the reverse primer and remove it
ind_r=f_right_string.find(r_match_string.replace("-",""))
if (ind_r > -1):
f_right_string=f_right_string[0:ind_r]
if verbosity:
print ('Forward Primer Match (Reverse Orientation):')
print(r_left_string + '\x1b[6;30;43m' + r_match_string + '\x1b[0m' + r_right_string)
full_sequence=f_match_string + f_right_string + f_left_string
full_sequence=full_sequence.replace("-","")
full_sequence=str(Seq(full_sequence).reverse_complement())
full_sequence=process_etandem_record(full_sequence,verbosity,tandem_min_repeat,tandem_max_repeat,tandem_threshold,tandem_mismatch,tandem_identity_threshold)
if not verbosity:
if ((len(full_sequence)>=minimum_read_length_threshold) and (len(full_sequence)<=maximum_read_length_threshold)):
return ">"+str(seq_record.id)+"_corrected_sequence="+str(len(full_sequence))+"\n"+str(full_sequence)
if __name__== "__main__":
main(argv[1:])