-
Notifications
You must be signed in to change notification settings - Fork 0
/
genome.cpp
287 lines (242 loc) · 7.5 KB
/
genome.cpp
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
#include "genome.h"
#include "readcontainer.h"
#include "utils.h"
#include "lockfreequeue.h"
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <memory>
#include <thread>
#include <sstream>
#include <cstdio>
#include <iostream>
Genome::Genome ( bool init_m, bool init_c )
: multistrand((init_m) ? new link_set_t : nullptr),
circular((init_c) ? new link_set_t : nullptr),
readPos(new chr_map_t)
{
}
Genome::~Genome() {
for (auto& chromosome : *readPos) {
delete chromosome;
}
if (multistrand != nullptr) {
delete multistrand;
}
if (circular != nullptr) {
delete circular;
}
}
chr_num_t Genome::getChrNum ( const std::string name ) const {
auto lookup = chrNums.find(name);
assume(lookup != chrNums.end(), "Genome::getChrNum: Chromosome " + name + " not found");
return lookup->second;
}
std::string Genome::getChrName ( const chr_num_t num ) const {
assume(!chrNames.empty(), "Genome::getChrName: name list empty");
assume(num < chrNames.size(),
"Genome::getChrName Index " + std::to_string(num) + " out of range (" +
std::to_string(chrNames.size()) + ")");
return chrNames[num];
}
short unsigned int Genome::getLength ( const chr_num_t n ) const {
// assume(n < chrLens.size(), "Genome::getLength: index out of range " + std::to_string(n));
return chrLens.at(n);
}
void Genome::createChromosome ( const std::string name, const uint32_t length ) {
chr_num_t N = chrNames.size();
chrNames.push_back(name);
chrLens.push_back(length);
chrNums.emplace( name, N );
readPos->push_back(new std::map<chr_pos_t, p_read_t>());
}
p_read_t Genome::createRead( const chr_num_t chr, const chr_pos_t pos, const short len ) {
p_read_t rc = getReadAt(chr, pos);
if (rc == nullptr) { // no read on chr:pos exists yet
rc = std::make_shared<ReadContainer>(chr, pos, len);
readPos->at(chr)->emplace(pos, rc); // register read with genome navigation map
}
return rc;
}
p_read_t Genome::getReadAt ( const chr_num_t chr, const chr_pos_t pos ) const {
auto chromosome = readPos->at(chr);
auto lookup = chromosome->find(pos);
if (lookup == chromosome->end()) {
return nullptr;
}
return (*lookup).second;
}
p_read_t Genome::getReadAt ( const std::string chr, const chr_pos_t pos ) const {
return getReadAt( getChrNum(chr), pos );
}
void Genome::read ( std::ifstream& input ) {
log("Genome: reading file...");
unsigned int L(1); // line counter
LockFreeQueue<std::vector<std::string>> q;
auto readerTask = [&q,&input]() {
debug("Starting reader thread...");
for (std::string line; getline(input, line);) {
q.push(strsplit(line, "\t"));
}
q.done();
debug("All data read and tokenized, closing reader rhread");
};
auto parserTask = [&q,&L,this]() {
debug("Starting parser thread...");
std::vector<std::string> splits;
while(q.pop(splits)) {
assume(parseDataLine(splits), "Could not parse data in line " + std::to_string(L), false);
L++;
}
debug("All tokens processed, closing parser thread");
};
// read and parse header lines sequentially
for (std::string line; getline(input, line);) {
// std::cout << "Line #" << L << std::endl;
if (line[0] == '@') {
assume(parseHeaderLine(line), "Could not parse header in line " + std::to_string(L), false);
}
else {
break; // header lines parsed, break and begin threaded processing
}
L++;
}
// init and start threads
std::thread readerThread(readerTask);
std::thread parserThread(parserTask);
// wait for threads to finish
readerThread.join();
parserThread.join();
if (multistrand != nullptr) {
log("Found " + std::to_string(multistrand->size()) + " strand switching events");
}
if (circular != nullptr) {
log("Found " + std::to_string(circular->size()) + " circular transcripts");
}
}
void Genome::read ( std::string& fileName ) {
std::ifstream input(fileName);
assume(input.good(), "Could not open file: " + fileName);
read(input);
}
std::ifstream& operator >> ( std::ifstream& input, Genome& target ) {
target.read(input);
return input;
}
bool Genome::parseHeaderLine ( const std::string& line ) {
try {
switch (line[1]) {
case 'H': // Token @HD: General file info
log(line + "\n" );
break;
case 'S': { // Token @SQ: Reference dictionary entry
auto tokens = strsplit(line, "\t", false);
std::string name = strsplit(tokens[1], ":")[1];
unsigned short len = atoi( strsplit(tokens[2], ":")[1].c_str() );
createChromosome(name, len);
}
break;
case 'P': // Token @PG: program used
log(line + "\n");
assume(strsplit(line, "\t")[1] == "ID:segemehl", "This program is designed for segemehl output only!", false);
default: // Ignore other tokens
break;
}
} catch (const std::exception e) { // keep going on errors, but warn
std::cerr << e.what();
return false;
}
return true;
}
bool Genome::parseDataLine ( std::vector<std::string>& tokens ) {
// auto tokens = strsplit(line, "\t");
unsigned char flags = 0;
chr_num_t chr = getChrNum(tokens[RNAME]);
chr_pos_t pos5 = atoi( tokens[POS].c_str() );
chr_pos_t pos3;
bool hasNext = false;
chr_num_t nextChr = 0;
chr_pos_t nextPos = 0;
bool hasPrev = false;
chr_num_t prevChr = 0;
chr_pos_t prevPos = 0;
int start = 0;
int end = 0;
auto iter = tokens.begin();
for (int i(0); i < QUAL; i++) iter++;
for (;iter != tokens.end(); iter++) {
if ((*iter)[0] == 'X') {
if ((*iter)[2] != ':' || (*iter)[4] != ':') { // doesn't suffice Xx:y:z, e. g. PHRED-String starting with X
continue;
}
switch((*iter)[1]) {
case 'X': // start of current split
start = atoi( (*iter).substr(5).c_str() );
break;
case 'Y': // end of current split
end = atoi( (*iter).substr(5).c_str() );
break;
case 'Q': // number of current split
flags |= ReadContainer::SPLIT;
break;
case 'P': // refseq of prev split
hasPrev = true;
prevChr = (chr_num_t)getChrNum( (*iter).substr(5) );
break;
case 'U': // 3' of prev
prevPos = (chr_pos_t)atoi( (*iter).substr(5).c_str() );
break;
case 'C': // refseq of next split
hasNext = true;
nextChr = (chr_num_t)getChrNum( (*iter).substr(5) );
break;
case 'V': // 5' of next split
nextPos = (chr_pos_t)atoi( (*iter).substr(5).c_str() );
break;
default:
break;
}
}
}
if (start == end);
pos3 = pos5 + calcLength(tokens[CIGAR]);
p_read_t rc = getReadAt(chr, pos5);
if (rc == nullptr) {
rc = std::make_shared<ReadContainer>(chr, pos5, pos3-pos5);
registerRead(rc);
rc->flags |= flags;
}
if (hasNext) {
rc->addDownstreamRead(*this, nextChr, nextPos);
}
if (hasPrev) {
rc->addUpstreamRead(*this, prevChr, -prevPos);
}
return true;
}
void Genome::registerRead ( p_read_t rc ) {
const chr_num_t chr = rc->chromosome;
readPos->at(chr)->emplace(rc->fivePrimeEnd, rc); // Link 5' end to identify as successor
readPos->at(chr)->emplace(-rc->threePrimeEnd, rc); // Link 3' end to identify as predecessor
}
uint Genome::calcLength ( const std::string cigar ) const {
int L = 0;
std::string tmp = "";
for (size_t i(0); i < cigar.length(); i++) {
if (cigar[i] == 'M' || cigar[i] == 'I') {
L += atoi(tmp.c_str());
tmp = "";
}
else if (cigar[i] == 'D') {
L -= atoi(tmp.c_str());
tmp = "";
}
else {
tmp += cigar[i];
}
}
return (uint)L;
}