File: FastaReader.cpp

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (421 lines) | stat: -rw-r--r-- 10,664 bytes parent folder | download | duplicates (4)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "Common/IOUtil.h"
#include "Common/StringUtil.h"
#include "DataLayer/FastaReader.h"
#include "DataLayer/Options.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

namespace opt {
	/**
	 * Copy the BX tag from the input SAM line to the FASTA/FASTQ comment.
	 * The BX tag contains the linked-reads barcode for the read.
	 * If BX tag is absent, the FASTA/FASTQ comment will be empty.
	 */
	int bxTag;

	/** Discard reads that failed the chastity filter. */
	int chastityFilter = 1;

	/** Trim masked (lower case) characters from the ends of
	 * sequences.
	 */
	int trimMasked = 1;

	/** minimum quality threshold */
	int qualityThreshold;

	/** quality offset, usually 33 or 64 */
	int qualityOffset;

	/** minimum quality for internal bases */
	int internalQThreshold;
}

/** Output an error message. */
ostream& FastaReader::die()
{
	return cerr << m_path << ':' << m_line << ": error: ";
}

FastaReader::FastaReader(const char* path, int flags, int len)
	: m_path(path), m_fin(path),
	m_in(strcmp(path, "-") == 0 ? cin : m_fin),
	m_flags(flags), m_line(0), m_unchaste(0),
	m_end(numeric_limits<streamsize>::max()),
	m_maxLength(len)
{
	if (strcmp(path, "-") != 0)
		assert_good(m_fin, path);
	if (m_in.peek() == EOF)
		cerr << m_path << ':' << m_line << ": warning: "
			"file is empty\n";
}

/** Split the fasta file into nsections and seek to the start
 * of section. */
void FastaReader::split(unsigned section, unsigned nsections)
{
	assert(nsections >= section);
	assert(section > 0);
	assert(strcmp(m_path, "-") != 0);
	if (nsections == 1)
		return;
	// Move the get pointer to the first entry in this section and
	// update the m_end if there is more than one section.
	m_in.seekg(0, ios::end);
	streampos length = m_in.tellg();
	assert(length > 0);
	streampos start = length * (section - 1) / nsections;
	streampos end = length * section / nsections;
	assert(end > 0);
	if (end < length) {
		m_in.seekg(end);
		if (m_in.peek() == '>')
			end += 1;
	}
	m_end = end;
	m_in.seekg(start);
	if (start > 0) {
		m_in.ignore(numeric_limits<streamsize>::max(), '\n');
		m_in.ignore(numeric_limits<streamsize>::max(), '>');
		if (m_in.peek() == EOF)
			cerr << m_path << ':' << section << ": warning: "
				"there are no contigs in this section\n";
		m_in.putback('>');
	}
	assert(m_end > 0);
	assert(m_in.good());
}

/** Return whether this read passed the chastity filter. */
bool FastaReader::isChaste(const string& s, const string& line)
{
	if (s == "1" || s == "Y") {
		return true;
	} else if (s == "0" || s == "N") {
		return false;
	} else {
		die() << "chastity filter should be one of 0, 1, N or Y\n"
			"and saw `" << s << "' near\n" << line << endl;
		exit(EXIT_FAILURE);
	}
}

/** Check that the seqeuence and quality agree in length. */
void FastaReader::checkSeqQual(const string& s, const string& q)
{
	if (s.length() != q.length()) {
		die() << "sequence and quality must be the same length near\n"
			<< s << '\n' << q << endl;
		exit(EXIT_FAILURE);
	}
}

/** Return whether the read seq is in colour space. */
static bool isColourSpace(const string& seq)
{
	assert(!seq.empty());
	size_t i = seq.find_first_of("ACGTacgt0123", 1);
	return i != string::npos && isdigit(seq[i]);
}

