-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastQReader.py
294 lines (252 loc) · 9.62 KB
/
FastQReader.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
#!/usr/bin/env python3
# Name:Gabriel Pires(gapires)
# Group Members: Jeffrey Nguyen (jehunguy)
"""
OVERVIEW:
This is the main converter (custom module).
fastqread.py takes in the input file, parsing the file
and checks for proper formatting. If it's incorrect, go to
next set of FastQ lines starting at the header. This allows for parsing
correctly while not having to have to use the ASCII characters
'@' or '+' to check for headers.
This program also holds the required methods to convert from
one format to either Sanger(Phred+33) or Illumina1.3(Phred+64).
INPUT:
Takes in File name from the commandline file.
OUTPUT:
Each method that maybe called from the FastQCommand program
yields each line given by my fastqread parse method.
For quality score lines, it yields the converted desired output format.
ASSUMPTION:
If incorrect input, does not tell user where, it just ignores it.
"""
#PSEUDOCODE:
#Take in the fastqfile.
# for line in fastqfile
# check line 1 for @
# check line 2
# check line 3 for +
# check len(line4)==len(line2)
# if all checks out: yield this line.
# for line given from file:
# yield line if lines 1-3
# convert quality score in line:
# convert to quality value ord(char)-33 or -64 to offset ASCII table.
# convert back to quality score of another format chr(ord(char)-33 or -64)+33 or +64)
# yield this converted string.
class FastQreader :
"""
Takes in file and parses it with readFastq.
Each method aside converts from one format to another.
This is converting to quality value with ord()
and conveting back to quality score with chr().
"""
def __init__ (self, fname):
"""
initializes filename passed in and the
output string and the empty store list
Initializes dictionaries.
"""
self.fname = fname
self.QualityScoreOut = ''
self.storeList=[[],[],[],[]]
#stores each line in fastq file
self.solexaDict = {-1:3, -2:2, -3:2, -4:1,-5:1,0:3, 1:4,2:4,3:5,4:5,
5:6,6:7,7:8,8:9,9:10,10:10,11:11,12:12,13:13,14:14,
15:15,16:16,17:17,18:18,19:19,20:20,21:21,22:22,23:23,24:24,
25:25,26:26,27:27,28:28,29:29,30:30,31:31,32:32,33:33,34:34,35:35,36:36,37:37,38:38,
39:39,40:40}
#Q conversion for solexa Q values. It was more convenient to type out the dictionary than to do the conversion in code.
def Illumina_Sanger(self):
"""
Converts Illumina (Phred+64) to Sanger(Phred+33)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut=line
i+=1
else:
for quality in line:
newQual = chr((ord(quality)-64)+33)
self.QualityScoreOut+=newQual
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Sanger_Illumina(self):
"""
Converts Sanger (Phred+33) to Illumina(Phred+64)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut=line
i+=1
else:
for quality in line:
newQual = chr((ord(quality)-33)+64)
self.QualityScoreOut+=newQual
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Solexa_Sanger(self):
"""
Converts Solexa (PhredSOL+64) to Sanger(Phred+33)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut=line
i+=1
else:
for quality in line:
newVal = (ord(quality)-64)
newVal2 = self.solexaDict[newVal]
quality = chr((newVal2)+33)
self.QualityScoreOut += quality
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Solexa_Illumina(self):
"""
Converts Solexa (PhredSOL+64) to Illumina(Phred+64)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut=line
i+=1
else:
for quality in line:
newVal = (ord(quality)-64)
newVal2 = self.solexaDict[newVal]
quality = chr((newVal2)+64)
self.QualityScoreOut += quality
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Illumina15_Sanger(self):
"""
Converts Illumina1.5+ (PhredB+64) to Sanger(Phred+33)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut = line
i+=1
else:
for quality in line:
if(quality == 'B'):
quality='!'
self.QualityScoreOut+=quality
else:
newVal = chr((ord(quality)-64)+33)
self.QualityScoreOut+=newVal
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Illumina15_Illumina(self):
"""
Converts Illumina1.5+ (PhredB+64) to Illumina(Phred+64)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut = line
i +=1
else:
for quality in line:
if(quality == 'B'):
quality='@' #quality value = 0 of illumina
self.QualityScoreOut+=quality
else:
newVal = chr((ord(quality)-64)+64)
self.QualityScoreOut+=newVal
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Illumina18_Sanger(self):
"""
Converts Illumina1.8+ (PhredILL+33) to Sanger(Phred+33)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut = line
i+=1
else:
for quality in line:
newVal = chr((ord(quality)-33)+33)
self.QualityScoreOut += newVal
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def Illumina18_Illumina(self):
"""
Converts Illumina1.8 (PhredILL+33) to Sanger(Phred+33)
"""
i=0
for line in self.readFastq():
if(i==0 or i==1 or i==2):
self.QualityScoreOut=line
i+=1
else:
for quality in line:
newVal = chr((ord(quality)-33)+64)
self.QualityScoreOut += newVal
i=0
yield(self.QualityScoreOut)
self.QualityScoreOut = ''
def readFastq (self):
"""
Iterates through lines of FastQ file and parses
every four lines with a counter.
It also compensates for incorrect formating and
resyncs if incorrect. Ignores incorrectly formated
lines.
"""
i = 0
for line in self.fname:
i+=1
#iterate through lines of FastQ file
if(i==1 and line.startswith('@')):
#Check condition of line 1 to start with @ then store it.
lineHeader=line.upper().rstrip()
self.storeList[0].append(lineHeader)
elif(i==2):
lineSeq = line.rstrip().replace('*','N').replace('.','N').replace('n','N')
self.storeList[1].append(lineSeq)
elif(i==3 and line.startswith('+')):
#Check condition of line 3 to start with + then store it
lineOption=line.rstrip()
self.storeList[2].append(lineOption)
elif(i==4 and self.storeList[0] and self.storeList[1] and self.storeList[2]):
#if conditions for line 1 and 3 are true, and then append line 4
lineQuality=line.rstrip()
self.storeList[3].append(lineQuality)
if(len(lineQuality)==len(lineSeq)):
#this line 4 is same length as line2 then yield storeList
for something in self.storeList:
yield(something[0])
self.storeList=[[],[],[],[]]
lineHeader=''
lineSeq=''
lineOption=''
lineQuality=''
i=0
else:
#if line4 isn't same length, skip over to next fastq lines.
i=0
self.storeList=[[],[],[],[]]
lineHeader=''
lineSeq=''
lineOption=''
lineQuality=''
else:
#if any one of these conditions is not correct, clean list and strings.
i=0
self.storeList=[[],[],[],[]]
lineHeader=''
lineSeq=''
lineOption=''
lineQuality=''