/
remove_adapter.py
executable file
·1275 lines (1114 loc) · 43.6 KB
/
remove_adapter.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
It trims (finds and/or removes by trimming) the paired-reads (FASTQ file
produced by Illumina Solexa) which overlap and contain the adapter. The adapter
is found automatically. Also the partial overlapping between a short read and
the adapter sequence is handled.
Author: Daniel Nicorici, Daniel.Nicorici@gmail.com
Copyright (c) 2009-2016 Daniel Nicorici
This file is part of FusionCatcher.
FusionCatcher 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.
FusionCatcher 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 FusionCatcher (see file 'COPYING.txt'). If not, see
<http://www.gnu.org/licenses/>.
By default, FusionCatcher is running BLAT aligner
<http://users.soe.ucsc.edu/~kent/src/> but it offers also the option to disable
all its scripts which make use of BLAT aligner if you choose explicitly to do so.
BLAT's license does not allow to be used for commercial activities. If BLAT
license does not allow to be used in your case then you may still use
FusionCatcher by forcing not use the BLAT aligner by specifying the option
'--skip-blat'. Fore more information regarding BLAT please see its license.
Please, note that FusionCatcher does not require BLAT in order to find
candidate fusion genes!
This file is not running/executing/using BLAT.
"""
import sys
import os
import datetime
import optparse
import multiprocessing
import itertools
import string
import itertools
import re
import gc
import shutil
import errno
import gzip
ttable = string.maketrans("ACGTYRSWKMBDHV-.","TGCARYSWMKVHDB-.") # global
empty_read = ['@N123\n','N\n','+\n','I\n'] # global
#
#
#
def remove_file(a_file):
if os.path.isfile(a_file) or os.path.islink(a_file):
os.remove(a_file)
elif os.path.isdir(a_file):
shutil.rmtree(a_file)
#
#
#
def linkit(file_input, file_output, kind ='soft'):
#
remove_file(file_output)
if os.path.islink(file_input):
linkto = os.readlink(file_input)
if kind == 'soft':
os.symlink(linkto, file_output)
elif kind == 'hard':
try:
os.link(linkto, file_output)
except OSError as er:
print >>sys.stderr,"WARNING: Cannot do hard links ('%s' and '%s')!" % (linkto,file_output)
shutil.copyfile(linkto, file_output)
# if er.errno == errno.EXDEV:
# # they are on different partitions
# # [Errno 18] Invalid cross-device link
# shutil.copyfile(linkto, file_output)
# else:
# print >>sys.stderr,"ERROR: Cannot do hard links ('%s' and '%s')!" % (linkto, file_output)
# print >>sys.stderr,er
# sys.exit(1)
else:
if kind == 'soft':
os.symlink(file_input, file_output)
elif kind == 'hard':
try:
os.link(file_input, file_output)
except OSError as er:
print >>sys.stderr,"WARNING: Cannot do hard links ('%s' and '%s')!" % (linkto,file_output)
shutil.copyfile(linkto, file_output)
# if er.errno == errno.EXDEV:
# # they are on different partitions
# # [Errno 18] Invalid cross-device link
# shutil.copyfile(file_input, file_output)
# else:
# print >>sys.stderr,"ERROR: Cannot do hard links ('%s' and '%s')!" % (file_input, file_output)
# print >>sys.stderr,er
# sys.exit(1)
#
#
#
def dnaReverseComplement(seq):
#seq = seq.upper()
seq = seq.translate(ttable)
return seq[::-1]
#
#
#
def read_first_fastq(file_name, first = 2000000, size_buffer = 10**8):
fid = None
if file_name.lower().endswith('.gz'):
fid = gzip.open(file_name,'r')
else:
fid = open(file_name,'r')
i = 0
while True:
gc.disable()
lines = fid.readlines(10**8)
gc.enable()
if (not lines) or i > first:
break
for line in lines:
if line[:-1]:
i = i + 1
if i > first:
break
else:
yield line
fid.close()
#
#
#
def read_fastq(file_name, size_buffer = 10**8):
fid = None
if file_name.lower().endswith('.gz'):
fid = gzip.open(file_name,'r')
else:
fid = open(file_name,'r')
while True:
gc.disable()
lines = fid.readlines(10**8)
gc.enable()
if not lines:
break
for line in lines:
if line[:-1]:
yield line
fid.close()
#
#
#
def first_reads_from_paired_fastq_file(file_name_1, file_name_2, first = 2000000, size_read_buffer = 10**8):
# it provides only the first N mate-reads
# if first == 0 then all mate-reads are provided
i1 = read_first_fastq(file_name_1, first = first*4+1, size_buffer = size_read_buffer / 2)
i2 = read_first_fastq(file_name_2, first = first*4+1, size_buffer = size_read_buffer / 2)
piece = [(None,None),(None,None),(None,None),(None,None)]
bucket = []
it = 0
global empty_read
#cut = 2000000 # first number of paired reads which are read
i = 0
x = True # get a quality score for the empty read
for (line_1, line_2) in itertools.izip(i1,i2):
piece[i] = (line_1,line_2)
i = i + 1
if first != 0 and it > first - 1:
continue
if i == 4:
bucket = [piece[0][0],
piece[1][0].rstrip('\r\n'),
"+\n",
piece[3][0],
piece[0][1],
piece[1][1].rstrip('\r\n'),
"+\n",
piece[3][1]
]
yield bucket
if x:
empty_read[3] = piece[3][0][0]+'\n'
x = False
piece = [(None,None),(None,None),(None,None),(None,None)]
i = 0
it = it + 1
#
#
#
def reads_from_paired_fastq_file(file_name_1, file_name_2, size_read_buffer = 10**8):
i1 = read_fastq(file_name_1, size_buffer = size_read_buffer/2)
i2 = read_fastq(file_name_2, size_buffer = size_read_buffer/2)
it = 0
piece = [(None,None),(None,None),(None,None),(None,None)]
for (line_1, line_2) in itertools.izip(i1,i2):
it = it + 1
piece[it-1] = (line_1,line_2)
#piece.append((line_1,line_2))
if it == 4:
bucket = [piece[0][0],
piece[1][0].rstrip('\r\n'),
"+\n",
piece[3][0],
piece[0][1],
piece[1][1].rstrip('\r\n'),
"+\n",
piece[3][1]
]
yield bucket
piece = [(None,None),(None,None),(None,None),(None,None)]
it = 0
if piece and len(piece) != 4:
print >>sys.stderr,"WARNING: Found unexpected ending of FASTQ files '%s' and '%s' but still continuing..." % (file_name_1,file_name_2)
#
#
#
class lines_to_file:
#
def __init__(self, file_name, size_buffer = 10**8):
self.file_name = file_name
if file_name:
remove_file(file_name)
self.file_handle = open(file_name,'w')
self.size_buffer = size_buffer
self.data = []
self.size = 0
#
def add_line(self, line):
# line = line.rstrip('\n')+'\n'
gc.disable()
self.data.append(line)
gc.enable()
self.size = self.size + len(line)
if self.size > self.size_buffer:
self.__write_buffer()
#
def add_lines(self,lines):
# lines=[line.rstrip('\r\n')+'\n' for line in lines]
gc.disable()
self.data.extend(lines)
gc.enable()
self.size = self.size + sum([len(line) for line in lines])
if self.size > self.size_buffer:
self.__write_buffer()
#
def __write_buffer(self):
self.file_handle.writelines(self.data)
self.size = 0
self.data = []
def is_filename_valid(self):
if self.file_name:
return True
else:
return False
def close(self):
if self.is_filename_valid():
if self.data:
self.__write_buffer()
self.file_handle.close()
self.file_name = None
def __del__(self):
self.close()
#
#
#
def fast_alignment_adapter(sa, sb, len_adapter = 13, overlap = 13):
lib = -1
na = len(sa)
nb = len(sb)
adapter5 = ''
adapter3 = ''
positions = [ (nb - overlap - x, nb - x) for x in range(3,nb/4,7) ]
#
for (pa,pb) in positions:
if pb - pa < overlap - 1 or pb < 0 or pa < 0:
break
p = sa.find(sb[pa:pb], 2)
if p != -1 and p < pa:
lib = p + nb - pa
if lib > na:
lib = na
s = pa - p
#t = ' ' * s
#xa = "%s%s" % (t,sa)
#xb = sb
#print xa
#print xb
# count the mismatches
mis = len([1 for ix in xrange(lib) if s+ix<nb and (sa[ix] != sb[s+ix] or (sa[ix] == 'N' and sb[s+ix] == 'N') )])
n_notn = len([1 for ix in xrange(lib) if s+ix<nb and (sa[ix] == 'N' or sb[s+ix] == 'N')])
#print "mismatches",mis
if lib > 0 and float(n_notn) / float(lib) > 0.3:
mis = mis + n_notn
if (mis > 0 and lib > 0 and float(mis) / float(lib) < 0.2 and lib != na):
continue
#print xa
#print xb
#print lib, len("%s%s" % (t,sb)),mis
#print sa[lib:lib+len_adapter]
#print sb[s-len_adapter:s]
#print ""
adapter5 = sa[lib:lib+len_adapter]
adapter3 = sb[s-len_adapter:s]
if len(adapter5) < len_adapter:
adapter5 = ""
if len(adapter3) < len_adapter:
adapter3 = ""
#print adapter5,adapter3
break
return (adapter5, adapter3)
#
#
#
def find_hard(ss,adp):
# try hard finding the adapter with one mismatch
p = -1
if adp:
adp_re = re.compile('|'.join(adp[:i] + '.' + adp[i+1:] for i in xrange(len(adp))))
for elx in adp_re.finditer(ss):
p = elx.start()
break
return p
#
#
#
def fast_alignment(sa, sb, overlap = 13, wiggle = 2, adpt5 = "", adpt3 = ""):
# align a read on top of its mate read if this is possible
na = len(sa)
nb = len(sb)
trim_a = na
trim_b = 0
len_adpt5 = 0
if adpt5:
len_adpt5 = len(adpt5)
len_adpt3 = 0
if adpt3:
len_adpt3 = len(adpt3)
positions = [ (nb - overlap - x, nb - x) for x in range(3,nb/4,7) ]
positions.insert(0,[-1,-1]) # this is I need it for allowing first to look for adapter
# do not forget! check that the positions are all positive
cut_mis_adapt = 0.3
cut_mis = 0.3
p = -1
p5 = -1
p3 = -1
lib = 0
s = -1
common = 0
mis = -1
mis5 = -1
mis3 = -1
first = True
for (pa, pb) in positions:
if first:
first = False
p5 = -1
if adpt5:
p5 = sa.find(adpt5)
p3 = -1
if adpt3:
p3 = sb.find(adpt3)
if p3 == -1:
if p5 == -1:
continue
else:
mis5 = 0
# try harder
p3 = find_hard(sb,adpt3)
if p3 == -1:
continue
else:
mis3 = 1
elif p5 == -1:
mis3 = 0
# try harder
p5 = find_hard(sa,adpt5)
if p5 == -1:
continue
else:
mis5 = 1
#do
lib = p5 # position on first read
s = p3 + len_adpt3 # position on second read (reverse-complemented)
trim_a = lib
trim_b = s
common = -1
elif pb - pa < overlap - 1 or pa < 0 or pb < 0:
break
else:
p = sa.find(sb[pa:pb], wiggle)
if p == -1 or p > pa:
continue
else:
lib = p + nb - pa
s = pa - p
if lib > na:
lib = na
#if lib == 0:
# print "lib=0"
#print "lib",lib
#print sa,p
#print sb,s
# t = ' ' * s
# xa = "%s%s" % (t,sa)
# xb = sb
# print xa
# print xb
# count the mismatches in the overlap; N is not considered a mismatch in the overlapping part
xlib = range(lib)
mis = len([1 for ix in xlib if s+ix<nb and ((sa[ix] != sb[s+ix] and sa[ix] != 'N' and sb[s+ix] != 'N') or ( sa[ix] == 'N' and sb[s+ix] == 'N'))])
n_notn = len([1 for ix in xlib if s+ix<nb and (sa[ix] == 'N' or sb[s+ix] == 'N')])
if lib > 0 and float(n_notn) / float(lib) > 0.3:
mis = mis + n_notn
if mis > 0 and lib > 0 and ((mis / float(lib) > cut_mis) or (lib == na and mis / float(lib) > 0.05)):
continue
mis5 = -1
if adpt5:
mis5 = len([1 for ix in xrange(len_adpt5) if lib+ix<na and adpt5[ix] != sa[lib+ix] and sa[lib+ix] != 'N' and sa[lib+ix] != '.'])
if float(float(mis5) / float(len_adpt5)) <= float(cut_mis_adapt):
# trim the read
trim_a = lib
mis3 = -1
if adpt3:
mis3 = len([1 for ix in xrange(len_adpt3) if s-len_adpt3 + ix>-1 and adpt3[ix] != sb[s-len_adpt3 + ix] and sb[s-len_adpt3 + ix] != 'N' and sb[s-len_adpt3 + ix] != '.'])
if float(mis3) / float(len_adpt3) <= float(cut_mis_adapt):
# trim the read
trim_b = s
#print xa
#print xb
#print lib, len("%s%s" % (t,sb)), mis, mis5, mis3
#print sa[lib:lib+len_adpt5]
#print sb[s-len_adpt3:s] if s-len_adpt3 > -1 else sb[0:s]
#print sa[:trim_a]
#print sb[trim_b:]
#print ""
#raw_input()
common = lib
break
if trim_a == na and trim_b == 0 and common != na:
common = -1
trim_a = p5
trim_b = -1
if p3 != -1:
trim_b = p3 + len_adpt3
#print ":::",trim_a,trim_b
return (trim_a, trim_b, common, mis, mis5, mis3)
#
#
#
def fix_N_in_overlap(ya, ybr, yb, la, lb, shift = -1):
# replaces N with its corresponding nucleotide from the mate read
# if the mate read overlaps over N
fixed_Ns = 0
if shift != -1 and (ya.find('N') != -1 or ybr.find('N') != -1):
#print "-----------------------"
#print la,lb,shift
#print ' '*shift+ya
#print ybr
e = lb - shift
if e > la:
e = la
f = lb
if la + shift < lb:
f = la + shift
ya1 = ya[0:e]
ya2 = ya[e:]
yb1 = ybr[0:shift]
yb2 = ybr[shift:f]
yb3 = ybr[f:lb]
c = []
#print "before"
#print ya1+'-'+ya2
#print yb2+'-'+yb3
for (ia,ib) in zip(ya1,yb2):
if ia == 'N' and ib != 'N':
c.append([ib,ib])
fixed_Ns = fixed_Ns + 1
elif ia != 'N'and ib == 'N':
c.append([ia,ia])
fixed_Ns = fixed_Ns + 1
else:
c.append([ia,ib])
c = zip(*c)
ya1 = ''.join(c[0])
yb2 = ''.join(c[1])
#print "after"
#print ya1
#print yb2
#print "final"
#print ya1 + ya2
#print dnaReverseComplement(yb1+yb2+yb3)
#print "-----------------------"
#
return (ya1 + ya2, dnaReverseComplement(yb1 + yb2 + yb3), fixed_Ns)
else:
return (ya, yb, fixed_Ns)
#
#
#
def compute(stuff):
#def compute(mate, reads_overlap, wiggle, adapter5, adapter3, flag_log):
mate = stuff[0]
w = stuff[1]
reads_overlap = w.reads_overlap
wiggle = w.wiggle
adapter5 = w.adapter5
adapter3 = w.adapter3
flag_log = w.flag_log
a = mate[1]
b = dnaReverseComplement(mate[5])
bb = mate[5]
na = len(a)
nb = len(b)
(qa, qb, com, ml, m5, m3) = fast_alignment(a, b, overlap = reads_overlap, wiggle = wiggle, adpt5 = adapter5, adpt3 = adapter3)
st1 = -1
st2 = -1
stn = -1
jj = 0
#print qa,qb,na,nb
# trimming
fixedns = 0
if com > -1:
if com == na: # full/perfect overlap
jj = jj + 1
(mate[1], mate[5], fixed_Ns) = fix_N_in_overlap(a, b, bb, na, nb, nb - com)
fixedns = fixedns + fixed_Ns
#mate[5] = 'N' * nb
#mate[5] = "N" ## orig
#mate[7] = mate[7][0] + '\n' ## orig
mate[5] = dnaReverseComplement(mate[1])
mate[7] = mate[3][:-1][::-1] + '\n'
stn = nb
elif qa != na: # partial overlap
if qb != 0:
jj = jj + 2
if qa == 0: # both have the adapter and no overlap
#mate[1] = 'N'
#mate[5] = 'N'
mate[1] = 'N'
mate[3] = mate[3][0] + '\n'
mate[5] = 'N'
mate[7] = mate[7][0] + '\n'
st1 = 0
st2 = 0
else: # qa != na & qa != 0 & qb != 0 => overlap and two adapters
(mate[1], mate[5], fixed_Ns) = fix_N_in_overlap(a, b, bb, na, nb, nb - com)
fixedns = fixedns + fixed_Ns
mate[1] = mate[1][0:qa]
mate[3] = mate[3][0:qa] + '\n'
#mate[5] = 'N' ## orig
#mate[7] = mate[7][0] + '\n' ## orig
mate[5] = dnaReverseComplement(mate[1])
mate[7] = mate[3][:-1][::-1] + '\n'
st1 = qa
st2 = nb-qb
stn = qa
else: # qa != na & qb == 0 => overlapping and only one adapter is found (on first read) instead of two adapters
jj = jj + 1
(mate[1], mate[5], fixed_Ns) = fix_N_in_overlap(a, b, bb, na, nb, nb - com)
fixedns = fixedns + fixed_Ns
mate[1] = mate[1][0:qa] # just trimming. should I set the other read to N?
mate[3] = mate[3][0:qa] + '\n'
#mate[5] = 'N'
#mate[7] = mate[7][0] + '\n'
st1 = qa
elif qb != 0: # qa == na & qb != 0 => overlapping and only one adapter is found (on second read) instead of two adapters
jj = jj + 1
(mate[1], mate[5], fixed_Ns) = fix_N_in_overlap(a, b, bb, na, nb, nb - com)
fixedns = fixedns + fixed_Ns
#mate[1] = 'N'
#mate[3] = mate[3][0]+'\n'
mate[5] = mate[5][0:nb-qb] # just trimming. should I set the other read to N?
mate[7] = mate[7][0:nb-qb] + '\n'
st1 = nb-qb
#statn[nb] = statn.get(nb,0) + 1
else: # if there is no overlap but still I find the adapter => trim
# brute force
# searching directly for the adapter
if qa != -1:
jj = jj + 1
m5 = 0
if qa == 0:
mate[1] = 'N'
mate[3] = mate[3][0] + '\n'
st1 = 0
else:
mate[1] = mate[1][0:qa]
mate[3] = mate[3][0:qa] + '\n'
st1 = qa
if qb != -1:
jj = jj + 1
m3 = 0
if qb == nb:
mate[5] = 'N'
mate[7] = mate[7][0] + '\n'
st1 = 0
else:
mate[5] = mate[5][:nb-qb]
mate[7] = mate[7][:nb-qb] + '\n'
st1 = nb-qb
mate[1] = mate[1] + '\n'
mate[5] = mate[5] + '\n'
x = ''
if flag_log:
x1 = ' ' * (nb - com) + ' ' * qa + '*' * (na-qa)+'\n' if com != -1 else ' ' * qa + '*' * (na-qa)+'\n' if m5 != -1 else '\n'
x2 = '-' * (nb - com) + a + '\n' if com != -1 else a+'\n'
x3 = b + '-' * (na - com)+'\n' if com != -1 else b + '\n'
x4 = '*' * qb+'\n'
x5 = "mismatches in overlapping part = %d \n" % (ml,)
x6 = "mismatches in 3-end adapter = %d \n" % (m5,)
x7 = "mismatches in 5-end adapter = %d \n" % (m3,)
x8 = "length overlapping = %d \n" % (com)
x = ''.join(["ID READ 1: "+mate[0],
"ID READ 2: "+mate[4],
"SEQ READ 1: "+a+'\n',
"SEQ READ 2: "+bb+'\n',
"FIXED SEQ READ 1: "+mate[1],
"FIXED SEQ READ 2: "+mate[5],
'\n',
'Alignment:\n',
x1,
x2,
x3,
x4,
x5,
x6,
x7,
x8,
'\n',
'\n',
'\n'
])
return (mate, st1, st2, stn, jj, x, fixedns)
def norepeats(x, t = 0.30):
cc = dict()
tt = float(len(x)-1) * t
xx = (x[i:i+2] for i in range(0,len(x)-1))
r = True
for u in xx:
cc[u] = cc.get(u,0) + 1
if cc[u] >= tt:
r = False
break
return r
#
#
#
class param:
def __init__(self):
self.reads_overlap = None
self.wiggle = None
self.adapter5 = None
self.adapter3 = None
self.flag_log = None
#
#
#
def trim_tail_n(s,q,count = 1):
# trim tails of N from a read
if s:
n = len(s)
if n != 1:
ts = s.rstrip('N').rstrip('.')
m = len(ts)
r = n - m
ts = ts.lstrip('N').rstrip('.')
l = m - len(ts)
if l+r >= count:
s = s[l:n-r]
q = q[l:n-r]
if not s:
s = "N"
q = "I"
return (s+'\n',q+'\n')
#
#
#
def trim_adapter(input_file_1,
input_file_2,
output_file_1,
output_file_2,
log_file,
align_file,
len_adapter,
reads_overlap,
reads_infer_adapter,
threshold_infer_adapter,
verbose = False,
link = 'soft',
shortest_read = 20,
trim_n = 3,
cpus = 0):
#
#
# finding automatically for adapters
# - read only the first few millions pairs of reads
if verbose:
print >>sys.stderr,"Reading the files for automated finding of adapters..."
print >>sys.stderr," - ",input_file_1
print >>sys.stderr," - ",input_file_2
i = 0
# looking for adapter
adapt5 = dict()
adapt3 = dict()
#
for mate in first_reads_from_paired_fastq_file(
input_file_1,
input_file_2,
reads_infer_adapter):
a = mate[1]
b = dnaReverseComplement(mate[5])
i = i + 1
(a5,a3) = fast_alignment_adapter(a, b, len_adapter = len_adapter)
if a5:
adapt5[a5] = adapt5.get(a5,0) + 1
if a3:
adapt3[a3] = adapt3.get(a3,0) + 1
#
# deal with the adapter
# sort the most common flanking sequences
#
a5 = [(v,k) for (k,v) in adapt5.items()]
a3 = [(v,k) for (k,v) in adapt3.items()]
# remove the adapters which contain homopolymers!
a5 = [(v,k) for (v,k) in a5 if len(set(k)) > 2]
a3 = [(v,k) for (v,k) in a3 if len(set(k)) > 2]
# remove the adapter which have repeats
a5 = [(v,k) for (v,k) in a5 if norepeats(k,0.3)]
a3 = [(v,k) for (v,k) in a3 if norepeats(k,0.3)]
# sort based on count
a5 = sorted(a5, reverse = True)
a3 = sorted(a3, reverse = True)
adapter5 = ""
adapter3 = ""
adapter3reverse = ""
if a5:
count_adapter5 = a5[0][0]
adapter5 = a5[0][1]
if a5[0][0] > threshold_infer_adapter * float(reads_infer_adapter):
if verbose:
print >>sys.stderr,"Adapter 3' end found!\n [%s...]\n [reverse-complement:...%s]\n [count=%d/%d] (%.5f%%)" % (adapter5,dnaReverseComplement(adapter5),a5[0][0],reads_infer_adapter,100*float(a5[0][0])/reads_infer_adapter)
else:
if verbose:
print >>sys.stderr,"Found [%s...]\n [reverse-complement=...%s]\n [count=%d/%d (%.5f%%)] but does not look like 3-end adapter! (too low count)" % (adapter5,dnaReverseComplement(adapter5), a5[0][0], reads_infer_adapter,100*float(a5[0][0])/reads_infer_adapter)
adapter5 = None
if a3:
count_adapter3 = a3[0][0]
adapter3 = a3[0][1]
adapter3reverse = dnaReverseComplement(adapter3)
if a3[0][0] > threshold_infer_adapter * float(reads_infer_adapter):
if verbose:
print >>sys.stderr,"Adapter 5' end found!\n [%s...]\n [reverse-complement:...%s]\n [count=%d/%d (%.5f%%)]" % (adapter3reverse, adapter3,a3[0][0],reads_infer_adapter,100*float(a3[0][0])/reads_infer_adapter)
else:
if verbose:
print >>sys.stderr,"Found [%s...]\n [reverse-complement=...%s]\n [count=%d/%d (%.5f%%)] but does not look like 5-end adapter! (too low count)" % (adapter3reverse, adapter3, a3[0][0], reads_infer_adapter,100*float(a3[0][0])/reads_infer_adapter)
adapter3 = None
#
# remove the adapters
#
threshold = 10 # if more than 10% of reads have adapter then use more than one process, else use only one process
if (reads_infer_adapter != 0) and adapter5 and adapter3 and ((100*float(a5[0][0])/reads_infer_adapter >= threshold) or (100*float(a3[0][0])/reads_infer_adapter >= threshold)):
if cpus == 0:
cpus = multiprocessing.cpu_count()
else:
if verbose:
print >>sys.stderr,"NOTE: Too few adapters found in order to use several processes! Only one CPU will be used!"
cpus = 1
if verbose:
print >>sys.stderr,"Using",cpus,"process(es)..."
#
flag_log = False
log = None
if align_file:
flag_log = True
log = lines_to_file(align_file)
log_stat = None
if log_file:
log_stat = lines_to_file(log_file)
stat = dict()
statn = dict()
if adapter5 and adapter3:
# I found both adapters
#
if verbose:
print >>sys.stderr,"Scanning for adapters..."
i = 0
j = 0
last_j = j
out_1 = lines_to_file(output_file_1)
out_2 = lines_to_file(output_file_2)
para = param()
para.reads_overlap = reads_overlap
para.wiggle = 2
para.adapter5 = adapter5
para.adapter3 = adapter3
para.flag_log = flag_log
#
all_fixed = 0
give = None
if cpus == 1:
for stuff in itertools.imap(
compute,
itertools.izip_longest(
reads_from_paired_fastq_file(
input_file_1,
input_file_2
),
[],
fillvalue = para
)
):
i = i + 1
mate = stuff[0]
st1 = stuff[1]
st2 = stuff[2]
stn = stuff[3]
jj = stuff[4]
xx = stuff[5]
fixed = stuff[6]
all_fixed = all_fixed + fixed
if st1 != -1:
stat[st1] = stat.get(st1,0) + 1
if st2 != -1:
stat[st2] = stat.get(st2,0) + 1
if stn != -1:
statn[stn] = statn.get(stn,0) + 1
j = j + jj
if trim_n:
# do N trimming from both ends
mm1 = mate[1].rstrip('\r\n')
if mm1.startswith('N') or mm1.endswith('N'):
mm2 = mate[3].rstrip('\r\n')
(mate[1], mate[3]) = trim_tail_n(mm1,mm2,trim_n)
mm1 = mate[5].rstrip('\r\n')
if mm1.startswith('N') or mm1.endswith('N'):
mm2 = mate[7].rstrip('\r\n')
(mate[5], mate[7]) = trim_tail_n(mm1,mm2,trim_n)
if len(mate[1]) < shortest_read + 1:
out_1.add_line(mate[0])
out_1.add_line(empty_read[1])
out_1.add_line(empty_read[2])
out_1.add_line(empty_read[3])
else:
out_1.add_lines(mate[0:4])
if len(mate[5]) < shortest_read + 1:
out_2.add_line(mate[4])
out_2.add_line(empty_read[1])
out_2.add_line(empty_read[2])
out_2.add_line(empty_read[3])
else:
out_2.add_lines(mate[4:8])
if flag_log and last_j != j:
log.add_line(xx)
last_j = j
if verbose and i % 1 == 10000000: # 10000000
print >>sys.stderr," %d pair-reads [ %d (%f%%) reads trimmed ]" % (i,j,100*float(j)/float(2*i))
else:
pool = multiprocessing.Pool(processes = cpus)
for stuff in pool.imap_unordered(
compute,
itertools.izip_longest(
reads_from_paired_fastq_file(
input_file_1,
input_file_2
),
[],
fillvalue = para
),
chunksize = 100
):
i = i + 1
mate = stuff[0]
st1 = stuff[1]
st2 = stuff[2]
stn = stuff[3]
jj = stuff[4]
xx = stuff[5]
fixed = stuff[6]
all_fixed = all_fixed + fixed
if st1 != -1:
stat[st1] = stat.get(st1,0) + 1
if st2 != -1:
stat[st2] = stat.get(st2,0) + 1
if stn != -1:
statn[stn] = statn.get(stn,0) + 1
j = j + jj
if trim_n:
# do N trimming from both ends
mm1 = mate[1].rstrip('\r\n')
if mm1.startswith('N') or mm1.endswith('N'):
mm2 = mate[3].rstrip('\r\n')
(mate[1], mate[3]) = trim_tail_n(mm1,mm2,trim_n)
mm1 = mate[5].rstrip('\r\n')
if mm1.startswith('N') or mm1.endswith('N'):
mm2 = mate[7].rstrip('\r\n')
(mate[5], mate[7]) = trim_tail_n(mm1,mm2,trim_n)
if len(mate[1]) < shortest_read + 1:
out_1.add_line(mate[0])
out_1.add_line(empty_read[1])
out_1.add_line(empty_read[2])
out_1.add_line(empty_read[3])
else:
out_1.add_lines(mate[0:4])
if len(mate[5]) < shortest_read + 1:
out_2.add_line(mate[4])