Exemple #1
0
        /// <summary>
        /// Writes specified alignment object to a file. The output is formatted according to the BAM specification.
        /// Also creates index file in the same location that of the specified filename.
        /// If the specified filename is sample.bam then the index file name will be sample.bam.bai.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="filename">BAM file name to write BAM data.</param>
        public void Format(SequenceAlignmentMap sequenceAlignmentMap, string filename)
        {
            if (sequenceAlignmentMap == null)
            {
                throw new ArgumentNullException("sequenceAlignmentMap");
            }

            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException("filename");
            }

            using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
            {
                if (CreateIndexFile)
                {
                    using (BAMIndexFile bamIndexFile = new BAMIndexFile(filename + Resource.BAM_INDEXFILEEXTENSION, FileMode.Create, FileAccess.Write))
                    {
                        WriteSequenceAlignment(sequenceAlignmentMap, fs, bamIndexFile);
                    }
                }
                else
                {
                    WriteSequenceAlignment(sequenceAlignmentMap, fs, null);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Writes specified alignment object to a file.
        /// The output is formatted according to the BAM specification.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="bamFilename">BAM filename to write BAM data.</param>
        /// <param name="indexFilename">BAM index filename to write index data.</param>
        public void Format(SequenceAlignmentMap sequenceAlignmentMap, string bamFilename, string indexFilename)
        {
            if (sequenceAlignmentMap == null)
            {
                throw new ArgumentNullException("sequenceAlignmentMap");
            }

            if (string.IsNullOrWhiteSpace(bamFilename))
            {
                throw new ArgumentNullException("bamFilename");
            }

            if (string.IsNullOrWhiteSpace(indexFilename))
            {
                throw new ArgumentNullException("indexFilename");
            }

            if (bamFilename.Equals(indexFilename))
            {
                throw new ArgumentException(Resource.BAM_BAMFileNIndexFileContbeSame);
            }

            using (FileStream fs = new FileStream(bamFilename, FileMode.Create, FileAccess.ReadWrite))
            {
                using (BAMIndexFile bamIndexFile = new BAMIndexFile(indexFilename, FileMode.Create, FileAccess.Write))
                {
                    WriteSequenceAlignment(sequenceAlignmentMap, fs, bamIndexFile);
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Creates index file for the specified bam file.
 /// </summary>
 /// <param name="bamFileName">BAM file name.</param>
 /// <param name="indexFileName">BAM index file name to write.</param>
 public static void CreateBAMIndexFile(string bamFileName, string indexFileName)
 {
     using (Stream bamStream = new FileStream(bamFileName, FileMode.Open, FileAccess.Read))
     {
         using (BAMIndexFile bamIndexFile = new BAMIndexFile(indexFileName, FileMode.Create, FileAccess.Write))
         {
             CreateBAMIndexFile(bamStream, bamIndexFile);
         }
     }
 }
Exemple #4
0
        /// <summary>
        /// Creates BAMIndex object from the specified BAM file and writes to specified BAMIndex file.
        /// </summary>
        /// <param name="compressedBAMStream"></param>
        /// <param name="indexFile"></param>
        private static void CreateBAMIndexFile(Stream compressedBAMStream, BAMIndexFile indexFile)
        {
            BAMParser parser = new BAMParser();
            BAMIndex  bamIndex;

            try
            {
                bamIndex = parser.GetIndexFromBAMFile(compressedBAMStream);
            }
            finally
            {
                parser.Dispose();
            }

            parser = null;

            indexFile.Write(bamIndex);
        }
Exemple #5
0
        /// <summary>
        /// Writes specified alignment object to a stream.
        /// The output is formatted according to the BAM specification.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="writer">Stream to write BAM data.</param>
        /// <param name="indexWriter">BAMIndexFile to write index data.</param>
        public void Format(SequenceAlignmentMap sequenceAlignmentMap, Stream writer, BAMIndexFile indexWriter)
        {
            if (sequenceAlignmentMap == null)
            {
                throw new ArgumentNullException("sequenceAlignmentMap");
            }

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (indexWriter == null)
            {
                throw new ArgumentNullException("indexWriter");
            }

            WriteSequenceAlignment(sequenceAlignmentMap, writer, indexWriter);
        }
Exemple #6
0
        /// <summary>
        /// Writes sequence alignment to specified stream.
        /// </summary>
        /// <param name="sequenceAlignment">Sequence alignment object</param>
        /// <param name="writer">Stream to write.</param>
        /// <param name="indexFile">BAMIndex file.</param>
        private void WriteSequenceAlignment(ISequenceAlignment sequenceAlignment, Stream writer, BAMIndexFile indexFile)
        {
            // validate sequenceAlignment.
            SequenceAlignmentMap sequenceAlignmentMap = ValidateAlignment(sequenceAlignment);

            string tempFilename = Path.GetTempFileName();

            // write bam struct to temp file.
            using (FileStream fstemp = new FileStream(tempFilename, FileMode.Create, FileAccess.ReadWrite))
            {
                WriteUncompressed(sequenceAlignmentMap, fstemp, CreateSortedBAMFile);

                fstemp.Seek(0, SeekOrigin.Begin);

                // Compress and write to the specified stream.
                CompressBAMFile(fstemp, writer);
            }

            // delete the temp file.
            File.Delete(tempFilename);

            // if index file need to be created.
            if (indexFile != null)
            {
                writer.Seek(0, SeekOrigin.Begin);
                CreateBAMIndexFile(writer, indexFile);
            }
        }