-
Notifications
You must be signed in to change notification settings - Fork 2
/
trimfastq.py
250 lines (229 loc) · 7.69 KB
/
trimfastq.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
##################################
# #
# Last modified 12/06/2012 #
# #
# Georgi Marinov #
# #
##################################
import sys
import os
try:
import psyco
psyco.full()
except:
pass
def main(argv):
if len(argv) < 2:
print 'usage: python %s <inputfilename> <bpToKeep | max> [-trim5 bp] [-flowcellID flowcell] [-addEnd 1 | 2] [-replace string newstring | blank] [-renameIDs prefix] [-stdout]' % argv[0]
print '\tthe -trim5 option will trim additional bp from the 5 end, i.e. if you want the middle 36bp of 38bp reads, use 36 as bp to keep and 1 as the trim5 argument'
print '\tUse - to specify standard input, and the -stdout option to tell the script to print to standard output'
print '\tThe script can read compressed files as long as they have the correct suffix - .bz2 or .gz'
print '\tReplace inputfilename with - if you want to read from standard input'
sys.exit(1)
inputfilename = argv[1]
doMax=False
if argv[2] == 'max':
doMax=True
trim='max'
else:
trim = int(argv[2])
outputfilename = inputfilename.split('/')[-1].split('.fastq')[0] + '.' +str(trim)+'mers.fastq'
doFlowcellID=False
doStdOut=False
if '-stdout' in argv:
doStdOut = True
if '-flowcellID' in argv:
doFlowcellID=True
flowcellID=argv[argv.index('-flowcellID')+1]
if doStdOut:
pass
else:
print 'will include flowcell ID', flowcellID, 'in reads headers'
doRenameIDs = False
if '-renameIDs' in argv:
doRenameIDs = True
RID = '@' + argv[argv.index('-renameIDs') + 1]
dotrim5=False
if '-trim5' in argv:
dotrim5=True
trim5=int(argv[argv.index('-trim5')+1])
if doStdOut:
pass
else:
print 'will trim ', trim5, 'bp from the 5-end'
outputfilename = inputfilename.split('.fastq')[0] + '.' +str(trim)+'bp-5prim-trim.fastq'
doAddEnd=False
if '-addEnd' in argv:
doAddEnd=True
END=argv[argv.index('-addEnd')+1]
if doStdOut:
pass
else:
print 'will add', '/'+END, 'to read IDs'
doReplace=False
if '-replace' in argv:
doReplace=True
oldstring=argv[argv.index('-replace')+1]
newstring=argv[argv.index('-replace')+2]
if newstring == 'blank':
newstring=''
if doStdOut:
pass
else:
print 'will replace', oldstring, 'with', newstring, 'in read IDs'
i=0
shorter=0
if doStdOut:
pass
else:
outfile = open(outputfilename, 'w')
doStdIn = False
if inputfilename != '-':
if inputfilename.endswith('.bz2'):
cmd = 'bzip2 -cd ' + inputfilename
elif inputfilename.endswith('.gz'):
cmd = 'gunzip -c ' + inputfilename
else:
cmd = 'cat ' + inputfilename
p = os.popen(cmd, "r")
else:
doStdIn = True
line = 'line'
if dotrim5:
i=1
j=0
while line != '':
if doStdIn:
line = sys.stdin.readline()
else:
line = p.readline()
if line == '':
break
if i==1 and line[0]=='@':
if doFlowcellID and flowcellID not in line:
ID='@'+flowcellID+'_'+line.replace(' ','_')[1:-1]+'\n'
else:
ID=line.replace(' ','_')
if doReplace:
ID=ID.replace(oldstring,newstring)
if doRenameIDs:
ID = RID + str(j)
if doAddEnd:
ID=ID.strip()+'/'+END+'\n'
i=2
continue
if i==2:
i=3
sequence=line[trim5:len(line)].strip()
continue
if i==3 and line[0]=='+':
plus='+\n'
i=4
continue
if i==4:
scores=line
i=1
scores=line[trim5:len(line)].strip()
scores=scores[0:trim]
j=j+1
if j % 5000000 == 0:
if doStdOut:
pass
else:
print str(j/1000000) + 'M reads processed'
if doMax:
sequence=sequence.replace('.','N')
else:
sequence=sequence[0:trim].replace('.','N')+'\n'
if doStdOut:
print ID.strip()
print sequence.strip()
print plus.strip()
print scores
else:
outfile.write(ID.strip()+'\n')
outfile.write(sequence.strip()+'\n')
outfile.write(plus.strip()+'\n')
outfile.write(scores + '\n')
continue
else:
i=1
j=0
while line != '':
if doStdIn:
line = sys.stdin.readline()
else:
line = p.readline()
if line == '':
break
if i==1 and line[0]=='@':
if doFlowcellID and flowcellID not in line:
ID='@'+flowcellID+'_'+line.replace(' ','_')[1:-1]+'\n'
else:
ID=line.replace(' ','_')
if doReplace:
ID=ID.replace(oldstring,newstring)
if doRenameIDs:
ID = RID + str(j)
if doAddEnd:
ID=ID.strip()+'/'+END+'\n'
i=2
continue
if i==2:
i=3
j=j+1
if j % 5000000 == 0:
if doStdOut:
pass
else:
print str(j/1000000) + 'M reads processed'
if doMax:
sequence=line
else:
if len(line.strip())<trim:
shorter+=1
sequence=line.strip().replace('.','N')+'\n'
else:
sequence=line[0:trim].replace('.','N')+'\n'
continue
if i==3 and line[0]=='+':
plus='+\n'
i=4
continue
if i==4:
i=1
if doMax:
scores=line
if doStdOut:
print ID.strip()
print sequence.strip()
print plus.strip()
print line.strip()
else:
outfile.write(ID)
outfile.write(sequence)
outfile.write(plus)
outfile.write(line)
else:
if len(line.strip())<trim:
continue
scores=line[0:trim]+'\n'
if doStdOut:
print ID.strip()
print sequence.strip()
print plus.strip()
print scores.strip()
else:
outfile.write(ID)
outfile.write(sequence)
outfile.write(plus)
outfile.write(scores)
continue
if doStdOut:
pass
else:
outfile.close()
if shorter>0:
print shorter, 'sequences shorter than desired length'
if __name__ == '__main__':
main(sys.argv)