Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
        public void TestFormatter()
        {
            const string filePath = @"TestUtils\BAM\SeqAlignment.bam";
            const string outputfilePath = "BAMTests123.bam";
            string outputIndexFile = outputfilePath + ".bai";
            BAMParser parser = new BAMParser();
            SequenceAlignmentMap alignmentMap = parser.ParseOne<SequenceAlignmentMap>(filePath);

            Assert.IsNotNull(alignmentMap);
            Assert.IsNotNull(alignmentMap.Header);
            Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
            Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
            Assert.IsNotNull(alignmentMap.QuerySequences);
            Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

            BAMFormatter formatter = new BAMFormatter();
            formatter.Format(alignmentMap, outputfilePath);

            formatter.CreateSortedBAMFile = true;
            formatter.CreateIndexFile = true;
            formatter.Format(alignmentMap, outputfilePath);
            Assert.IsTrue(File.Exists("BAMTests123.bam.bai"));

            alignmentMap = parser.ParseOne<SequenceAlignmentMap>(outputfilePath);
            Assert.IsNotNull(alignmentMap);
            Assert.IsNotNull(alignmentMap.Header);
            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.IsNotNull(alignmentMap);
            Assert.IsNotNull(alignmentMap.Header);
            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.IsNotNull(alignmentMap);
            Assert.IsNotNull(alignmentMap.Header);
            Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
            Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
            Assert.AreEqual(alignmentMap.QuerySequences.Count, 1);

            File.Delete(outputfilePath);
            File.Delete(outputIndexFile);
        }
Esempio n. 3
0
        /// <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;
        }
Esempio n. 4
0
        /// <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");
                }
            }


        }
