Writes a SequenceAlignmentMap to a particular location, usually a file. The output is formatted according to the BAM file format. Documentation for the latest BAM file format can be found at http://samtools.sourceforge.net/SAM1.pdf
Inheritance: ISequenceAlignmentFormatter
Ejemplo n.º 1
0
        /// <summary>
        /// Write out the given SequenceAlignmentMap to the file
        /// </summary>
        /// <param name="formatter">BAMFormatter</param>
        /// <param name="sam">SequenceAlignmentMap</param>
        /// <param name="filename">File to write to</param>
        public static void Format(this BAMFormatter formatter, SequenceAlignmentMap sam, string filename)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException("formatter");
            }
            if (sam == null)
            {
                throw new ArgumentNullException("sam");
            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException("filename");
            }

            using (var fs = File.Create(filename))
            {
                // Create the IndexFile if necessary
                if (formatter.CreateIndexFile)
                {
                    using (var bamIndexFile = new BAMIndexStorage(
                               File.Create(filename + Properties.Resource.BAM_INDEXFILEEXTENSION)))
                    {
                        formatter.Format(fs, bamIndexFile, sam);
                    }
                }
                else
                {
                    formatter.Format(fs, sam);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write out the given SequenceAlignmentMap to the file
        /// </summary>
        /// <param name="formatter">BAMFormatter</param>
        /// <param name="sam">SequenceAlignmentMap</param>
        /// <param name="filename">File to write to</param>
        /// <param name="indexFilename">BAM index file</param>
        public static void Format(this BAMFormatter formatter, ISequenceAlignment sam, string filename, string indexFilename)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException("formatter");
            }
            if (sam == null)
            {
                throw new ArgumentNullException("sam");
            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException("filename");
            }
            if (string.IsNullOrWhiteSpace(indexFilename))
            {
                throw new ArgumentNullException("indexFilename");
            }

            if (filename == indexFilename)
            {
                throw new ArgumentException("Use different filenames for index and alignment.", "indexFilename");
            }

            using (var fs = File.Create(filename))
                using (var bamIndexFile = new BAMIndexStorage(File.Create(indexFilename)))
                {
                    formatter.Format(fs, bamIndexFile, sam);
                }
        }
Ejemplo n.º 3
0
        public void TestFormatter()
        {
            string filePath = @"TestUtils\BAM\SeqAlignment.bam";
            string outputfilePath = "BAMTests123.bam";
            BAMParser parser = null;
            try
            {
                parser = new BAMParser();
                BAMFormatter formatter = new BAMFormatter();
                SequenceAlignmentMap alignmentMap = parser.Parse(filePath);

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                formatter.Format(alignmentMap, outputfilePath);

                formatter.CreateSortedBAMFile = true;
                formatter.CreateIndexFile = true;
                alignmentMap = parser.Parse(filePath);
                formatter.Format(alignmentMap, outputfilePath);

                Assert.IsTrue(File.Exists("BAMTests123.bam.bai"));

                alignmentMap = parser.Parse(outputfilePath);

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                alignmentMap = parser.ParseRange("BAMTests123.bam", new SequenceRange("chr20", 0, int.MaxValue));

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                alignmentMap = parser.ParseRange("BAMTests123.bam", new SequenceRange("chr20", 0, 28833));

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 1);
            }
            finally
            {
                if (parser != null)
                    parser.Dispose();
            }
        }
Ejemplo n.º 4
0
        public void BAMProperties()
        {
            using (BAMParser parser = new BAMParser())
            {
                Assert.AreEqual(parser.Name, Properties.Resource.BAM_NAME);
                Assert.AreEqual(parser.Description, Properties.Resource.BAMPARSER_DESCRIPTION);
                Assert.AreEqual(parser.SupportedFileTypes, Properties.Resource.BAM_FILEEXTENSION);
            }

            BAMFormatter formatter = new BAMFormatter();
            Assert.AreEqual(formatter.Name, Properties.Resource.BAM_NAME);
            Assert.AreEqual(formatter.Description, Properties.Resource.BAMFORMATTER_DESCRIPTION);
            Assert.AreEqual(formatter.SupportedFileTypes, Properties.Resource.BAM_FILEEXTENSION);
        }
Ejemplo n.º 5
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);
        }
Ejemplo 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);
            }
        }
Ejemplo n.º 7
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.Parse(inputFilePath);

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                formatter.CreateSortedBAMFile = true;
                formatter.SortType = BAMSortByFields.ChromosomeCoordinates;
                formatter.Format(alignmentMap, outputFilePath1);

                alignmentMap = parser.Parse(inputFilePath);
                formatter.Format(alignmentMap, outputFilePath2);

                Assert.IsTrue(File.Exists(outputFilePath1));
                Assert.IsTrue(File.Exists(outputFilePath2));

                Assert.AreEqual(true, FileCompare(outputFilePath1, outputFilePath2));
            }
            finally
            {
                if (parser != null)
                    parser.Dispose();
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo 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);
            }
        }
