-
Notifications
You must be signed in to change notification settings - Fork 30
/
Recab.cpp
919 lines (785 loc) · 29.2 KB
/
Recab.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
* Copyright (C) 2010-2015, 2019 Christian Fuchsberger,
* Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <ctime>
#include <unistd.h>
#include <math.h>
//
#include "Error.h"
#include "Recab.h"
#include "Logger.h"
#include "Parameters.h"
#include "SimpleStats.h"
#include "BaseUtilities.h"
#include "SamFlag.h"
#include "BgzfFileType.h"
// STL headers
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <set>
Recab::Recab()
: myParamsSetup(false),
myRefFile(""),
myDbsnpFile(""),
myQField(""),
myStoreQualTag(""),
myBuildExcludeFlags("0x0F04"),
myApplyExcludeFlags("0x0000"),
myIntBuildExcludeFlags(0),
myIntApplyExcludeFlags(0)
{
myMappedCount = 0;
myUnMappedCount = 0;
mySecondaryCount = 0;
mySupplementaryCount = 0;
myDupCount = 0;
myQCFailCount = 0;
myMapQual0Count = 0;
myMapQual255Count = 0;
myNumBuildSkipped = 0;
myNumBuildReads = 0;
myNumApplySkipped = 0;
myNumApplyReads = 0;
myNumQualTagErrors = 0;
myNumDBSnpSkips = 0;
mySubMinQual = 0;
myAmbiguous = 0;
myBMatchCount = 0;
myBMismatchCount = 0;
myBasecounts = 0;
myBlendedWeight = 0;
myFitModel = false;
myFast = false;
myKeepPrevDbsnp = false;
myKeepPrevNonAdjacent = false;
myLogReg = false;
myMinBaseQual = DEFAULT_MIN_BASE_QUAL;
myMaxBaseQual = DEFAULT_MAX_BASE_QUAL;
myMaxBaseQualChar = BaseUtilities::getAsciiQuality(DEFAULT_MAX_BASE_QUAL);
}
Recab::~Recab()
{
}
void Recab::printRecabDescription(std::ostream& os)
{
os << " recab - Recalibrate\n";
}
void Recab::printDescription(std::ostream& os)
{
printRecabDescription(os);
}
void Recab::printUsage(std::ostream& os)
{
os << "Usage: ./bam recab (options) --in <InputBamFile> --out <OutputFile> [--log <logFile>] [--verbose] [--noeof] [--params] ";
printRecabSpecificUsageLine(os);
os << std::endl << std::endl;
os << "Required General Parameters :" << std::endl;
os << "\t--in <infile> : input BAM file name" << std::endl;
os << "\t--out <outfile> : output recalibration file name" << std::endl;
os << "Optional General Parameters : " << std::endl;
os << "\t--log <logfile> : log and summary statistics (default: [outfile].log)" << std::endl;
os << "\t--verbose : Turn on verbose mode" << std::endl;
os << "\t--noeof : do not expect an EOF block on a bam file." << std::endl;
os << "\t--params : print the parameter settings" << std::endl;
printRecabSpecificUsage(os);
os << "\n" << std::endl;
}
void Recab::printRecabSpecificUsageLine(std::ostream& os)
{
os << "--refFile <ReferenceFile> [--dbsnp <dbsnpFile>] [--minBaseQual <minBaseQual>] [--maxBaseQual <maxBaseQual>] [--blended <weight>] [--fitModel] [--fast] [--keepPrevDbsnp] [--keepPrevNonAdjacent] [--useLogReg] [--qualField <tag>] [--storeQualTag <tag>] [--buildExcludeFlags <flag>] [--applyExcludeFlags <flag>] ";
mySqueeze.printBinningUsageLine(os);
}
void Recab::printRecabSpecificUsage(std::ostream& os)
{
os << "\nRecab Specific Required Parameters\n";
os << "\t--refFile <reference file> : reference file name" << std::endl;
os << "Recab Specific Optional Parameters : " << std::endl;
os << "\t--dbsnp <known variance file> : dbsnp file of positions" << std::endl;
os << "\t--minBaseQual <minBaseQual> : minimum base quality of bases to recalibrate (default: " << DEFAULT_MIN_BASE_QUAL << ")" << std::endl;
os << "\t--maxBaseQual <maxBaseQual> : maximum recalibrated base quality (default: " << DEFAULT_MAX_BASE_QUAL << ")" << std::endl;
os << "\t qualities over this value will be set to this value." << std::endl;
os << "\t This setting is applied after binning (if applicable)." << std::endl;
os << "\t--blended <weight> : blended model weight" << std::endl;
os << "\t--fitModel : check if the logistic regression model fits the data" << std::endl;
os << "\t overriden by fast, but automatically applied by useLogReg" << std::endl;
os << "\t--fast : use a compact representation that only allows:" << std::endl;
os << "\t * at most " << (BaseData::getFastMaxRG() + 1) << " Read Groups" << std::endl;
os << "\t * maximum quality " << BaseData::getFastMaxQual() << std::endl;
os << "\t * at most " << BaseData::getFastMaxCycles() << " cycles" << std::endl;
os << "\t overrides fitModel, but is overridden by useLogReg" << std::endl;
os << "\t uses up to about 2.25G more memory than running without --fast." << std::endl;
os << "\t--keepPrevDbsnp : do not exclude entries where the previous base is in dbsnp when\n";
os << "\t building the recalibration table" << std::endl;
os << "\t By default they are excluded from the table." << std::endl;
os << "\t--keepPrevNonAdjacent : do not exclude entries where the previous base is not adjacent\n";
os << "\t (not a Cigar M/X/=) when building the recalibration table" << std::endl;
os << "\t By default they are excluded from the table (except the first cycle)." << std::endl;
os << "\t--useLogReg : use logistic regression calculated quality for the new quality" << std::endl;
os << "\t automatically applies fitModel and overrides fast." << std::endl;
os << "\t--qualField <quality tag> : tag to get the starting base quality\n";
os << "\t (default is to get it from the Quality field)" << std::endl;
os << "\t--storeQualTag <quality tag> : tag to store the previous quality into" << std::endl;
os << "\t--buildExcludeFlags <flag> : exclude reads with any of these flags set when building the" << std::endl;
os << "\t recalibration table. Default is 0xF04" << std::endl;
os << "\t--applyExcludeFlags <flag> : do not apply the recalibration table to any reads with any of these flags set" << std::endl;
mySqueeze.printBinningUsage(os);
}
int Recab::execute(int argc, char *argv[])
{
bool verboseFlag = false;
String inFile,outFile,logFile;
bool noeof = false;
bool params = false;
SamFile samIn,samOut;
ParameterList inputParameters;
LongParamContainer parameters;
parameters.addGroup("Required Generic Parameters");
parameters.addString("in", &inFile);
parameters.addString("out", &outFile);
parameters.addGroup("Optional Generic Parameters");
parameters.addString("log", &logFile);
parameters.addBool("verbose", &verboseFlag);
parameters.addBool("noeof", &noeof);
parameters.addBool("params", ¶ms);
parameters.addPhoneHome(VERSION);
addRecabSpecificParameters(parameters);
inputParameters.Add(new LongParameters ("Input Parameters",
parameters.getLongParameterList()));
// parameters start at index 2 rather than 1.
inputParameters.Read(argc, argv, 2);
// If no eof block is required for a bgzf file, set the bgzf file type to
// not look for it.
if(noeof)
{
// Set that the eof block is not required.
BgzfFileType::setRequireEofBlock(false);
}
if(inFile.IsEmpty())
{
printUsage(std::cerr);
inputParameters.Status();
std::cerr << "Missing required --in parameter" << std::endl;
return EXIT_FAILURE;
}
// inFile is not empty, so there is at least one character. Check if
// it is specifing stdin since that is not supported for Recab.
if(inFile[0] == '-')
{
// ERROR: stdin specified, but since Recab requires 2 passes through
// the input file, stdin is not supported.
printUsage(std::cerr);
inputParameters.Status();
std::cerr << "ERROR: stdin ('" << inFile << "') is not a supported input file because Recab requires two passes through the input file." << std::endl;
return EXIT_FAILURE;
}
if(outFile.IsEmpty())
{
printUsage(std::cerr);
inputParameters.Status();
std::cerr << "Missing required --out parameter" << std::endl;
return EXIT_FAILURE;
}
int status = processRecabParam();
if(status != 0)
{
inputParameters.Status();
return(status);
}
if ( logFile.IsEmpty() )
{
logFile = outFile + ".log";
}
if(params)
{
inputParameters.Status();
}
Logger::gLogger = new Logger(logFile.c_str(), verboseFlag);
////////////////
////// Errormodel
Logger::gLogger->writeLog("Initialize errormodel structure...");
////////////////////////////////////////
// SAM/BAM file open
////////////////////////////////////////
////////////////////////////////////////
// Iterate SAM records
if(!samIn.OpenForRead(inFile.c_str()))
{
Logger::gLogger->error("Failed to open SAM/BAM file %s",inFile.c_str() );
return EXIT_FAILURE;
}
Logger::gLogger->writeLog("Start iterating SAM/BAM file %s",inFile.c_str());
time_t now = time(0);
tm* localtm = localtime(&now);
Logger::gLogger->writeLog("Start: %s", asctime(localtm));
SamRecord samRecord;
SamFileHeader samHeader;
samIn.ReadHeader(samHeader);
srand (time(NULL));
int numRecs = 0;
while(samIn.ReadRecord(samHeader, samRecord) == true)
{
processReadBuildTable(samRecord);
//Status info
numRecs++;
if(verboseFlag)
{
if(numRecs%10000000==0)
Logger::gLogger->writeLog("%ld records processed", numRecs);
}
}
now = time(0);
localtm = localtime(&now);
Logger::gLogger->writeLog("End: %s", asctime(localtm));
if((outFile[0] == '-') && (logFile[0] != '-'))
{
// Since outFile is to stdout, and logfile isn't, pass logfile name
modelFitPrediction(logFile);
}
else
{
modelFitPrediction(outFile);
}
Logger::gLogger->writeLog("Writing recalibrated file %s",outFile.c_str());
////////////////////////
////////////////////////
//// Write file
samIn.OpenForRead(inFile.c_str());
samOut.OpenForWrite(outFile.c_str());
samIn.ReadHeader(samHeader);
samOut.WriteHeader(samHeader);
while(samIn.ReadRecord(samHeader, samRecord) == true)
{
// Recalibrate.
processReadApplyTable(samRecord);
samOut.WriteRecord(samHeader, samRecord);
}
Logger::gLogger->writeLog("Total # Reads recab table not applied to: %ld", myNumApplySkipped);
Logger::gLogger->writeLog("Total # Reads recab table applied to: %ld", myNumApplyReads);
Logger::gLogger->writeLog("Recalibration successfully finished");
return EXIT_SUCCESS;
}
void Recab::addRecabSpecificParameters(LongParamContainer& params)
{
params.addGroup("Required Recab Parameters");
params.addString("refFile", &myRefFile);
params.addGroup("Optional Recab Parameters");
params.addString("dbsnp", &myDbsnpFile);
params.addInt("minBaseQual", &myMinBaseQual);
params.addInt("maxBaseQual", &myMaxBaseQual);
params.addInt("blended", &myBlendedWeight);
params.addBool("fitModel", &myFitModel);
params.addBool("fast", &myFast);
params.addBool("keepPrevDbsnp", &myKeepPrevDbsnp);
params.addBool("keepPrevNonAdjacent", &myKeepPrevNonAdjacent);
params.addBool("useLogReg", &myLogReg);
params.addString("qualField", &myQField);
params.addString("storeQualTag", &myStoreQualTag);
params.addString("buildExcludeFlags", &myBuildExcludeFlags);
params.addString("applyExcludeFlags", &myApplyExcludeFlags);
myParamsSetup = false;
mySqueeze.addBinningParameters(params);
}
int Recab::processRecabParam()
{
if(myRefFile.IsEmpty())
{
std::cerr << "Missing required --refFile parameter" << std::endl;
return EXIT_FAILURE;
}
myMaxBaseQualChar = BaseUtilities::getAsciiQuality(myMaxBaseQual);
return(mySqueeze.processBinningParam());
}
bool Recab::processReadBuildTable(SamRecord& samRecord)
{
static BaseData data;
static std::string chromosomeName;
static std::string readGroup;
static std::string aligTypes;
int seqLen = samRecord.getReadLength();
// Check if the parameters have been processed.
if(!myParamsSetup)
{
// This throws an exception if the reference cannot be setup.
processParams();
}
uint16_t flag = samRecord.getFlag();
if(!SamFlag::isMapped(flag))
{
// Unmapped, skip processing
++myUnMappedCount;
}
else
{
// This read is mapped.
++myMappedCount;
}
if(SamFlag::isSecondary(flag))
{
// Secondary read
++mySecondaryCount;
}
if(flag & SamFlag::SUPPLEMENTARY_ALIGNMENT)
{
// Supplementary read
++mySupplementaryCount;
}
if(SamFlag::isDuplicate(flag))
{
++myDupCount;
}
if(SamFlag::isQCFailure(flag))
{
++myQCFailCount;
}
// Check if the flag contains an exclude.
if((flag & myIntBuildExcludeFlags) != 0)
{
// Do not use this read for building the recalibration table.
++myNumBuildSkipped;
return(false);
}
if(samRecord.getMapQuality() == 0)
{
// 0 mapping quality, so skip processing.
++myMapQual0Count;
++myNumBuildSkipped;
return(false);
}
if(samRecord.getMapQuality() == 255)
{
// 255 mapping quality, so skip processing.
++myMapQual255Count;
++myNumBuildSkipped;
return(false);
}
chromosomeName = samRecord.getReferenceName();
readGroup = samRecord.getString("RG").c_str();
// Look for the read group in the map.
// TODO - extra string constructor??
RgInsertReturn insertRet =
myRg2Id.insert(std::pair<std::string, uint16_t>(readGroup, 0));
if(insertRet.second == true)
{
// New element inserted.
insertRet.first->second = myId2Rg.size();
myId2Rg.push_back(readGroup);
}
data.rgid = insertRet.first->second;
//reverse
bool reverse;
if(SamFlag::isReverse(flag))
reverse = true;
else
reverse = false;
if(myReferenceGenome == NULL)
{
throw std::runtime_error("Failed to setup Reference File.\n");
}
genomeIndex_t mapPos =
myReferenceGenome->getGenomePosition(chromosomeName.c_str(),
samRecord.get1BasedPosition());
if(mapPos==INVALID_GENOME_INDEX)
{
Logger::gLogger->warning("INVALID_GENOME_INDEX (chrom:pos %s:%ld) and record skipped... Reference in BAM is different from the ref used here!", chromosomeName.c_str(), samRecord.get1BasedPosition());
++myNumBuildSkipped;
return false;
}
if(!myQField.IsEmpty())
{
// Check if there is an old quality.
const String* oldQPtr =
samRecord.getStringTag(myQField.c_str());
if((oldQPtr != NULL) && (oldQPtr->Length() == seqLen))
{
// There is an old quality, so use that.
myQualityStrings.oldq = oldQPtr->c_str();
}
else
{
// Tag was not found, so use the current quality.
++myNumQualTagErrors;
if(myNumQualTagErrors == 1)
{
Logger::gLogger->warning("Recab: %s tag was not found/invalid, so using the quality field in records without the tag", myQField.c_str());
}
myQualityStrings.oldq = samRecord.getQuality();
}
//printf("%s\n",samRecord.getQuality());
//printf("%s:%s\n",myQField.c_str(),temp.c_str());
}
else
{
myQualityStrings.oldq = samRecord.getQuality();
}
if(myQualityStrings.oldq.length() != (unsigned int)seqLen)
{
Logger::gLogger->warning("Quality is not the correct length, so skipping recalibration on that record.");
++myNumBuildSkipped;
return(false);
}
aligTypes = "";
Cigar* cigarPtr = samRecord.getCigarInfo();
if(cigarPtr == NULL)
{
Logger::gLogger->warning("Failed to get the cigar");
++myNumBuildSkipped;
return(false);
}
// This read will be used for building the recab table.
++myNumBuildReads;
////////////////
////// iterate sequence
////////////////
genomeIndex_t refPos = 0;
int32_t refOffset = 0;
int32_t prevRefOffset = Cigar::INDEX_NA;
int32_t seqPos = 0;
int seqIncr = 1;
if(reverse)
{
seqPos = seqLen - 1;
seqIncr = -1;
}
// read
if(!SamFlag::isPaired(flag) || SamFlag::isFirstFragment(flag))
// Mark as first if it is not paired or if it is the
// first in the pair.
data.read = 0;
else
data.read = 1;
// Set unsetbase for curBase.
// This will be used for the prebase of cycle 0.
data.curBase = 'K';
for (data.cycle = 0; data.cycle < seqLen; data.cycle++, seqPos += seqIncr)
{
// Store the previous current base in preBase.
data.preBase = data.curBase;
// Get the current base before checking if we are going to
// process this position so it will be set for the next position.
data.curBase = samRecord.getSequence(seqPos);
if(reverse)
{
// Complement the current base.
// The prebase is already complemented.
data.curBase =
BaseAsciiMap::base2complement[(unsigned int)(data.curBase)];
}
// Get the reference offset.
refOffset = cigarPtr->getRefOffset(seqPos);
if(refOffset == Cigar::INDEX_NA)
{
// Not a match/mismatch, so continue to the next one which will
// not have a previous match/mismatch.
// Set previous ref offset to a negative so
// the next one won't be kept.
prevRefOffset = -2;
continue;
}
// This one is a match.
refPos = mapPos + refOffset;
// Check to see if we should process this position.
// Do not process if it is cycle 0 and:
// 1) current base is in dbsnp
if(data.cycle == 0)
{
if(!(myDbsnpFile.IsEmpty()) && myDbSNP[refPos])
{
// Save the previous reference offset.
++myNumDBSnpSkips;
prevRefOffset = refOffset;
continue;
}
}
else
{
// Do not process if it is not cycle 0 and:
// 1) previous reference position not adjacent
// (not a match/mismatch)
// 2) previous base is in dbsnp
// 3) current base is in dbsnp
if((!myKeepPrevNonAdjacent && (refOffset != (prevRefOffset + seqIncr))) ||
(data.preBase == 'K'))
{
// Save the previous reference offset.
prevRefOffset = refOffset;
continue;
}
if(!(myDbsnpFile.IsEmpty()) &&
(myDbSNP[refPos] ||
(!myKeepPrevDbsnp && myDbSNP[refPos - seqIncr])))
{
++myNumDBSnpSkips;
// Save the previous reference offset.
prevRefOffset = refOffset;
continue;
}
}
// Save the previous reference offset.
prevRefOffset = refOffset;
// Set the reference & read bases in the Covariates
char refBase = (*myReferenceGenome)[refPos];
if(BaseUtilities::isAmbiguous(refBase))
{
// N reference, so skip it when building the table.
++myAmbiguous;
continue;
}
if(reverse)
{
refBase = BaseAsciiMap::base2complement[(unsigned int)(refBase)];
}
// Get quality char
data.qual =
BaseUtilities::getPhredBaseQuality(myQualityStrings.oldq[seqPos]);
// skip bases with quality below the minimum set.
if(data.qual < myMinBaseQual)
{
++mySubMinQual;
continue;
}
if(BaseUtilities::areEqual(refBase, data.curBase)
&& (BaseAsciiMap::base2int[(unsigned int)(data.curBase)] < 4))
myBMatchCount++;
else
myBMismatchCount++;
hasherrormodel.setCell(data, refBase);
myBasecounts++;
}
return true;
}
bool Recab::processReadApplyTable(SamRecord& samRecord)
{
static BaseData data;
static std::string readGroup;
static std::string aligTypes;
int seqLen = samRecord.getReadLength();
uint16_t flag = samRecord.getFlag();
// Check if the flag contains an exclude.
if((flag & myIntApplyExcludeFlags) != 0)
{
// Do not apply the recalibration table to this read.
++myNumApplySkipped;
return(false);
}
++myNumApplyReads;
readGroup = samRecord.getString("RG").c_str();
// Look for the read group in the map.
// TODO - extra string constructor??
RgInsertReturn insertRet =
myRg2Id.insert(std::pair<std::string, uint16_t>(readGroup, 0));
if(insertRet.second == true)
{
// New element inserted.
insertRet.first->second = myId2Rg.size();
myId2Rg.push_back(readGroup);
}
data.rgid = insertRet.first->second;
if(!myQField.IsEmpty())
{
// Check if there is an old quality.
const String* oldQPtr =
samRecord.getStringTag(myQField.c_str());
if((oldQPtr != NULL) && (oldQPtr->Length() == seqLen))
{
// There is an old quality, so use that.
myQualityStrings.oldq = oldQPtr->c_str();
}
else
{
myQualityStrings.oldq = samRecord.getQuality();
}
}
else
{
myQualityStrings.oldq = samRecord.getQuality();
}
if(myQualityStrings.oldq.length() != (unsigned int)seqLen)
{
Logger::gLogger->warning("Quality is not the correct length, so skipping recalibration on that record.");
return(false);
}
myQualityStrings.newq.resize(seqLen);
////////////////
////// iterate sequence
////////////////
int32_t seqPos = 0;
int seqIncr = 1;
bool reverse;
if(SamFlag::isReverse(flag))
{
reverse = true;
seqPos = seqLen - 1;
seqIncr = -1;
}
else
reverse = false;
// Check which read - this will be the same for all positions, so
// do this outside of the smaller loop.
if(!SamFlag::isPaired(flag) || SamFlag::isFirstFragment(flag))
// Mark as first if it is not paired or if it is the
// first in the pair.
data.read = 0;
else
data.read = 1;
// Set unsetbase for curBase.
// This will be used for the prebase of cycle 0.
data.curBase = 'K';
for (data.cycle = 0; data.cycle < seqLen; data.cycle++, seqPos += seqIncr)
{
// Set the preBase to the previous cycle's current base.
// For cycle 0, curBase was set to a default value.
data.preBase = data.curBase;
// Get the current base.
data.curBase = samRecord.getSequence(seqPos);
if(reverse)
{
// Complement the current base.
data.curBase =
BaseAsciiMap::base2complement[(unsigned int)(data.curBase)];
}
// Get quality
data.qual =
BaseUtilities::getPhredBaseQuality(myQualityStrings.oldq[seqPos]);
// skip bases with quality below the minimum set.
if(data.qual < myMinBaseQual)
{
myQualityStrings.newq[seqPos] = myQualityStrings.oldq[seqPos];
continue;
}
// Update quality score
uint8_t qemp = hasherrormodel.getQemp(data);
qemp = mySqueeze.getQualCharFromQemp(qemp);
if(qemp > myMaxBaseQualChar)
{
qemp = myMaxBaseQualChar;
}
myQualityStrings.newq[seqPos] = qemp;
}
if(!myStoreQualTag.IsEmpty())
{
samRecord.addTag(myStoreQualTag, 'Z', myQualityStrings.oldq.c_str());
}
samRecord.setQuality(myQualityStrings.newq.c_str());
return true;
}
void Recab::modelFitPrediction(const char* outputBase)
{
Logger::gLogger->writeLog("# mapped Reads observed: %ld", myMappedCount);
Logger::gLogger->writeLog("# unmapped Reads observed: %ld", myUnMappedCount);
Logger::gLogger->writeLog("# Secondary Reads observed: %ld", mySecondaryCount);
Logger::gLogger->writeLog("# Supplementary Reads observed: %ld", mySupplementaryCount);
Logger::gLogger->writeLog("# Duplicate Reads observed: %ld", myDupCount);
Logger::gLogger->writeLog("# QC Failure Reads observed: %ld", myQCFailCount);
Logger::gLogger->writeLog("# Mapping Quality 0 Reads skipped: %ld", myMapQual0Count);
Logger::gLogger->writeLog("# Mapping Quality 255 Reads skipped: %ld", myMapQual255Count);
Logger::gLogger->writeLog("Total # Reads skipped for building recab table: %ld", myNumBuildSkipped);
Logger::gLogger->writeLog("Total # Reads used for building recab table: %ld", myNumBuildReads);
Logger::gLogger->writeLog("# Bases observed: %ld - #match: %ld; #mismatch: %ld",
myBasecounts, myBMatchCount, myBMismatchCount);
Logger::gLogger->writeLog("# Bases Skipped for DBSNP: %ld, for BaseQual < %ld: %ld, ref 'N': %ld",
myNumDBSnpSkips, myMinBaseQual, mySubMinQual, myAmbiguous);
if(myNumQualTagErrors != 0)
{
Logger::gLogger->warning("%ld records did not have tag %s or it was invalid, so the quality field was used for those records.", myNumQualTagErrors, myQField.c_str());
}
////////////////////////
////////////////////////
if(myLogReg || myFitModel)
{
//// Model fitting + prediction
std::string modelfile = outputBase;
modelfile += ".model";
if(outputBase[0] == '-')
{
modelfile = "";
}
prediction.setErrorModel(&(hasherrormodel));
Logger::gLogger->writeLog("Start model fitting!");
if(!prediction.fitModel(true,modelfile))
{
Logger::gLogger->error("Could not fit model!");
}
hasherrormodel.addPrediction(prediction.getModel(),myBlendedWeight);
if(outputBase[0] != '-')
{
std::string recabFile = outputBase;
recabFile += ".recab";
Logger::gLogger->writeLog("Writing recalibration table %s",recabFile.c_str());
if(!(hasherrormodel.writeTableQemp(recabFile,
myId2Rg, true)))
Logger::gLogger->error("Writing errormodel not possible!");
}
}
if(outputBase[0] != '-')
{
std::string qempFile = outputBase;
qempFile += ".qemp";
Logger::gLogger->writeLog("Writing recalibration table %s",qempFile.c_str());
if(!(hasherrormodel.writeTableQemp(qempFile,
myId2Rg, false)))
Logger::gLogger->error("Writing errormodel not possible!");
}
}
void Recab::processParams()
{
if(myReferenceGenome == NULL)
{
Logger::gLogger->writeLog("Open reference");
myReferenceGenome = new GenomeSequence(myRefFile);
if(myReferenceGenome == NULL)
{
throw std::runtime_error("Failed to open Reference File.\n");
}
Logger::gLogger->writeLog("Done! Sequence length %u",
myReferenceGenome->sequenceLength());
//dbSNP
if(myDbsnpFile.IsEmpty())
{
Logger::gLogger->writeLog("No dbSNP File");
}
else if(myReferenceGenome->loadDBSNP(myDbSNP,myDbsnpFile.c_str()))
{
Logger::gLogger->error("Failed to open dbSNP file.");
}
}
if(myLogReg && myFast)
{
Logger::gLogger->writeLog("Cannot use both --fast & --useLogReg, so just using --useLogReg");
myFast = false;
}
if(myFast)
{
myFitModel = false;
}
if(myLogReg)
{
// Set fitModel.
myFitModel = true;
}
HashErrorModel::setUseLogReg(myLogReg);
HashErrorModel::setUseFast(myFast);
myIntBuildExcludeFlags = myBuildExcludeFlags.AsInteger();
myIntApplyExcludeFlags = myApplyExcludeFlags.AsInteger();
myParamsSetup = true;
}