Example #1
0
        public void ValidateSAMParserHeader()
        {
            string filePath = utilityObj.xmlUtil.GetTextValue(
                Constants.SmallSAMFileNode, Constants.FilePathNode);

            string[] expectedHeaderTagValues = utilityObj.xmlUtil.GetTextValue(
                Constants.SmallSAMFileNode, Constants.RecordTagValuesNode).Split(',');
            string[] expectedHeaderTagKeys = utilityObj.xmlUtil.GetTextValue(
                Constants.SmallSAMFileNode, Constants.RecordTagKeysNode).Split(',');
            string[] expectedHeaderTypes = utilityObj.xmlUtil.GetTextValue(
                Constants.SmallSAMFileNode, Constants.HeaderTyepsNodes).Split(',');
            SAMAlignmentHeader aligntHeader =
                SAMParser.ParseSAMHeader(filePath);

            int tagKeysCount   = 0;
            int tagValuesCount = 0;

            for (int index = 0; index < aligntHeader.RecordFields.Count; index++)
            {
                Assert.AreEqual(expectedHeaderTypes[index].Replace("/", ""),
                                aligntHeader.RecordFields[index].Typecode.ToString((IFormatProvider)null).Replace("/", ""));
                for (int tags = 0; tags < aligntHeader.RecordFields[index].Tags.Count; tags++)
                {
                    Assert.AreEqual(
                        expectedHeaderTagKeys[tagKeysCount].Replace("/", ""),
                        aligntHeader.RecordFields[index].Tags[tags].Tag.ToString((IFormatProvider)null).Replace("/", ""));
                    Assert.AreEqual(
                        expectedHeaderTagValues[tagValuesCount].Replace("/", ""),
                        aligntHeader.RecordFields[index].Tags[tags].Value.ToString((IFormatProvider)null).Replace("/", "").Replace("\r", "").Replace("\n", ""));
                    tagKeysCount++;
                    tagValuesCount++;
                }
            }
        }
        /// <summary>
        ///   Parses the BAM file and returns the Header.
        /// </summary>
        private SAMAlignmentHeader GetHeader()
        {
            var header = new SAMAlignmentHeader();

            RefSeqNames    = new RegexValidatedStringList(SAMAlignedSequenceHeader.RNameRegxExprPattern);
            _refSeqLengths = new List <int>();

            ReadStream.Seek(0, SeekOrigin.Begin);
            _deCompressedStream = null;
            var array = new byte[8];

            ReadUnCompressedData(array, 0, 8);
            var lText         = Helper.GetInt32(array, 4);
            var samHeaderData = new byte[lText];

            if (lText != 0)
            {
                ReadUnCompressedData(samHeaderData, 0, lText);
            }

            ReadUnCompressedData(array, 0, 4);
            var noofRefSeqs = Helper.GetInt32(array, 0);

            for (var i = 0; i < noofRefSeqs; i++)
            {
                ReadUnCompressedData(array, 0, 4);
                var len     = Helper.GetInt32(array, 0);
                var refName = new byte[len];
                ReadUnCompressedData(refName, 0, len);
                ReadUnCompressedData(array, 0, 4);
                var refLen = Helper.GetInt32(array, 0);
                RefSeqNames.Add(Encoding.ASCII.GetString(refName, 0, refName.Length - 1));
                _refSeqLengths.Add(refLen);
            }

            if (samHeaderData.Length != 0)
            {
                var str = Encoding.ASCII.GetString(samHeaderData);
                using (var reader = new StringReader(str))
                {
                    header = SAMParser.ParseSAMHeader(reader);
                }
            }

            header.ReferenceSequences.Clear();

            for (var i = 0; i < RefSeqNames.Count; i++)
            {
                var refname = RefSeqNames[i];
                var length  = _refSeqLengths[i];
                header.ReferenceSequences.Add(new ReferenceSequenceInfo(refname, length));
            }

            return(header);
        }
Example #3
0
        /// <summary>
        /// Genaral method to Invalidate ISequence Alignment
        /// <param name="method">enum type to execute different overload</param>
        /// </summary>
        private static void ValidateISeqAlignParser(ParseOrFormatTypes method)
        {
            ISequenceAlignmentParser parser = new SAMParser();

            try
            {
                switch (method)
                {
                case ParseOrFormatTypes.ParseOrFormatText:
                    parser.Parse(null as TextReader);
                    break;

                case ParseOrFormatTypes.ParseOrFormatTextWithFlag:
                    parser.Parse(null as TextReader, true);
                    break;

                case ParseOrFormatTypes.ParseOrFormatFileName:
                    parser.Parse(null as string);
                    break;

                case ParseOrFormatTypes.ParseOrFormatFileNameWithFlag:
                    parser.Parse(null as string, true);
                    break;

                case ParseOrFormatTypes.ParseOneOrFormatHeader:
                    SAMParser.ParseSAMHeader(null as TextReader);
                    break;

                case ParseOrFormatTypes.ParseOneOrFormatHeaderFn:
                    SAMParser.ParseSAMHeader(null as string);
                    break;

                default:
                    break;
                }

                Assert.Fail();
            }
            catch (ArgumentNullException)
            {
                ApplicationLog.WriteLine(
                    "SAM Parser P2 : Successfully validated the exception");
                Console.WriteLine(
                    "SAM Parser P2 : Successfully validated the exception");
            }
        }
