示例#1
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);
                }
            }
        }
示例#2
0
            internal LinearIndexArrayMaker(int refSeqLength)
            {
                int size;

                if (refSeqLength <= 0)
                {
                    size = BAMIndexFile.MAX_LINERINDEX_ARRAY_SIZE;
                }
                else
                {
                    size = BAMIndexFile.LargestBinPossibleForSequenceLength(refSeqLength);
                }
                //TODO: Plus one now because weird things could happen with alignment length such that the bin is one higher
                //never seen this, but not sure it can't happen.
                offSetArray = new FileOffset[size + 1];
            }
示例#3
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 CreateIndexFile(Stream compressedBAMStream, BAMIndexFile indexFile)
        {
            BAMParser parser = new BAMParser();
            BAMIndex  bamIndex;

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

            parser = null;

            indexFile.Write(bamIndex);
        }
示例#4
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);
        }
示例#5
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))
            {
                using (BAMIndexFile bamIndexFile = new BAMIndexFile(filename + Resource.BAM_INDEXFILEEXTENSION, FileMode.Create, FileAccess.Write))
                {
                    WriteSequenceAlignment(sequenceAlignmentMap, fs, bamIndexFile);
                }
            }
        }
        public SAMAlignmentHeader GetFileHeader()
        {
            SAMAlignmentHeader header;

            using (FileStream bamStream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string bamIndexFileName = getBAMIndexFileName(_fileName);
                using (BAMIndexFile bamIndexFile = new BAMIndexFile(bamIndexFileName, FileMode.Open, FileAccess.Read))
                {
                    readStream = bamStream;
                    if (readStream == null || readStream.Length == 0)
                    {
                        throw new FileFormatException(Properties.Resource.BAM_InvalidBAMFile);
                    }
                    ValidateReader();
                    header = GetHeader();
                }
            }
            return(header);
        }
 public IEnumerable <CompactSAMSequence> ParseRangeAsEnumerableSequences(string fileName, string refSeqName, int start = 0, int end = Int32.MaxValue)
 {
     if (refSeqName == null)
     {
         throw new ArgumentNullException("refSeqName");
     }
     using (FileStream bamStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         string bamIndexFileName = getBAMIndexFileName(fileName);
         using (BAMIndexFile bamIndexFile = new BAMIndexFile(bamIndexFileName, FileMode.Open, FileAccess.Read))
         {
             readStream = bamStream;
             if (readStream == null || readStream.Length == 0)
             {
                 throw new FileFormatException(Properties.Resource.BAM_InvalidBAMFile);
             }
             ValidateReader();
             SAMAlignmentHeader header = GetHeader();
             // verify whether there is any reads related to chromosome.
             int refSeqIndex = refSeqNames.IndexOf(refSeqName);
             if (refSeqIndex < 0)
             {
                 string message = string.Format(CultureInfo.InvariantCulture, Properties.Resource.BAM_RefSeqNotFound, refSeqName);
                 throw new ArgumentException(message, "refSeqName");
             }
             BAMIndex            bamIndexInfo = bamIndexFile.Read();
             BAMReferenceIndexes refIndex     = bamIndexInfo.RefIndexes[refSeqIndex];
             IList <Chunk>       chunks       = GetChunks(refIndex, start, end);
             foreach (var s in EnumerateAlignedSequences(chunks))
             {
                 if (s != null && (s.RName == "*" || (s.Pos >= (start - 1) && s.RefEndPos < end)))
                 {
                     yield return(s);
                 }
             }
             readStream = null;
         }
     }
 }
示例#8
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);

                fstemp.Seek(0, SeekOrigin.Begin);

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

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

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