private void binFragments()
{
// Sanity check: The BAM index file must exist, in order for us to seek to our target chromosome!
if (!Bam.Index.Exists)
{
throw new Exception(string.Format("Fatal error: Bam index not found at {0}", Bam.Index.FullName));
}
long pairedAlignmentCount = 0; // keep track of paired alignments
usableFragmentCount = 0;
using (BamReader reader = new BamReader(Bam.BamFile.FullName))
{
int desiredRefIndex = -1;
desiredRefIndex = reader.GetReferenceIndex(Chromosome);
if (desiredRefIndex == -1)
{
throw new ApplicationException(
string.Format("Unable to retrieve the reference sequence index for {0} in {1}.", Chromosome, Bam.BamFile.FullName));
}
bool result = reader.Jump(desiredRefIndex, 0);
if (!result)
{
// Note: This is not necessarily an error, it just means that there *are* no reads for this chromosome in this
// .bam file. That is not uncommon e.g. for truseq amplicon.
return;
}
Dictionary<string, int> readNameToBinIndex = new Dictionary<string, int>();
HashSet<string> samePositionReadNames = new HashSet<string>();
int binIndexStart = 0;
int prevPosition = -1;
BamAlignment alignment = new BamAlignment();
while (reader.GetNextAlignment(ref alignment, true))
{
int refID = alignment.RefID;
// quit if the current reference index is different from the desired reference index
if (refID != desiredRefIndex)
break;
if (refID == -1)
continue;
if (alignment.Position < prevPosition) // Make sure the BAM is properly sorted
{
throw new ApplicationException(
string.Format("The alignment on {0} are not properly sorted in {1}: {2}", Chromosome, Bam.BamFile.FullName, alignment.Name));
}
prevPosition = alignment.Position;
if (alignment.IsPaired()) { pairedAlignmentCount++; }
BinOneAlignment(alignment, FragmentBinnerConstants.MappingQualityThreshold, readNameToBinIndex,
samePositionReadNames, ref usableFragmentCount, Bins, ref binIndexStart);
}
}
if (pairedAlignmentCount == 0)
{
throw new ApplicationException(string.Format("No paired alignments found for {0} in {1}", Chromosome, Bam.BamFile.FullName));
}
}