-
Notifications
You must be signed in to change notification settings - Fork 4
/
01_merge_paired_fastq_v4.0.py
executable file
·265 lines (205 loc) · 7.8 KB
/
01_merge_paired_fastq_v4.0.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
#!/usr/bin/python
## AUTHOR: Eric Fontanillas
## LAST VERSION: 14.04.2011
## DESCRIPTION: merge read1.fastq and read2.fastq files (from illumina 1.5 outputs with different numbers of read1 and read2)and remove sequences with low quality Phred score (if too much B at the end of a reads)
import string, os, sys
#N=2 ## Fasta format
N=4 ## Fastq format
## CHECK IF ALL ARGUMENTS
if len(sys.argv) == 5:
IN1 = open(sys.argv[1], "r")
IN2 = open(sys.argv[2], "r")
PREFIX = sys.argv[3]
else:
print "\n\tThe command takes 4 arguments!! USAGE:\n\n\t\t$ merge_paired_fastq.py read1 read2 prefix_name_for_output Max_B_allowed\n"
sys.exit()
## Phred quality score cutoff
B_cutoff = int(sys.argv[4]) ## Max of "B" authorized => if B_Cutoff > read length ===> means no cutoff are apply (no low quality sequence excluded!)
OUT1 = open("../tmp/%s_Bcutoff%d_paired.fas" %(PREFIX, B_cutoff), "w") ## paired fasta
OUT1_1 = open("../tmp/%s_Bcutoff%d_paired_read1.fas" %(PREFIX, B_cutoff), "w") ## paired fasta
OUT1_2 = open("../tmp/%s_Bcutoff%d_paired_read2.fas" %(PREFIX, B_cutoff), "w") ## paired fasta
OUT2 = open("../tmp/%s_Bcutoff%d_paired.fasq" %(PREFIX, B_cutoff), "w") ## paired fastq
OUT2_1 = open("../tmp/%s_Bcutoff%d_paired_read1.fasq" %(PREFIX, B_cutoff), "w") ## paired fastq
OUT2_2 = open("../tmp/%s_Bcutoff%d_paired_read2.fasq" %(PREFIX, B_cutoff), "w") ## paired fastq
OUT3 = open("../tmp/%s_Bcutoff%d_single1.fas" %(PREFIX, B_cutoff), "w") ## single 1 fasta
OUT4 = open("../tmp/%s_Bcutoff%d_single1.fasq" %(PREFIX, B_cutoff), "w") ## single 1 fastq
OUT5 = open("../tmp/%s_Bcutoff%d_single2.fas" %(PREFIX, B_cutoff), "w") ## single 2 fasta
OUT6 = open("../tmp/%s_Bcutoff%d_single2.fasq" %(PREFIX, B_cutoff), "w") ## single 2 fastq
OUT_read1_badQuality = open("../tmp/%s_Bcutoff%d_read1_badQuality.fasq" %(PREFIX, B_cutoff), "w")
OUT_read2_badQuality = open("../tmp/%s_Bcutoff%d_read2_badQuality.fasq" %(PREFIX, B_cutoff), "w")
LOG = open("../tmp/%s_Bcutoff%d_Counts_reads_and_pairs.log" %(PREFIX, B_cutoff), "w")
#i=1
j=1
b=0
bash1={}
while 1:
new = IN1.readline()
if not new:
LOG.write("[Reads 1]\n")
LOG.write("TOTAL reads number = %d reads\n" %j)
percent_removed = float(b)/float(j)*100
remaining = j - b
percent_remaining = float(remaining)/float(j)*100
LOG.write("Reads keeped (Good Phred Quality Score) = %d reads (%d %%)\n" %(remaining,percent_remaining))
LOG.write("Reads removed (Too low Phred Quality Score) = %d reads (%d %%)\n\n" %(b,percent_removed))
break
if j%10000 ==0:
print "[READ1] %d" %j
if new[0] == "@":
j=j+1
name = new[1:-2] ## remove "@" + "1" and "\n"
name_fasta = ">" + new[1:-1]
name_fastq = new[:-1]
new = IN1.readline()
seq = new[:-1]
new = IN1.readline() ## remove the name beginning with "+"
phred_name = new[:-1]
new = IN1.readline()
phred_quality = new[:-1]
B = string.count(phred_quality, "B")
if B <= B_cutoff:
bash1[name] =[seq,name_fasta,name_fastq, phred_name, phred_quality]
else:
b=b+1
OUT_read1_badQuality.write("%s\n" %name_fastq)
OUT_read1_badQuality.write("%s\n" %seq)
OUT_read1_badQuality.write("%s\n" %phred_name)
OUT_read1_badQuality.write("%s\n" %phred_quality)
#i=i+1
IN1.close()
#i=1
j=1
b=0
bash2={}
while 1:
new = IN2.readline()
if not new:
#print "\n\tTOTAL Lines = %d lines\n" %i
#seq_nb = j
LOG.write("[Reads 2]\n")
LOG.write("TOTAL reads number = %d reads\n" %j)
percent_removed = float(b)/float(j)*100
remaining = j - b
percent_remaining = float(remaining)/float(j)*100
LOG.write("Reads keeped (Good Phred Quality Score) = %d reads (%d %%)\n" %(remaining,percent_remaining))
LOG.write("Reads removed (Low Phred Quality Score) = %d reads (%d %%)\n\n" %(b,percent_removed))
break
if j%10000 ==0:
print "[READ2] %d" %j
if new[0] == "@":
j=j+1
name = new[1:-2] ## remove "@" + "1" and "\n"
name_fasta = ">" + new[1:-1]
name_fastq = new[:-1]
new = IN2.readline()
seq = new[:-1]
new = IN2.readline() ## remove the name beginning with "+"
phred_name = new[:-1]
new = IN2.readline()
phred_quality = new[:-1]
B = string.count(phred_quality, "B")
if B <= B_cutoff:
bash2[name] =[seq,name_fasta,name_fastq, phred_name, phred_quality]
else:
b=b+1
OUT_read2_badQuality.write("%s\n" %name_fastq)
OUT_read2_badQuality.write("%s\n" %seq)
OUT_read2_badQuality.write("%s\n" %phred_name)
OUT_read2_badQuality.write("%s\n" %phred_quality)
#i=i+1
IN2.close()
######################################
p = 0
s1 = 0
s2 = 0
for name in bash1.keys():
list1 = bash1[name]
seq1 = list1[0]
name_fasta1 = list1[1]
name_fastq1 = list1[2]
name_phred1 = list1[3]
phred_quality1 = list1[4]
## 1 ## Save "paired 1 and 2 reads"
try:
list2 = bash2[name]
seq2 = list2[0]
name_fasta2 = list2[1]
name_fastq2 = list2[2]
name_phred2 = list2[3]
phred_quality2 = list2[4]
## a ## Print fasta output
OUT1.write("%s\n" %name_fasta1)
OUT1.write("%s\n" %seq1)
OUT1_1.write("%s\n" %name_fasta1)
OUT1_1.write("%s\n" %seq1)
OUT1.write("%s\n" %name_fasta2)
OUT1.write("%s\n" %seq2)
OUT1_2.write("%s\n" %name_fasta2)
OUT1_2.write("%s\n" %seq2)
## b ## Print fastq output
p=p+1
OUT2.write("%s\n" %name_fastq1)
OUT2.write("%s\n" %seq1)
OUT2.write("%s\n" %name_phred1)
OUT2.write("%s\n" %phred_quality1)
OUT2_1.write("%s\n" %name_fastq1)
OUT2_1.write("%s\n" %seq1)
OUT2_1.write("%s\n" %name_phred1)
OUT2_1.write("%s\n" %phred_quality1)
OUT2.write("%s\n" %name_fastq2)
OUT2.write("%s\n" %seq2)
OUT2.write("%s\n" %name_phred2)
OUT2.write("%s\n" %phred_quality2)
OUT2_2.write("%s\n" %name_fastq2)
OUT2_2.write("%s\n" %seq2)
OUT2_2.write("%s\n" %name_phred2)
OUT2_2.write("%s\n" %phred_quality2)
## 2 ## Save "single 1 reads"
except:
## a ## Print fasta output
OUT3.write("%s\n" %name_fasta1)
OUT3.write("%s\n" %seq1)
## b ## Print fastq output
s1 = s1 + 1
OUT4.write("%s\n" %name_fasta1)
OUT4.write("%s\n" %seq1)
OUT4.write("%s\n" %name_phred1)
OUT4.write("%s\n" %phred_quality1)
continue
for name in bash2.keys():
list2 = bash2[name]
seq2 = list2[0]
name_fasta2 = list2[1]
name_fastq2 = list2[2]
name_phred2 = list2[3]
phred_quality2 = list2[4]
try: ## Already done
list1 = bash1[name]
seq1 = list1[0]
name_fasta1 = list1[1]
name_fastq1 = list1[2]
name_phred1 = list1[3]
phred_quality1 = list1[4]
except: ## 3 ## Save "single 2 reads"
## a ## Print fasta output
OUT5.write("%s\n" %name_fasta2)
OUT5.write("%s\n" %seq2)
## b ## Print fastq output
s2 = s2 + 2
OUT6.write("%s\n" %name_fasta2)
OUT6.write("%s\n" %seq2)
OUT6.write("%s\n" %name_phred2)
OUT6.write("%s\n" %phred_quality2)
continue
LOG.write("[Merged]\n")
LOG.write("Pairs recovered = %d\n" %p)
LOG.write("Short single from read1 = %d\n" %s1)
LOG.write("Short single from read2 = %d\n" %s2)
OUT1.close()
OUT2.close()
OUT3.close()
OUT4.close()
OUT5.close()
OUT6.close()
OUT_read1_badQuality.close()
OUT_read2_badQuality.close()