public void TestFormatter() { string filePath = @"TestUtils\BAM\SeqAlignment.bam"; string outputfilePath = "BAMTests123.bam"; BAMParser parser = null; try { parser = new BAMParser(); BAMFormatter formatter = new BAMFormatter(); SequenceAlignmentMap alignmentMap = parser.Parse(filePath); Assert.IsTrue(alignmentMap != null); Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1); Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1); Assert.AreEqual(alignmentMap.QuerySequences.Count, 2); formatter.Format(alignmentMap, outputfilePath); formatter.CreateSortedBAMFile = true; formatter.CreateIndexFile = true; alignmentMap = parser.Parse(filePath); formatter.Format(alignmentMap, outputfilePath); Assert.IsTrue(File.Exists("BAMTests123.bam.bai")); alignmentMap = parser.Parse(outputfilePath); Assert.IsTrue(alignmentMap != null); Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1); Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1); Assert.AreEqual(alignmentMap.QuerySequences.Count, 2); alignmentMap = parser.ParseRange("BAMTests123.bam", new SequenceRange("chr20", 0, int.MaxValue)); Assert.IsTrue(alignmentMap != null); Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1); Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1); Assert.AreEqual(alignmentMap.QuerySequences.Count, 2); alignmentMap = parser.ParseRange("BAMTests123.bam", new SequenceRange("chr20", 0, 28833)); Assert.IsTrue(alignmentMap != null); Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1); Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1); Assert.AreEqual(alignmentMap.QuerySequences.Count, 1); } finally { if (parser != null) parser.Dispose(); } }
public void TestFormatterWithSort() { string inputFilePath = @"TestUtils\BAM\SeqAlignment.bam"; string outputFilePath1 = "output1.bam"; string outputFilePath2 = "output2.bam"; BAMParser parser = null; try { parser = new BAMParser(); BAMFormatter formatter = new BAMFormatter(); SequenceAlignmentMap alignmentMap = parser.Parse(inputFilePath); Assert.IsTrue(alignmentMap != null); Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1); Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1); Assert.AreEqual(alignmentMap.QuerySequences.Count, 2); formatter.CreateSortedBAMFile = true; formatter.SortType = BAMSortByFields.ChromosomeCoordinates; formatter.Format(alignmentMap, outputFilePath1); alignmentMap = parser.Parse(inputFilePath); formatter.Format(alignmentMap, outputFilePath2); Assert.IsTrue(File.Exists(outputFilePath1)); Assert.IsTrue(File.Exists(outputFilePath2)); Assert.AreEqual(true, FileCompare(outputFilePath1, outputFilePath2)); } finally { if (parser != null) parser.Dispose(); } }
public void TestParser() { string filePath = @"TestUtils\BAM\SeqAlignment.bam"; BAMParser parser = null; try { parser = new BAMParser(); SequenceAlignmentMap alignmentMap = parser.Parse(filePath); Assert.IsTrue(alignmentMap != null); Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1); Assert.AreEqual(alignmentMap.QuerySequences.Count, 2); } finally { if (parser != null) parser.Dispose(); } }
/// <summary> /// Format BAM file using IsequenceAlignment object. /// </summary> /// <param name="nodeName">Different xml nodes used for different test cases</param> private void InValidateBAMFormatterWithSequenceAlignment(string nodeName) { // Get input and output values from xml node. string bamFilePath = _utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); ISequenceAlignmentParser bamParserObj = new BAMParser(); IList<ISequenceAlignment> seqList = bamParserObj.Parse(bamFilePath).ToList(); try { using (var BAMIndexStorageObj = new BAMIndexStorage(File.Create(Constants.BAMTempIndexFileForInvalidData))) { // Create a BAM formatter object. var formatterObj = new BAMFormatter(); InvalidateBAmFormatter(formatterObj, seqList); InvalidateBAmFormatterWithWithInvalidValues(formatterObj,seqList, bamFilePath, BAMIndexStorageObj); InvalidateBAmFormatterWithWithNullValues(formatterObj,seqList); } } finally { (bamParserObj as BAMParser).Dispose(); File.Delete(Constants.BAMTempIndexFileForInvalidData); } }
/// <summary> /// Parse BAM and validate parsed aligned sequences by creating /// ISequenceAlignment interface object and its properties. /// </summary> /// <param name="nodeName">Different xml nodes used for different test cases</param> /// <param name="BAMParserPam">BAM Parse method parameters</param> private void InValidateSeqAlignmentMapBAMParser(string nodeName, BAMParserParameters BAMParserPam) { // Get input and output values from xml node. string bamFilePath = _utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string exception = string.Empty; using (var bamParser = new BAMParser()) { // Parse a BAM file with different parameters. switch (BAMParserPam) { case BAMParserParameters.StreamReader: try { bamParser.Parse(null as Stream); Assert.Fail(); } catch (ArgumentNullException ex) { exception = ex.Message; } try { using (Stream stream = new FileStream(bamFilePath, FileMode.Open, FileAccess.Read)) { bamParser.Parse(stream).ToList(); Assert.Fail(); } } catch (Exception ex) { exception = ex.Message; } break; case BAMParserParameters.FileName: try { bamParser.Parse(null as string).First(); Assert.Fail(); } catch (ArgumentNullException ex) { exception = ex.Message; } break; case BAMParserParameters.ParseRangeWithIndex: try { bamParser.ParseRange(null, 0); Assert.Fail(); } catch (ArgumentNullException ex) { exception = ex.Message; } try { bamParser.ParseRange(bamFilePath, -2); Assert.Fail(); } catch (ArgumentOutOfRangeException ex) { exception = ex.Message; } break; case BAMParserParameters.ParseRangeFileName: try { bamParser.ParseRange(null, new SequenceRange("chr20", 0, 10)); Assert.Fail(); } catch (ArgumentNullException ex) { exception = ex.Message; } try { bamParser.ParseRange(bamFilePath, null as SequenceRange); Assert.Fail(); } catch (ArgumentNullException ex) { exception = ex.Message; } break; default: break; } // Log to VSTest GUI. ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", exception)); } }
/// <summary> /// Parse BAM File and Invalidate parsed aligned sequences by creating /// ISequenceAlignment interface object and its properties. /// </summary> /// <param name="nodeName">Different xml nodes used for different test cases</param> /// <param name="BAMParserPam">BAM Parse method parameters</param> private void InValidateISequenceAlignmentBAMParser(string nodeName, BAMParserParameters BAMParserPam) { // Get input and output values from xml node. string bamFilePath = _utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string exception = string.Empty; ISequenceAlignmentParser bamParser = null; bamParser = new BAMParser(); try { // Parse a BAM file with different invalid parameters. switch (BAMParserPam) { case BAMParserParameters.Stream: try { using (var reader = File.OpenRead(bamFilePath)) { bamParser.Parse(reader); Assert.Fail(); } } catch (NotSupportedException ex) { exception = ex.Message; } break; case BAMParserParameters.ParseOneStream: try { using (var reader = File.OpenRead(bamFilePath)) { bamParser.ParseOne(reader); Assert.Fail(); } } catch (NotSupportedException ex) { exception = ex.Message; } break; default: break; } // Log to VSTest GUI. ApplicationLog.WriteLine(string.Format(null, "BAM Parser P2 : Validated Exception {0} successfully", exception)); } finally { (bamParser as BAMParser).Dispose(); } }
/// <summary> /// Format BAM file using IsequenceAlignment object. /// </summary> /// <param name="nodeName">Different xml nodes used for different test cases</param> /// <param name="BAMParserPam">BAM Format method parameters</param> /// <param name="WriteBAMIndexData">True if writting BAM index data to BAMIndex file, /// false otherwise</param> /// <param name="IsNotSupportedMethods">True if validating notsuportedMethods, /// false otherwise</param> void ValidateBAMFormatterWithSequenceAlignment(string nodeName, BAMParserParameters BAMParserPam, bool WriteBAMIndexData, bool IsNotSupportedMethods) { // Get input and output values from xml node. string BAMStoragePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string expectedAlignedSeqFilePath = this.utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); string alignedSeqCount = this.utilityObj.xmlUtil.GetTextValue( nodeName, Constants.AlignedSeqCountNode); BAMIndexStorage BAMIndexStorageObj = null; ISequenceAlignmentParser bamParserObj = new BAMParser(); try { using (BAMParser bamSeqMapParserObj = new BAMParser()) { IEnumerable<ISequenceAlignment> seqList = bamParserObj.Parse(BAMStoragePath); try { // Write BAm index data to BAM Index File. if (WriteBAMIndexData) { BAMIndexStorageObj = new BAMIndexStorage( File.Create(Constants.BAMTempIndexFileForSequenceAlignment)); } // Create a BAM formatter object. BAMFormatter formatterObj = new BAMFormatter { CreateSortedBAMFile = true, CreateIndexFile = true }; // Write/Format aligned sequences to BAM file. switch (BAMParserPam) { case BAMParserParameters.StreamWriter: try { using (var writer = File.Create(Constants.BAMTempFileName)) { foreach (ISequenceAlignment seq in seqList) { formatterObj.Format(writer, seq); Assert.Fail(); } } } catch (NotSupportedException ex) { string message = ex.Message; ApplicationLog.WriteLine(string.Format((IFormatProvider)null, "BAM Formatter P1 : Validated the exception {0} successfully" , message)); } break; case BAMParserParameters.Stream: using (Stream stream = new FileStream(Constants.BAMTempFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { foreach (ISequenceAlignment seq in seqList) { formatterObj.Format(stream, seq); } } File.Exists(Constants.BAMTempFileName); break; case BAMParserParameters.FileName: foreach (ISequenceAlignment seq in seqList) { formatterObj.Format(seq, Constants.BAMTempFileName); } File.Exists(Constants.BAMTempFileName); break; case BAMParserParameters.StreamAndIndexFile: using (Stream stream = new FileStream(Constants.BAMTempFileName, FileMode.Create, FileAccess.ReadWrite)) { foreach (ISequenceAlignment seq in seqList) { formatterObj.Format(stream, BAMIndexStorageObj, seq); } } File.Exists(Constants.BAMTempFileName); break; case BAMParserParameters.IndexFile: foreach (ISequenceAlignment seq in seqList) { formatterObj.Format(seq, Constants.BAMTempFileName, Constants.BAMTempIndexFile); } File.Exists(Constants.BAMTempFileName); break; default: break; } if (!IsNotSupportedMethods) { // Parse formatted BAM file and validate aligned sequences. SequenceAlignmentMap expectedSeqAlignmentMap = bamSeqMapParserObj.ParseOne<SequenceAlignmentMap>( Constants.BAMTempFileName); IList<SAMAlignedSequence> alignedSeqs = expectedSeqAlignmentMap.QuerySequences; Assert.AreEqual(alignedSeqCount, alignedSeqs.Count.ToString((IFormatProvider)null)); // Get expected sequences FastAParser parserObj = new FastAParser(); IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedAlignedSeqFilePath); IList<ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate aligned sequences from BAM file. for (int index = 0; index < alignedSeqs.Count; index++) { Assert.AreEqual(new string(expectedSequencesList[index].Select(a => (char)a).ToArray()), new string(alignedSeqs[index].QuerySequence.Select(a => (char)a).ToArray())); // Log to VSTest GUI. ApplicationLog.WriteLine(string.Format((IFormatProvider)null, "BAM Formatter P1 : Validated Aligned sequence :{0} successfully", alignedSeqs[index].QuerySequence.ToString())); } } } finally { if (BAMIndexStorageObj != null) BAMIndexStorageObj.Dispose(); } } } finally { (bamParserObj as BAMParser).Dispose(); File.Delete(Constants.BAMTempFileName); File.Delete(Constants.BAMTempIndexFile); } }
/// <summary> /// Parse BAM and validate parsed aligned sequences by creating /// ISequenceAlignment interface object and its properties. /// </summary> /// <param name="nodeName">Different xml nodes used for different test cases</param> /// <param name="BAMParserPam">BAM Parse method parameters</param> void ValidateISequenceAlignmentBAMParser(string nodeName, BAMParserParameters BAMParserPam) { // Get input and output values from xml node. string BAMStoragePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string expectedAlignedSeqFilePath = this.utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); IEnumerable<ISequenceAlignment> seqAlignmentList = null; ISequenceAlignmentParser bamParser = null; ISequenceAlignment seqAlignment = null; IList<IAlignedSequence> alignedSeqs = null; bamParser = new BAMParser(); // Parse a BAM file with different parameters. switch (BAMParserPam) { case BAMParserParameters.FileName: seqAlignmentList = bamParser.Parse(BAMStoragePath); alignedSeqs = seqAlignmentList.First().AlignedSequences; break; case BAMParserParameters.ParseOne: seqAlignment = bamParser.ParseOne(File.OpenRead(BAMStoragePath)); alignedSeqs = seqAlignment.AlignedSequences; break; default: break; } // Get expected sequences FastAParser parserObj = new FastAParser(); IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedAlignedSeqFilePath); IList<ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate aligned sequences from BAM file. for (int index = 0; index < alignedSeqs.Count; index++) { Assert.AreEqual(new string(expectedSequencesList[index].Select(a => (char)a).ToArray()), new string(alignedSeqs[index].Sequences[0].Select(a => (char)a).ToArray())); // Log to VSTest GUI. ApplicationLog.WriteLine(string.Format((IFormatProvider)null, "BAM Parser P1 : Validated Aligned sequence :{0} successfully", alignedSeqs[index].Sequences.ToString())); } }