Esempio n. 5
0
 public void TestParser()
 {
     const string FilePath = @"TestUtils\BAM\SeqAlignment.bam";
     BAMParser parser = null;
     try
     {
         parser = new BAMParser();
         SequenceAlignmentMap alignmentMap = parser.ParseOne<SequenceAlignmentMap>(FilePath);
         Assert.IsTrue(alignmentMap != null);
         Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
         Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);
     }
     finally
     {
         if (parser != null)
             parser.Dispose();
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Public method to sort BAM file.
        /// SAMUtil.exe in.bam out.bam
        /// </summary>
        public void DoSort()
        {
            string sortExtension = ".sort";
            if (string.IsNullOrEmpty(InputFilename))
            {
                throw new InvalidOperationException(Resources.SortHelp);
            }

            BAMParser parse = new BAMParser();
            SequenceAlignmentMap map = null;

            try
            {
                map = parse.ParseOne<SequenceAlignmentMap>(InputFilename);
            }
            catch(Exception ex)
            {
                throw new InvalidOperationException(Resources.InvalidBAMFile, ex);
            }

            BAMFormatter format = new BAMFormatter
                                  {
                                      CreateSortedBAMFile = true,
                                      SortType =
                                          this.SortByReadName
                                              ? BAMSortByFields.ReadNames
                                              : BAMSortByFields.ChromosomeCoordinates
                                  };

            if (string.IsNullOrEmpty(OutputFilename))
            {
                OutputFilename = InputFilename + sortExtension;
                autoGeneratedOutputFilename = true;
            }

            format.Format(map, OutputFilename);

            if (autoGeneratedOutputFilename)
            {
                Console.WriteLine(Resources.SuccessMessageWithOutputFileName, OutputFilename);
            }
        }
Esempio n. 7
0
        /// <summary>
        ///     Parse BAM and validate parsed aligned sequences and its properties.
        /// </summary>
        /// <param name="nodeName">Different xml nodes used for different test cases</param>
        /// <param name="BAMParserPam">BAM Parse method parameters</param>
        /// <param name="IsEncoding">
        ///     True for BAMParser ctor with encoding.
        ///     False otherwise
        /// </param>
        private void ValidateBAMParser(string nodeName,
                                       BAMParserParameters BAMParserPam,
                                       bool IsReferenceIndex)
        {
            // Get input and output values from xml node.
            string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                 Constants.FilePathNode);
            string expectedAlignedSeqFilePath = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            string refIndexValue = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.RefIndexNode);
            string startIndexValue = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.StartIndexNode);
            string endIndexValue = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.EndIndexNode);
            string alignedSeqCount = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.AlignedSeqCountNode);
            string refSeqName = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ChromosomeNameNode);
            SequenceAlignmentMap seqAlignment = null;
            BAMParser bamParser = null;
            try
            {
                bamParser = new BAMParser();

                // Parse a BAM file with different parameters.
                switch (BAMParserPam)
                {
                    case BAMParserParameters.StreamReader:
                        using (Stream stream = new FileStream(bamFilePath, FileMode.Open,
                                                              FileAccess.Read))
                        {
                            seqAlignment = bamParser.ParseOne(stream);
                        }
                        break;
                    case BAMParserParameters.FileName:
                        seqAlignment = bamParser.ParseOne<SequenceAlignmentMap>(bamFilePath);
                        break;
                    case BAMParserParameters.ParseRangeFileName:
                        seqAlignment = bamParser.ParseRange(bamFilePath,
                                                            Convert.ToInt32(refIndexValue, null));
                        break;
                    case BAMParserParameters.ParseRangeWithIndex:
                        seqAlignment = bamParser.ParseRange(bamFilePath,
                                                            Convert.ToInt32(refIndexValue, null),
                                                            Convert.ToInt32(startIndexValue, null),
                                                            Convert.ToInt32(endIndexValue, null));
                        break;
                    case BAMParserParameters.ParseRangeUsingRefSeq:
                        seqAlignment = bamParser.ParseRange(bamFilePath, refSeqName);
                        break;
                    case BAMParserParameters.ParseRangeUsingRefSeqAndFlag:
                        seqAlignment = bamParser.ParseRange(bamFilePath, refSeqName);
                        break;
                    case BAMParserParameters.ParseRangeUsingRefSeqUsingIndex:
                        seqAlignment = bamParser.ParseRange(bamFilePath, refSeqName,
                                                            Convert.ToInt32(startIndexValue, null),
                                                            Convert.ToInt32(endIndexValue, null));
                        break;
                    case BAMParserParameters.ParseRangeUsingIndexesAndFlag:
                        seqAlignment = bamParser.ParseRange(bamFilePath, refSeqName,
                                                            Convert.ToInt32(startIndexValue, null),
                                                            Convert.ToInt32(endIndexValue, null));
                        break;
                }

                // Validate BAM Header record fileds.
                if (!IsReferenceIndex)
                {
                    this.ValidateBAMHeaderRecords(nodeName, seqAlignment);
                }

                IList<SAMAlignedSequence> alignedSeqs = seqAlignment.QuerySequences;
                Assert.AreEqual(alignedSeqCount, alignedSeqs.Count.ToString((IFormatProvider) null));
                // Get expected sequences
                var 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.IsFalse(alignedSeqs[index].IsDummyRead);
                        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(null,
                                                               "BAM Parser BVT : Validated Aligned sequence :{0} successfully",
                                                               alignedSeqs[index].QuerySequence));
                    }
                }
            }
            finally
            {
                bamParser.Dispose();
            }
        }
Esempio n. 8
0
        public void ValidateSortByChromosomeNameAndCoordinates()
        {
            using (var parser = new BAMParser())
            {
                string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(Constants.MediumSizeBAMFileNode,
                                                                     Constants.FilePathNode);
                Assert.IsNotNull(bamFilePath);

                SequenceAlignmentMap seqAlignment = parser.ParseOne<SequenceAlignmentMap>(bamFilePath);
                this.ValidateSort(seqAlignment, BAMSortByFields.ChromosomeNameAndCoordinates);
            }
        }
Esempio n. 9
0
        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);
            }
        }
