File: SeqInput.h

package info (click to toggle)
flexbar 1%3A3.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: cpp: 3,524; xml: 603; sh: 128; perl: 51; makefile: 18
file content (204 lines) | stat: -rw-r--r-- 4,490 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
// SeqInput.h

#ifndef FLEXBAR_SEQINPUT_H
#define FLEXBAR_SEQINPUT_H

#include <seqan/seq_io.h>
#include "QualTrimming.h"


template <typename TSeqStr, typename TString>
class SeqInput {

private:
	
	seqan::FlexbarReadsSeqFileIn seqFileIn;
	const flexbar::QualTrimType m_qtrim;
	const flexbar::FileFormat m_format;
	
	const bool m_preProcess, m_useStdin, m_qtrimPostRm, m_iupacInput;
	const int m_maxUncalled, m_preTrimBegin, m_preTrimEnd, m_qtrimThresh, m_qtrimWinSize;
	std::atomic<unsigned long> m_nrReads, m_nrChars, m_nLowPhred;
	
public:
	
	SeqInput(const Options &o, const std::string filePath, const bool preProcess, const bool useStdin) :
		
		m_preProcess(preProcess),
		m_useStdin(useStdin),
		m_maxUncalled(o.maxUncalled),
		m_preTrimBegin(o.cutLen_begin),
		m_preTrimEnd(o.cutLen_end),
		m_qtrim(o.qTrim),
		m_qtrimThresh(o.qtrimThresh),
		m_qtrimWinSize(o.qtrimWinSize),
		m_qtrimPostRm(o.qtrimPostRm),
		m_iupacInput(o.iupacInput),
		m_format(o.format),
		m_nrReads(0),
		m_nrChars(0),
		m_nLowPhred(0){
		
		using namespace std;
		
		if(m_useStdin){
			if(! open(seqFileIn, cin)){
				cerr << "\nERROR: Could not open input stream.\n" << endl;
				exit(1);
			}
		}
		else{
			if(! open(seqFileIn, filePath.c_str())){
				cerr << "\nERROR: Could not open file " << filePath << "\n" << endl;
				exit(1);
			}
		}
	};
	
	virtual ~SeqInput(){
		close(seqFileIn);
	};
	
	
	// returns number of read SeqReads
	unsigned int loadSeqReads(seqan::StringSet<bool> &uncalled, flexbar::TStrings &ids, flexbar::TSeqStrs &seqs, flexbar::TStrings &quals, const unsigned int nReads){
		
		using namespace std;
		using namespace flexbar;
		
		using seqan::prefix;
		using seqan::suffix;
		using seqan::length;
		
		try{
			if(! atEnd(seqFileIn)){
				
				reserve(ids,      nReads);
				reserve(seqs,     nReads);
				reserve(uncalled, nReads);
				
				if(! m_iupacInput){
					
					if(m_format == FASTA){
						readRecords(ids, seqs, seqFileIn, nReads);
					}
					else{
						reserve(quals, nReads);
						readRecords(ids, seqs, quals, seqFileIn, nReads);
					}
				}
				else{
					seqan::StringSet<seqan::IupacString> seqsIupac;
					
					reserve(seqsIupac, nReads);
					
					if(m_format == FASTA){
						readRecords(ids, seqsIupac, seqFileIn, nReads);
					}
					else{
						reserve(quals, nReads);
						readRecords(ids, seqsIupac, quals, seqFileIn, nReads);
					}
					
					seqs = seqsIupac;
				}
				
				for(unsigned int i = 0; i < length(ids); ++i){
					
					TString &id  =  ids[i];
					TSeqStr &seq = seqs[i];
					
					if(length(id) < 1){
						cerr << "\nERROR: Input read without name.\n" << endl;
						close(seqFileIn);
						exit(1);
					}
					if(length(seq) < 1){
						cerr << "\nERROR: Input read without sequence.\n" << endl;
						close(seqFileIn);
						exit(1);
					}
					
					m_nrChars += length(seq);
					
					appendValue(uncalled, isUncalledSequence(seq));
					
					if(m_preProcess){
						
						if(m_preTrimBegin > 0 && length(seq) > 1){
							
							int idx = m_preTrimBegin;
							if(idx >= length(seq)) idx = length(seq) - 1;
							
							erase(seq, 0, idx);
							
							if(m_format == FASTQ)
							erase(quals[i], 0, idx);
						}
						
						if(m_preTrimEnd > 0 && length(seq) > 1){
							
							int idx = m_preTrimEnd;
							if(idx >= length(seq)) idx = length(seq) - 1;
							
							seq = prefix(seq, length(seq) - idx);
							
							if(m_format == FASTQ)
							quals[i] = prefix(quals[i], length(quals[i]) - idx);
						}
						
						if(m_qtrim != QOFF && ! m_qtrimPostRm){
							if(qualTrim(seq, quals[i], m_qtrim, m_qtrimThresh, m_qtrimWinSize)) ++m_nLowPhred;
						}
					}
				}
				
				m_nrReads += length(ids);
				
				return length(ids);
			}
			
			else return 0;  // end of file
		}
		catch(seqan::Exception const &e){
			cerr << "\nERROR: " << e.what() << "\nProgram execution aborted.\n" << endl;
			close(seqFileIn);
			exit(1);
		}
	}
	
	
	// returns TRUE if read contains too many uncalled bases
	bool isUncalledSequence(TSeqStr &seq){
		
		using namespace seqan;
		
		typename Iterator<TSeqStr>::Type it, itEnd;
		
		it    = begin(seq);
		itEnd = end(seq);
		int n = 0;
		
		while(it != itEnd){
			 if(*it == 'N') n++;
			 ++it;
		}
		return(n > m_maxUncalled);
	}
	
	
	unsigned long getNrLowPhredReads() const {
		return m_nLowPhred;
	}
	
	unsigned long getNrProcessedReads() const {
		return m_nrReads;
	}
	
	unsigned long getNrProcessedChars() const {
		return m_nrChars;
	}
	
};

#endif