-
Notifications
You must be signed in to change notification settings - Fork 1
/
deblur_fastq_trim_cat.py
executable file
·154 lines (124 loc) · 4.77 KB
/
deblur_fastq_trim_cat.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
#!/usr/bin/env python
"""
Reads a pair of fastqs, trims each to indicated length, and returns the
concatenation of the read pairs. e.g.:
in1: ACGCTCATCTAGGGCATACGCGT
=================------
in2: GCTCTATCGCGATACGCGTATGC
=================------
out: ACGCTCATCTAGGGCATGCTCTATCGCGATACGC
1111111111111111122222222222222222
optionally can choose which orientation, e.g. ff, rf, fr, or rr, but NOT
reverse complement.
usage:
filter_fastq_by_fastq.py -r1 [fastq f read] -r2 [fastq r read] -t1 150 -t2 150 > out.fastq
"""
from __future__ import print_function
import sys
import gzip
import argparse
from itertools import izip
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-r1', '--read1_fastq',
type=str,
help='path to fastq for read one')
parser.add_argument('-r2', '--read2_fastq',
type=str,
help='path to fastq for read two')
parser.add_argument('-t1', '--read1_trim',
type=int,
help="number of bases from 5' end for read 1 (default all bases)")
parser.add_argument('-t2', '--read2_trim',
type=int,
help="number of bases from 5' end for read 2 (default all bases)")
parser.add_argument('-o', '--orientation',
type=str, default='ff',
help='path to fastq for read two (default: %(default)s')
def readfq(fp): # this is a generator function
"""
From https://github.com/lh3/readfq/blob/master/readfq.py
"""
last = None # this is a buffer keeping the last unprocessed line
while True: # mimic closure; is it a bad idea?
if not last: # the first record or a record following a fastq
for l in fp: # search for the start of the next record
if l[0] in '>@': # fasta/q header line
last = l[:-1] # save this line
break
if not last: break
name, seqs, last = last[1:].partition(" ")[0], [], None
for l in fp: # read the sequence
if l[0] in '@+>':
last = l[:-1]
break
seqs.append(l[:-1])
if not last or last[0] != '+': # this is a fasta record
yield name, ''.join(seqs), None # yield a fasta record
if not last: break
else: # this is a fastq record
seq, leng, seqs = ''.join(seqs), 0, []
for l in fp: # read the quality
seqs.append(l[:-1])
leng += len(l) - 1
if leng >= len(seq): # have read enough quality
last = None
yield name, seq, ''.join(seqs); # yield a fastq record
break
if last: # reach EOF before reading enough quality
yield name, seq, None # yield a fasta record instead
break
def trim_cat_fastqs(r1_f, r2_f, r1_t, r2_t, orientation='ff'):
i = 0
for ((n1,s1,q1),(n2,s2,q2)) in izip(readfq(r1_f),readfq(r2_f)):
i += 1
if n1[-2:] in ['/1','/2']:
n1 = n1[:-2]
if n2[-2:] in ['/1','/2']:
n2 = n2[:-2]
if n1 != n2:
raise IOError("Sequences don't match!\nname1: {0}\nname2: {1}".format(n1,n2))
if len(s1) < r1_t:
raise IndexError("Read 1 is too short to trim: "
"\n{0}\n{1}\n{2}\n{3}".format(n1,s1,q1,'='*r1_t))
if len(s2) < r2_t:
raise IndexError("Read 2 is too short to trim: "
"\n{0}\n{1}\n{2}\n{3}".format(n2,s2,q2,'='*r2_t))
s1_t = s1[:r1_t]
q1_t = q1[:r1_t]
if orientation[0] == 'r':
s1_t = s1_t[::-1]
q1_t = q1_t[::-1]
s2_t = s2[:r2_t]
q2_t = q2[:r2_t]
if orientation[1] == 'r':
s2_t = s2_t[::-1]
q2_t = q2_t[::-1]
print('@{0}\n{1}\n+\n{2}'.format(n1, s1_t + s2_t, q1_t + q2_t))
return(i)
def main():
args = parser.parse_args()
read1_fastq = args.read1_fastq
read2_fastq = args.read2_fastq
read1_trim = args.read1_trim
read2_trim = args.read2_trim
orientation = args.orientation
i = 0
try:
r1_f = gzip.open(read1_fastq)
r2_f = gzip.open(read2_fastq)
i = trim_cat_fastqs(r1_f, r2_f, read1_trim, read2_trim, orientation)
except IOError:
try:
r1_f = open(read1_fastq)
r2_f = open(read2_fastq)
i = trim_cat_fastqs(r1_f, r2_f, read1_trim, read2_trim, orientation)
except IOError as e:
print('Could not open input files:\n%s' % e,file=sys.stderr)
except Exception as e:
print(e,file=sys.stderr)
except Exception as e:
print(e,file=sys.stderr)
print('Joined {0} records'.format(i, file=sys.stderr),file=sys.stderr)
if __name__ == "__main__":
main()