Esempio n. 10
0
        public void ValidateGetInsertLengthWithValidate()
        {
            // Get input and output values from xml node.
            string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(Constants.PairedReadTypesForLibraryInfoNode,
                                                                 Constants.FilePathNode);
            string mean = this.utilityObj.xmlUtil.GetTextValue(
                Constants.PairedReadTypesForLibraryInfoNode, Constants.MeanNode);
            string deviation = this.utilityObj.xmlUtil.GetTextValue(
                Constants.PairedReadTypesForLibraryInfoNode, Constants.DeviationValueNode);
            string[] insertLength = this.utilityObj.xmlUtil.GetTextValue(
                Constants.PairedReadTypesForLibraryInfoNode, Constants.InsertLengthNode).Split(',');

            using (var bamParser = new BAMParser())
            {
                SequenceAlignmentMap seqAlignmentMapObj = bamParser.ParseOne<SequenceAlignmentMap>(bamFilePath);
                int i = 0;
                try
                {
                    IList<PairedRead> pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                        float.Parse(deviation, null));
                    foreach (PairedRead read in pairedReads)
                    {
                        //pass true for validate parameter
                        int length = PairedRead.GetInsertLength(read.Read1, read.Read2, true);
                        Assert.AreEqual(length.ToString((IFormatProvider) null), insertLength[i]);
                        i++;
                    }
                    ApplicationLog.WriteLine(string.Format(null,
                                                           "BAM Parser BVT : Validated GetInsertLength Successfully"));
                }
                finally
                {
                    bamParser.Dispose();
                }
            }
        }
Esempio n. 11
0
 public void ValidateSortByReadNames()
 {
     using (var parser = new BAMParser())
     {
         string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(Constants.MediumSizeBAMFileNode,
                                                              Constants.FilePathNode);
         SequenceAlignmentMap seqAlignment = parser.ParseOne<SequenceAlignmentMap>(bamFilePath);
         this.ValidateSort(seqAlignment, BAMSortByFields.ReadNames);
     }
 }
Esempio n. 12
0
        /// <summary>
        ///     Validate different paired read types
        /// </summary>
        /// <param name="nodeName">XML node name</param>
        /// <param name="pams">GetPairedReadTypes method parameters</param>
        private void ValidatePairedReadTypes(string nodeName, GetPairedReadTypeParameters pams)
        {
            // Get input and output values from xml node.
            string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                 Constants.FilePathNode);
            string mean = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.MeanNode);
            string deviation = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.DeviationValueNode);
            string library = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.LibraryNameNode);
            string[] pairedReadType = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.PairedReadTypeNode).Split(',');
            string[] insertLength = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.InsertLengthNode).Split(',');

            var bamParser = new BAMParser();
            SequenceAlignmentMap seqAlignmentMapObj = bamParser.ParseOne<SequenceAlignmentMap>(bamFilePath);
            CloneLibraryInformation libraryInfo;
            int i = 0;
            try
            {
                IList<PairedRead> pairedReads;
                switch (pams)
                {
                    case GetPairedReadTypeParameters.PaireReadTypeUsingLibraryName:
                        pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                                                                        float.Parse(deviation, null));
                        foreach (PairedRead read in pairedReads)
                        {
                            PairedReadType type = PairedRead.GetPairedReadType(read, library);
                            Assert.AreEqual(type.ToString(), pairedReadType[i]);
                            i++;
                        }
                        break;
                    case GetPairedReadTypeParameters.PaireReadTypeUsingCloneLibraryInfo:
                        pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                                                                        float.Parse(deviation, null));
                        libraryInfo = CloneLibrary.Instance.GetLibraryInformation(library);
                        foreach (PairedRead read in pairedReads)
                        {
                            PairedReadType type = PairedRead.GetPairedReadType(read, libraryInfo);
                            Assert.AreEqual(type.ToString(), pairedReadType[i]);
                            i++;
                        }
                        break;
                    case GetPairedReadTypeParameters.PaireReadTypeUsingMeanAndDeviation:
                        pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                                                                        float.Parse(deviation, null));
                        foreach (PairedRead read in pairedReads)
                        {
                            PairedReadType type = PairedRead.GetPairedReadType(read, float.Parse(mean, null),
                                                                               float.Parse(deviation, null));
                            Assert.AreEqual(type.ToString(), pairedReadType[i]);
                            i++;
                        }
                        break;
                    case GetPairedReadTypeParameters.PaireReadTypeUsingReadsAndLibrary:
                        pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                                                                        float.Parse(deviation, null));
                        foreach (PairedRead read in pairedReads)
                        {
                            PairedReadType type = PairedRead.GetPairedReadType(read.Read1,
                                                                               read.Read2, library);
                            Assert.AreEqual(type.ToString(), pairedReadType[i]);
                            i++;
                        }
                        break;
                    case GetPairedReadTypeParameters.PaireReadTypeUsingReadsAndLibraryInfo:
                        pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                                                                        float.Parse(deviation, null));
                        libraryInfo = CloneLibrary.Instance.GetLibraryInformation(library);
                        foreach (PairedRead read in pairedReads)
                        {
                            PairedReadType type = PairedRead.GetPairedReadType(read.Read1,
                                                                               read.Read2, libraryInfo);
                            Assert.AreEqual(type.ToString(), pairedReadType[i]);
                            i++;
                        }
                        break;
                    case GetPairedReadTypeParameters.GetInsertLength:
                        pairedReads = seqAlignmentMapObj.GetPairedReads(float.Parse(mean, null),
                                                                        float.Parse(deviation, null));
                        libraryInfo = CloneLibrary.Instance.GetLibraryInformation(library);
                        foreach (PairedRead read in pairedReads)
                        {
                            int length = PairedRead.GetInsertLength(read.Read1, read.Read2);
                            Assert.AreEqual(length.ToString((IFormatProvider) null), insertLength[i]);
                            i++;
                        }
                        break;
                }
                ApplicationLog.WriteLine(string.Format(null,
                                                       "BAM Parser BVT : Validated Paired read Type Successfully"));
            }

            finally
            {
                bamParser.Dispose();
            }
        }
