Beispiel #1
0
        /// <summary>
        /// Writes specified sequence alignment to stream.
        /// The output is formatted according to the BAM structure.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="writer">Stream to write.</param>
        /// <param name="createSortedFile">If this flag is true output file will be sorted.</param>
        private void WriteUncompressed(SequenceAlignmentMap sequenceAlignmentMap, Stream writer, bool createSortedFile)
        {
            SAMAlignmentHeader header = sequenceAlignmentMap.Header;

            if (createSortedFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates)
            {
                header            = GetHeaderWithSortedSQFields(header, true);
                this.refSequences = header.GetReferenceSequenceRanges();
            }

            if (this.refSequences == null)
            {
                this.refSequences = header.GetReferenceSequenceRanges();
            }

            WriteHeader(header, writer);
            writer.Flush();
            if (createSortedFile)
            {
                WriteUncompressedSortedBAM(sequenceAlignmentMap, writer);
            }
            else
            {
                foreach (SAMAlignedSequence seq in sequenceAlignmentMap.QuerySequences)
                {
                    SAMAlignedSequence alignedSeq = seq;
                    this.ValidateSQHeader(alignedSeq.RName);
                    this.WriteAlignedSequence(alignedSeq, writer);
                    writer.Flush();
                }
            }

            writer.Flush();
        }
Beispiel #2
0
        /// <summary>
        /// Writes BAM header to the specified stream in BAM format.
        /// </summary>
        /// <param name="header">SAMAlignmentHeader object</param>
        /// <param name="writer">Stream to write.</param>
        public void WriteHeader(SAMAlignmentHeader header, Stream writer)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

            string samHeader;

            if (_refSequences == null)
            {
                _refSequences = SortSequenceRanges(header.GetReferenceSequenceRanges());
            }

            using (StringWriter strwriter = new StringWriter(CultureInfo.InvariantCulture))
            {
                SAMFormatter.WriteHeader(header, strwriter);
                samHeader = strwriter.ToString();
            }

            int samHeaderLen = samHeader.Length;

            byte[] bytes          = System.Text.ASCIIEncoding.ASCII.GetBytes(samHeader);
            byte[] bamMagicNumber = new byte[4] {
                66, 65, 77, 1
            };

            // write BAM magic number
            writer.Write(bamMagicNumber, 0, 4);

            // Length of the header text
            writer.Write(Helper.GetLittleEndianByteArray(samHeaderLen), 0, 4);

            //Plain header text in SAM
            writer.Write(bytes, 0, bytes.Length);
            // number of reference sequences
            writer.Write(Helper.GetLittleEndianByteArray(_refSequences.Count), 0, 4);

            for (int i = 0; i < _refSequences.Count; i++)
            {
                int len = _refSequences[i].ID.Length;

                byte[] array = System.Text.ASCIIEncoding.ASCII.GetBytes(_refSequences[i].ID);
                writer.Write(Helper.GetLittleEndianByteArray(len + 1), 0, 4);
                writer.Write(array, 0, len);
                writer.WriteByte((byte)'\0');
                writer.Write(Helper.GetLittleEndianByteArray((int)_refSequences[i].End), 0, 4);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Writes BAM header to the specified stream in BAM format.
        /// </summary>
        /// <param name="header">SAMAlignmentHeader object</param>
        /// <param name="writer">Stream to write.</param>
        public void WriteHeader(SAMAlignmentHeader header, Stream writer)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

            string samHeader;

            if (this.refSequences == null)
            {
                this.refSequences = header.GetReferenceSequenceRanges();
            }

            using (StringWriter strwriter = new StringWriter(CultureInfo.InvariantCulture))
            {
                SAMFormatter.WriteHeader(strwriter, header);
                samHeader = strwriter.ToString();
            }

            int samHeaderLen = samHeader.Length;

            byte[] bytes          = Encoding.UTF8.GetBytes(samHeader);
            byte[] bamMagicNumber = { 66, 65, 77, 1 };

            // write BAM magic number
            writer.Write(bamMagicNumber, 0, 4);

            // Length of the header text
            writer.Write(Helper.GetLittleEndianByteArray(samHeaderLen), 0, 4);

            //Plain header text in SAM
            writer.Write(bytes, 0, bytes.Length);
            // number of reference sequences
            writer.Write(Helper.GetLittleEndianByteArray(this.refSequences.Count), 0, 4);

            foreach (SequenceRange range in this.refSequences)
            {
                int len = range.ID.Length;

                byte[] array = Encoding.UTF8.GetBytes(range.ID);
                writer.Write(Helper.GetLittleEndianByteArray(len + 1), 0, 4);
                writer.Write(array, 0, len);
                writer.WriteByte((byte)'\0');
                writer.Write(Helper.GetLittleEndianByteArray((int)range.End), 0, 4);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Writes SAMAlignedSequence to specified stream.
        /// </summary>
        /// <param name="header">Header from SAM object.</param>
        /// <param name="alignedSeq">SAMAlignedSequence object.</param>
        /// <param name="writer">Stream to write.</param>
        public void WriteAlignedSequence(SAMAlignmentHeader header, SAMAlignedSequence alignedSeq, Stream writer)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

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

            _refSequences = SortSequenceRanges(header.GetReferenceSequenceRanges());
            WriteAlignedSequence(alignedSeq, writer);
        }