/// <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); } }
/// <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); } }
public void InvalidateSAMWriteTextWriter() { SAMAlignmentHeader header = new SAMAlignmentHeader(); try { SAMFormatter.WriteHeader(null, header); Assert.Fail(); } catch (ArgumentNullException) { ApplicationLog.WriteLine( "SAM Formatter P2 : Successfully validated the exception"); } }
/// <summary> /// Writes the header to output stream /// </summary> /// <param name="header"></param> private void WriteHeader(SAMAlignmentHeader header) { if (!Header && !HeaderOnly) { return; } if (UnCompressedBAM || BAMOutput) { // Incase of compressed bamoutput uncompressed file will be compressed before sending it to output stream. bamformatter.WriteHeader(header, bamUncompressedOutStream); } else { SAMFormatter.WriteHeader(header, writer); } }