Esempio n. 13
0
 public void ValidateBAMParserWithDummyReads()
 {
     
     string bamFilePath = utilityObj.xmlUtil.GetTextValue(Constants.BAMFileWithDummyReads,
                                                         Constants.FilePathNode);
     SequenceAlignmentMap seqAlignment = null;
     BAMParser bamParser = null;
     bamParser = new BAMParser();
     seqAlignment = bamParser.ParseOne<SequenceAlignmentMap>(bamFilePath);
     var seq = seqAlignment.QuerySequences.First();
     Assert.AreEqual("fakeref", seq.RName);
     Assert.AreEqual("1M", seq.CIGAR);
     Assert.AreEqual(10, seq.Pos);
     Assert.IsNull(seq.QuerySequence);
     var optField = seq.OptionalFields.First();
     Assert.AreEqual("CT", optField.Tag);
     Assert.AreEqual("Z", optField.VType);
     Assert.AreEqual(".;ESDN;", optField.Value);
     Assert.IsTrue(seq.IsDummyRead);
 }
Esempio n. 14
0
        /// <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.");
            }
        }
Esempio n. 15
0
        /// <summary>
        ///     Validate GetPaired method
        /// </summary>
        /// <param name="nodeName">XML node name</param>
        /// <param name="pams">GetPairedReads method parameters</param>
        private void ValidatePairedReads(string nodeName, GetPairedReadParameters pams)
        {
            // Get input and output values from xml node.
            string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                 Constants.FilePathNode);
            string expectedAlignedSeqFilePath = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            string mean = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.MeanNode);
            string deviation = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.DeviationValueNode);
            string library = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.LibraryNameNode);
            string pairedReadsCount = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.PairedReadsNode);
            string[] insertLength = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.InsertLengthNode).Split(',');
            string[] pairedReadType = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.PairedReadTypeNode).Split(',');

            SequenceAlignmentMap seqAlignment = null;
            IList<PairedRead> pairedReads = null;
            var bamParser = new BAMParser();
            var parserObj = new FastAParser();

            try
            {
                seqAlignment = bamParser.ParseOne<SequenceAlignmentMap>(bamFilePath);
                IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedAlignedSeqFilePath);

                switch (pams)
                {
                    case GetPairedReadParameters.GetPairedReadWithParameters:
                        pairedReads = seqAlignment.GetPairedReads(float.Parse(mean, null),
                                                                  float.Parse(deviation, null));
                        break;
                    case GetPairedReadParameters.GetPairedReadWithLibraryName:
                        pairedReads = seqAlignment.GetPairedReads(library);
                        break;
                    case GetPairedReadParameters.GetPairedReadWithCloneLibraryInfo:
                        CloneLibraryInformation libraryInfo =
                            CloneLibrary.Instance.GetLibraryInformation(library);
                        pairedReads = seqAlignment.GetPairedReads(libraryInfo);
                        break;
                    case GetPairedReadParameters.Default:
                        pairedReads = seqAlignment.GetPairedReads();
                        break;
                }

                Assert.AreEqual(pairedReadsCount, pairedReads.Count.ToString((IFormatProvider) null));

                int i = 0;
                foreach (PairedRead read in pairedReads)
                {
                    Assert.AreEqual(insertLength[i], read.InsertLength.ToString((IFormatProvider) null));
                    Assert.AreEqual(pairedReadType[i], read.PairedType.ToString());

                    foreach (SAMAlignedSequence seq in read.Reads)
                    {
                        Assert.AreEqual(new string(expectedSequences.ElementAt(i).Select(a => (char) a).ToArray()),
                                        new string(seq.QuerySequence.Select(a => (char) a).ToArray()));

                        // Log to VSTest GUI.
                        ApplicationLog.WriteLine(string.Format(null,
                                                               "BAM Parser BVT : Validated Paired read :{0} successfully",
                                                               seq.QuerySequence));
                    }
                    i++;
                }
            }

            finally
            {
                bamParser.Dispose();
            }
        }
