File: fastqread.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (355 lines) | stat: -rwxr-xr-x 15,107 bytes parent folder | download | duplicates (2)
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
//
//  fastqread.cpp
//  Mothur
//
//  Created by Sarah Westcott on 1/26/15.
//  Copyright (c) 2015 Schloss Lab. All rights reserved.
//

#include "fastqread.h"


/*******************************************************************************/
FastqRead::FastqRead() {
    try {
        m = MothurOut::getInstance();
        format = "illumina1.8+"; name = ""; sequence = ""; scores.clear();
        //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
        for (int i = -64; i < 65; i++) {
            char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
            convertTable.push_back(temp);
            convertBackTable.push_back(((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499)));
        }
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
/*******************************************************************************/
FastqRead::FastqRead(Sequence s, QualityScores q) {
    try {
        m = MothurOut::getInstance(); format = "illumina1.8+";
        
        //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
        for (int i = -64; i < 65; i++) {
            char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
            convertTable.push_back(temp);
            convertBackTable.push_back(((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499)));
        }
        
        if (s.getName() != q.getName()) { m->mothurOut("[ERROR]: sequence name does not match quality score name. Found sequence named " + s.getName() + " quality scores named " + q.getName() + " Cannot construct fastq object.\n"); m->setControl_pressed(true); }
        else {
            name = s.getName();
            comment = s.getComment();
            sequence = s.getUnaligned();
            scores = q.getScores();
            scoreString = convertQual(scores);
        }
        
        
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
/*******************************************************************************/
FastqRead::FastqRead(Sequence s, QualityScores q, string f) {
    try {
        m = MothurOut::getInstance(); format = f;
        
        //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
        for (int i = -64; i < 65; i++) {
            char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
            convertTable.push_back(temp);
            convertBackTable.push_back(((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499)));
        }
        
        if (s.getName() != q.getName()) { m->mothurOut("[ERROR]: sequence name does not match quality score name. Found sequence named " + s.getName() + " quality scores named " + q.getName() + " Cannot construct fastq object.\n"); m->setControl_pressed(true); }
        else {
            name = s.getName();
            comment = s.getComment();
            sequence = s.getUnaligned();
            scores = q.getScores();
            scoreString = convertQual(scores);
        }
        
        
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
/*******************************************************************************/
FastqRead::FastqRead(string f) {
    try {
        m = MothurOut::getInstance();
        format = f; name = ""; sequence = ""; scores.clear();
        //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
        for (int i = -64; i < 65; i++) {
            char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
            convertTable.push_back(temp);
            convertBackTable.push_back(((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499)));
        }
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
/*******************************************************************************/

FastqRead::FastqRead(string f, string n, string s, vector<int> sc) {
    try {
        m = MothurOut::getInstance();
        format = f; name = n; sequence = s; scores = sc;
        //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
        for (int i = -64; i < 65; i++) {
            char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
            convertTable.push_back(temp);
            convertBackTable.push_back(((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499)));
        }
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
/*******************************************************************************/

FastqRead::FastqRead(ifstream& in, bool& ignore, string f) {
    try {
        m = MothurOut::getInstance();
        
        ignore = false;
        format = f;
        //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
        for (int i = -64; i < 65; i++) {
            char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
            convertTable.push_back(temp);
        }
        
        //read sequence name
        string line = util.getline(in); gobble(in);
        vector<string> pieces = util.splitWhiteSpace(line);
        name = "";  if (pieces.size() != 0) { name = pieces[0]; }
        if (name == "") {  m->mothurOut("[WARNING]: Blank fasta name, ignoring read.\n");  ignore=true;  }
        else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read.\n");  ignore=true; }
        else { name = name.substr(1); }
        if (pieces.size() > 1) { pieces.erase(pieces.begin()); comment = util.getStringFromVector(pieces, " "); }
        
        //read sequence
        sequence = util.getline(in); gobble(in);
        if (sequence == "") {  m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }

        //read sequence name
        line = util.getline(in); gobble(in);
        pieces = util.splitWhiteSpace(line);
        string name2 = "";  if (pieces.size() != 0) { name2 = pieces[0]; }
        if (name2 == "") {  m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
        else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
        else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
        
        //read quality scores
        string quality = util.getline(in); gobble(in);
        if (quality == "") {  m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
        
        //sanity check sequence length and number of quality scores match
        if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
        if (quality.length() != sequence.length()) { m->mothurOut("[WARNING]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores, ignoring read."); ignore=true; }
        
        scoreString = quality;
        scores = convertQual(quality);
        util.checkName(name);
        
        if (m->getDebug()) { m->mothurOut("[DEBUG]: " + name + " " + sequence + " " + quality + "\n"); }
    
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
//**********************************************************************************************************************
#ifdef USE_BOOST
FastqRead::FastqRead(boost::iostreams::filtering_istream& in, bool& ignore, string f) {
    try {
        m = MothurOut::getInstance();
        
        ignore = false;
        format = f;
        
        if (in.eof()) { ignore = true; }
        else {
            //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
            for (int i = -64; i < 65; i++) {
                char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
                convertTable.push_back(temp);
            }
            
            //read sequence name
            string line = util.getline(in); gobble(in);
            vector<string> pieces = util.splitWhiteSpace(line);
            name = "";  if (pieces.size() != 0) { name = pieces[0];  }
            if (name == "") {  m->mothurOut("[WARNING]: Blank fasta name, ignoring read.\n");  ignore=true;  }
            else if (name[0] != '@') { m->mothurOut("[WARNING]: reading " + name + " expected a name with @ as a leading character, ignoring read.\n");  ignore=true; }
            else { name = name.substr(1); }
            if (pieces.size() > 1) { pieces.erase(pieces.begin()); comment = util.getStringFromVector(pieces, " "); }
            
            //read sequence
            sequence = util.getline(in); gobble(in);
            if (sequence == "") {  m->mothurOut("[WARNING]: missing sequence for " + name + ", ignoring."); ignore=true; }
            
            //read sequence name
            line = util.getline(in); gobble(in);
            pieces = util.splitWhiteSpace(line);
            string name2 = "";  if (pieces.size() != 0) { name2 = pieces[0]; }
            if (name2 == "") {  m->mothurOut("[WARNING]: expected a name with + as a leading character, ignoring."); ignore=true; }
            else if (name2[0] != '+') { m->mothurOut("[WARNING]: reading " + name2 + " expected a name with + as a leading character, ignoring."); ignore=true; }
            else { name2 = name2.substr(1); if (name2 == "") { name2 = name; } }
            
            //read quality scores
            string quality = util.getline(in); gobble(in);
            if (quality == "") {  m->mothurOut("[WARNING]: missing quality for " + name2 + ", ignoring."); ignore=true; }
            
            //sanity check sequence length and number of quality scores match
            if (name2 != "") { if (name != name2) { m->mothurOut("[WARNING]: names do not match. read " + name + " for fasta and " + name2 + " for quality, ignoring."); ignore=true; } }
            if (quality.length() != sequence.length()) { m->mothurOut("[WARNING]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores, ignoring read."); ignore=true; }
            
            scoreString = quality;
            scores = convertQual(quality);
            util.checkName(name);
            
            if (m->getDebug()) { m->mothurOut("[DEBUG]: " + name + " " + sequence + " " + quality + "\n"); }
        }
        
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "FastqRead");
        exit(1);
    }
}
#endif
//**********************************************************************************************************************
vector<int> FastqRead::convertQual(string qual) {
    try {
        vector<int> qualScores;
        bool negativeScores = false;
        
        for (int i = 0; i < qual.length(); i++) {
            
            int temp = 0;
            temp = int(qual[i]);
            if (format == "illumina") {
                temp -= 64; //char '@'
            }else if (format == "illumina1.8+") {
                temp -= int('!'); //char '!' //33
            }else if (format == "solexa") {
                temp = int(convertTable[temp]); //convert to sanger
                temp -= int('!'); //char '!' //33
            }else {
                temp -= int('!'); //char '!' //33
            }
            
            if (temp < 0) { negativeScores = true; temp = 0; }
            qualScores.push_back(temp);
        }
        
        if (negativeScores) { m->mothurOut("[ERROR]: finding negative quality scores, do you have the right format selected? http://en.wikipedia.org/wiki/FASTQ_format#Encoding \n");  m->setControl_pressed(true);  }
        
        return qualScores;
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "convertQual");
        exit(1);
    }
}
//**********************************************************************************************************************
string FastqRead::convertQual(vector<int> qual) {
    try {
        string scoreString = "";
        
        for (int i = 0; i < qual.size(); i++) {
            int controlChar = int('!');
            if (format == "illumina") {  controlChar = int('@');  }

            int temp = qual[i] + controlChar;
            
            if (format == "solexa") { temp = convertBackTable[temp];  }
            
            char qualChar = (char) temp;
            
            scoreString += qualChar;
        }
        
        return scoreString;
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "convertQual");
        exit(1);
    }
}
//**********************************************************************************************************************
void FastqRead::setScores(vector<int> qual) {
    try {
        scoreString = "";
        scores = qual;
        
        for (int i = 0; i < qual.size(); i++) {
            int controlChar = int('!');
            if (format == "illumina") {  controlChar = int('@');  }

            int temp = qual[i] + controlChar;
            
            if (format == "solexa") { temp = convertBackTable[temp];  }
            
            char qualChar = (char) temp;
            
            scoreString += qualChar;
        }
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "setScores");
        exit(1);
    }
}
//**********************************************************************************************************************
Sequence FastqRead::getSequence() {
    try {
        Sequence temp(name, sequence);
        return temp;
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "getSequence");
        exit(1);
    }
}
//**********************************************************************************************************************
void FastqRead::printFastq(ostream& out) {
    try {
        out << "@" << name << " " << comment << endl;
        out << sequence << endl;
        out << "+" << endl;
        out << scoreString << endl;
        
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "printFastq");
        exit(1);
    }
}
//**********************************************************************************************************************
QualityScores FastqRead::getQuality() {
    try {
        QualityScores temp(name, scores);
        return temp;
    }
    catch(exception& e) {
        m->errorOut(e, "FastqRead", "getQuality");
        exit(1);
    }
}

/*******************************************************************************/