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); }
/// <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); }
// compares chromosome name in the specified fields. private int CompareByChromosomeName(SAMRecordField field1, SAMRecordField field2) { string chr1 = field1.Tags.FirstOrDefault(Tag => Tag.Tag.Equals("SN")).Value; string chr2 = field2.Tags.FirstOrDefault(Tag => Tag.Tag.Equals("SN")).Value; return string.Compare(chr1, chr2, StringComparison.Ordinal); }
/// <summary> /// Add a sequence to the filtered output file header /// </summary> private void AddToHeader(SAMAlignedSequence seq) { newHeader.ReferenceSequences.Add(new ReferenceSequenceInfo(seq.RName, GetSequence(seq).Length)); // for each good cluster SAMRecordField sq = new SAMRecordField("SQ"); sq.Tags.Add(new SAMRecordFieldTag("SN", seq.RName)); sq.Tags.Add(new SAMRecordFieldTag("LN", GetSequence(seq).Length.ToString(ci))); newHeader.RecordFields.Add(sq); }