Esempio n. 16
0
        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.ParseOne<SequenceAlignmentMap>(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.ParseOne<SequenceAlignmentMap>(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();
                File.Delete(outputFilePath1);
                File.Delete(outputFilePath2);
            }
        }
Esempio n. 17
0
        /// <summary>
        ///     Validate formatted BAM file.
        /// </summary>
        /// <param name="nodeName">Different xml nodes used for different test cases</param>
        /// <param name="BAMParserPam">BAM Format method parameters</param>
        private void ValidateBAMFormatter(string nodeName,
                                          BAMParserParameters BAMParserPam)
        {
            // Get input and output values from xml node.
            string bamFilePath = 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);

            using (var bamParserObj = new BAMParser())
            {
                // Parse a BAM file.
                var seqAlignment = bamParserObj.ParseOne<SequenceAlignmentMap>(bamFilePath);
                // Create a BAM formatter object.
                var formatterObj = new BAMFormatter();
                // Write/Format aligned sequences to BAM file.
                switch (BAMParserPam)
                {
                    case BAMParserParameters.StreamWriter:
                        Stream stream;
                        using (stream = new FileStream(Constants.BAMTempFileName, FileMode.Create, FileAccess.Write))
                        {
                            formatterObj.Format(stream, seqAlignment);
                        }
                        break;
                    case BAMParserParameters.FileName:
                        formatterObj.Format(seqAlignment, Constants.BAMTempFileName);
                        break;
                    case BAMParserParameters.IndexFile:
                        formatterObj.Format(seqAlignment, Constants.BAMTempFileName, Constants.BAMTempIndexFile);
                        File.Exists(Constants.BAMTempIndexFile);
                        break;
                    default:
                        break;
                }

                // Parse formatted BAM file and validate aligned sequences.
                SequenceAlignmentMap expectedSeqAlignmentMap = bamParserObj.ParseOne<SequenceAlignmentMap>(Constants.BAMTempFileName);

                // Validate Parsed BAM file Header record fileds.
                this.ValidateBAMHeaderRecords(nodeName, expectedSeqAlignmentMap);
                IList<SAMAlignedSequence> alignedSeqs = expectedSeqAlignmentMap.QuerySequences;
                Assert.AreEqual(alignedSeqCount, alignedSeqs.Count.ToString((IFormatProvider) null));

                // Get expected sequences
                var 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("BAM Formatter BVT : Validated Aligned sequence :{0} successfully", alignedSeqs[index].QuerySequence));
                    }
                }
            }
            File.Delete(Constants.BAMTempFileName);
            File.Delete(Constants.BAMTempIndexFile);
        }
