File: TestGenome_Index_TableQ.cpp

package info (click to toggle)
perm 0.4.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 976 kB
  • sloc: cpp: 13,499; makefile: 98; sh: 12
file content (373 lines) | stat: -rw-r--r-- 15,617 bytes parent folder | download | duplicates (5)
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
#include "TestGenome_Index_TableQ.h"

SimulateLongRead::SimulateLongRead(CGenomeInBits* pgenomeNTInBits, unsigned int startIndex)
{
    unsigned int halfLength = this->uiReadLength / 2;
    this->goodRead = pgenomeNTInBits->fragACGTKmerInBits(this->half1st, startIndex, halfLength);
    this->goodRead = this->goodRead & (pgenomeNTInBits->fragACGTKmerInBits(half2nd, startIndex + halfLength, halfLength));

    if (goodRead) {
        bool bOddReadLength = (this->uiReadLength % 2 == 1);
        decodeLongRead(this->half1st, this->half2nd, this->read, bOddReadLength);
    }
}


SimulateLongRead::~SimulateLongRead(void)
{
}

void introduceMutation(char* kmer, int mutationPattern)
{
    if (mutationPattern == 2 || mutationPattern == 3 || mutationPattern == 4) {
        mutateRead(kmer, mutationPattern);
    } else if (mutationPattern == (int)FULL_SENSITIVE_OPT_TO_ONE_BASE_ONE_COLOR_MIS) {
        mutatePairsOfConsecutiveBases(kmer, 1);
        mutateRead(kmer, 1);
    } else if (mutationPattern == (int)FULL_SENSITIVE_OPT_TO_TWO_BASE_MIS ) {
        mutatePairsOfConsecutiveBases(kmer, 2);
    } else if (mutationPattern == (int)FULL_SENSITIVE_OPT_TO_ONE_BASE_TWO_COLOR_MIS) {
        mutatePairsOfConsecutiveBases(kmer, 1);
        mutateRead(kmer, 2);
    }
}

/*
int getTestChrId(CGenome_Index_TableQ* table) {
}

const char* getTestChr(CGenome_Index_TableQ* table) {
}*/

/*
 * Test function which mutates every sliding windows on the reference genome and check if the all hits are found
 */
bool testGenome_Index_TableQ(CGenome_Index_TableQ* table)
{
    bool pass = true;
    CAlignmentsQ alignmentsQ('B');

    CchromosomeNTdata* testChr;
    unsigned int testChrId;
    if (table->pgenomeNT->iNo_of_chromosome > 1) {
        testChrId = 1; // If there are more than one chromosome, test the second one.
        testChr = table->pgenomeNT->paChromosomes[1];
    } else {
        testChrId = 0;
        testChr = table->pgenomeNT->paChromosomes[0];
    }
    unsigned int testGenomeIdStart = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0);
    unsigned int testGenomeIdEnd   = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0)
                                     + testChr->iChromosome_size - table->uiRead_Length;

    for (unsigned int i = testGenomeIdStart; i < testGenomeIdEnd; i++) {
        CReadInBits slideWindows;
        bool goodRead;
        goodRead = table->pgenomeNTInBits->fragACGTKmerInBits(slideWindows, i, table->uiRead_Length);
        if (table->bMapReadInColors) {
            slideWindows = bases2Colors(slideWindows);
        }

        if (goodRead) {
            char read[wordSize + 1];
            char originalRead[wordSize + 1];
            slideWindows.decode(read);

            CReadInBits readInBits(read);
            alignmentsQ.clearHits();
            // (1) Test exact match
            if (table->bMapReadInColors) {
                table->queryReadColors(readInBits, alignmentsQ, true, true);
            } else {
                table->queryReadBases(readInBits, alignmentsQ, true, true);
            }
            if (alignmentsQ.MinDiff != 0) {
                cout << "Miss exact match " << i << endl;
                pass = false;
                PRINT_MASKED_REPERAT_FLAG(table, i);
            }

            // (2) Test mutated reads
            myStrCpy(originalRead, read, MAX_READ_LENGTH);
            introduceMutation(read, table->chosenSeedId);
            CReadInBits mReadInBits(read);
            alignmentsQ.clearHits();

            if (table->bMapReadInColors) {
                table->queryReadColors(mReadInBits, alignmentsQ, true, true);
            } else {
                table->queryReadBases(mReadInBits, alignmentsQ, true, true);
            }
            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }

            // (3) Check reversed complement reads
            if (table->bMapReadInColors) {
                table->pgenomeNTInBits->fragACGTKmerInBits(slideWindows, i, table->uiRead_Length);
                CReadInBits rcRead = reverseCompliment(table->uiRead_Length, slideWindows);
                CReadInBits rcReadInColors = bases2Colors(rcRead);

                rcReadInColors.decode(read);
                myStrCpy(originalRead, read, MAX_READ_LENGTH);
                introduceMutation(read, table->chosenSeedId);
                CReadInBits rcMreadInBits(read);
                alignmentsQ.clearHits();
                table->queryReadColors(rcMreadInBits, alignmentsQ, true, false);
            } else {
                reverseComplementKmer(read);
                CReadInBits rcMreadInBits(read);
                alignmentsQ.MinDiff = table->uiRead_Length; // SET A LARGE NUMBER
                table->queryReadBases(rcMreadInBits, alignmentsQ, true, false);
            }

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss reversed mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }
        }
    }
    return(pass);
}

