-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcomputePnSlippageDefault.cpp
747 lines (699 loc) · 27 KB
/
computePnSlippageDefault.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <algorithm>
#include <fstream>
#include <numeric>
#include <seqan/sequence.h>
#include <seqan/basic.h>
#include <seqan/file.h>
#include <seqan/modifier.h>
#include <seqan/stream.h>
#include <seqan/arg_parse.h>
#include <liblinear.hpp>
namespace computePnSlippageDefault
{
using namespace std;
using namespace seqan;
//For storing number of members in each class and their pValue-sum
struct LabelProps {
double p1;
double p2;
double p3;
int nMarkers;
} ;
//For storing marker information
struct Marker {
string chrom;
int start;
int end;
string motif;
} ;
struct AttributeLine {
float ratioBf;
float ratioAf;
float numOfRepeats;
unsigned locationShift;
unsigned mateEditDist;
float purity;
float ratioOver20In;
int label;
double pValue;
} ;
struct GenotypeInfo {
String<Pair<float> > genotypes;
String<long double> pValues;
Pair<float> genotype;
double pValue;
int numOfReads;
map<float, int> alleleToFreq; //This maps from reported alleles to their frequencies
double pValueSum;
} ;
//For storing marker values
struct MarkerStats
{
model* regressionModel;
std::map<string, double> pnToPsum;
double slippage;
unsigned nAlleles;
double stutter;
double fullMotifSlippageSum;
unsigned nPns;
double posSlippProb;
double negSlippProb;
};
//So I can map from Markers
bool operator<(const Marker & left, const Marker & right)
{
return left.start < right.start;
}
//stores various marker specific values
map<Marker, MarkerStats> markerToStats;
//Store AttributeLine for all reads
map<Pair<string, Marker>, String<AttributeLine> > markerAndPnToReads;
//Store alleles at each marker
map<Marker, Pair<std::set<float>, String<Pair<float> >> > markerToAllelesAndGenotypes;
//For storing command line arguments
struct ComputePnSlippageOptions
{
CharString pnList, attributesDirectory, outputFile, markerSlippageFile, modelDirectory;
unsigned firstPnIdx;
} ;
ArgumentParser::ParseResult parseCommandLine(ComputePnSlippageOptions & options, int argc, char const ** argv)
{
ArgumentParser parser("computePnSlippageDefault");
setShortDescription(parser, "Compute individual specific slippage rate for the provided individuals.");
setVersion(parser, "1.4");
setDate(parser, "April 2019");
addUsageLine(parser, "\\fI-PL\\fP pnList \\fI-AD\\fP attributesDirectory \\fI-OF\\fP outputFile \\fI-FP\\fP firstPnIdx \\fI-MS\\fP markerSlippageFile \\fI-MD\\fP regressionModelDirectory");
addDescription(parser, "This program will estimate an individual specific slipppage rate for the individuals specified based on the marker slippage rates and models provided.");
addOption(parser, ArgParseOption("PL", "pnList", "A list of PNs whose slippage will be estimated.", ArgParseArgument::INPUT_FILE, "IN-FILE"));
setRequired(parser, "pnList");
addOption(parser, ArgParseOption("AD", "attributesDirectory", "Path to attributes directory.", ArgParseArgument::INPUT_FILE, "IN-FILE"));
setRequired(parser, "attributesDirectory");
addOption(parser, ArgParseOption("OF", "outputFile", "The slippage rate estimated will be appended to this file.", ArgParseArgument::INPUT_FILE, "OUT-FILE"));
setRequired(parser, "outputFile");
addOption(parser, ArgParseOption("FP", "firstPnIdx", "Index of first Pn in pnList within the attributeFile.", ArgParseArgument::INTEGER, "INTEGER"));
setRequired(parser, "firstPnIdx");
addOption(parser, ArgParseOption("MS", "markerSlippageFile", "A file containing slippage rates for the microsatellites.", ArgParseArgument::OUTPUT_FILE, "OUT-FILE"));
setRequired(parser, "markerSlippageFile");
addOption(parser, ArgParseOption("MD", "modelDirectory", "A directory where logistic regression models for all markers in the markerSlippageFile are stored.", ArgParseArgument::OUTPUT_FILE, "IN-DIR"));
setRequired(parser, "modelDirectory");
ArgumentParser::ParseResult res = parse(parser, argc, argv);
if (res != ArgumentParser::PARSE_OK)
return res;
getOptionValue(options.pnList, parser, "pnList");
getOptionValue(options.attributesDirectory, parser, "attributesDirectory");
getOptionValue(options.outputFile, parser, "outputFile");
getOptionValue(options.firstPnIdx, parser, "firstPnIdx");
getOptionValue(options.markerSlippageFile, parser, "markerSlippageFile");
getOptionValue(options.modelDirectory, parser, "modelDirectory");
return ArgumentParser::PARSE_OK;
}
//Fills in the x-part of a problem structure from an AttributeLine structure
void fillProblemX(int idx, AttributeLine currentLine, problem& myProb)
{
myProb.x[idx][0].index = 1;
myProb.x[idx][0].value = currentLine.ratioBf;
myProb.x[idx][1].index = 2;
myProb.x[idx][1].value = currentLine.ratioAf;
myProb.x[idx][2].index = 3;
myProb.x[idx][2].value = currentLine.locationShift;
myProb.x[idx][3].index = 4;
myProb.x[idx][3].value = currentLine.mateEditDist;
myProb.x[idx][4].index = 5;
myProb.x[idx][4].value = currentLine.purity;
myProb.x[idx][5].index = 6;
myProb.x[idx][5].value = currentLine.ratioOver20In;
myProb.x[idx][6].index = -1; // This is to indicate that there aren't any more attributes to read in.
myProb.x[idx][6].value = 0;
}
void readMarkerSlippage(ifstream& markerSlippageFile, CharString regressionModelDirectory)
{
Marker currMarker;
string tempVal;
CharString currMarkerModelDir = regressionModelDirectory;
//unsigned i = 1;
while (!markerSlippageFile.eof())
{
markerSlippageFile >> currMarker.chrom;
markerSlippageFile >> currMarker.start;
markerSlippageFile >> currMarker.end;
markerSlippageFile >> currMarker.motif;
markerSlippageFile >> tempVal;
markerSlippageFile >> markerToStats[currMarker].slippage; //marker slippage rate
markerSlippageFile >> markerToStats[currMarker].nPns; //how many pns available to estimate the marker slippage
markerSlippageFile >> markerToStats[currMarker].nAlleles; //read number of alleles
markerSlippageFile >> markerToStats[currMarker].stutter; //marker stutter rate
markerSlippageFile >> markerToStats[currMarker].posSlippProb; //probability of adding repeats in slippage
markerSlippageFile >> markerToStats[currMarker].negSlippProb; //probability of losing repeats in slippage
append(currMarkerModelDir, "/model_");
append(currMarkerModelDir, to_string(currMarker.start));
append(currMarkerModelDir, "_");
append(currMarkerModelDir, currMarker.motif);
const char *model_in_file = toCString(currMarkerModelDir);
markerToStats[currMarker].regressionModel = load_model(model_in_file);
currMarkerModelDir = regressionModelDirectory;
//if (i % 1000==0 && i>0)
//cout << "Working on marker number: " << i << endl;
//i++;
}
cout << "Finished reading marker slippage." << endl;
}
//Count number of words in a sentence, use to parse input from attribute file
Pair<int, String<string> > countNumberOfWords(string sentence)
{
int numberOfWords = 0;
String<string> words;
resize(words, 9);
if (!isspace(sentence[0]))
{
numberOfWords++;
words[0] = sentence[0];
}
for (unsigned i = 1; i < sentence.length(); i++)
{
if ((!isspace(sentence[i])) && (isspace(sentence[i-1])))
{
numberOfWords++;
words[numberOfWords-1] = sentence[i];
}
else
{
if (!isspace(sentence[i]))
words[numberOfWords-1].push_back(sentence[i]);
}
}
resize(words, numberOfWords);
return Pair<int, String<string> >(numberOfWords, words);
}
double getPval(Marker marker, AttributeLine currentLine)
{
double predict_label;
model* model_ = markerToStats[marker].regressionModel;
double *prob_estimates = (double *) malloc(2*sizeof(double));
problem prob;
prob.bias = -1;
prob.l = 1;
prob.n = 6;
prob.x = (feature_node **) malloc(prob.l * sizeof(feature_node *));
prob.x[0] = (feature_node *) malloc(10 * sizeof(feature_node));
fillProblemX(0, currentLine, prob);
predict_label = predict_probability(model_,prob.x[0],prob_estimates);
return prob_estimates[0];
}
//Parses one line from attribute file by filling up and returning an AttributeLine, also initializes markerToSizeAndModel map using the labels
AttributeLine parseNextLine(float winner, float second, String<string> read, Marker& marker, map<string, LabelProps>& pnToLabelProps, string pnId)
{
AttributeLine currentLine;
string temp;
lexicalCast(currentLine.numOfRepeats, read[0]);
lexicalCast(currentLine.ratioBf, read[1]);
lexicalCast(currentLine.ratioAf, read[2]);
lexicalCast(currentLine.locationShift, read[3]);
lexicalCast(currentLine.mateEditDist, read[4]);
lexicalCast(currentLine.purity, read[5]);
lexicalCast(currentLine.ratioOver20In, read[6]);
currentLine.pValue = getPval(marker, currentLine);
markerToStats[marker].pnToPsum[pnId] += currentLine.pValue;
if (currentLine.numOfRepeats == winner || currentLine.numOfRepeats == second)
{
pnToLabelProps[pnId].p1 += currentLine.pValue;
currentLine.label = 1;
}
else
{
float diff1 = fabs(currentLine.numOfRepeats - winner), diff2 = fabs(currentLine.numOfRepeats - second);
if (std::min(diff1,diff2)>=0.9) //full motif slippage
{
pnToLabelProps[pnId].p2 += currentLine.pValue;
currentLine.label = 2;
}
else //stutter
{
pnToLabelProps[pnId].p3 += currentLine.pValue;
currentLine.label = 3;
}
}
return currentLine;
}
double estimateSlippage(double current_sp, string pnId)
{
vector<double> weights;
vector<double> slippFragments;
double currMarkSlipp, currPvalSum, weightSum = 0, fullMotifSlippageSum = 0;
for (auto& marker: markerToStats)
{
if ( marker.second.pnToPsum[pnId] == 0.0)
continue;
if (marker.second.slippage == 0)
currMarkSlipp = 0.001;
else
currMarkSlipp = marker.second.slippage;
currPvalSum = marker.second.pnToPsum[pnId];
weights.push_back(currPvalSum/((current_sp+currMarkSlipp)*(1-(current_sp+currMarkSlipp))));
}
weightSum = accumulate(weights.begin(),weights.end(),0.0);
unsigned index = 0;
for (auto& marker: markerToStats)
{
if ( marker.second.pnToPsum[pnId] == 0.0)
continue;
Pair<string, Marker> mapKey = Pair<string, Marker>(pnId, marker.first);
String<AttributeLine> readsAtI = markerAndPnToReads[mapKey];
for (unsigned i=0; i<length(readsAtI); ++i)
{
if (readsAtI[i].label == 2)
fullMotifSlippageSum += readsAtI[i].pValue;
}
slippFragments.push_back((weights[index]/weightSum)*((fullMotifSlippageSum/marker.second.pnToPsum[pnId]) - marker.second.slippage));
fullMotifSlippageSum = 0;
++index;
}
double slippage = std::max(0.0,accumulate(slippFragments.begin(),slippFragments.end(),0.0));
return slippage;
}
String<Pair<float> > makeGenotypes(std::set<float>& alleles)
{
String<Pair<float> > genotypes;
String<float> alleleString;
std::set<float>::reverse_iterator allelesBegin = alleles.rend();
for (std::set<float>::reverse_iterator alleleIt = alleles.rbegin(); alleleIt!=allelesBegin; ++alleleIt)
appendValue(alleleString, *alleleIt);
for (unsigned i=0; i<length(alleleString); ++i)
{
appendValue(genotypes,Pair<float>(alleleString[i],alleleString[i]));
if (i == (length(alleleString)-1))
break;
for (unsigned j=i+1; j<length(alleleString); ++j)
appendValue(genotypes,Pair<float>(alleleString[j],alleleString[i]));
}
reverse(genotypes);
return genotypes;
}
int findMaxIndex(String<long double>& probs)
{
int maxIndex = 0;
long double maxValue = probs[0];
for (unsigned i = 1; i<length(probs); ++i)
{
if (probs[i]>maxValue)
{
maxIndex=i;
maxValue=probs[i];
}
}
return maxIndex;
}
float dgeom(int diff, double psucc)
{
if (diff < 0)
return 0;
double p = psucc;
for (int i = 0; i < diff; i++)
{
p = p*(1-psucc);
}
return p;
}
float dpois(int step, float mean) {
if (step < 0)
return 0;
float p = exp(-1*mean);
for (int i = 0; i < step; i++) {
p = p*mean;
p = p/(i+1);
}
return p;
}
void relabelReads(std::set<float>& newGenotype, String<AttributeLine>& reads)
{
for (unsigned i = 0; i < length(reads); ++i)
{
if (newGenotype.find(reads[i].numOfRepeats) != newGenotype.end())
reads[i].label = 1;
else
{
std::set<float>::iterator it=newGenotype.begin();
float allele1 = *it;
++it;
float allele2 = *it;
float diff1 = fabs(allele1-reads[i].numOfRepeats), diff2 = fabs(allele2-reads[i].numOfRepeats);
if (std::min(diff1,diff2)>=0.9)
reads[i].label = 2;
else
reads[i].label = 3;
}
}
}
bool determineGenotype(String<AttributeLine>& reads, double s_ij, String<Pair<float> > genotypes, int numberOfAlleles, int motifLength, double psucc, double posSlippProb, double negSlippProb)
{
Pair<float> genotypeToCheck;
AttributeLine readToCheck;
String<long double> probs;
std::set<float> currentGenotype, newGenotype;
resize(probs, length(genotypes));
bool isHomo;
float posNegSlipp = 1, posNegSlipp2 = 1, lambda = std::max((double)0.001,s_ij), diff, diff2;
int indexOfWinner;
for (unsigned i=0; i<length(genotypes); ++i)
{
probs[i] = 1;
genotypeToCheck = genotypes[i];
isHomo = genotypeToCheck.i1 == genotypeToCheck.i2;
for (unsigned j=0; j<length(reads); ++j)
{
posNegSlipp = 1;
posNegSlipp2 = 1;
readToCheck = reads[j];
if (readToCheck.label == 1)
currentGenotype.insert(readToCheck.numOfRepeats);
if (isHomo)
{
if (readToCheck.numOfRepeats - genotypeToCheck.i1 < -0.9)
posNegSlipp = negSlippProb;
if (readToCheck.numOfRepeats - genotypeToCheck.i1 > 0.9)
posNegSlipp = posSlippProb;
diff = fabs(readToCheck.numOfRepeats - genotypeToCheck.i1);
probs[i] *= (readToCheck.pValue * dgeom(static_cast<int>(roundf((diff-(float)floor(diff))*motifLength)), psucc) * dpois(floor(diff), lambda) * posNegSlipp + ((double)(1.0-readToCheck.pValue)/(double)numberOfAlleles));
}
else
{
if (readToCheck.numOfRepeats - genotypeToCheck.i1 < -0.9)
posNegSlipp = negSlippProb;
if (readToCheck.numOfRepeats - genotypeToCheck.i1 > 0.9)
posNegSlipp = posSlippProb;
if (readToCheck.numOfRepeats - genotypeToCheck.i2 < -0.9)
posNegSlipp2 = negSlippProb;
if (readToCheck.numOfRepeats - genotypeToCheck.i2 > 0.9)
posNegSlipp2 = posSlippProb;
diff = fabs(readToCheck.numOfRepeats - genotypeToCheck.i1);
diff2 = fabs(readToCheck.numOfRepeats - genotypeToCheck.i2);
probs[i] *= (readToCheck.pValue * 0.5 * (dgeom(static_cast<int>(roundf((diff-(float)floor(diff))*motifLength)), psucc) * dpois(floor(diff), lambda) * posNegSlipp + dgeom(static_cast<int>(roundf((diff2-(float)floor(diff2))*motifLength)), psucc) * dpois(floor(diff2), lambda) * posNegSlipp2) + ((double)(1.0-readToCheck.pValue)/(double)numberOfAlleles));
}
}
}
indexOfWinner = findMaxIndex(probs);
newGenotype.insert(genotypes[indexOfWinner].i1);
newGenotype.insert(genotypes[indexOfWinner].i2);
if (newGenotype == currentGenotype)
return false;
else
{
relabelReads(newGenotype,reads);
return true;
}
}
int updateGenotypes(double current_sp, string pnId)
{
int nChanged = 0;
bool changed;
String<Pair<float> > genotypes;
for (auto& marker: markerToStats)
{
if ( marker.second.pnToPsum[pnId] == 0.0)
continue;
genotypes = markerToAllelesAndGenotypes[marker.first].i2;
Pair<string, Marker> mapKey = Pair<string, Marker>(pnId, marker.first);
changed = determineGenotype(markerAndPnToReads[mapKey], current_sp+marker.second.slippage, genotypes, marker.second.nAlleles, marker.first.motif.size(), marker.second.stutter, marker.second.posSlippProb, marker.second.negSlippProb);
if (changed)
++nChanged;
}
return nChanged;
}
map<string, LabelProps> readPnList(CharString & pnInfoFile)
{
map<string, LabelProps> pnToLabelProps;
ifstream pnList(toCString(pnInfoFile));
while (!pnList.eof())
{
string PN_ID;
pnList >> PN_ID;
if (PN_ID.length() == 0 || pnList.eof())
break;
LabelProps slippCount;
slippCount.p1 = 0;
slippCount.p2 = 0;
slippCount.p3 = 0;
pnToLabelProps[PN_ID] = slippCount;
}
cout << "Finished reading PnList.\n";
return pnToLabelProps;
}
long int readOffSets(ifstream & attsFile, unsigned firstPnIdx, unsigned nPns)
{
long int offset = 0;
for (unsigned i = 1; i<=firstPnIdx; ++i)
{
attsFile >> offset;
if (offset == -69)
return 0;
}
while (offset == 0 && !attsFile.eof())
{
++firstPnIdx;
attsFile >> offset;
}
//cout << "firstPnIdx: " << firstPnIdx << "\n";
//cout << "offset: "<< offset << "\n";
if (firstPnIdx > nPns)
return 0;
else
return offset;
}
Pair<float> findMostFrequent(std::vector<String<string> > & currentReadList)
{
std::set<string> presentAlleles;
std::vector<string> allAlleles;
int winnerFreq = 0, secondFreq = 0, currentFreq;
float winner, second, fallele;
for (auto read : currentReadList)
{
presentAlleles.insert(read[0]);
allAlleles.push_back(read[0]);
}
for (auto allele : presentAlleles)
{
currentFreq = count(allAlleles.begin(), allAlleles.end(), allele);
lexicalCast(fallele, allele);
if ( currentFreq > winnerFreq)
{
secondFreq = winnerFreq;
second = winner;
winnerFreq = currentFreq;
winner = fallele;
}
else
{
if (currentFreq > secondFreq)
{
secondFreq = currentFreq;
second = fallele;
}
else
{
if(currentFreq == secondFreq)
second = max(second,fallele);
}
}
}
if (secondFreq < 0.10*winnerFreq)
second = winner;
return {winner, second};
}
void readMarkerData(CharString attributesDirectory, Marker marker, map<string, LabelProps>& pnToLabelProps, unsigned firstPnIdx)
{
//variables
int numberOfReads = 0, nReadsThisBatch = 0, pnsFound = 0;
float winner = 0, second = 0, numOfRepeats = 0;
string nextLine, temp, prevPn= "PeterGriffin";
Pair<int, String<string> > numberOfWordsAndWords;
AttributeLine currentLine;
std::vector<String<string> > currentReadList;
bool moreThanOneBam = false;
//make input stream
append(attributesDirectory, "/");
append(attributesDirectory, to_string(marker.start));
append(attributesDirectory, "_");
append(attributesDirectory, marker.motif);
//cout << "Reading data from " << attributesDirectory << endl;
ifstream attsFile(toCString(attributesDirectory));
long int offset = readOffSets(attsFile, firstPnIdx, firstPnIdx + pnToLabelProps.size() - 1);
//cout << "Read offset, it's:" << offset << endl;
if (offset != 0)
attsFile.seekg(offset);
else
return;
while (!attsFile.eof() && pnsFound < pnToLabelProps.size())
{
getline (attsFile,nextLine);
if (nextLine.length() == 0)
continue;
numberOfWordsAndWords = countNumberOfWords(nextLine);
if (numberOfWordsAndWords.i1 == 1)
{
//first check if we passed the last pn in our map
if (nextLine > pnToLabelProps.rbegin()->first)
{
break;
}
//make sure we want to compute pnSlippage for this pn
if (pnToLabelProps.count(nextLine) != 0 )
{
//New PN -> process and store data from previous one if its a new PN and not the first one
if (nextLine.compare(prevPn) != 0 && prevPn.compare("PeterGriffin") != 0)
{
if (currentReadList.size() >= 10)
{
if (moreThanOneBam)
{
Pair<float> winnerSecond = findMostFrequent(currentReadList);
winner = winnerSecond.i1;
second = winnerSecond.i2;
}
++pnToLabelProps[prevPn].nMarkers;
//cout << "Processing " << currentReadList.size() << " reads for " << prevPn << " at " << marker.start << "inside of loop" << endl;
for (auto read : currentReadList)
{
currentLine = parseNextLine(winner, second, read, marker, pnToLabelProps, prevPn);
Pair<string,Marker> mapKey = Pair<string,Marker>(prevPn, marker);
appendValue(markerAndPnToReads[mapKey],currentLine);
markerToAllelesAndGenotypes[marker].i1.insert(currentLine.numOfRepeats);
}
}
else
markerToStats[marker].pnToPsum[nextLine] = 0.0;
currentReadList.clear();
++pnsFound;
moreThanOneBam = false;
}
if (nextLine.compare(prevPn) == 0)
moreThanOneBam = true;
prevPn = nextLine;
//read line with markerInfo
std::getline(attsFile, nextLine);
numberOfWordsAndWords = countNumberOfWords(nextLine);
lexicalCast(nReadsThisBatch, numberOfWordsAndWords.i2[5]);
if (moreThanOneBam)
numberOfReads = numberOfReads + nReadsThisBatch;
else
numberOfReads = nReadsThisBatch;
lexicalCast(winner, numberOfWordsAndWords.i2[6]);
lexicalCast(second, numberOfWordsAndWords.i2[7]);
//Read lines for read atts
for (unsigned i = 0; i < nReadsThisBatch; ++i)
{
std::getline (attsFile, nextLine);
numberOfWordsAndWords = countNumberOfWords(nextLine);
if (nextLine.length() == 0)
{
--i;
continue;
}
currentReadList.push_back(numberOfWordsAndWords.i2);
}
}
// Don't want this pn, walk on by
else
{
std::getline(attsFile, nextLine);
numberOfWordsAndWords = countNumberOfWords(nextLine);
lexicalCast(numberOfReads, numberOfWordsAndWords.i2[5]);
for (unsigned i = 0; i < numberOfReads; ++i)
{
getline (attsFile,nextLine);
if (nextLine.length() == 0)
{
--i;
continue;
}
}
}
}
else
{
cerr << "Something went sideways while reading attributes @: " << attributesDirectory << " for " << prevPn << "\n";
cout << nextLine << endl;
}
}
//Process last pn after reading has finished
if (currentReadList.size() >= 10)
{
if (moreThanOneBam)
{
Pair<float> winnerSecond = findMostFrequent(currentReadList);
winner = winnerSecond.i1;
second = winnerSecond.i2;
}
++pnToLabelProps[prevPn].nMarkers;
for (auto read : currentReadList)
{
//cout << read << endl;
currentLine = parseNextLine(winner, second, read, marker, pnToLabelProps, prevPn);
Pair<string,Marker> mapKey = Pair<string,Marker>(prevPn, marker);
appendValue(markerAndPnToReads[mapKey],currentLine);
markerToAllelesAndGenotypes[marker].i1.insert(currentLine.numOfRepeats);
}
}
else
markerToStats[marker].pnToPsum[nextLine] = 0.0;
currentReadList.clear();
markerToAllelesAndGenotypes[marker].i2 = makeGenotypes(markerToAllelesAndGenotypes[marker].i1);
}
int main(int argc, char const ** argv)
{
ComputePnSlippageOptions options;
ArgumentParser::ParseResult res = parseCommandLine(options, argc, argv);
if (res != seqan::ArgumentParser::PARSE_OK)
return res == seqan::ArgumentParser::PARSE_ERROR;
CharString modelDir = options.modelDirectory;
ifstream slippageFile(toCString(options.markerSlippageFile));
if(slippageFile.fail())
{
cout << "Unable to locate slippageFile: " << options.markerSlippageFile << endl;
return 1;
}
else
readMarkerSlippage(slippageFile, options.modelDirectory);
//Read pn list
map<string, LabelProps> pnToLabelProps = readPnList(options.pnList);
//make output file
ofstream outputFile;
outputFile.open(toCString(options.outputFile), ios_base::app);
if(outputFile.fail())
{
cout << "Unable to create output file." << endl;
return 1;
}
for (auto &marker: markerToStats)
{
readMarkerData(options.attributesDirectory, marker.first, pnToLabelProps, options.firstPnIdx);
}
cout << "Finished reading attributes data." << endl;
for (auto& pn: pnToLabelProps)
{
if (pn.second.nMarkers < 1)
{
cout << "No markers with more than minimum number of reads for: " << pn.first << endl;
outputFile << pn.first << "\t" << 0.0 << endl;
continue;
}
double current_sp = (0.5*pn.second.p2)/(pn.second.p1 + pn.second.p2 + pn.second.p3);
double changed = 1, nChanged = 0;
while (changed > 0.005)
{
current_sp = estimateSlippage(current_sp, pn.first);
//cout << "Estimated slippage." << endl;
nChanged = updateGenotypes(current_sp, pn.first);
//cout << "Updated genotypes." << endl;
changed = (float)nChanged/(float)pn.second.nMarkers;
//cout << nChanged << " " << pn.second.nMarkers << endl;
}
cout << "Number of markers available for estimating pnSlippage for " << pn.first << " is: " << pn.second.nMarkers << endl;
outputFile << pn.first << "\t" << current_sp << endl;
}
return 0;
}
} // namespace computePnSlippageDefault