Esempio n. 18
0
        /// <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);
            }
        }
Esempio n. 19
0
        /// <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()));
            }
        }
Esempio n. 20
0
        /// <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);
            }
        }
Esempio n. 21
0
        /// <summary>
        ///     Format BAM file and validate.
        /// </summary>
        /// <param name="nodeName">Different xml nodes used for different test cases</param>
        private void InValidateBAMFormatter(string nodeName)
        {
            // Get input and output values from xml node.
            string bamFilePath = _utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode);

            using (var bamParserObj = new BAMParser())
            {
                using (var storage = new BAMIndexStorage(File.Create(Constants.BAMTempIndexFileForIndexData)))
                {
                    // Parse a BAM file.
                    var seqAlignment = bamParserObj.ParseOne <SequenceAlignmentMap>(bamFilePath);

                    // Create a BAM formatter object.
                    var formatterObj = new BAMFormatter();

                    // Null filename
                    try
                    {
                        formatterObj.Format(seqAlignment, null, Constants.BAMTempIndexFileForIndexData);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    // Same filename
                    try
                    {
                        formatterObj.Format(seqAlignment, Constants.BAMTempFileName, Constants.BAMTempFileName);
                    }
                    catch (ArgumentException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    // Null sequence
                    try
                    {
                        formatterObj.Format(null, bamFilePath, Constants.BAMTempIndexFileForIndexData);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    // Null index
                    try
                    {
                        formatterObj.Format(seqAlignment, bamFilePath, null);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    // Invalidate BAM Parser Format(SeqAlignmentMap, BamFileName)
                    try
                    {
                        formatterObj.Format(null as SequenceAlignmentMap,
                                            Constants.BAMTempFileName);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    try
                    {
                        formatterObj.Format(seqAlignment, null as string);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    // Invalidate Format(SequenceAlignmentMap, StreamWriter)
                    try
                    {
                        formatterObj.Format(null, seqAlignment);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    Stream stream;
                    try
                    {
                        using (stream = new FileStream(Constants.BAMTempFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            formatterObj.Format(stream, null as ISequenceAlignment);
                        }
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    // Invalidate Format(SequenceAlignmentMap, StreamWriter, IndexFile)
                    try
                    {
                        formatterObj.Format(null, storage, seqAlignment);
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    try
                    {
                        using (stream = new FileStream(Constants.BAMTempFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            formatterObj.Format(stream, storage, null);
                        }
                    }
                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    try
                    {
                        using (stream = new FileStream(Constants.BAMTempFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            formatterObj.Format(stream, null, seqAlignment);
                        }
                    }

                    catch (ArgumentNullException ex)
                    {
                        ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
                    }

                    formatterObj = null;
                }
            }
        }
Esempio n. 22
0
        /// <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();
            }
        }
Esempio n. 23
0
        public void ValidateAlignedSeqProperties()
        {
            // Get input and output values from xml node.
            string BAMStoragePath = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.FilePathNode);
            string expectedFlagValue = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.FlagValueNode);
            string expectedISize = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.Isize);
            string expectedMapQ = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.MapQValue);
            string expectedMetadataCount = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.Metadata);
            string expectedMPos = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.MPos);
            string expectedOptionalFields = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.OptionalFieldsNode);
            string expectedPos = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.Pos);
            string expectedQueryLength = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.QueryLength);
            string expectedRName = this.utilityObj.xmlUtil.GetTextValue(
                Constants.BAMAlignedSeqPropertiesNode, Constants.RName);

            // Parse a BAM file.
            using (BAMParser bamParseObj = new BAMParser())
            {
                BAMFormatter bamFormatterObj = new BAMFormatter();
                SequenceAlignmentMap seqAlignment = bamParseObj.ParseOne<SequenceAlignmentMap>(BAMStoragePath);

                // Get Aligned sequences.
                IList<SAMAlignedSequence> alignedSeqs = seqAlignment.QuerySequences;

                // Validate BAM Formatter Properties.
                Assert.AreEqual(Constants.BAMFileName, bamFormatterObj.Name);
                Assert.AreEqual(Constants.BAMFileType, bamFormatterObj.SupportedFileTypes);
                Assert.AreEqual(Constants.BAMFormatterDescription,
                    bamFormatterObj.Description.Replace("\r", "").Replace("\n", ""));

                // Validate BAM Parser Properties.
                Assert.AreEqual(Constants.BAMFileName, bamParseObj.Name);
                Assert.AreEqual(Constants.BAMFileType, bamParseObj.SupportedFileTypes);
                Assert.AreEqual(Constants.BAMDescription,
                    bamParseObj.Description.Replace("\r", "").Replace("\n", ""));

                // Validate all properties of aligned sequence.
                Assert.AreEqual(expectedFlagValue, alignedSeqs[0].Flag.ToString());
                Assert.AreEqual(expectedISize, alignedSeqs[0].ISize.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedMapQ, alignedSeqs[0].MapQ.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedMetadataCount,
                    alignedSeqs[0].Metadata.Count.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedMPos, alignedSeqs[0].MPos.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedOptionalFields,
                    alignedSeqs[0].OptionalFields.Count.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedPos, alignedSeqs[0].Pos.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedQueryLength,
                    alignedSeqs[0].RefEndPos.ToString((IFormatProvider)null));
                Assert.AreEqual(expectedRName, alignedSeqs[0].RName.ToString((IFormatProvider)null));
            }

            // Log to VSTest GUI.
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                "BAM Parser P1 : Validated the Aligned sequence properties successfully"));
        }