bool testMappingLongRead(CGenome_Index_TableQ* table)
{
    bool bOddReadLength = false; // Currently only test even long read
    bool pass = true;
    CAlignmentsQ alignmentsQ('B');

    CchromosomeNTdata* testChr;
    unsigned int testChrId;
    if (table->pgenomeNT->iNo_of_chromosome > 1) {
        testChrId = 1; // If there are more than one chromosome, test the second one.
        testChr = table->pgenomeNT->paChromosomes[1];
    } else {
        testChrId = 0;
        testChr = table->pgenomeNT->paChromosomes[0];
    }
    const unsigned halfLength = table->uiRead_Length;
    unsigned int testGenomeIdStart = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0);
    unsigned int testGenomeIdEnd   = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0)
                                     + testChr->iChromosome_size - halfLength;

    for (unsigned int i = testGenomeIdStart; i < testGenomeIdEnd; i++) {
        CReadInBits half1st, half2nd;
        bool goodRead;

        goodRead = table->pgenomeNTInBits->fragACGTKmerInBits(half1st, i, halfLength);
        goodRead = goodRead & (table->pgenomeNTInBits->fragACGTKmerInBits(half2nd, i + halfLength, halfLength));

        if (goodRead) {
            char read[MAX_LINE];
            char originalRead[MAX_LINE];
            decodeLongRead(half1st, half2nd, read, bOddReadLength);

            alignmentsQ.clearHits();
            // (1) Test exact match
            table->queryLongReadBases(half1st, half2nd, bOddReadLength, alignmentsQ, 1, true, true);

            if (alignmentsQ.MinDiff != 0) {
                cout << "Miss exact match " << i << endl;
                pass = false;
                PRINT_MASKED_REPERAT_FLAG(table, i);
            }

            // (2) Test mutated reads
            myStrCpy(originalRead, read, MAX_LINE);
            introduceMutation(read, table->chosenSeedId * 2);
            encodeLongRead(read, half1st, half2nd);
            alignmentsQ.clearHits();
            table->queryLongReadBases(half1st, half2nd, bOddReadLength, alignmentsQ, 1, true, true);

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }

            // (3) Check reversed complement reads
            reverseComplementKmer(read);
            CReadInBits rcR1, rcR2;
            encodeLongRead(read, rcR1, rcR2);
            alignmentsQ.MinDiff = table->uiRead_Length; // SET A LARGE NUMBER
            table->queryLongReadBases(rcR1, rcR2, bOddReadLength, alignmentsQ, 1, true, false);
            table->queryLongReadBases(rcR1, rcR2, bOddReadLength, alignmentsQ, 2, true, false);

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss reversed mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }
        }
    }
    return(pass);
}

