forked from twolinin/longphase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhasingProcess.cpp
174 lines (145 loc) · 6.67 KB
/
PhasingProcess.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
#include "PhasingProcess.h"
#include "PhasingGraph.h"
#include "ParsingBam.h"
PhasingProcess::PhasingProcess(PhasingParameters params)
{
std::cerr<< "LongPhase 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<< "REF File : " << params.fastaFile << "\n";
std::cerr<< "Output Prefix : " << params.resultPrefix << "\n";
std::cerr<< "Generate Dot : " << ( params.generateDot ? "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<< "Seq Platform : " << ( params.isONT ? "ONT" : "PB" ) << "\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<< "Edge Threshold : " << params.edgeThreshold << "\n";
std::cerr<< "Mapping Quality : " << params.mappingQuality << "\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 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);
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);
std::cerr<< difftime(time(NULL), begin) << "s\n";
// get all detected chromosome
std::vector<std::string> chrName = snpFile.getChrVec();
// record all phasing result
ChrPhasingResult chrPhasingResult;
// Initialize an empty map in chrPhasingResult to store all phasing results.
// This is done to prevent issues with multi-threading, by defining an empty map first.
for (std::vector<std::string>::iterator chrIter = chrName.begin(); chrIter != chrName.end(); chrIter++) {
chrPhasingResult[*chrIter] = PhasingResult();
}
// set chrNumThreads and bamParserNumThreads based on parameters
int chrNumThreads,bamParserNumThreads;
setPhasingNumThreads(chrName.size(), params.numThreads, chrNumThreads, bamParserNumThreads);
begin = time(NULL);
// loop all chromosome
#pragma omp parallel for schedule(dynamic) num_threads(chrNumThreads)
for(std::vector<std::string>::iterator chrIter = chrName.begin(); chrIter != chrName.end() ; chrIter++ ){
std::time_t chrbegin = time(NULL);
// get lase SNP variant position
int lastSNPpos = snpFile.getLastSNP((*chrIter));
// therer is no variant on SNP file.
if( lastSNPpos == -1 ){
continue;
}
// create a bam parser object and prepare to fetch varint from each vcf file
BamParser *bamParser = new BamParser((*chrIter), params.bamFile, snpFile, svFile, modFile);
// fetch chromosome string
std::string chr_reference = fastaParser.chrString.at(*chrIter);
// use to store variant
std::vector<ReadVariant> readVariantVec;
// run fetch variant process
bamParser->direct_detect_alleles(lastSNPpos, bamParserNumThreads, params, readVariantVec ,chr_reference);
// free memory
delete bamParser;
// filter variants prone to switch errors in ONT sequencing.
if(params.isONT){
snpFile.filterSNP((*chrIter), readVariantVec, chr_reference);
}
// bam files are partial file or no read support this chromosome's SNP
if( readVariantVec.size() == 0 ){
continue;
}
// create a graph object and prepare to phasing.
VairiantGraph *vGraph = new VairiantGraph(chr_reference, params);
// trans read-snp info to edge info
vGraph->addEdge(readVariantVec);
// run main algorithm
vGraph->phasingProcess();
// push result to phasingResult
vGraph->exportResult((*chrIter), chrPhasingResult[*chrIter]);
// generate dot file
if(params.generateDot){
vGraph->writingDotFile((*chrIter));
}
// release the memory used by the object.
vGraph->destroy();
// free memory
readVariantVec.clear();
readVariantVec.shrink_to_fit();
delete vGraph;
std::cerr<< "(" << (*chrIter) << "," << difftime(time(NULL), chrbegin) << "s)";
}
std::cerr<< "\nparsing total: " << difftime(time(NULL), begin) << "s\n";
begin = time(NULL);
std::cerr<< "merge results ... ";
// Create a container for merged phasing results.
PhasingResult mergedPhasingResult;
// Merge phasing results from all chromosomes.
mergeAllChrPhasingResult(chrPhasingResult, mergedPhasingResult);
std::cerr<< difftime(time(NULL), begin) << "s\n";
begin = time(NULL);
std::cerr<< "writeResult SNP ... ";
snpFile.writeResult(mergedPhasingResult);
std::cerr<< difftime(time(NULL), begin) << "s\n";
if(params.svFile!=""){
begin = time(NULL);
std::cerr<< "write SV Result ... ";
svFile.writeResult(mergedPhasingResult);
std::cerr<< difftime(time(NULL), begin) << "s\n";
}
if(params.modFile!=""){
begin = time(NULL);
std::cerr<< "write mod Result ... ";
modFile.writeResult(mergedPhasingResult);
std::cerr<< difftime(time(NULL), begin) << "s\n";
}
std::cerr<< "\ntotal process: " << difftime(time(NULL), processBegin) << "s\n";
return;
};
PhasingProcess::~PhasingProcess(){
};