-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhasingProcess.cpp
More file actions
230 lines (200 loc) · 10.1 KB
/
PhasingProcess.cpp
File metadata and controls
230 lines (200 loc) · 10.1 KB
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
#include "PhasingProcess.h"
#include "PhasingGraph.h"
#include "ParsingBam.h"
PhasingProcess::PhasingProcess(PhasingParameters params)
{
std::cerr<< "LongPhase-TO Ver " << params.version << "\n";
std::cerr<< "\n";
std::cerr<< "--- File Parameter --- \n";
std::cerr<< "SNP File : " << params.snpFile << "\n";
std::cerr<< "SV File : " << params.svFile << "\n";
std::cerr<< "MOD File : " << params.modFile << "\n";
std::cerr<< "PON File : " << params.ponFile << "\n";
std::cerr<< "Strict PON File: " << params.strictPonFile << "\n";
std::cerr<< "REF File : " << params.fastaFile << "\n";
std::cerr<< "Output Prefix : " << params.resultPrefix << "\n";
std::cerr<< "Generate Dot : " << ( params.generateDot ? "True" : "False" ) << "\n";
std::cerr<< "Output LOH : " << ( params.outputLOH ? "True" : "False" ) << "\n";
std::cerr<< "Output SGE : " << ( params.outputSGE ? "True" : "False" ) << "\n";
std::cerr<< "Output LGE : " << ( params.outputLGE ? "True" : "False" ) << "\n";
std::cerr<< "Output GE : " << ( params.outputGE ? "True" : "False" ) << "\n";
std::cerr<< "BAM File : ";
for( auto file : params.bamFile){
std::cerr<< file <<" " ;
}
std::cerr << "\n";
std::cerr<< "\n";
std::cerr<< "--- Phasing Parameter --- \n";
std::cerr<< "Caller : " << params.callerStr << "\n";
std::cerr<< "Phase Indel : " << ( params.phaseIndel ? "True" : "False" ) << "\n";
std::cerr<< "Distance Threshold : " << params.distance << "\n";
std::cerr<< "Connect Adjacent : " << params.connectAdjacent << "\n";
std::cerr<< "Somatic Connect Adjacent : " << params.somaticConnectAdjacent << "\n";
std::cerr<< "Edge Threshold : " << params.edgeThreshold << "\n";
std::cerr<< "Overlap Threshold : " << params.overlapThreshold << "\n";
std::cerr<< "Mapping Quality : " << params.mappingQuality << "\n";
std::cerr<< "Mismatch Rate : " << params.mismatchRate << "\n";
std::cerr<< "Variant Confidence : " << params.snpConfidence << "\n";
std::cerr<< "ReadTag Confidence : " << params.readConfidence << "\n";
std::cerr<< "\n";
std::time_t processBegin = time(NULL);
// load SNP vcf file
std::time_t begin = time(NULL);
std::cerr<< "parsing VCF ... ";
SnpParser snpFile(params);
std::cerr<< difftime(time(NULL), begin) << "s\n";
// load PON vcf and strict PON vcf file
begin = time(NULL);
std::cerr<< "parsing PON VCF ... ";
snpFile.setGermline(params.ponFile, params.strictPonFile);
std::cerr<< difftime(time(NULL), begin) << "s\n";
// load SV vcf file
begin = time(NULL);
std::cerr<< "parsing SV VCF ... ";
SVParser svFile(params, snpFile);
std::cerr<< difftime(time(NULL), begin) << "s\n";
//Parse mod vcf file
begin = time(NULL);
std::cerr<< "parsing Meth VCF ... ";
METHParser modFile(params, snpFile, svFile);
std::cerr<< difftime(time(NULL), begin) << "s\n";
// parsing ref fasta
begin = time(NULL);
std::cerr<< "reading reference ... ";
std::vector<int> last_pos;
for(auto chr :snpFile.getChrVec()){
last_pos.push_back(snpFile.getLastSNP(chr));
}
FastaParser fastaParser(params.fastaFile, snpFile.getChrVec(), last_pos, params.numThreads);
std::cerr<< difftime(time(NULL), begin) << "s\n";
// get all detected chromosome
std::vector<std::string> chrName = snpFile.getChrVec();
// record chromosome info
std::map<std::string, ChrInfo> chrInfoMap;
// Initialize chrInfoMap entries for each chromosome name in advance
// to avoid potential issues during parallel processing later
for (std::vector<std::string>::iterator chrIter = chrName.begin(); chrIter != chrName.end(); chrIter++) {
chrInfoMap[*chrIter] = ChrInfo();
}
// init data structure and get core n
htsThreadPool threadPool = {NULL, 0};
// creat thread pool
if (!(threadPool.pool = hts_tpool_init(params.numThreads))) {
fprintf(stderr, "Error creating thread pool\n");
}
if (!params.disableCalling){
std::cerr << "parsing BAM, somatic calling, and phasing" << std::endl;
}else{
std::cerr << "parsing BAM, and phasing" << std::endl;
}
begin = time(NULL);
// loop all chromosome
#pragma omp parallel for schedule(dynamic) num_threads(params.numThreads)
for(std::vector<std::string>::iterator chrIter = chrName.begin(); chrIter != chrName.end() ; chrIter++ ){
std::time_t chrbegin = time(NULL);
ChrInfo &chrInfo = chrInfoMap[*chrIter];
// get lase SNP variant position
int lastSNPpos = snpFile.getLastSNP((*chrIter));
// therer is no variant on SNP file.
if( lastSNPpos == -1 ){
continue;
}
// fetch chromosome string
std::string chr_reference = fastaParser.chrString.at(*chrIter);
// create a bam parser object and prepare to fetch varint from each vcf file
BamParser *bamParser = new BamParser((*chrIter), params.bamFile, snpFile, svFile, modFile, chr_reference);
// use to store variant
std::vector<ReadVariant> *readVariantVec = new std::vector<ReadVariant>();
// run fetch variant process
bamParser->direct_detect_alleles(lastSNPpos, threadPool, params, *readVariantVec, chrInfo.clipCount, chr_reference);
// free memory
delete bamParser;
// bam files are partial file or no read support this chromosome's SNP
if( readVariantVec->size() == 0 ){
delete readVariantVec;
continue;
}
// create a clip object and prepare to detect Interval
Clip *clip = new Clip(*chrIter);
// get the interval of the genomic event
clip->detectGenomicEventInterval(chrInfo.clipCount, chrInfo.largeGenomicEventInterval, chrInfo.smallGenomicEventRegion);
// get the region of the LOH
clip->detectLOHRegion(snpFile, chrInfo.LOHSegments);
// free memory
delete clip;
// create a graph object and prepare to phasing.
VairiantGraph *vGraph = new VairiantGraph(chr_reference, params, (*chrIter));
chrInfo.vGraph = vGraph;
// trans read-snp info to edge info
vGraph->addEdge(readVariantVec, chrInfo.LOHSegments);
if(!params.disableCalling){
// run somatic calling algorithm
vGraph->somaticCalling(snpFile.getVariants((*chrIter)));
}else{
vGraph->tagSomatic(snpFile.getVariants((*chrIter)));
}
// run main algorithm
vGraph->phasingProcess(chrInfo.posPhasingResult, chrInfo.LOHSegments, &chrInfo.ploidyRatioMap);
std::cerr<< "(" << (*chrIter) << "," << difftime(time(NULL), chrbegin) << "s)";
}
std::map<std::string, std::map<double, int>> mergedPloidyRatioMap;
for (const auto& chrInfo : chrInfoMap) {
mergedPloidyRatioMap[chrInfo.first] = chrInfo.second.ploidyRatioMap;
}
double purity = params.purity;
if (purity < 0.0) {
purity = PurityCalculator::getPurity(mergedPloidyRatioMap, params.resultPrefix, params.caller, chrInfoMap, fastaParser.chrLength);
}
std::cerr << std::endl;
std::cerr << "purity: " << purity << std::endl;
bool highPurity = purity > 0.9;
if(highPurity){
std::cerr << "second round phasing, ";
}
std::cerr << "export phasing result" << std::endl;
#pragma omp parallel for schedule(dynamic) num_threads(params.numThreads)
for(std::vector<std::string>::iterator chrIter = chrName.begin(); chrIter != chrName.end() ; chrIter++ ){
std::time_t chrbegin = time(NULL);
ChrInfo &chrInfo = chrInfoMap[*chrIter];
if(chrInfo.vGraph == nullptr)continue;
VairiantGraph *vGraph = chrInfo.vGraph;
if(highPurity){
// convert non-pon variants to somatic variants
vGraph->convertNonGermlineToSomatic();
// reset phasing result
chrInfo.posPhasingResult = PosPhasingResult();
// run main algorithm
vGraph->phasingProcess(chrInfo.posPhasingResult, chrInfo.LOHSegments, nullptr);
}
// export phasing result
vGraph->exportPhasingResult(chrInfo.posPhasingResult, chrInfo.LOHSegments);
// generate dot file
if(params.generateDot){
vGraph->writingDotFile((*chrIter));
}
// release the memory used by the object.
vGraph->destroy();
delete vGraph;
std::cerr<< "(" << (*chrIter) << "," << difftime(time(NULL), chrbegin) << "s)";
}
hts_tpool_destroy(threadPool.pool);
// Transfer phasing results from chrInfoMap to chrPhasingResult
ChrPhasingResult chrPhasingResult;
for (const auto& chrInfo : chrInfoMap) {
chrPhasingResult[chrInfo.first] = chrInfo.second.posPhasingResult;
}
std::cerr<< "\nparsing total: " << difftime(time(NULL), begin) << "s\n";
// write result to file
GenomicWriter genomicWriter(params.resultPrefix, chrName, chrInfoMap);
genomicWriter.measureTime("LOH", params.outputLOH, [&]() { genomicWriter.writeLOH(); });
genomicWriter.measureTime("SGE", params.outputSGE, [&]() { genomicWriter.writeSGE(); });
genomicWriter.measureTime("LGE", params.outputLGE, [&]() { genomicWriter.writeLGE(); });
genomicWriter.measureTime("GE", params.outputGE, [&]() { genomicWriter.writeAllEvents(); });
genomicWriter.measureTime("SNP", true, [&]() { snpFile.writeResult(chrPhasingResult, purity); });
genomicWriter.measureTime("SV", params.svFile != "", [&]() { svFile.writeResult(chrPhasingResult); });
genomicWriter.measureTime("MOD", params.modFile != "", [&]() { modFile.writeResult(chrPhasingResult); });
std::cerr<< "\ntotal process: " << difftime(time(NULL), processBegin) << "s\n";
return;
};
PhasingProcess::~PhasingProcess(){
};