-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alignment.cpp
374 lines (333 loc) · 11 KB
/
Alignment.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
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
#include "Alignment.h"
#include "Parser.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <utility>
#include <cmath>
using namespace std;
Alignment::Alignment() {
pairs.reserve(N);
}
void Alignment::clear() {
header.clear();
index = 0;
insideIntron = false;
donorFlag = false;
introns.clear();
alignmentStart = 0;
}
int Alignment::parse(ifstream& inputStream) {
clear();
bool geneStarted = false;
vector<string> blockLines(BLOCK_ITEMS_CNT);
// Read header
string line;
int status = FORMAT_FAIL;
if (getline(inputStream, line)) {
status = parseHeader(line);
}
if (status != READ_SUCCESS) {
cerr << "error: Invalid alignment header " << endl;
return status;
}
// Read content
while (getline(inputStream, line)) {
if (line.empty()) {
break;
}
if (!geneStarted) {
if (line[0] != '-') {
if (readAlignmentStart(line) != READ_SUCCESS) {
cerr << "warning: error in alignment " << header[1] << "-" << header[2];
cerr << ": Invalid alignment start" << endl;
return FORMAT_FAIL;
}
geneStarted = true;
}
}
// Read block of 5 five lines containing alignment
// The last block might be shorter than BLOCK_LENGTH
unsigned int blockLength = line.find(" ", BLOCK_OFFSET) - BLOCK_OFFSET;
if (line.size() <= BLOCK_OFFSET) {
printLineError();
return FORMAT_FAIL;
}
blockLines[0] = line.substr(BLOCK_OFFSET, blockLength);
for (unsigned i = 1; i < BLOCK_ITEMS_CNT; i++) {
if (getline(inputStream, line) && !line.empty() && line.size() > BLOCK_OFFSET) {
blockLines[i] = line.substr(BLOCK_OFFSET, blockLength);
if (blockLines[i].size() != blockLength) {
printLineError();
return FORMAT_FAIL;
}
} else {
printLineError();
return FORMAT_FAIL;
}
}
// Parse this block
parseBlock(blockLines);
}
if (!geneStarted) {
cerr << "warning: error in alignment " << header[1] << "-" << header[2];
cerr << ": No alignment" << endl;
return FORMAT_FAIL;
}
return READ_SUCCESS;
}
int Alignment::parseHeader(const string& headerString) {
stringstream ss(headerString);
string column;
while (getline(ss, column, '\t')) {
header.push_back(column);
}
if (header.size() != HEADER_SIZE) {
return FORMAT_FAIL;
}
return READ_SUCCESS;
}
int Alignment::readAlignmentStart(const string& headerString) {
stringstream ss(headerString);
ss >> alignmentStart;
if (ss.fail()) {
return FORMAT_FAIL;
}
realPositionCounter = alignmentStart;
return READ_SUCCESS;
}
void Alignment::parseBlock(const vector<string>& lines) {
// Parse individual pairs
for (unsigned i = 0; i < lines[0].size(); i++) {
AlignedPair pair(lines[0][i], lines[1][i], lines[2][i],
lines[3][i], lines[4][i]);
mapCodons(pair);
checkForIntron(pair);
if (pair.nucleotide != '-') {
pair.realPosition = realPositionCounter++;
}
// Reuse space if possible
if ((int) pairs.size() <= index) {
pairs.push_back(pair);
} else {
pairs[index] = pair;
}
index++;
}
}
void Alignment::printLineError() {
cerr << "warning: error in alignment " << header[1] << "-" << header[2];
cerr << ": corrupted alignment - wrong line length.";
cerr << " The rest of this alignment is skipped." << endl;
}
void Alignment::mapCodons(AlignedPair& pair) {
if (index > 0) {
if (pair.translatedCodon >= 'A' && pair.translatedCodon <= 'Z') {
pairs[index - 1].translatedCodon = '1';
}
if (pair.protein >= 'A' && pair.protein <= 'Z') {
pairs[index - 1].protein = '1';
}
if (pair.translatedCodon == ' ' && pairs[index - 1].translatedCodon >= 'A'
&& pairs[index - 1].translatedCodon <= 'Z') {
pair.translatedCodon = '3';
}
if (pair.protein == ' ' && pairs[index - 1].protein >= 'A'
&& pairs[index - 1].protein <= 'Z') {
pair.protein = '3';
}
}
}
void Alignment::checkForIntron(AlignedPair& pair) {
if (donorFlag) {
introns.back().donor[1] = pair.nucleotide;
donorFlag = false;
// If the read is at donor position, there is nothing else to check for
return;
}
if (!insideIntron && pair.type == 'i') { // intron start
Intron i;
i.start = index;
i.donor[0] = pair.nucleotide;
introns.push_back(i);
insideIntron = true;
donorFlag = true;
} else if (insideIntron && pair.type != 'i') { // intron end
introns.back().end = index - 1;
introns.back().acceptor[0] = pairs[index - 2].nucleotide;
introns.back().acceptor[1] = pairs[index - 1].nucleotide;
insideIntron = false;
}
}
void Alignment::storeIntrons(IntronStorage& storage) {
for (unsigned int i = 0; i < introns.size(); i++) {
string spliceSites(introns[i].donor, 2);
spliceSites.append("_");
spliceSites.append(introns[i].acceptor, 2);
if (!introns[i].scoreSet) {
introns[i].score = 0;
}
storage.storeIntron(header[2], header[1],
pairs[introns[i].start].realPosition,
pairs[introns[i].end].realPosition,
'+', spliceSites, introns[i].score, i + 1);
}
}
void Alignment::print(ostream& os) {
for (int i = 0; i < index; i += BLOCK_LENGTH) {
for (int j = 0; (j < BLOCK_LENGTH && i + j < index); j++) {
os << pairs[i + j].nucleotide;
}
os << endl;
for (int j = 0; (j < BLOCK_LENGTH && i + j < index); j++) {
os << pairs[i + j].translatedCodon;
}
os << endl;
for (int j = 0; (j < BLOCK_LENGTH && i + j < index); j++) {
os << pairs[i + j].protein;
}
os << endl;
for (int j = 0; (j < BLOCK_LENGTH && i + j < index); j++) {
os << pairs[i + j].type;
}
os << endl;
for (int j = 0; (j < BLOCK_LENGTH && i + j < index); j++) {
os << '*';
}
os << endl;
}
}
bool Alignment::hasIntrons() {
return introns.size() != 0;
}
string Alignment::getGene() {
return header[1];
}
string Alignment::getProtein() {
return header[2];
}
int Alignment::getLength() {
return index;
}
Alignment::Intron::Intron() {
scoreSet = false;
}
void Alignment::scoreIntrons(int windowWidth, bool multiply,
const ScoreMatrix * scoreMatrix, Kernel * kernel) {
this->scoreMatrix = scoreMatrix;
this->kernel = kernel;
this->kernel->setWidth(windowWidth);
for (unsigned int i = 0; i < introns.size(); i++) {
if (!introns[i].scoreSet) {
scoreIntron(introns[i], windowWidth, multiply);
}
}
}
double Alignment::scoreIntron(Intron& intron, int windowWidth, bool multiply) {
intron.leftScore = intron.rightScore = 0;
intron.leftWeightSum = intron.rightWeightSum = 0;
int left, right;
// Determine if codon is split and how
if (pairs[intron.start - 1].protein == '3' ||
pairs[intron.start - 1].translatedCodon == '3') {
// Codon is not split
left = intron.start - 2;
right = intron.end + 2;
} else {
if (pairs[intron.start - 2].protein == '3'
|| pairs[intron.start - 2].translatedCodon == '3') {
// Codon is split after the first nucleotide
left = intron.start - 3;
right = intron.end + 4;
// Majority of the codon belongs to the right side
double weight = kernel->getWeight(0);
intron.rightScore += pairs[intron.start - 1].score(scoreMatrix) * weight;
intron.rightWeightSum += weight;
} else {
// Codon is split after the second nucleotide
left = intron.start - 4;
right = intron.end + 3;
// Majority of the codon belongs to the left side
double weight = kernel->getWeight(0);
intron.leftScore += pairs[intron.start - 1].score(scoreMatrix) * weight;
intron.leftWeightSum += weight;
}
}
scoreLeft(intron, left, windowWidth);
scoreRight(intron, right, windowWidth);
// Normalize alignments by their length, otherwise alignments
// in short exons between introns are penalized
if (multiply) {
if (intron.leftWeightSum == 0 || intron.rightWeightSum == 0 ||
intron.leftScore < 0 || intron.rightScore < 0) {
intron.score = 0;
} else {
intron.score = (intron.leftScore / (intron.leftWeightSum)) *
(intron.rightScore / (intron.rightWeightSum));
intron.score = sqrt(intron.score);
}
} else {
intron.score = (intron.leftScore + intron.rightScore) /
(intron.leftWeightSum + intron.rightWeightSum);
if (intron.score < 0) {
intron.score = 0;
}
}
intron.scoreSet = true;
return intron.score;
}
void Alignment::scoreLeft(Intron & intron, int start, int windowWidth) {
for (int i = start; i > (start - windowWidth * 3); i -= 3) {
// Check for end of local alignment
if (i < 0 || pairs[i].type != 'e') {
return;
}
double weight = kernel->getWeight((i - start) / 3);
intron.leftWeightSum += weight;
intron.leftScore += pairs[i].score(scoreMatrix) * weight;
}
}
void Alignment::scoreRight(Intron & intron, int start, int windowWidth) {
for (int i = start; i < (start + windowWidth * 3); i += 3) {
// Check for end of local alignment
if (i >= index || pairs[i].type != 'e') {
return;
}
double weight = kernel->getWeight((i - start) / 3);
intron.rightWeightSum += weight;
intron.rightScore += pairs[i].score(scoreMatrix) * weight;
}
}
Alignment::AlignedPair::AlignedPair(char n, char tc, char q, char p, char type) :
nucleotide(n),
translatedCodon(tc),
protein(p) {
if (type == '*') {
if (protein == '.') {
this->type = 'i';
} else {
this->type = 'e';
}
} else {
this->type = ' ';
}
// Assign random amino acid to a stop codon, so it is treated as a gap
if (translatedCodon == '*') {
translatedCodon = 'A';
}
quality = BAD_MATCH;
if (q == '|') {
quality = COMPLETE_MATCH;
} else if (q == '+') {
quality = GOOD_MATCH;
} else if (nucleotide == '-' || protein == '-') {
quality = GAP;
}
}
double Alignment::AlignedPair::score(const ScoreMatrix * scoreMatrix) {
if (scoreMatrix != NULL) {
return scoreMatrix->getScore(translatedCodon, protein);
}
return quality;
}