bool testMappingLongSOLiDRead(CGenome_Index_TableQ* table)
{
    bool bOddReadLength = false;
    bool pass = true;
    CAlignmentsQ alignmentsQ('B');

    CchromosomeNTdata* testChr;
    unsigned int testChrId;
    if (table->pgenomeNT->iNo_of_chromosome > 1) {
        testChrId = 1; // If there are more than one chromosome, test the second one.
        testChr = table->pgenomeNT->paChromosomes[1];
    } else {
        testChrId = 0;
        testChr = table->pgenomeNT->paChromosomes[0];
    }
    const unsigned halfLength = table->uiRead_Length;
    unsigned int testGenomeIdStart = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0);
    unsigned int testGenomeIdEnd   = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0)
                                     + testChr->iChromosome_size - halfLength;

    for (unsigned int i = testGenomeIdStart; i < testGenomeIdEnd; i++) {
        CReadInBits half1st, half2nd, half1stInColors, half2ndInColors;
        bool goodRead;

        goodRead = table->pgenomeNTInBits->fragACGTKmerInBits(half1st, i, halfLength);
        goodRead = goodRead & (table->pgenomeNTInBits->fragACGTKmerInBits(half2nd, i + halfLength, halfLength));

        if (goodRead) {
            char read[MAX_LINE];
            char originalRead[MAX_LINE];
            decodeLongRead(half1st, half2nd, read, bOddReadLength);
            // TODO: encode each half in colors signals
            alignmentsQ.clearHits();
            // (1) Test exact match
            table->queryLongReadColors(half1stInColors, half2ndInColors, bOddReadLength, alignmentsQ, 1, true, true);

            if (alignmentsQ.MinDiff != 0) {
                cout << "Miss exact match " << i << endl;
                pass = false;
                PRINT_MASKED_REPERAT_FLAG(table, i);
            }

            // (2) Test mutated reads
            myStrCpy(originalRead, read, MAX_LINE);
            introduceMutation(read, table->chosenSeedId * 2);
            encodeLongRead(read, half1st, half2nd);
            // TODO: encode each half in colors signals
            alignmentsQ.clearHits();
            table->queryLongReadColors(half1stInColors, half2ndInColors, bOddReadLength, alignmentsQ, 1, true, true);

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }

            // (3) Check reversed complement reads
            reverseComplementKmer(read);
            CReadInBits rcR1, rcR2;
            encodeLongRead(read, rcR1, rcR2);
            // TODO: encode each half in colors signals
            alignmentsQ.MinDiff = table->uiRead_Length; // SET A LARGE NUMBER
            table->queryLongReadBases(rcR1, rcR2, bOddReadLength, alignmentsQ, 1, true, false);
            table->queryLongReadBases(rcR1, rcR2, bOddReadLength, alignmentsQ, 2, true, false);

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss reversed mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }
        }
    }
    return(pass);
}

bool testMappingLongPairedRead(CGenome_Index_TableQ* table)
{

    bool bOddReadLength = false;
    bool pass = true;

    /*
    CAlignmentsQ alignmentsQ('B');

    CchromosomeNTdata* testChr;
    unsigned int testChrId;
    if (table->pgenomeNT->iNo_of_chromosome > 1) {
        testChrId = 1; // If there are more than one chromosome, test the second one.
        testChr = table->pgenomeNT->paChromosomes[1];
    } else {
        testChrId = 0;
        testChr = table->pgenomeNT->paChromosomes[0];
    }
    int separation = 300;
    const unsigned halfLength = table->uiRead_Length;
    SimulateLongRead::uiReadLength = table->uiRead_Length * 2;
    unsigned int testGenomeIdStart = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0);
    unsigned int testGenomeIdEnd   = table->pgenomeNT->chrIndex2genomelocusID(testChrId, 0)
                                     + testChr->iChromosome_size - halfLength = separation;

    for (unsigned int i = testGenomeIdStart; i < testGenomeIdEnd; i++) {
        CReadInBits half1st, half2nd, half1stInColors, half2ndInColors;
        SimulateLongRead r1(table->pgenomeNTInBits, i);
    	SimulateLongRead r2(table->pgenomeNTInBits, i + separation);
    	if (r1.goodRead && r2.goodRead) {
            char originalRead[MAX_LINE];
            alignmentsQ.clearHits();
            // (1) Test exact match
    		table->queryLongReadColors(r1.half1st, r1.half2nd, bOddReadLength, alignmentsQ, 1, true, true);

            if (alignmentsQ.MinDiff != 0) {
                cout << "Miss exact match " << i << endl;
                pass = false;
                PRINT_MASKED_REPERAT_FLAG(table, i);
            }

            // (2) Test mutated reads
            myStrCpy(originalRead, r.read, MAX_LINE);
            introduceMutation(read, table->chosenSeedId * 2);
            encodeLongRead(read, half1st, half2nd);
            // TODO: encode each half in colors signals
            alignmentsQ.clearHits();
            table->queryLongReadColors(half1stInColors, half2ndInColors, bOddReadLength, alignmentsQ, 1, true, true);

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }

            // (3) Check reversed complement reads
            reverseComplementKmer(read);
            CReadInBits rcR1, rcR2;
            encodeLongRead(read, rcR1, rcR2);
            // TODO: encode each half in colors signals
            alignmentsQ.MinDiff = table->uiRead_Length; // SET A LARGE NUMBER
            table->queryLongReadBases(rcR1, rcR2, bOddReadLength, alignmentsQ, 1, true, false);
            table->queryLongReadBases(rcR1, rcR2, bOddReadLength, alignmentsQ, 2, true, false);

            if (alignmentsQ.MinDiff > table->uiSubDiffThreshold) {
                cout << "Miss reversed mutated read" << i << endl;
                pass = false;
                // PRINT_MASKED_REPERAT_FLAG(table, i);
            } else if (alignmentsQ.MinDiff < table->uiSubDiffThreshold) {
                // cout << "Found better alignment!" << endl;
            }
        }
    }
    */
    return(pass);
}