Ejemplo n.º 10
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);
            }
        }
        /// <summary>
        /// Initialise bam output file, if required and if not already initialised
        /// </summary>
        private void InitBamOutputFiles()
        {
            if (writeToFilteredBam && bamStream == null && bamFormatter == null)
            {
                bamFormatter = new BAMFormatter();
                newHeader = new SAMAlignmentHeader();
                bamOutputQueue = new Queue<Collection<SAMAlignedSequence>>();

                // Create the output file for filtered sequences
                string file = fileName + "\\sequences.bam";
                if(File.Exists(file))
                {
                    File.Delete(file);
                }
                bamStream = File.Create(file);
            }
        }
Ejemplo n.º 12
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.");
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        ///  Initializes required parsers, formatters, input and output files based on user option.
        /// </summary>
        private void Initialize()
        {
            bamparser = new BAMParser();
            bamformatter = new BAMFormatter();

            bamUncompressedOutStream = null;
            bamCompressedOutStream = null;

            if (string.IsNullOrEmpty(OutputFilename))
            {
                writer = Console.Out;
            }
            else
            {
                if (UnCompressedBAM || BAMOutput)
                {
                    writer = null;

                    if (UnCompressedBAM)
                    {
                        bamUncompressedOutStream = new FileStream(OutputFilename, FileMode.Create, FileAccess.ReadWrite);
                    }
                    else
                    {
                        bamCompressedOutStream = new FileStream(OutputFilename, FileMode.Create, FileAccess.ReadWrite);
                    }
                }
                else
                {
                    writer = new StreamWriter(OutputFilename);
                }
            }

            #region Intialize temp files
            long inputfileSize = (new FileInfo(InputFilePath)).Length;
            long unCompressedSize = inputfileSize;

            if (!SAMInput)
            {
                unCompressedSize = inputfileSize * 4; // as uncompressed bam file will be Aprox 4 times that of the compressed file.
            }

            long compressedSize = unCompressedSize / 4;

            // uncompressed file is required for both uncompressed and compressed outputs.
            if ((UnCompressedBAM || BAMOutput) && bamUncompressedOutStream == null)
            {
                if (HeaderOnly || (MemStreamLimit >= unCompressedSize))
                {
                    bamUncompressedOutStream = new MemoryStream();
                }
                else
                {
                    uncompressedTempfile = Path.GetTempFileName();
                    bamUncompressedOutStream = new FileStream(uncompressedTempfile, FileMode.Open, FileAccess.ReadWrite);
                }
            }

            if (BAMOutput && !UnCompressedBAM && bamCompressedOutStream == null)
            {
                if (HeaderOnly || (MemStreamLimit >= compressedSize))
                {
                    bamCompressedOutStream = new MemoryStream((int)(inputfileSize));
                }
                else
                {
                    compressedTempfile = Path.GetTempFileName();
                    bamCompressedOutStream = new FileStream(compressedTempfile, FileMode.Open, FileAccess.ReadWrite);
                }
            }
            #endregion Intialize temp files
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     Format BAM file using IsequenceAlignment object.
        /// </summary>
        /// <param name="nodeName">Different xml nodes used for different test cases</param>
        private void InValidateBAMFormatterWithSequenceAlignment(string nodeName)
        {
            // Get input and output values from xml node.
            string bamFilePath = _utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode);

            ISequenceAlignmentParser bamParserObj = new BAMParser();
            IList<ISequenceAlignment> seqList = bamParserObj.Parse(bamFilePath).ToList();
            try
            {
                using (var BAMIndexStorageObj = new BAMIndexStorage(File.Create(Constants.BAMTempIndexFileForInvalidData)))
                {
                    // Create a BAM formatter object.
                    var formatterObj = new BAMFormatter();
                    InvalidateBAmFormatter(formatterObj, seqList);
                    InvalidateBAmFormatterWithWithInvalidValues(formatterObj,seqList, bamFilePath, BAMIndexStorageObj);
                    InvalidateBAmFormatterWithWithNullValues(formatterObj,seqList);
                }
            }
            finally
            {
                (bamParserObj as BAMParser).Dispose();
                File.Delete(Constants.BAMTempIndexFileForInvalidData);
            }
        }
Ejemplo n.º 15
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;
                }
            }
        }
