/// <summary> /// Get chromoses with orphan regions /// </summary> /// <param name="filename">Path of the BAM file</param> /// <param name="mean">Mean value</param> /// <param name="deviation">Standard deviation</param> /// <returns></returns> private void DisplayOrphans(string filename) { SequenceAlignmentMap alignmentMapobj = null; if (!SAMInput) { BAMParser bamParser = new BAMParser(); alignmentMapobj = bamParser.ParseOne<SequenceAlignmentMap>(filename); } else { SAMParser samParser = new SAMParser(); alignmentMapobj = samParser.ParseOne<SequenceAlignmentMap>(filename); } // get reads from sequence alignment map object. IList<PairedRead> pairedReads = null; // Get Aligned sequences IList<SAMAlignedSequence> alignedSeqs = alignmentMapobj.QuerySequences; pairedReads = alignmentMapobj.GetPairedReads(0, 0); // Get the orphan regions. var orphans = pairedReads.Where(PR => PR.PairedType == PairedReadType.Orphan); int count = orphans.Count(); if (count == 0) { Console.WriteLine("No Orphans to display"); } var orphanRegions = new List<ISequenceRange>(count); orphanRegions.AddRange(orphans.Select(orphanRead => GetRegion(orphanRead.Read1))); // Get sequence range grouping object. SequenceRangeGrouping rangeGroup = new SequenceRangeGrouping(orphanRegions); if (!rangeGroup.GroupIDs.Any()) { Console.Write("\r\nNo Orphan reads to display"); } else { Console.Write("Region of Orphan reads:"); DisplaySequenceRange(rangeGroup); } SequenceRangeGrouping mergedRegions = rangeGroup.MergeOverlaps(); if (!mergedRegions.GroupIDs.Any()) { Console.Write("\r\nNo hot spots to display"); } else { Console.Write("\r\nChromosomal hot spot:"); DisplaySequenceRange(mergedRegions); } }
public void ValidateSAMParserWithEmptyAlignmentMap() { SAMParser parser = new SAMParser(); { SequenceAlignmentMap alignment = parser.ParseOne<SequenceAlignmentMap>(utilityObj.xmlUtil.GetTextValue(Constants.EmptySamFileNode, Constants.FilePathNode)); Assert.IsNotNull(alignment); } }
/// <summary> /// Get Chimera data /// </summary> /// <param name="filename">Path of the BAM file</param> /// <param name="mean">Mean value</param> /// <param name="deviation">Standard deviation</param> /// <returns></returns> private Matrix<string, string, string> GetChimeraData(string filename) { SequenceAlignmentMap alignmentMapobj = null; if (!SAMInput) { BAMParser bamParser = new BAMParser(); alignmentMapobj = bamParser.ParseOne<SequenceAlignmentMap>(filename); } else { SAMParser samParser = new SAMParser(); alignmentMapobj = samParser.ParseOne<SequenceAlignmentMap>(filename); } // get reads from sequence alignment map object. IList<PairedRead> pairedReads = null; pairedReads = alignmentMapobj.GetPairedReads(200, 50); // select chimeras from reads. var chimeras = pairedReads.Where(PE => PE.PairedType == PairedReadType.Chimera); // Group chimeras based on first reads chromosomes name. var groupedChimeras = chimeras.GroupBy(PR => PR.Read1.RName); IList<string> chrs = alignmentMapobj.GetRefSequences(); // Declare sparse matrix to store statistics. SparseMatrix<string, string, string> statistics = SparseMatrix<string, string, string>.CreateEmptyInstance( chrs, chrs, "0"); // For each group create sub group depending on the second reads chromosomes. foreach (var group in groupedChimeras) { foreach (var subgroup in group.GroupBy(PE => PE.Read2.RName)) { // store the count to stats statistics[group.Key, subgroup.Key] = subgroup.Count().ToString(); } } return statistics; }
/// <summary> /// Display Sequence Item occurences percentage /// </summary> /// <param name="inputFile">Path of the input file</param> /// <param name="possibleOccurence">True to display Nculeaotide distribution</param> public void DisplaySequenceItemOccurences(string inputFile, bool possibleOccurence) { if (string.IsNullOrEmpty(inputFile)) { throw new InvalidOperationException("Input File Not specified"); } SequenceAlignmentMap alignmentMapobj = null; if (!SAMInput) { BAMParser bamParser = new BAMParser(); alignmentMapobj = bamParser.ParseOne<SequenceAlignmentMap>(inputFile); } else { SAMParser samParser = new SAMParser(); alignmentMapobj = samParser.ParseOne<SequenceAlignmentMap>(inputFile); } IList<string> chromosomes = alignmentMapobj.GetRefSequences(); if (possibleOccurence) { Console.Write("Nucleotide Distribution:"); Console.Write("\r\nPosition\tA\tT\tG\tC\tPossibility Of Occurences"); foreach (string str in chromosomes) { GetCoverage(str, alignmentMapobj, "true"); } } else { Console.Write("Coverage Profile:"); Console.Write("\r\nPosition\tA\tT\tG\tC"); foreach (string str in chromosomes) { GetCoverage(str, alignmentMapobj, "false"); } } }
/// <summary> /// General method to validate SAM parser method. /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMParserSeqAlign( string nodeName, ParseOrFormatTypes method) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); SAMParser parser = new SAMParser(); { SequenceAlignmentMap alignments = null; // Parse SAM File switch (method) { case ParseOrFormatTypes.ParseOrFormatText: using (var reader = File.OpenRead(filePath)) { alignments = parser.Parse(reader); } break; case ParseOrFormatTypes.ParseOrFormatFileName: alignments = (SequenceAlignmentMap) parser.ParseOne(filePath); break; } // Get expected sequences FastAParser parserObj = new FastAParser(); { IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile); IList<ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate parsed output with expected output for (int index = 0; index < alignments.QuerySequences.Count; index++) { for (int count = 0; count < alignments.QuerySequences[index].Sequences.Count; count++) { Assert.AreEqual(new string(expectedSequencesList[index].Select(a => (char)a).ToArray()), new string(alignments.QuerySequences[index].Sequences[count].Select(a => (char)a).ToArray())); } } } } }
/// <summary> /// Validate parser parse one method overloads with filePath\textreader /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMParserWithParseOne(string nodeName, ParseOrFormatTypes parseTypes) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); ISequenceAlignmentParser parser = new SAMParser(); ISequenceAlignment alignment = null; // Parse SAM File switch (parseTypes) { case ParseOrFormatTypes.ParseOrFormatText: using (var reader = File.OpenRead(filePath)) { alignment = parser.ParseOne(reader); } break; case ParseOrFormatTypes.ParseOrFormatFileName: alignment = parser.ParseOne(filePath); break; } // Get expected sequences FastAParser parserObj = new FastAParser(); { IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile); IList<ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate parsed output with expected output int count = 0; foreach (IAlignedSequence alignedSequence in alignment.AlignedSequences) { foreach (ISequence sequence in alignedSequence.Sequences) { Assert.AreEqual(expectedSequencesList[count].ConvertToString(), sequence.ConvertToString()); count++; } } } }
/// <summary> /// Genaral method to Invalidate SAM Formatter /// <param name="method">enum type to execute different overload</param> /// </summary> void ValidateSamFormatter(ParseOrFormatTypes method) { string filePath = _utilityObj._xmlUtil.GetTextValue( Constants.SmallSAMFileNode, Constants.FilePathNode); ISequenceAlignmentParser parser = new SAMParser(); ISequenceAlignment alignment = null; try { switch (method) { case ParseOrFormatTypes.ParseOrFormatSeqText: new SAMFormatter().Format( null as ISequenceAlignment, null as TextWriter); break; case ParseOrFormatTypes.ParseOrFormatSeqTextWithFlag: alignment = parser.ParseOne(filePath); new SAMFormatter().Format( alignment, null as TextWriter); break; case ParseOrFormatTypes.ParseOrFormatIseq: new SAMFormatter().Format( null as ISequenceAlignment, null as string); break; case ParseOrFormatTypes.ParseOrFormatIseqFile: alignment = parser.ParseOne(filePath); new SAMFormatter().Format( alignment, null as string); break; case ParseOrFormatTypes.ParseOrFormatCollString: new SAMFormatter().Format( null as ICollection <ISequenceAlignment>, null as string); break; case ParseOrFormatTypes.ParseOrFormatCollection: new SAMFormatter().Format( null as ICollection <ISequenceAlignment>, null as TextWriter); break; case ParseOrFormatTypes.ParseOneOrFormatSeq: SequenceAlignmentMap align = new SequenceAlignmentMap(); new SAMFormatter().Format( align, null as string); break; case ParseOrFormatTypes.ParseOneOrFormatSeqFile: new SAMFormatter().Format( null as SequenceAlignmentMap, null as string); break; case ParseOrFormatTypes.ParseOrFormatIseqT: SequenceAlignmentMap alignments = new SequenceAlignmentMap(); new SAMFormatter().Format( alignments, null as string); break; case ParseOrFormatTypes.ParseOrFormatIseqText: new SAMFormatter().Format( null as SequenceAlignmentMap, null as string); break; case ParseOrFormatTypes.ParseOrFormatFormatString: new SAMFormatter().FormatString(null); break; default: break; } } catch (ArgumentNullException) { ApplicationLog.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); Console.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); } catch (NotSupportedException) { ApplicationLog.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); Console.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); } finally { (parser as SAMParser).Dispose(); } }
/// <summary> /// Genaral method to Invalidate SAM Parser /// <param name="method">enum type to execute different overload</param> /// </summary> private static void ValidateSAMParser(ParseOrFormatTypes method) { try { switch (method) { case ParseOrFormatTypes.ParseOrFormatText: using (SAMParser sParserObj = new SAMParser()) { sParserObj.Parse(null as TextReader); } break; case ParseOrFormatTypes.ParseOrFormatTextWithFlag: using (SAMParser sParserObj = new SAMParser()) { sParserObj.Parse(null as TextReader, true); } break; case ParseOrFormatTypes.ParseOrFormatFileName: using (SAMParser sParserObj = new SAMParser()) { sParserObj.Parse(null as string); } break; case ParseOrFormatTypes.ParseOrFormatFileNameWithFlag: using (SAMParser sParserObj = new SAMParser()) { sParserObj.Parse(null as string, true); } break; case ParseOrFormatTypes.ParseOneOrFormatText: using (SAMParser sParserObj = new SAMParser()) { sParserObj.ParseOne(null as TextReader); } break; case ParseOrFormatTypes.ParseOneOrFormatTextWithFlag: using (SAMParser sParserObj = new SAMParser()) { sParserObj.ParseOne(null as TextReader, true); } break; case ParseOrFormatTypes.ParseOneOrFormatFileName: using (SAMParser sParserObj = new SAMParser()) { sParserObj.ParseOne(null as string); } break; case ParseOrFormatTypes.ParseOneOrFormatFileNameWithFlag: using (SAMParser sParserObj = new SAMParser()) { sParserObj.ParseOne(null as string, true); } break; default: break; } Assert.Fail(); } catch (ArgumentNullException) { ApplicationLog.WriteLine( "SAM Parser P2 : Successfully validated the exception"); Console.WriteLine( "SAM Parser P2 : Successfully validated the exception"); } }
/// <summary> /// Merge multiple sorted alignments. /// SAMUtil.exe out.bam in1.bam in2.bam /// </summary> public void DoMerge() { if (FilePaths == null) { throw new InvalidOperationException("FilePath"); } if (FilePaths.Length < 2) { throw new InvalidOperationException(Resources.MergeHelp); } IList <IList <BAMSortedIndex> > sortedIndexes = new List <IList <BAMSortedIndex> >(); IList <SequenceAlignmentMap> sequenceAlignmentMaps = new List <SequenceAlignmentMap>(); IList <int> help = new List <int>(); Parallel.For(0, FilePaths.Length, (int index) => { IList <BAMSortedIndex> sortedIndex; BAMParser parser = new BAMParser();; SequenceAlignmentMap map; if (index == 0) { try { map = parser.ParseOne <SequenceAlignmentMap>(FilePaths[0]); } catch { throw new InvalidOperationException(Resources.InvalidBAMFile); } if (map == null) { throw new InvalidOperationException(Resources.EmptyFile); } if (string.IsNullOrEmpty(HeaderFile) && map.Header.RecordFields.Count == 0) { throw new InvalidOperationException(Resources.HeaderMissing); } if (!string.IsNullOrEmpty(HeaderFile)) { SAMParser parse = new SAMParser(); SequenceAlignmentMap head; try { head = parse.ParseOne <SequenceAlignmentMap>(HeaderFile); } catch { throw new InvalidOperationException(Resources.IncorrectHeaderFile); } if (head == null) { throw new InvalidOperationException(Resources.EmptyFile); } header = head.Header; } else { header = map.Header; } sortedIndex = Sort(map, SortByReadName ? BAMSortByFields.ReadNames : BAMSortByFields.ChromosomeCoordinates); } else { try { map = parser.ParseOne <SequenceAlignmentMap>(FilePaths[index]); } catch { throw new InvalidOperationException(Resources.InvalidBAMFile); } if (map == null) { throw new InvalidOperationException(Resources.EmptyFile); } sortedIndex = Sort(map, SortByReadName ? BAMSortByFields.ReadNames : BAMSortByFields.ChromosomeCoordinates); } lock (sortedIndexes) { sortedIndexes.Add(sortedIndex); sequenceAlignmentMaps.Add(map); } }); if (string.IsNullOrEmpty(OutputFilename)) { OutputFilename = "out.bam"; autoGeneratedOutputFilename = true; } string filePath = Path.GetTempFileName(); using (FileStream fstemp = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) { BAMFormatter formatter = new BAMFormatter(); formatter.WriteHeader(header, fstemp); int[] indexes = new int[sortedIndexes.Count]; if (SortByReadName) { IList <BAMSortedIndex> sortedIndex = sortedIndexes.Select(a => a.First()).ToList(); WriteMergeFileSortedByReadName(sortedIndex, fstemp, formatter, sequenceAlignmentMaps); } else { WriteMergeFile(sortedIndexes, fstemp, formatter, sequenceAlignmentMaps); } using (FileStream fsoutput = new FileStream(OutputFilename, FileMode.Create, FileAccess.Write)) { fstemp.Seek(0, SeekOrigin.Begin); formatter.CompressBAMFile(fstemp, fsoutput); } } File.Delete(filePath); if (autoGeneratedOutputFilename) { Console.WriteLine(Properties.Resources.SuccessMessageWithOutputFileName, OutputFilename); } }
/// <summary> /// General method to validate SAM Formatter method. /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMFormatter(string nodeName) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); SAMParser parser = new SAMParser(); { SequenceAlignmentMap alignments = (SequenceAlignmentMap) parser.ParseOne(filePath); SAMFormatter formatter = new SAMFormatter(); using (var writer = File.Create(Constants.SAMTempFileName)) { formatter.Format(writer, alignments); } alignments = parser.ParseOne<SequenceAlignmentMap>(Constants.SAMTempFileName); // Get expected sequences FastAParser parserObj = new FastAParser(); { IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile); IList<ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate parsed output with expected output for (int index = 0; index < alignments.QuerySequences.Count; index++) { for (int count = 0; count < alignments.QuerySequences[index].Sequences.Count; count++) { Assert.AreEqual( new string(expectedSequencesList[index].Select(a => (char)a).ToArray()), new string(alignments.QuerySequences[index].Sequences[count].Select(a => (char)a).ToArray())); } } } } }
/// <summary> /// Merge multiple sorted alignments. /// SAMUtil.exe out.bam in1.bam in2.bam /// </summary> public void DoMerge() { if (FilePaths == null) { throw new InvalidOperationException("FilePath"); } if (FilePaths.Length < 2) { throw new InvalidOperationException(Resources.MergeHelp); } IList<IList<BAMSortedIndex>> sortedIndexes = new List<IList<BAMSortedIndex>>(); IList<SequenceAlignmentMap> sequenceAlignmentMaps = new List<SequenceAlignmentMap>(); Parallel.For(0, FilePaths.Length, (int index) => { IList<BAMSortedIndex> sortedIndex; BAMParser parser = new BAMParser(); ; SequenceAlignmentMap map; if (index == 0) { try { map = parser.ParseOne<SequenceAlignmentMap>(FilePaths[0]); } catch { throw new InvalidOperationException(Resources.InvalidBAMFile); } if (map == null) { throw new InvalidOperationException(Resources.EmptyFile); } if (string.IsNullOrEmpty(HeaderFile) && map.Header.RecordFields.Count == 0) { throw new InvalidOperationException(Resources.HeaderMissing); } if (!string.IsNullOrEmpty(HeaderFile)) { SAMParser parse = new SAMParser(); SequenceAlignmentMap head; try { head = parse.ParseOne<SequenceAlignmentMap>(HeaderFile); } catch { throw new InvalidOperationException(Resources.IncorrectHeaderFile); } if (head == null) { throw new InvalidOperationException(Resources.EmptyFile); } header = head.Header; } else { header = map.Header; } sortedIndex = Sort(map, SortByReadName ? BAMSortByFields.ReadNames : BAMSortByFields.ChromosomeCoordinates); } else { try { map = parser.ParseOne<SequenceAlignmentMap>(FilePaths[index]); } catch { throw new InvalidOperationException(Resources.InvalidBAMFile); } if (map == null) { throw new InvalidOperationException(Resources.EmptyFile); } sortedIndex = Sort(map, SortByReadName ? BAMSortByFields.ReadNames : BAMSortByFields.ChromosomeCoordinates); } lock (sortedIndexes) { sortedIndexes.Add(sortedIndex); sequenceAlignmentMaps.Add(map); } }); if (string.IsNullOrEmpty(OutputFilename)) { OutputFilename = "out.bam"; autoGeneratedOutputFilename = true; } string filePath = Path.GetTempFileName(); using (FileStream fstemp = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) { BAMFormatter formatter = new BAMFormatter(); formatter.WriteHeader(header, fstemp); if (SortByReadName) { IList<BAMSortedIndex> sortedIndex = sortedIndexes.Select(a => a.First()).ToList(); WriteMergeFileSortedByReadName(sortedIndex, fstemp, formatter, sequenceAlignmentMaps); } else { WriteMergeFile(sortedIndexes, fstemp, formatter, sequenceAlignmentMaps); } using (FileStream fsoutput = new FileStream(OutputFilename, FileMode.Create, FileAccess.Write)) { fstemp.Seek(0, SeekOrigin.Begin); formatter.CompressBAMFile(fstemp, fsoutput); } } File.Delete(filePath); if (autoGeneratedOutputFilename) { Console.WriteLine(Properties.Resources.SuccessMessageWithOutputFileName, OutputFilename); } }
/// <summary> /// Validate parser parse one method overloads with filePath\textreader /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMParserWithParseOne(string nodeName, ParseOrFormatTypes parseTypes) { // Gets the expected sequence from the Xml string filePath = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); string expectedSequenceFile = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); ISequenceAlignmentParser parser = new SAMParser(); try { ISequenceAlignment alignment = null; // Parse SAM File switch (parseTypes) { case ParseOrFormatTypes.ParseOrFormatText: using (TextReader reader = new StreamReader(filePath)) { alignment = parser.ParseOne(reader); } break; case ParseOrFormatTypes.ParseOrFormatTextWithFlag: using (TextReader reader = new StreamReader(filePath)) { alignment = parser.ParseOne(reader, true); } break; case ParseOrFormatTypes.ParseOrFormatFileName: alignment = parser.ParseOne(filePath); break; case ParseOrFormatTypes.ParseOrFormatFileNameWithFlag: alignment = parser.ParseOne(filePath, true); break; } // Get expected sequences using (FastaParser parserObj = new FastaParser()) { IList <ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile); // Validate parsed output with expected output int count = 0; for (int ialigned = 0; ialigned < alignment.AlignedSequences.Count; ialigned++) { for (int iseq = 0; iseq < alignment.AlignedSequences[ialigned].Sequences.Count; iseq++) { Assert.AreEqual(expectedSequences[count].ToString(), alignment.AlignedSequences[ialigned].Sequences[iseq].ToString()); count++; } } } } finally { (parser as SAMParser).Dispose(); } }
/// <summary> /// Indentify hot spot chromosomes for length anamoly regions. /// </summary> /// <param name="inputFile"> Input file</param> /// <param name="mean">Mean value</param> /// <param name="standardDeviation">Standard deviation</param> private void IdentifyLentghAnamolies(string filename, float mean = -1, float deviation = -1) { bool calculateMeanNdeviation = false; if (mean == -1 || deviation == -1) { calculateMeanNdeviation = true; } SequenceAlignmentMap alignmentMapobj = null; if (!SAMInput) { BAMParser bamParser = new BAMParser(); alignmentMapobj = bamParser.ParseOne <SequenceAlignmentMap>(filename); } else { SAMParser samParser = new SAMParser(); alignmentMapobj = samParser.ParseOne <SequenceAlignmentMap>(filename); } // get reads from sequence alignment map object. IList <PairedRead> pairedReads = null; if (calculateMeanNdeviation) { pairedReads = alignmentMapobj.GetPairedReads(); } else { pairedReads = alignmentMapobj.GetPairedReads(mean, deviation); } // Get the orphan regions. var orphans = pairedReads.Where(PR => PR.PairedType == PairedReadType.Orphan); if (orphans.Count() == 0) { Console.WriteLine("No Orphans to display"); } List <ISequenceRange> orphanRegions = new List <ISequenceRange>(orphans.Count()); foreach (PairedRead orphanRead in orphans) { orphanRegions.Add(GetRegion(orphanRead.Read1)); } // Get sequence range grouping for Orphan regions. SequenceRangeGrouping orphanRangegroup = new SequenceRangeGrouping(orphanRegions); // Get the Length anomalies regions. var lengthAnomalies = pairedReads.Where(PE => PE.PairedType == PairedReadType.LengthAnomaly); if (lengthAnomalies.Count() == 0) { Console.WriteLine("No Anomalies to display"); } List <ISequenceRange> lengthAnomalyRegions = new List <ISequenceRange>(lengthAnomalies.Count()); foreach (PairedRead laRead in lengthAnomalies) { SequenceRange range = new SequenceRange(); range.ID = laRead.Read1.RName; range.Start = laRead.Read1.Pos; range.End = laRead.Read1.Pos + laRead.InsertLength; lengthAnomalyRegions.Add(range); } // Get sequence range grouping for length anomaly regions. SequenceRangeGrouping lengthAnomalyRangegroup = new SequenceRangeGrouping(lengthAnomalyRegions); if (lengthAnomalyRangegroup.GroupIDs.Count() == 0) { Console.Write("\r\nNo Length anomalies reads to display"); } else { Console.Write("Region of length anomaly:"); DisplaySequenceRange(lengthAnomalyRangegroup); } if (orphanRangegroup.GroupIDs.Count() == 0) { Console.Write("\r\nNo Orphan reads to display"); } else { Console.Write("\r\nRegion of Orphan reads:"); DisplaySequenceRange(orphanRangegroup); } SequenceRangeGrouping intersectedRegions = lengthAnomalyRangegroup.Intersect(orphanRangegroup); if (intersectedRegions.GroupIDs.Count() == 0) { Console.Write("\r\nNo Hot spots found"); } else { Console.Write("\r\nChromosomal Hot spot of length anomaly and Orphan region:"); DisplaySequenceRange(intersectedRegions); } }
/// <summary> /// Validate SAM to BAM conversion. /// </summary> /// <param name="nodeName">Different xml node name used for different test cases</param> void ValidateSAMToBAMConversion(string nodeName) { // Get values from xml config file. string expectedBAMStoragePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string samFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode1); BAMParser bamParserObj = new BAMParser(); SAMParser samParserObj = new SAMParser(); BAMFormatter bamFormatterObj = new BAMFormatter { CreateSortedBAMFile = true, CreateIndexFile = true }; SequenceAlignmentMap samSeqAlignment = null; SequenceAlignmentMap bamSeqAlignment = null; // Parse expected BAM file. SequenceAlignmentMap expextedBamAlignmentObj = bamParserObj.ParseOne<SequenceAlignmentMap>(expectedBAMStoragePath); // Parse a SAM file. samSeqAlignment = samParserObj.ParseOne<SequenceAlignmentMap>(samFilePath); try { // Format SAM sequenceAlignment object to BAM file. bamFormatterObj.Format(samSeqAlignment, Constants.BAMTempFileName); // Parse a formatted BAM file. bamSeqAlignment = bamParserObj.ParseOne<SequenceAlignmentMap>(Constants.BAMTempFileName); // Validate converted BAM file with expected BAM file. Assert.IsTrue(CompareSequencedAlignmentHeader(bamSeqAlignment, expextedBamAlignmentObj)); // Validate BAM file aligned sequences. Assert.IsTrue(CompareAlignedSequences(bamSeqAlignment, expextedBamAlignmentObj)); // Log message to VSTest GUI. ApplicationLog.WriteLine(string.Format((IFormatProvider)null, "BAM Parser P1 : Validated the SAM->BAM conversion successfully")); } finally { // Delete temporary file. File.Delete(Constants.BAMTempFileName); ApplicationLog.WriteLine("Deleted the temp file created."); } }
/// <summary> /// Genaral method to Invalidate SAM Formatter /// <param name="method">enum type to execute different overload</param> /// </summary> void ValidateSamFormatter(ParseOrFormatTypes method) { string filePath = utilityObj.xmlUtil.GetTextValue( Constants.SmallSAMFileNode, Constants.FilePathNode); ISequenceAlignmentParser parser = new SAMParser(); ISequenceAlignment alignment = null; try { switch (method) { case ParseOrFormatTypes.ParseOrFormatSeqText: new SAMFormatter().Format(null, null as ISequenceAlignment); break; case ParseOrFormatTypes.ParseOrFormatSeqTextWithFlag: alignment = parser.ParseOne(filePath); new SAMFormatter().Format(null, alignment); break; case ParseOrFormatTypes.ParseOrFormatIseq: new SAMFormatter().Format( null as ISequenceAlignment, null as string); break; case ParseOrFormatTypes.ParseOrFormatIseqFile: alignment = parser.ParseOne(filePath); new SAMFormatter().Format( alignment, null as string); break; case ParseOrFormatTypes.ParseOrFormatCollString: new SAMFormatter().Format( null as ICollection<ISequenceAlignment>, null as string); break; case ParseOrFormatTypes.ParseOrFormatCollection: new SAMFormatter().Format(null, null as ICollection<ISequenceAlignment>); break; case ParseOrFormatTypes.ParseOneOrFormatSeq: SequenceAlignmentMap align = new SequenceAlignmentMap(); new SAMFormatter().Format(align, null as string); break; case ParseOrFormatTypes.ParseOneOrFormatSeqFile: new SAMFormatter().Format(null as SequenceAlignmentMap, null as string); break; case ParseOrFormatTypes.ParseOrFormatIseqT: SequenceAlignmentMap alignments = new SequenceAlignmentMap(); new SAMFormatter().Format(alignments, null as string); break; case ParseOrFormatTypes.ParseOrFormatIseqText: new SAMFormatter().Format(null as SequenceAlignmentMap, null as string); break; case ParseOrFormatTypes.ParseOrFormatFormatString: break; default: break; } } catch (ArgumentNullException) { ApplicationLog.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); } catch (NotSupportedException) { ApplicationLog.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); } }
/// <summary> /// Parses SAM/BAm file based on input file. /// </summary> private void PerformParse() { string samExtension = ".sam"; string bamExtension = ".bam"; if (Helper.IsBAM(InputFilename)) { BAMParser parser = new BAMParser(); try { _sequenceAlignmentMap = parser.ParseOne<SequenceAlignmentMap>(InputFilename); } catch (Exception ex) { throw new InvalidOperationException(Resources.InvalidBAMFile, ex); } if (string.IsNullOrEmpty(OutputFilename)) { OutputFilename = InputFilename + samExtension; } } else { SAMParser parser = new SAMParser(); try { _sequenceAlignmentMap = parser.ParseOne<SequenceAlignmentMap>(InputFilename); } catch (Exception ex) { throw new InvalidOperationException(Resources.InvalidSAMFile, ex); } _isSAM = true; if (string.IsNullOrEmpty(OutputFilename)) { OutputFilename = InputFilename + bamExtension; } } }
/// <summary> /// Get chromoses with orphan regions /// </summary> /// <param name="filename">Path of the BAM file</param> /// <param name="mean">Mean value</param> /// <param name="deviation">Standard deviation</param> /// <returns></returns> private void DisplayOrphans(string filename) { SequenceAlignmentMap alignmentMapobj = null; if (!SAMInput) { BAMParser bamParser = new BAMParser(); alignmentMapobj = bamParser.ParseOne <SequenceAlignmentMap>(filename); } else { SAMParser samParser = new SAMParser(); alignmentMapobj = samParser.ParseOne <SequenceAlignmentMap>(filename); } // get reads from sequence alignment map object. IList <PairedRead> pairedReads = null; // Get Aligned sequences IList <SAMAlignedSequence> alignedSeqs = alignmentMapobj.QuerySequences; pairedReads = alignmentMapobj.GetPairedReads(0, 0); // Get the orphan regions. var orphans = pairedReads.Where(PR => PR.PairedType == PairedReadType.Orphan); int count = orphans.Count(); if (count == 0) { Console.WriteLine("No Orphans to display"); } var orphanRegions = new List <ISequenceRange>(count); orphanRegions.AddRange(orphans.Select(orphanRead => GetRegion(orphanRead.Read1))); // Get sequence range grouping object. SequenceRangeGrouping rangeGroup = new SequenceRangeGrouping(orphanRegions); if (!rangeGroup.GroupIDs.Any()) { Console.Write("\r\nNo Orphan reads to display"); } else { Console.Write("Region of Orphan reads:"); DisplaySequenceRange(rangeGroup); } SequenceRangeGrouping mergedRegions = rangeGroup.MergeOverlaps(); if (!mergedRegions.GroupIDs.Any()) { Console.Write("\r\nNo hot spots to display"); } else { Console.Write("\r\nChromosomal hot spot:"); DisplaySequenceRange(mergedRegions); } }
public void ValidateSAMToBAMConversion() { // Get values from xml config file. string expectedBamFilePath = this.utilityObj.xmlUtil.GetTextValue( Constants.BAMToSAMConversionNode, Constants.FilePathNode); string samFilePath = this.utilityObj.xmlUtil.GetTextValue( Constants.BAMToSAMConversionNode, Constants.FilePathNode1); // Parse expected BAM file. var bamParserObj = new BAMParser(); SequenceAlignmentMap expextedBamAlignmentObj = bamParserObj.ParseOne<SequenceAlignmentMap>(expectedBamFilePath); // Parse a SAM file. var samParserObj = new SAMParser(); SequenceAlignmentMap samSeqAlignment = samParserObj.ParseOne<SequenceAlignmentMap>(samFilePath); try { // Format SAM sequenceAlignment object to BAM file. var bamFormatterObj = new BAMFormatter(); bamFormatterObj.Format(samSeqAlignment, Constants.BAMTempFileName); // Parse a formatted BAM file. SequenceAlignmentMap bamSeqAlignment = bamParserObj.ParseOne<SequenceAlignmentMap>(Constants.BAMTempFileName); // Validate converted BAM file with expected BAM file. Assert.IsTrue(CompareSequencedAlignmentHeader(bamSeqAlignment, expextedBamAlignmentObj)); // Validate BAM file aligned sequences. Assert.IsTrue(CompareAlignedSequences(bamSeqAlignment, expextedBamAlignmentObj)); } finally { // Delete temporary file. File.Delete(Constants.BAMTempFileName); } }
/// <summary> /// Indentify hot spot chromosomes for length anamoly regions. /// </summary> /// <param name="inputFile"> Input file</param> /// <param name="mean">Mean value</param> /// <param name="standardDeviation">Standard deviation</param> private void IdentifyLentghAnamolies(string filename, float mean = -1, float deviation = -1) { bool calculateMeanNdeviation = false; if (mean == -1 || deviation == -1) { calculateMeanNdeviation = true; } SequenceAlignmentMap alignmentMapobj = null; if (!SAMInput) { BAMParser bamParser = new BAMParser(); alignmentMapobj = bamParser.ParseOne<SequenceAlignmentMap>(filename); } else { SAMParser samParser = new SAMParser(); alignmentMapobj = samParser.ParseOne<SequenceAlignmentMap>(filename); } // get reads from sequence alignment map object. IList<PairedRead> pairedReads = null; if (calculateMeanNdeviation) { pairedReads = alignmentMapobj.GetPairedReads(); } else { pairedReads = alignmentMapobj.GetPairedReads(mean, deviation); } // Get the orphan regions. var orphans = pairedReads.Where(PR => PR.PairedType == PairedReadType.Orphan); if (orphans.Count() == 0) { Console.WriteLine("No Orphans to display"); } List<ISequenceRange> orphanRegions = new List<ISequenceRange>(orphans.Count()); foreach (PairedRead orphanRead in orphans) { orphanRegions.Add(GetRegion(orphanRead.Read1)); } // Get sequence range grouping for Orphan regions. SequenceRangeGrouping orphanRangegroup = new SequenceRangeGrouping(orphanRegions); // Get the Length anomalies regions. var lengthAnomalies = pairedReads.Where(PE => PE.PairedType == PairedReadType.LengthAnomaly); if (lengthAnomalies.Count() == 0) { Console.WriteLine("No Anomalies to display"); } List<ISequenceRange> lengthAnomalyRegions = new List<ISequenceRange>(lengthAnomalies.Count()); foreach (PairedRead laRead in lengthAnomalies) { SequenceRange range = new SequenceRange(); range.ID = laRead.Read1.RName; range.Start = laRead.Read1.Pos; range.End = laRead.Read1.Pos + laRead.InsertLength; lengthAnomalyRegions.Add(range); } // Get sequence range grouping for length anomaly regions. SequenceRangeGrouping lengthAnomalyRangegroup = new SequenceRangeGrouping(lengthAnomalyRegions); if (lengthAnomalyRangegroup.GroupIDs.Count() == 0) { Console.Write("\r\nNo Length anomalies reads to display"); } else { Console.Write("Region of length anomaly:"); DisplaySequenceRange(lengthAnomalyRangegroup); } if (orphanRangegroup.GroupIDs.Count() == 0) { Console.Write("\r\nNo Orphan reads to display"); } else { Console.Write("\r\nRegion of Orphan reads:"); DisplaySequenceRange(orphanRangegroup); } SequenceRangeGrouping intersectedRegions = lengthAnomalyRangegroup.Intersect(orphanRangegroup); if (intersectedRegions.GroupIDs.Count() == 0) { Console.Write("\r\nNo Hot spots found"); } else { Console.Write("\r\nChromosomal Hot spot of length anomaly and Orphan region:"); DisplaySequenceRange(intersectedRegions); } }