| 1 |
#include "CNV.h" |
| 2 |
|
| 3 |
bool CNVMap::load(string const& filename) { |
| 4 |
string line; |
| 5 |
ifstream cnvFile(filename.c_str(), ios::in); |
| 6 |
if (cnvFile.is_open()) { |
| 7 |
while (getline (cnvFile, line)) { |
| 8 |
vector<string> fields = split(line, " \t"); |
| 9 |
if (fields.size() == 2) { |
| 10 |
const string& sample = fields.at(0); |
| 11 |
int ploidy = atoi(fields.at(1).c_str()); |
| 12 |
setSamplePloidy(sample, ploidy); |
| 13 |
} else if (fields.size() == 5) { |
| 14 |
// note conversion between 1 and 0 based |
| 15 |
const string& sample = fields.at(3); |
| 16 |
const string& seq = fields.at(0); |
| 17 |
long int start = atol(fields.at(1).c_str()); |
| 18 |
long int end = atol(fields.at(2).c_str()); |
| 19 |
int ploidy = atoi(fields.at(4).c_str()); |
| 20 |
setPloidy(sample, seq, start, end, ploidy); |
| 21 |
} else { |
| 22 |
cerr << "error [freebayes::CNVMap] could not parse CNVmap line " << line << endl; |
| 23 |
exit(1); |
| 24 |
} |
| 25 |
} |
| 26 |
} else { |
| 27 |
return false; |
| 28 |
} |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
void CNVMap::setDefaultPloidy(int defploidy) { |
| 33 |
defaultPloidy = defploidy; |
| 34 |
} |
| 35 |
|
| 36 |
void CNVMap::setSamplePloidy(const string& sample, int ploidy) { |
| 37 |
samplePloidy[sample] = ploidy; |
| 38 |
} |
| 39 |
|
| 40 |
void CNVMap::setPloidy(string const& sample, string const& seq, long int start, long int end, int ploidy) { |
| 41 |
sampleSeqCNV[sample][seq].push_back(make_tuple(start, end, ploidy)); |
| 42 |
} |
| 43 |
|
| 44 |
int CNVMap::ploidy(string const& sample, string const& seq, long int position) { |
| 45 |
|
| 46 |
int basePloidy = (samplePloidy.find(sample) != samplePloidy.end()) ? samplePloidy[sample] : defaultPloidy; |
| 47 |
|
| 48 |
if (sampleSeqCNV.empty()) { |
| 49 |
return basePloidy; |
| 50 |
} |
| 51 |
|
| 52 |
SampleSeqCNVMap::iterator scnv = sampleSeqCNV.find(sample); |
| 53 |
|
| 54 |
if (scnv == sampleSeqCNV.end()) { |
| 55 |
return basePloidy; |
| 56 |
} else { |
| 57 |
map<string, vector<tuple<long int, long int, int> > >::iterator c = scnv->second.find(seq); |
| 58 |
if (c == scnv->second.end()) { |
| 59 |
return basePloidy; |
| 60 |
} else { |
| 61 |
vector<tuple<long int, long int, int> >& cnvs = c->second; |
| 62 |
|
| 63 |
vector<tuple<long int, long int, int> >::iterator i = upper_bound(cnvs.begin(), cnvs.end(), |
| 64 |
position, [](long int position, tuple<long int, long int, int> const& element) { |
| 65 |
return position < get<1>(element); |
| 66 |
}); |
| 67 |
if (i == cnvs.end()) { |
| 68 |
return basePloidy; |
| 69 |
} |
| 70 |
|
| 71 |
long int start = get<0>(*i); |
| 72 |
int copyNumber = get<2>(*i); |
| 73 |
if (start <= position) { |
| 74 |
return copyNumber; |
| 75 |
} else { |
| 76 |
return basePloidy; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
} |