Ejemplo n.º 16
0
        public void InvalidateCompressBAMFile()
        {
            //pass null for stream
            try
            {
                string tmpFileName = Path.GetTempFileName();
                var formatter = new BAMFormatter();
                using (var stream = new FileStream(tmpFileName, FileMode.Create))
                {
                    formatter.CompressBAMFile(null, stream);
                    Assert.Fail();
                    File.Delete(tmpFileName);
                }
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.Write("BAM P2 : ArgumentNullException successfully thrown : " + ex.Message);
            }

            //pass null for stream
            try
            {
                string tmpFileName = Path.GetTempFileName();
                var formatter = new BAMFormatter();
                using (var stream = new FileStream(tmpFileName, FileMode.Create))
                {
                    formatter.CompressBAMFile(stream, null);
                    Assert.Fail();
                    File.Delete(tmpFileName);
                }
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.Write("BAM P2 : ArgumentNullException successfully thrown : " + ex.Message);
            }
        }
Ejemplo n.º 17
0
        public void InvalidateWriteAlignedSequence()
        {
            //pass nul for SAMAlignmentHeader
            try
            {
                string tmpFileName = Path.GetTempFileName();
                var formatter = new BAMFormatter();
                using (var stream = new FileStream(tmpFileName, FileMode.Create))
                {
                    formatter.WriteAlignedSequence(null, new SAMAlignedSequence(), stream);
                    Assert.Fail();
                    File.Delete(tmpFileName);
                }
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.Write("BAM P2 : ArgumentNullException successfully thrown : " + ex.Message);
            }

            //pass null for SAMAlignedSequence
            try
            {
                string tmpFileName = Path.GetTempFileName();
                var formatter = new BAMFormatter();
                using (var stream = new FileStream(tmpFileName, FileMode.Create))
                {
                    formatter.WriteAlignedSequence(new SAMAlignmentHeader(), null, stream);
                    Assert.Fail();
                    File.Delete(tmpFileName);
                }
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.Write("BAM P2 : ArgumentNullException successfully thrown : " + ex.Message);
            }

            //pass null for stream
            try
            {
                var formatter = new BAMFormatter();
                formatter.WriteAlignedSequence(new SAMAlignmentHeader(), new SAMAlignedSequence(), null);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.Write("BAM P2 : ArgumentNullException successfully thrown : " + ex.Message);
            }
        }
Ejemplo n.º 18
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);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        ///     Invalidate BAMFormatter.
        /// </summary>
        /// <param name="formatterObj">Bam formatter obj</param>
        /// <param name="seqList">List of sequences</param>
        private static void InvalidateBAmFormatter(BAMFormatter formatterObj,
                                                   IList<ISequenceAlignment> seqList)
        {
            // Invalidate BAM Parser Format(SeqAlignment, BamFileName)
            try
            {
                formatterObj.Format(null as ISequenceAlignment,
                                    Constants.BAMTempFileName);
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
            }

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

            // Invalidate Format(IseqAlignment, BAMFile, IndexFile)
            try
            {
                foreach (ISequenceAlignment seq in seqList)
                {
                    formatterObj.Format(seq, null,
                                        Constants.BAMTempIndexFileForIndexData);
                }
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
            }

            try
            {
                foreach (ISequenceAlignment seq in seqList)
                {
                    formatterObj.Format(seq, Constants.BAMTempFileName,
                                        Constants.BAMTempFileName);
                }
            }
            catch (ArgumentException ex)
            {
                ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully", ex.Message));
            }
        }
