protected void ProcessBamFile(string bamPath)
{
Console.WriteLine("{0} Looping over bam records from {1}", DateTime.Now, bamPath);
int overallCount = 0;
int nextVariantIndex = 0;
using (BamReader reader = new BamReader(bamPath))
{
BamAlignment read = new BamAlignment();
int refID = reader.GetReferenceIndex(this.Chromosome);
if (refID < 0)
{
throw new ArgumentException(string.Format("Error: Chromosome name '{0}' does not match bam file at '{1}'", this.Chromosome, bamPath));
}
Console.WriteLine("Jump to refid {0} {1}", refID, this.Chromosome);
reader.Jump(refID, 0);
while (true)
{
bool result = reader.GetNextAlignment(ref read, false);
if (!result) break;
if (!read.HasPosition() || read.RefID > refID) break; // We're past our chromosome of interest.
if (read.RefID < refID) continue; // We're not yet on our chromosome of interest.
overallCount++;
if (overallCount % 1000000 == 0)
{
Console.WriteLine("Record {0} at {1}...", overallCount, read.Position);
}
// Skip over unaligned or other non-count-worthy reads:
if (!read.IsPrimaryAlignment()) continue;
if (!read.IsMapped()) continue;
if (read.IsDuplicate()) continue;
if (read.MapQuality <= MinimumMapQ) continue;
// Scan forward through the variants list, to keep up with our reads:
while (nextVariantIndex < this.Variants.Count && this.Variants[nextVariantIndex].ReferencePosition < read.Position)
{
nextVariantIndex++;
}
if (nextVariantIndex >= this.Variants.Count) break;
// If the read doesn't look like it has a reasonable chance of touching the next variant, continue:
if (read.Position + 1000 < this.Variants[nextVariantIndex].ReferencePosition) continue;
// This read potentially overlaps next variant (and further variants). Count bases!
ProcessReadBases(read, nextVariantIndex);
}
}
Console.WriteLine("Looped over {0} bam records in all", overallCount);
}