/** Read a single record. */
Sequence FastaReader::read(string& id, string& comment,
		char& anchor, string& q)
{
next_record:
	id.clear();
	comment.clear();
	anchor = 0;
	q.clear();

	// Discard comments.
	while (m_in.peek() == '#')
		ignoreLines(1);

	signed char recordType = m_in.peek();
	Sequence s;

	unsigned qualityOffset = 0;
	if (recordType == EOF || m_in.tellg() >= m_end) {
		m_in.seekg(0, ios::end);
		m_in.clear(std::ios::eofbit | std::ios::failbit);
		return s;
	} else if (recordType == '>' || recordType == '@') {
		// Read the header.
		string header;
		getline(header);
		istringstream headerStream(header);

		// Ignore SAM headers.
		if (header[0] == '@' && isalpha(header[1])
				&& isalpha(header[2]) && header[3] == '\t')
			goto next_record;

		headerStream >> recordType >> id >> ws;
		std::getline(headerStream, comment);

		// Casava FASTQ format
		if (comment.size() > 3
				&& comment[1] == ':' && comment[3] == ':') {
			// read, chastity, flags, index: 1:Y:0:AAAAAA
			if (opt::chastityFilter && comment[2] == 'Y') {
				m_unchaste++;
				if (recordType == '@') {
					ignoreLines(3);
				} else {
					while (m_in.peek() != '>' && m_in.peek() != '#'
							&& ignoreLines(1))
						;
				}
				goto next_record;
			}
			if (id.size() > 2 && id.rbegin()[1] != '/') {
				// Add the read number to the ID.
				id += '/';
				id += comment[0];
			}
		}

		getline(s);
		if (recordType == '>') {
			// Read a multi-line FASTA record.
			string line;
			while (m_in.peek() != '>' && m_in.peek() != '#'
					&& getline(line))
				s += line;
			if (m_in.eof())
				m_in.clear();
		}

		if (recordType == '@') {
			char c = m_in.get();
			if (c != '+') {
				string line;
				getline(line);
				die() << "expected `+' and saw ";
				if (m_in.eof())
					cerr << "end-of-file\n";
				else
					cerr << "`" << c << "' near\n"
					<< c << line << "\n";
				exit(EXIT_FAILURE);
			}
			ignoreLines(1);
			getline(q);
		} else
			q.clear();

		if (s.empty()) {
			die() << "sequence with ID `" << id << "' is empty\n";
			exit(EXIT_FAILURE);
		}

		bool colourSpace = isColourSpace(s);
		if (colourSpace && !isdigit(s[0])) {
			// The first character is the primer base. The second
			// character is the dibase read of the primer and the
			// first base of the sample, which is not part of the
			// assembly.
			assert(s.length() > 2);
			anchor = colourToNucleotideSpace(s[0], s[1]);
			s.erase(0, 2);
			q.erase(0, 1);
		}

		if (!q.empty())
			checkSeqQual(s, q);

		if (opt::trimMasked && !colourSpace) {
			// Removed masked (lower case) sequence at the beginning
			// and end of the read.
			size_t trimFront = 0;
			while (trimFront <= s.length() && islower(s[trimFront]))
				trimFront++;
			size_t trimBack = s.length();
			while (trimBack > 0 && islower(s[trimBack - 1]))
				trimBack--;
			s.erase(trimBack);
			s.erase(0, trimFront);
			if (!q.empty()) {
				q.erase(trimBack);
				q.erase(0, trimFront);
			}
		}
		if (flagFoldCase())
			transform(s.begin(), s.end(), s.begin(), ::toupper);

		qualityOffset = 33;
	} else {
		string line;
		vector<string> fields;
		fields.reserve(22);
		getline(line);
		istringstream in(line);
		string field;
		while (std::getline(in, field, '\t'))
			fields.push_back(field);

		if (fields.size() >= 11
				&& (fields[9].length() == fields[10].length()
					|| fields[10] == "*")) {
			// SAM
			unsigned flags = strtoul(fields[1].c_str(), NULL, 0);
			if (flags & 0x100) // FSECONDARY
				goto next_record;
			if (opt::chastityFilter && (flags & 0x200)) { // FQCFAIL
				m_unchaste++;
				goto next_record;
			}
			id = fields[0];
			char which_read = '0';
			switch (flags & 0xc1) { // FPAIRED|FREAD1|FREAD2
			  case 0:
			  case 1: // FPAIRED
				which_read = '0';
				break;
			  case 0x41: // FPAIRED|FREAD1
				id += "/1";
				which_read = '1';
				break;
			  case 0x81: // FPAIRED|FREAD2
				id += "/2";
				which_read = '2';
				break;
			  default:
				die() << "invalid flags: `" << id << "' near"
					<< line << endl;
				exit(EXIT_FAILURE);
			}
			if (opt::bxTag) {
				// Copy the linked-reads barcode BX tag to the FASTA comment.
				for (unsigned i = 11; i < fields.size(); ++i) {
					if (startsWith(fields[i], "BX:Z:")) {
						comment = fields[i];
						break;
					}
				}
			} else {
				comment = flags & 0x200 ? "0:Y:0:" : "0:N:0:"; // FQCFAIL
				comment[0] = which_read;
			}

			s = fields[9];
			q = fields[10];
			if (s == "*")
				s.clear();
			if (q == "*")
				q.clear();
			if (flags & 0x10) { // FREVERSE
				s = reverseComplement(s);
				reverse(q.begin(), q.end());
			}
			qualityOffset = 33;
			if (!q.empty())
				checkSeqQual(s, q);

		} else if (fields.size() == 11 || fields.size() == 22) {
			// qseq or export
			if (opt::chastityFilter
					&& !isChaste(fields.back(), line)) {
				m_unchaste++;
				goto next_record;
			}

			ostringstream o;
			o << fields[0];
			for (int i = 1; i < 6; i++)
				if (!fields[i].empty())
					o << ':' << fields[i];
			if (!fields[6].empty() && fields[6] != "0")
				o << '#' << fields[6];
			// The reverse read is typically the second read, but is
			// the third read of an indexed run.
			o << '/' << (fields[7] == "3" ? "2" : fields[7]);
			id = o.str();
			comment = fields[7];
			comment += isChaste(fields.back(), line)
				? ":N:0:" : ":Y:0:";
			s = fields[8];
			q = fields[9];
			qualityOffset = 64;
			checkSeqQual(s, q);
		} else {
			die() << "Expected either `>' or `@' or 11 fields\n"
					"and saw `" << recordType << "' and "
					<< fields.size() << " fields near\n"
					<< line << endl;
			exit(EXIT_FAILURE);
		}
	}

	if (opt::qualityOffset > 0)
		qualityOffset = opt::qualityOffset;

	// Trim from the 3' end to the maximum length. Then, trim based on
	// quality.
	if (m_maxLength > 0) {
		s.erase(m_maxLength);
		q.erase(m_maxLength);
	}

	static const char ASCII[] =
		" !\"#$%&'()*+,-./0123456789:;<=>?"
		"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
		"`abcdefghijklmnopqrstuvwxyz{|}~";
	if (opt::qualityThreshold > 0 && !q.empty()) {
		assert(s.length() == q.length());
		assert(qualityOffset > (unsigned)ASCII[0]);
		const char* goodQual = ASCII + (qualityOffset - ASCII[0])
			+ opt::qualityThreshold;
		size_t trimFront = q.find_first_of(goodQual);
		size_t trimBack = q.find_last_of(goodQual) + 1;
		if (trimFront >= trimBack) {
			// The entire read is poor quality.
			s.erase(1);
			q.erase(1);
		} else if (trimFront > 0 || trimBack < q.length()) {
			s.erase(trimBack);
			s.erase(0, trimFront);
			q.erase(trimBack);
			q.erase(0, trimFront);
		}
	}

	if (opt::internalQThreshold > 0 && !q.empty()) {
		assert(s.length() == q.length());
		assert(qualityOffset > (unsigned)ASCII[0]);
		const char* internalGoodQual = ASCII
			+ (qualityOffset - ASCII[0])
			+ opt::internalQThreshold;
		size_t i = 0;
		while ((i = q.find_first_not_of(internalGoodQual, i))
				!= string::npos)
			s[i++] = 'N';
	}

	assert(qualityOffset >= 33);
	if (flagConvertQual() && qualityOffset != 33) {
		// Convert to standard quality (ASCII 33).
		for (string::iterator it = q.begin(); it != q.end(); ++it) {
			int x = *it - qualityOffset;
			if (x < -5 || x > 41) {
				die() << "quality " << x
					<< " is out of range -5 <= q <= 41 near\n"
					<< q << '\n'
					<< string(it - q.begin(), ' ') << "^\n";
				exit(EXIT_FAILURE);
			}
			*it = 33 + max(0, x);
		}
	}

	return s;
}