Example #4
0
        public void InvalidateSAMParseHeaderBioReader()
        {
            string filePath = utilityObj.xmlUtil.GetTextValue(
                Constants.InvalidSamBioReaderNode,
                Constants.FilePathNode).TestDir();

            try
            {
                using (TextReader reader = new StreamReader(filePath))
                {
                    SAMParser.ParseSAMHeader(reader);
                }

                Assert.Fail();
            }
            catch (FormatException)
            {
                ApplicationLog.WriteLine(
                    "SAM Parser P2 : Successfully validated the exception");
            }
        }
Example #5
0
        /// <summary>
        /// Extract/print all or sub alignments in SAM or BAM format.
        /// By default, this command assumes the file on the command line is in
        /// BAM format and it prints the alignments in SAM.
        /// SAMUtil.exe view in.bam
        /// </summary>
        public void ViewResult()
        {
            try
            {
                if (string.IsNullOrEmpty(InputFilePath))
                {
                    throw new InvalidOperationException("Input File Not specified");
                }

                if (!string.IsNullOrEmpty(Region))
                {
                    StringToRegionConverter();
                }

                Initialize();
                SAMAlignmentHeader header = null;

                if (!SAMInput)
                {
                    Stream stream = new FileStream(InputFilePath, FileMode.Open, FileAccess.Read);
                    try
                    {
                        header = bamparser.GetHeader(stream);
                    }
                    catch
                    {
                        throw new InvalidOperationException(Resources.InvalidBAMFile);
                    }


                    WriteHeader(header);

                    if (!HeaderOnly)
                    {
                        if (!string.IsNullOrEmpty(Library))
                        {
                            rgRecFields = header.RecordFields.Where(R => R.Typecode.ToUpper().Equals("RG")).ToList();
                        }

                        foreach (SAMAlignedSequence alignedSequence in GetAlignedSequence(stream))
                        {
                            WriteAlignedSequence(header, alignedSequence);
                        }
                    }
                }
                else
                {
                    try
                    {
                        header = SAMParser.ParseSAMHeader(InputFilePath);
                    }
                    catch
                    {
                        throw new InvalidOperationException(Resources.InvalidSAMFile);
                    }

                    if (header == null)
                    {
                        throw new InvalidOperationException("SAM file doesn't contian header");
                    }

                    WriteHeader(header);

                    if (!HeaderOnly)
                    {
                        if (!string.IsNullOrEmpty(Library))
                        {
                            rgRecFields = header.RecordFields.Where(R => R.Typecode.ToUpper().Equals("RG")).ToList();
                        }

                        using (StreamReader textReader = new StreamReader(InputFilePath))
                        {
                            foreach (SAMAlignedSequence alignedSeq in GetAlignedSequence(textReader))
                            {
                                WriteAlignedSequence(header, alignedSeq);
                            }
                        }
                    }

                    if (UnCompressedBAM)
                    {
                        bamUncompressedOutStream.Flush();
                        if (writer != null)
                        {
                            DisplayBAMContent(bamUncompressedOutStream);
                        }
                    }

                    if (BAMOutput && !UnCompressedBAM)
                    {
                        bamUncompressedOutStream.Flush();
                        bamUncompressedOutStream.Seek(0, SeekOrigin.Begin);
                        bamformatter.CompressBAMFile(bamUncompressedOutStream, bamCompressedOutStream);
                        bamCompressedOutStream.Flush();
                        if (writer != null)
                        {
                            DisplayBAMContent(bamCompressedOutStream);
                        }
                    }
                }
            }
            finally
            {
                Close();
            }
        }
Example #6
0
        /// <summary>
        /// Converts the input SAM to BAM file format.
        /// </summary>
        private void ConvertFromSAMTOBAM()
        {
            SAMAlignmentHeader header = null;

            try
            {
                header = SAMParser.ParseSAMHeader(InputFilePath);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(Resources.InvalidSAMFile, ex);
            }

            if (header == null)
            {
                Console.Error.WriteLine("Warning: SAM file doesn't contian header");
            }

            if (HeaderOnly)
            {
                if (header != null)
                {
                    WriteHeader(header);
                }
            }
            else
            {
                if (header == null)
                {
                    header = new SAMAlignmentHeader();
                }

                if (!string.IsNullOrEmpty(Library))
                {
                    rgRecFields = header.RecordFields.Where(R => R.Typecode.ToUpper().Equals("RG")).ToList();
                }

                if (!string.IsNullOrEmpty(ReferenceNamesAndLength))
                {
                    this.UpdateReferenceInformationFromFile(header);
                }
                else if (header.ReferenceSequences.Count == 0)
                {
                    this.UpdateReferenceInformationFromReads(header);
                }

                WriteHeader(header);
                using (StreamReader textReader = new StreamReader(InputFilePath))
                {
                    foreach (SAMAlignedSequence alignedSeq in GetAlignedSequence(textReader))
                    {
                        WriteAlignedSequence(header, alignedSeq);
                    }
                }
            }

            if (UnCompressedBAM)
            {
                bamUncompressedOutStream.Flush();
                if (writer != null)
                {
                    DisplayBAMContent(bamUncompressedOutStream);
                }
            }

            if (BAMOutput && !UnCompressedBAM)
            {
                bamUncompressedOutStream.Flush();
                bamUncompressedOutStream.Seek(0, SeekOrigin.Begin);
                bamformatter.CompressBAMFile(bamUncompressedOutStream, bamCompressedOutStream);
                bamCompressedOutStream.Flush();
                if (writer != null)
                {
                    DisplayBAMContent(bamCompressedOutStream);
                }
            }
        }