/// <summary> /// This method expects a single XML document and returns one BlastResult. /// </summary> /// <param name="doc">A Stringbuilder containing the XML document.</param> /// <returns>The BlastResult.</returns> private static BlastResult ParseXML(StringBuilder doc) { BlastResult result = new BlastResult(); try { XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; // don't error when encountering a DTD spec // Setting the XmlResolver to null causes the DTDs specified in the XML // header to be ignored. settings.XmlResolver = null; StringReader sr = null; try { sr = new StringReader(doc.ToString()); using (XmlReader r = XmlReader.Create(sr, settings)) { string curElement = string.Empty; BlastSearchRecord curRecord = new BlastSearchRecord(); Hit curHit = null; Hsp curHsp = null; BlastStatistics curStatistics = null; BlastXmlMetadata curMetadata = null; while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: curElement = r.Name; // ApplicationLog.WriteLine("element: " + curElement); if (curElement == "Hit") { curHit = new Hit(); } else if (curElement == "Hsp") { curHsp = new Hsp(); } else if (curElement == "Statistics") { curStatistics = new BlastStatistics(); } else if (curElement == "BlastOutput") { curMetadata = new BlastXmlMetadata(); } break; case XmlNodeType.Text: // ApplicationLog.WriteLine("text: " + r.Value); if (curElement.StartsWith("BlastOutput_", StringComparison.OrdinalIgnoreCase)) { DoBlastOutput(curElement, r.Value, curMetadata); } else if (curElement.StartsWith("Parameters_", StringComparison.OrdinalIgnoreCase)) { DoParameters(curElement, r.Value, curMetadata); } else if (curElement.StartsWith("Iteration_", StringComparison.OrdinalIgnoreCase)) { DoIteration(curElement, r.Value, curRecord); } else if (curElement.StartsWith("Statistics_", StringComparison.OrdinalIgnoreCase)) { DoStatistics(curElement, r.Value, curStatistics); } else if (curElement.StartsWith("Hit_", StringComparison.OrdinalIgnoreCase)) { DoHit(curElement, r.Value, curHit); } else if (curElement.StartsWith("Hsp_", StringComparison.OrdinalIgnoreCase)) { DoHsp(curElement, r.Value, curHsp); } else { ApplicationLog.WriteLine("BlastXMLParser Unhandled: curElement '{0}'", curElement); } break; case XmlNodeType.XmlDeclaration: // ApplicationLog.WriteLine("declaration: {0}, {1}", r.Name, r.Value); break; case XmlNodeType.ProcessingInstruction: // ApplicationLog.WriteLine("instruction: {0}, {1}", r.Name, r.Value); break; case XmlNodeType.Comment: // ApplicationLog.WriteLine("comment: " + r.Value); break; case XmlNodeType.EndElement: // ApplicationLog.WriteLine("endelement: " + r.Name); if (r.Name == "Iteration") { result.Records.Add(curRecord); curRecord = new BlastSearchRecord(); } else if (r.Name == "Statistics") { curRecord.Statistics = curStatistics; } else if (r.Name == "Hit") { curRecord.Hits.Add(curHit); } else if (r.Name == "Hsp") { curHit.Hsps.Add(curHsp); } else if (r.Name == "BlastOutput") { result.Metadata = curMetadata; } break; } } } } finally { if (sr != null) { sr.Dispose(); } } } catch (Exception e) { ApplicationLog.Exception(e); throw; } return(result); }
private static void DoHsp(string element, string value, Hsp hsp) { switch (element) { case "Hsp_num": // ignore break; case "Hsp_bit-score": hsp.BitScore = double.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_score": hsp.Score = double.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_evalue": hsp.EValue = double.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_query-from": hsp.QueryStart = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_query-to": hsp.QueryEnd = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_hit-from": hsp.HitStart = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_hit-to": hsp.HitEnd = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_query-frame": hsp.QueryFrame = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_hit-frame": hsp.HitFrame = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_identity": hsp.IdentitiesCount = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_positive": hsp.PositivesCount = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_align-len": hsp.AlignmentLength = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_density": hsp.Density = int.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_qseq": hsp.QuerySequence = value; break; case "Hsp_hseq": hsp.HitSequence = value; break; case "Hsp_midline": hsp.Midline = value; break; case "Hsp_pattern-from": hsp.PatternFrom = int.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_pattern-to": hsp.PatternTo = int.Parse(value, CultureInfo.InvariantCulture); break; case "Hsp_gaps": hsp.Gaps = int.Parse(value, CultureInfo.InvariantCulture); break; default: string message = String.Format( CultureInfo.CurrentCulture, Properties.Resource.UnknownElement, "DoHsp", element); Trace.Report(message); throw new FormatException(message); } }