Пример #1
0
        /// <summary>
        /// Writes specified SAMAlignedHeader to specified text writer.
        /// </summary>
        /// <param name="header">Header to write.</param>
        /// <param name="writer">Text writer.</param>
        public static void WriteHeader(SAMAlignmentHeader header, TextWriter writer)
        {
            if (header == null)
            {
                return;
            }

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

            string message = header.IsValid();

            if (!string.IsNullOrEmpty(message))
            {
                throw new ArgumentException(message);
            }

            StringBuilder headerLine = null;

            for (int i = 0; i < header.RecordFields.Count; i++)
            {
                headerLine = new StringBuilder();
                headerLine.Append("@");
                headerLine.Append(header.RecordFields[i].Typecode);
                for (int j = 0; j < header.RecordFields[i].Tags.Count; j++)
                {
                    headerLine.Append("\t");
                    headerLine.Append(header.RecordFields[i].Tags[j].Tag);
                    headerLine.Append(":");
                    headerLine.Append(header.RecordFields[i].Tags[j].Value);
                }

                writer.WriteLine(headerLine.ToString());
            }

            foreach (string comment in header.Comments)
            {
                headerLine = new StringBuilder();
                headerLine.Append("@CO");
                headerLine.Append("\t");
                headerLine.Append(comment);
                writer.WriteLine(headerLine.ToString());
            }

            writer.Flush();
        }
Пример #2
0
        private static SAMAlignmentHeader ParseSamHeader(List <string> headerStrings)
        {
            SAMAlignmentHeader samHeader = new SAMAlignmentHeader();

            foreach (string headerString in headerStrings)
            {
                string[] tokens         = headerString.Split(tabDelim, StringSplitOptions.RemoveEmptyEntries);
                string   recordTypecode = tokens[0].Substring(1);
                // Validate the header format.
                ValidateHeaderLineFormat(headerString);

                SAMRecordField headerLine = null;
                if (string.Compare(recordTypecode, "CO", StringComparison.OrdinalIgnoreCase) != 0)
                {
                    List <string> tags = new List <string>();
                    headerLine = new SAMRecordField(recordTypecode);
                    for (int i = 1; i < tokens.Length; i++)
                    {
                        string tagToken = tokens[i];
                        string tagName  = tagToken.Substring(0, 2);
                        tags.Add(tagName);
                        headerLine.Tags.Add(new SAMRecordFieldTag(tagName, tagToken.Substring(3)));
                    }

                    samHeader.RecordFields.Add(headerLine);
                }
                else
                {
                    samHeader.Comments.Add(headerString.Substring(4));
                }
            }

            IList <ReferenceSequenceInfo> referenceSeqsInfo = samHeader.GetReferenceSequencesInfoFromSQHeader();

            foreach (var item in referenceSeqsInfo)
            {
                samHeader.ReferenceSequences.Add(item);
            }

            string message = samHeader.IsValid();

            if (!string.IsNullOrEmpty(message))
            {
                throw new FormatException(message);
            }

            return(samHeader);
        }
Пример #3
0
        /// <summary>
        /// Writes specified SAMAlignedHeader to specified text writer.
        /// </summary>
        /// <param name="writer">Text Writer</param>
        /// <param name="header">Header to write.</param>
        public static void WriteHeader(TextWriter writer, SAMAlignmentHeader header)
        {
            if (header == null)
            {
                return;
            }

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

            string message = header.IsValid();

            if (!string.IsNullOrEmpty(message))
            {
                throw new ArgumentException(message);
            }

            StringBuilder headerLine = null;

            foreach (SAMRecordField record in header.RecordFields)
            {
                headerLine = new StringBuilder();
                headerLine.Append("@");
                headerLine.Append(record.Typecode);
                foreach (SAMRecordFieldTag tag in record.Tags)
                {
                    headerLine.Append("\t");
                    headerLine.Append(tag.Tag);
                    headerLine.Append(":");
                    headerLine.Append(tag.Value);
                }

                writer.WriteLine(headerLine.ToString());
            }

            foreach (string comment in header.Comments)
            {
                headerLine = new StringBuilder();
                headerLine.Append("@CO");
                headerLine.Append("\t");
                headerLine.Append(comment);
                writer.WriteLine(headerLine.ToString());
            }

            writer.Flush();
        }
Пример #4
0
        /// <summary>
        /// Parses SAM alignment header from specified BioTextReader.
        /// </summary>
        /// <param name="bioReader">Bio text reader.</param>
        private static SAMAlignmentHeader ParserSAMHeader(BioTextReader bioReader)
        {
            SAMAlignmentHeader samHeader = new SAMAlignmentHeader();

            if (bioReader.HasLines && bioReader.Line.StartsWith(@"@", StringComparison.OrdinalIgnoreCase))
            {
                while (bioReader.HasLines && bioReader.Line.StartsWith(@"@", StringComparison.OrdinalIgnoreCase))
                {
                    string[] tokens         = bioReader.Line.Split(tabDelim, StringSplitOptions.RemoveEmptyEntries);
                    string   recordTypecode = tokens[0].Substring(1);
                    // Validate the header format.
                    ValidateHeaderLineFormat(bioReader.Line);

                    SAMRecordField headerLine = null;
                    if (string.Compare(recordTypecode, "CO", StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        List <string> tags = new List <string>();
                        headerLine = new SAMRecordField(recordTypecode);
                        for (int i = 1; i < tokens.Length; i++)
                        {
                            string tagToken = tokens[i];
                            string tagName  = tagToken.Substring(0, 2);
                            tags.Add(tagName);
                            headerLine.Tags.Add(new SAMRecordFieldTag(tagName, tagToken.Substring(3)));
                        }

                        samHeader.RecordFields.Add(headerLine);
                    }
                    else
                    {
                        samHeader.Comments.Add(bioReader.Line.Substring(4));
                    }

                    bioReader.GoToNextLine();
                }

                string message = samHeader.IsValid();
                if (!string.IsNullOrEmpty(message))
                {
                    throw new FormatException(message);
                }
            }

            return(samHeader);
        }
Пример #5
0
 // Validates alignment header.
 private static void ValidateAlignmentHeader(SAMAlignmentHeader header)
 {
     string message = header.IsValid();
     if (!string.IsNullOrEmpty(message))
     {
         throw new ArgumentException(message);
     }
 }
Пример #6
0
        /// <summary>
        /// Writes specified SAMAlignedHeader to specified text writer.
        /// </summary>
        /// <param name="writer">Text Writer</param>
        /// <param name="header">Header to write.</param>
        public static void WriteHeader(TextWriter writer, SAMAlignmentHeader header)
        {
            if (header == null)
            {
                return;
            }

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

            string message = header.IsValid();
            if (!string.IsNullOrEmpty(message))
            {
                throw new ArgumentException(message);
            }

            StringBuilder headerLine = null;
            foreach (SAMRecordField record in header.RecordFields)
            {
                headerLine = new StringBuilder();
                headerLine.Append("@");
                headerLine.Append(record.Typecode);
                foreach (SAMRecordFieldTag tag in record.Tags)
                {
                    headerLine.Append("\t");
                    headerLine.Append(tag.Tag);
                    headerLine.Append(":");
                    headerLine.Append(tag.Value);
                }

                writer.WriteLine(headerLine.ToString());
            }

            foreach (string comment in header.Comments)
            {
                headerLine = new StringBuilder();
                headerLine.Append("@CO");
                headerLine.Append("\t");
                headerLine.Append(comment);
                writer.WriteLine(headerLine.ToString());
            }

            writer.Flush();
        }