Esempio n. 24
0
        /// <summary>
        /// Parse BAM and validate parsed aligned sequences and its properties.
        /// </summary>
        /// <param name="nodeName">Different xml nodes used for different test cases</param>
        /// <param name="BAMParserPam">Different Parser parameters used for different testcases</param>
        void ValidateBAMParserForQualitySequences(string nodeName,
            BAMParserParameters BAMParserPam)
        {
            // Get input and output values from xml node.
            string BAMStoragePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                Constants.FilePathNode);
            string expectedQualitySeqFilePath = this.utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            string alignedSeqCount = this.utilityObj.xmlUtil.GetTextValue(
                 nodeName, Constants.AlignedSeqCountNode);

            SequenceAlignmentMap seqAlignment = null;
            BAMParser bamParser = new BAMParser();

            // Parse a BAM file with different parameters.
            switch (BAMParserPam)
            {
                case BAMParserParameters.FileName:
                    seqAlignment = bamParser.ParseOne<SequenceAlignmentMap>(BAMStoragePath);
                    break;
                case BAMParserParameters.StreamReader:
                    using (Stream stream = new FileStream(BAMStoragePath, FileMode.Open,
                        FileAccess.Read))
                    {
                        seqAlignment = bamParser.ParseOne(stream);
                    }
                    break;
                default:
                    break;
            }

            // Validate Aligned sequence CIGAR,QName and Bin index values.
            this.ValidateAlignedSeqValues(nodeName, seqAlignment);

            IList<SAMAlignedSequence> alignedSeqs = seqAlignment.QuerySequences;

            Assert.AreEqual(alignedSeqCount, alignedSeqs.Count.ToString((IFormatProvider)null));

            // Get expected quality sequences
            FastAParser parserObj = new FastAParser();
            IEnumerable<ISequence> expectedQualitySequences = parserObj.Parse(expectedQualitySeqFilePath);
            IList<ISequence> expectedSequencesList = expectedQualitySequences.ToList();
            // Validate quality 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 Parser P1 : Validated Quality sequence :{0} successfully",
                    alignedSeqs[index].QuerySequence.ToString()));
            }

        }
Esempio n. 25
0
        /// <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;
                }
            }
        }
Esempio n. 26
0
        /// <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);
            }



        }