| 1 |
#include "Sample.h" |
| 2 |
|
| 3 |
|
| 4 |
// sample tracking and allele sorting |
| 5 |
// the number of observations for this allele |
| 6 |
int Sample::observationCount(Allele& allele) { |
| 7 |
return observationCount(allele.currentBase); |
| 8 |
} |
| 9 |
|
| 10 |
int Sample::observationCountInclPartials(void) { |
| 11 |
return observationCount() + partialObservationCount(); |
| 12 |
} |
| 13 |
|
| 14 |
double Sample::observationCountInclPartials(Allele& allele) { |
| 15 |
return observationCountInclPartials(allele.currentBase); |
| 16 |
} |
| 17 |
|
| 18 |
double Sample::partialObservationCount(Allele& allele) { |
| 19 |
return partialObservationCount(allele.currentBase); |
| 20 |
} |
| 21 |
|
| 22 |
// the number of observations for this base |
| 23 |
int Sample::observationCount(const string& base) { |
| 24 |
Sample::iterator g = find(base); |
| 25 |
if (g != end()) |
| 26 |
return g->second.size(); |
| 27 |
else |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
int Sample::partialObservationCount(void) { |
| 32 |
return reversePartials.size(); |
| 33 |
} |
| 34 |
|
| 35 |
double Sample::partialObservationCount(const string& base) { |
| 36 |
double scaledPartialCount = 0; |
| 37 |
map<string, vector<Allele*> >::iterator g = partialSupport.find(base); |
| 38 |
if (g != partialSupport.end()) { |
| 39 |
vector<Allele*>& supportingObs = g->second; |
| 40 |
for (vector<Allele*>::iterator a = supportingObs.begin(); a != supportingObs.end(); ++a) { |
| 41 |
scaledPartialCount += (double) 1 / (double) reversePartials[*a].size(); |
| 42 |
} |
| 43 |
} |
| 44 |
return scaledPartialCount; |
| 45 |
} |
| 46 |
|
| 47 |
double Sample::observationCountInclPartials(const string& base) { |
| 48 |
return observationCount(base) + partialObservationCount(base); |
| 49 |
} |
| 50 |
|
| 51 |
// the total number of observations |
| 52 |
int Sample::observationCount(void) { |
| 53 |
int count = 0; |
| 54 |
for (Sample::iterator g = begin(); g != end(); ++g) { |
| 55 |
count += g->second.size(); |
| 56 |
} |
| 57 |
return count; |
| 58 |
} |
| 59 |
|
| 60 |
int Sample::qualSum(Allele& allele) { |
| 61 |
return qualSum(allele.currentBase); |
| 62 |
} |
| 63 |
|
| 64 |
int Sample::qualSum(const string& base) { |
| 65 |
Sample::iterator g = find(base); |
| 66 |
int qsum = 0; |
| 67 |
if (g != end()) { |
| 68 |
vector<Allele*>& alleles = g->second; |
| 69 |
for (vector<Allele*>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 70 |
qsum += (*a)->quality; |
| 71 |
} |
| 72 |
} |
| 73 |
return qsum; |
| 74 |
} |
| 75 |
|
| 76 |
double Sample::partialQualSum(Allele& allele) { |
| 77 |
return partialQualSum(allele.currentBase); |
| 78 |
} |
| 79 |
|
| 80 |
double Sample::partialQualSum(const string& base) { |
| 81 |
Sample::iterator g = partialSupport.find(base); |
| 82 |
double qsum = 0; |
| 83 |
if (g != partialSupport.end()) { |
| 84 |
vector<Allele*>& alleles = g->second; |
| 85 |
for (vector<Allele*>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 86 |
qsum += (double) (*a)->quality / (double) reversePartials[*a].size(); |
| 87 |
} |
| 88 |
} |
| 89 |
return qsum; |
| 90 |
} |
| 91 |
|
| 92 |
// sample tracking and allele sorting |
| 93 |
// the number of observations for this allele |
| 94 |
int Samples::observationCount(Allele& allele) { |
| 95 |
return observationCount(allele.currentBase); |
| 96 |
} |
| 97 |
|
| 98 |
double Samples::observationCountInclPartials(Allele& allele) { |
| 99 |
return observationCountInclPartials(allele.currentBase); |
| 100 |
} |
| 101 |
|
| 102 |
double Samples::partialObservationCount(Allele& allele) { |
| 103 |
return partialObservationCount(allele.currentBase); |
| 104 |
} |
| 105 |
|
| 106 |
// the number of observations for this base |
| 107 |
int Samples::observationCount(const string& base) { |
| 108 |
int c = 0; |
| 109 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 110 |
c += s->second.observationCount(base); |
| 111 |
} |
| 112 |
return c; |
| 113 |
} |
| 114 |
|
| 115 |
double Samples::partialObservationCount(const string& base) { |
| 116 |
double c = 0; |
| 117 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 118 |
c += s->second.partialObservationCount(base); |
| 119 |
} |
| 120 |
return c; |
| 121 |
} |
| 122 |
|
| 123 |
double Samples::observationCountInclPartials(const string& base) { |
| 124 |
return observationCount(base) + partialObservationCount(base); |
| 125 |
} |
| 126 |
|
| 127 |
// the total number of observations |
| 128 |
int Samples::observationCount(void) { |
| 129 |
int c = 0; |
| 130 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 131 |
c += s->second.observationCount(); |
| 132 |
} |
| 133 |
return c; |
| 134 |
} |
| 135 |
|
| 136 |
double Samples::observationCountInclPartials(void) { |
| 137 |
double c = 0; |
| 138 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 139 |
c += s->second.observationCountInclPartials(); |
| 140 |
} |
| 141 |
return c; |
| 142 |
} |
| 143 |
|
| 144 |
int Samples::qualSum(Allele& allele) { |
| 145 |
return qualSum(allele.currentBase); |
| 146 |
} |
| 147 |
|
| 148 |
int Samples::qualSum(const string& base) { |
| 149 |
int q = 0; |
| 150 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 151 |
q += s->second.qualSum(base); |
| 152 |
} |
| 153 |
return q; |
| 154 |
} |
| 155 |
|
| 156 |
double Samples::partialQualSum(Allele& allele) { |
| 157 |
return partialQualSum(allele.currentBase); |
| 158 |
} |
| 159 |
|
| 160 |
double Samples::partialQualSum(const string& base) { |
| 161 |
double q = 0; |
| 162 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 163 |
q += s->second.partialQualSum(base); |
| 164 |
} |
| 165 |
return q; |
| 166 |
} |
| 167 |
|
| 168 |
map<string, double> Samples::estimatedAlleleFrequencies(void) { |
| 169 |
map<string, long double> qualsums; |
| 170 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 171 |
Sample& sample = s->second; |
| 172 |
for (Sample::iterator o = sample.begin(); o != sample.end(); ++o) { |
| 173 |
const string& base = o->first; |
| 174 |
qualsums[base] += sample.qualSum(base); |
| 175 |
} |
| 176 |
} |
| 177 |
long double total = 0; |
| 178 |
for (map<string, long double>::iterator q = qualsums.begin(); q != qualsums.end(); ++q) { |
| 179 |
total += q->second; |
| 180 |
} |
| 181 |
map<string, double> freqs; |
| 182 |
for (map<string, long double>::iterator q = qualsums.begin(); q != qualsums.end(); ++q) { |
| 183 |
freqs[q->first] = q->second / total; |
| 184 |
//cerr << "estimated frequency " << q->first << " " << freqs[q->first] << endl; |
| 185 |
} |
| 186 |
return freqs; |
| 187 |
} |
| 188 |
|
| 189 |
// puts alleles into the right bins if they have changed their base (as |
| 190 |
// occurs in the case of reference alleles) |
| 191 |
void Sample::sortReferenceAlleles(void) { |
| 192 |
for (Sample::iterator g = begin(); g != end(); ++g) { |
| 193 |
const string& groupBase = g->first; |
| 194 |
vector<Allele*>& alleles = g->second; |
| 195 |
for (vector<Allele*>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 196 |
const string& base = (*a)->currentBase; |
| 197 |
if (base != groupBase) { |
| 198 |
Sample::iterator g = find(base); |
| 199 |
if (g != end()) { |
| 200 |
g->second.push_back(*a); |
| 201 |
} else { |
| 202 |
vector<Allele*> alleles; |
| 203 |
alleles.push_back(*a); |
| 204 |
insert(begin(), make_pair(base, alleles)); |
| 205 |
} |
| 206 |
*a = NULL; |
| 207 |
} |
| 208 |
} |
| 209 |
alleles.erase(remove(alleles.begin(), alleles.end(), (Allele*)NULL), alleles.end()); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
StrandBaseCounts |
| 214 |
Sample::strandBaseCount(string refbase, string altbase) { |
| 215 |
|
| 216 |
int forwardRef = 0; |
| 217 |
int reverseRef = 0; |
| 218 |
int forwardAlt = 0; |
| 219 |
int reverseAlt = 0; |
| 220 |
|
| 221 |
for (Sample::iterator s = begin(); s != end(); ++s) { |
| 222 |
vector<Allele*>& alleles = s->second; |
| 223 |
for (vector<Allele*>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 224 |
string base = (*a)->currentBase; |
| 225 |
AlleleStrand strand = (*a)->strand; |
| 226 |
if (base == refbase) { |
| 227 |
if (strand == STRAND_FORWARD) |
| 228 |
++forwardRef; |
| 229 |
else if (strand == STRAND_REVERSE) |
| 230 |
++reverseRef; |
| 231 |
} else if (base == altbase) { |
| 232 |
if (strand == STRAND_FORWARD) |
| 233 |
++forwardAlt; |
| 234 |
else if (strand == STRAND_REVERSE) |
| 235 |
++reverseAlt; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return StrandBaseCounts(forwardRef, forwardAlt, reverseRef, reverseAlt); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
int Sample::baseCount(string base, AlleleStrand strand) { |
| 245 |
|
| 246 |
int count = 0; |
| 247 |
for (Sample::iterator g = begin(); g != end(); ++g) { |
| 248 |
vector<Allele*>& alleles = g->second; |
| 249 |
for (vector<Allele*>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 250 |
if ((*a)->currentBase == base && (*a)->strand == strand) |
| 251 |
++count; |
| 252 |
} |
| 253 |
} |
| 254 |
return count; |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
string Sample::tojson(void) { |
| 260 |
stringstream out; |
| 261 |
out << "["; |
| 262 |
bool first = true; |
| 263 |
for (map<string, vector<Allele*> >::iterator g = this->begin(); g != this->end(); ++g) { |
| 264 |
vector<Allele*>& alleles = g->second; |
| 265 |
for (vector<Allele*>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 266 |
if (!first) { out << ","; } else { first = false; } |
| 267 |
out << (*a)->tojson(); |
| 268 |
} |
| 269 |
} |
| 270 |
out << "]"; |
| 271 |
return out.str(); |
| 272 |
} |
| 273 |
|
| 274 |
void groupAlleles(Samples& samples, map<string, vector<Allele*> >& alleleGroups) { |
| 275 |
for (Samples::iterator s = samples.begin(); s != samples.end(); ++s) { |
| 276 |
Sample& sample = s->second; |
| 277 |
for (Sample::iterator g = sample.begin(); g != sample.end(); ++g) { |
| 278 |
const string& base = g->first; |
| 279 |
const vector<Allele*>& alleles = g->second; |
| 280 |
vector<Allele*>& group = alleleGroups[base]; |
| 281 |
group.reserve(group.size() + distance(alleles.begin(), alleles.end())); |
| 282 |
group.insert(group.end(), alleles.begin(), alleles.end()); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
bool sufficientAlternateObservations(Samples& samples, int mincount, float minfraction) { |
| 288 |
|
| 289 |
int totalAlternateCount = 0; |
| 290 |
int totalReferenceCount = 0; |
| 291 |
|
| 292 |
for (Samples::iterator s = samples.begin(); s != samples.end(); ++s) { |
| 293 |
|
| 294 |
//cerr << s->first << endl; |
| 295 |
Sample& sample = s->second; |
| 296 |
int alternateCount = 0; |
| 297 |
int observationCount = 0; |
| 298 |
|
| 299 |
for (Sample::iterator group = sample.begin(); group != sample.end(); ++group) { |
| 300 |
const string& base = group->first; |
| 301 |
//cerr << base << endl; |
| 302 |
vector<Allele*>& alleles = group->second; |
| 303 |
//cerr << alleles.size() << endl; |
| 304 |
if (alleles.size() == 0) |
| 305 |
continue; |
| 306 |
if (alleles.front()->type != ALLELE_REFERENCE) { |
| 307 |
alternateCount += alleles.size(); |
| 308 |
} else { |
| 309 |
totalReferenceCount += alleles.size(); |
| 310 |
} |
| 311 |
observationCount += alleles.size(); |
| 312 |
} |
| 313 |
|
| 314 |
//cerr << "alternateCount " << alternateCount << " ratio " << ((float) alternateCount / (float) observationCount) << endl; |
| 315 |
|
| 316 |
if (alternateCount >= mincount && ((float) alternateCount / (float) observationCount) >= minfraction) |
| 317 |
return true; |
| 318 |
totalAlternateCount += alternateCount; |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
// always analyze if we have more alternate observations than reference observations |
| 323 |
// this is meant to catch the case in which the reference is the rare allele |
| 324 |
// it will probably also catch cases in which we have very low coverage |
| 325 |
if (totalReferenceCount < totalAlternateCount) { |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
return false; |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
int countAlleles(Samples& samples) { |
| 335 |
|
| 336 |
int count = 0; |
| 337 |
for (Samples::iterator s = samples.begin(); s != samples.end(); ++s) { |
| 338 |
Sample& sample = s->second; |
| 339 |
for (Sample::iterator sg = sample.begin(); sg != sample.end(); ++sg) { |
| 340 |
count += sg->second.size(); |
| 341 |
} |
| 342 |
} |
| 343 |
return count; |
| 344 |
|
| 345 |
} |
| 346 |
|
| 347 |
ostream& operator<<(ostream& out, Sample& sample) { |
| 348 |
for (Sample::iterator s = sample.begin(); s != sample.end(); ++s) { |
| 349 |
out << s->first << " #" << s->second.size() << endl << s->second << endl; |
| 350 |
} |
| 351 |
return out; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
void Samples::assignPartialSupport(vector<Allele>& alleles, |
| 356 |
vector<Allele*>& partialObservations, |
| 357 |
map<string, vector<Allele*> >& partialObservationGroups, |
| 358 |
map<Allele*, set<Allele*> >& partialObservationSupport, |
| 359 |
unsigned long haplotypeStart, |
| 360 |
int haplotypeLength) { |
| 361 |
|
| 362 |
// clean up results of any previous calls to this function |
| 363 |
clearPartialObservations(); |
| 364 |
|
| 365 |
for (vector<Allele>::iterator a = alleles.begin(); a != alleles.end(); ++a) { |
| 366 |
Allele& allele = *a; |
| 367 |
//string& base = allele.currentBase; |
| 368 |
// hacks here |
| 369 |
string& aseq = allele.alternateSequence; |
| 370 |
//cerr << "alternate, seeking partial support " << aseq << endl |
| 371 |
// << "allele: " << allele << endl; |
| 372 |
// construct pseudo-sequence |
| 373 |
for (vector<Allele*>::iterator p = partialObservations.begin(); p != partialObservations.end(); ++p) { |
| 374 |
Allele& partial = **p; |
| 375 |
string pseq = partial.alternateSequence; |
| 376 |
bool same = false; |
| 377 |
// if the partial could support the alternate if we consider "reference-matching" |
| 378 |
// sequence beyond the haplotype window, add it to the comparison |
| 379 |
if (partial.position == haplotypeStart && partial.referenceLength == haplotypeLength) { |
| 380 |
if (pseq.size() + partial.basesLeft <= aseq.size()) { |
| 381 |
pseq = partial.read5p(); |
| 382 |
} else if (pseq.size() + partial.basesRight <= aseq.size()) { |
| 383 |
pseq = partial.read3p(); |
| 384 |
} |
| 385 |
} |
| 386 |
// otherwise, we should be fine to go with the seqs |
| 387 |
// basically, this is the partial reference coordinate-matching case |
| 388 |
//cerr << partial << " bp l/r " << partial.basesLeft << "/" << partial.basesRight |
| 389 |
// << " szes , " << pseq.size() << " vs " << aseq.size() << endl; |
| 390 |
if (!pseq.empty() |
| 391 |
&& aseq.size() >= pseq.size() |
| 392 |
&& ((partial.alternateSequence.size() + partial.basesRight <= aseq.size() |
| 393 |
&& (aseq.substr(0, pseq.size()) == pseq)) |
| 394 |
|| (partial.alternateSequence.size() + partial.basesLeft <= aseq.size() |
| 395 |
&& (aseq.substr(aseq.size()-pseq.size()) == pseq)))) { |
| 396 |
// dAY's du saem |
| 397 |
partialObservationGroups[allele.currentBase].push_back(*p); |
| 398 |
partialObservationSupport[*p].insert(&*a); |
| 399 |
//cerr << "partial support of " << *a << " by " << *p << endl; |
| 400 |
same = true; |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
for (vector<Allele*>::iterator p = partialObservations.begin(); p != partialObservations.end(); ++p) { |
| 406 |
// get the sample |
| 407 |
Allele& partial = **p; |
| 408 |
Samples::iterator siter = find(partial.sampleID); |
| 409 |
if (siter == end()) { |
| 410 |
continue; |
| 411 |
} |
| 412 |
Sample& sample = siter->second; |
| 413 |
map<Allele*, set<Allele*> >::iterator sup = partialObservationSupport.find(*p); |
| 414 |
if (sup != partialObservationSupport.end()) { |
| 415 |
set<Allele*>& supported = sup->second; |
| 416 |
for (set<Allele*>::iterator s = supported.begin(); s != supported.end(); ++s) { |
| 417 |
sample.partialSupport[(*s)->currentBase].push_back(*p); |
| 418 |
sample.supportedAlleles.insert((*s)->currentBase); |
| 419 |
} |
| 420 |
if (!supported.empty()) { |
| 421 |
sample.reversePartials[*p] = supported; |
| 422 |
} |
| 423 |
} |
| 424 |
//sample.partialObservations.push_back(*p); |
| 425 |
} |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
bool Sample::observationSupports(Allele* obs, Allele* allele) { |
| 430 |
if (obs->currentBase == allele->currentBase) { |
| 431 |
return true; |
| 432 |
} else { |
| 433 |
map<Allele*, set<Allele*> >::iterator p = reversePartials.find(obs); |
| 434 |
if (p != reversePartials.end()) { |
| 435 |
set<Allele*>& supports = p->second; |
| 436 |
if (supports.find(allele) != supports.end()) { |
| 437 |
return true; |
| 438 |
} |
| 439 |
} |
| 440 |
return false; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
void Samples::clearFullObservations(void) { |
| 445 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 446 |
s->second.clear(); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
void Samples::clearPartialObservations(void) { |
| 451 |
for (Samples::iterator s = begin(); s != end(); ++s) { |
| 452 |
s->second.clearPartialObservations(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
void Sample::clearPartialObservations(void) { |
| 457 |
supportedAlleles.clear(); |
| 458 |
for (Sample::iterator a = begin(); a != end(); ++a) |
| 459 |
supportedAlleles.insert(a->first); |
| 460 |
partialSupport.clear(); |
| 461 |
reversePartials.clear(); |
| 462 |
} |
| 463 |
|
| 464 |
void Sample::setSupportedAlleles(void) { |
| 465 |
for (Sample::iterator a = begin(); a != end(); ++a) |
| 466 |
supportedAlleles.insert(a->first); |
| 467 |
} |
| 468 |
|
| 469 |
void Samples::setSupportedAlleles(void) { |
| 470 |
for (Samples::iterator s = begin(); s != end(); ++s) |
| 471 |
s->second.setSupportedAlleles(); |
| 472 |
} |