/// <summary> /// Parses specified BAM file using index file. /// Index file is assumed to be in the same location as that of the specified bam file with the name "filename".bai /// For example, if the specified bam file name is D:\BAMdata\sample.bam then index file name will be taken as D:\BAMdata\sample.bam.bai /// If index file is not available then this method throw an exception. /// </summary> /// <param name="parser">BAM parser</param> /// <param name="fileName">BAM file name.</param> /// <param name="refSeqName">Name of reference sequence.</param> /// <returns>SequenceAlignmentMap object which contains alignments for specified reference sequence.</returns> public static SequenceAlignmentMap ParseRange(this BAMParser parser, string fileName, string refSeqName) { if (parser == null) { throw new ArgumentNullException("parser"); } if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentNullException("fileName"); } if (refSeqName == null) { throw new ArgumentNullException("refSeqName"); } using (var bamStream = File.OpenRead(fileName)) { string bamIndexFileName = GetBAMIndexFileName(fileName); using (FileStream bamIndexFile = File.OpenRead(bamIndexFileName)) using (var bamIndexStorage = new BAMIndexStorage(bamIndexFile)) { return(parser.GetAlignment(bamStream, bamIndexStorage, refSeqName)); } } }
/// <summary> /// Parses specified BAM file using index file. /// </summary> /// <param name="parser">BAM parser</param> /// <param name="fileName">BAM file name.</param> /// <param name="range">SequenceRange object which contains reference sequence name and start and end co-ordinates.</param> /// <returns>SequenceAlignmentMap object which contains alignments for specified reference sequence and for specified range.</returns> public static SequenceAlignmentMap ParseRange(this BAMParser parser, string fileName, SequenceRange range) { if (parser == null) { throw new ArgumentNullException("parser"); } if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentNullException("fileName"); } if (range == null) { throw new ArgumentNullException("range"); } if (string.IsNullOrEmpty(range.ID)) { throw new ArgumentException("Reference sequence name (range.ID) can't empty or null."); } int start = range.Start >= int.MaxValue ? int.MaxValue : (int)range.Start; int end = range.End >= int.MaxValue ? int.MaxValue : (int)range.End; using (FileStream bamStream = File.OpenRead(fileName)) { string bamIndexFileName = GetBAMIndexFileName(fileName); using (FileStream bamIndexFile = File.OpenRead(bamIndexFileName)) using (BAMIndexStorage bamIndexStorage = new BAMIndexStorage(bamIndexFile)) { if (start == 0 && end == int.MaxValue) { return(parser.GetAlignment(bamStream, bamIndexStorage, range.ID)); } return(parser.GetAlignment(bamStream, bamIndexStorage, range.ID, start, end)); } } }