Ejemplo n.º 20
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"));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Displays pending data and closes all streams.
        /// 
        /// </summary>
        private void Close()
        {
            if (writer != null)
            {
                writer.Close();
            }

            if (bamCompressedOutStream != null)
            {
                bamCompressedOutStream.Close();
                bamCompressedOutStream = null;
            }

            if (bamUncompressedOutStream != null)
            {
                bamUncompressedOutStream.Close();
                bamUncompressedOutStream = null;
            }

            if (string.IsNullOrEmpty(uncompressedTempfile) && File.Exists(uncompressedTempfile))
            {
                File.Delete(uncompressedTempfile);
            }

            if (string.IsNullOrEmpty(compressedTempfile) && File.Exists(compressedTempfile))
            {
                File.Delete(compressedTempfile);
            }

            bamformatter = null;
            if (bamparser != null)
            {
                bamparser.Dispose();
                bamparser = null;
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        ///     Invalidate BAMFormatter with null values.
        /// </summary>
        /// <param name="formatterObj">Bam formatter obj</param>
        /// <param name="seqList">List of sequences</param>
        private static void InvalidateBAmFormatterWithWithNullValues(BAMFormatter formatterObj,
                                                                     IList<ISequenceAlignment> seqList)
        {
            try
            {
                using (Stream stream = new
                    FileStream(Constants.BAMTempFileName,
                               FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    foreach (ISequenceAlignment seq in seqList)
                    {
                        formatterObj.Format(stream, null, seq);
                    }
                }
            }

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

            // Invalidate Format(IseqAlignment, StreamWriter)
            try
            {
                foreach (ISequenceAlignment seq in seqList)
                {
                    formatterObj.Format(null as Stream, seq);
                }
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.WriteLine(string.Format("BAM Parser P2 : Validated Exception {0} successfully",
                                                       ex.Message));
            }

            try
            {
                using (Stream 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));
            }
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Writes the SAM object to file in SAM/BAM format.
 /// </summary>
 private void PerformFormat()
 {
     if (_isSAM)
     {
         BAMFormatter format = new BAMFormatter();
         try
         {
             format.Format(_sequenceAlignmentMap, OutputFilename);
         }
         catch (Exception ex)
         {
             throw new InvalidOperationException(Resources.WriteBAM + Environment.NewLine + ex.Message);
         }
     }
     else
     {
         SAMFormatter format = new SAMFormatter();
         try
         {
             format.Format(_sequenceAlignmentMap, OutputFilename);
         }
         catch (Exception ex)
         {
             throw new InvalidOperationException(Resources.WriteSAM + Environment.NewLine + ex.Message);
         }
     }
 }
Ejemplo n.º 24
0
        /// <summary>
        /// Sort and merge multiple SAM objects
        /// </summary>
        /// <param name="sortedIndexes">Sorted Indexes of SAM object.</param>
        /// <param name="fstemp">Temporary tream to write alignments.</param>
        /// <param name="formatter">Format aligned sequences in BAM format.</param>
        /// <param name="sequenceAlignmentMaps">List of SAM objects to be merged.</param>
        private void WriteMergeFile(IList<IList<BAMSortedIndex>> sortedIndexes, FileStream fstemp, BAMFormatter formatter, IList<SequenceAlignmentMap> sequenceAlignmentMaps)
        {
            List<SAMAlignedSequence> alignedSeqs = new List<SAMAlignedSequence>();
            int[] sortedIndex = new int[sequenceAlignmentMaps.Count];

            for (int i = 0; i < sortedIndexes.Count; i++)
            {
                BAMSortedIndex bamSortedIndex = sortedIndexes[i].ElementAt(sortedIndex[i]);
                if (bamSortedIndex != null)
                {
                    if (bamSortedIndex.MoveNext())
                    {
                        alignedSeqs.Add(sequenceAlignmentMaps[i].QuerySequences[bamSortedIndex.Current]);
                    }
                    else
                    {
                        alignedSeqs.Add(null);
                    }
                }
                else
                {
                    alignedSeqs.Add(null);
                }
            }

            int smallestIndex = -1;

            do
            {
                for (int index = 0; index < alignedSeqs.Count; index++)
                {
                    if (alignedSeqs[index] != null)
                    {
                        if (smallestIndex == -1)
                        {
                            smallestIndex = index;
                        }
                        else
                        {
                            if (0 < string.Compare(alignedSeqs[smallestIndex].RName, alignedSeqs[index].RName, StringComparison.OrdinalIgnoreCase))
                            {
                                smallestIndex = index;
                            }
                            else if (alignedSeqs[smallestIndex].RName.Equals(alignedSeqs[index].RName))
                            {
                                if (alignedSeqs[smallestIndex].Pos > alignedSeqs[index].Pos)
                                {
                                    smallestIndex = index;
                                }
                            }
                        }
                    }
                }

                if (smallestIndex > -1)
                {
                    SAMAlignedSequence alignSeqTowrite = alignedSeqs[smallestIndex];

                    if (sortedIndexes[smallestIndex].ElementAt(sortedIndex[smallestIndex]).MoveNext())
                    {
                        int nextIndex = sortedIndexes[smallestIndex].ElementAt(sortedIndex[smallestIndex]).Current;
                        alignedSeqs[smallestIndex] = sequenceAlignmentMaps[smallestIndex].QuerySequences[nextIndex];
                    }
                    else
                    {
                        sortedIndex[smallestIndex]++;
                        if (sortedIndex[smallestIndex] < sortedIndexes[smallestIndex].Count &&
                            sortedIndexes[smallestIndex].ElementAt(sortedIndex[smallestIndex]).MoveNext())
                        {
                            int nextIndex = sortedIndexes[smallestIndex].ElementAt(sortedIndex[smallestIndex]).Current;
                            alignedSeqs[smallestIndex] = sequenceAlignmentMaps[smallestIndex].QuerySequences[nextIndex];
                        }
                        else
                        {
                            alignedSeqs[smallestIndex] = null;
                            smallestIndex = -1;
                        }
                    }

                    formatter.WriteAlignedSequence(header, alignSeqTowrite, fstemp);
                }

            } while (!alignedSeqs.All(a => a == null));

        }