/// <summary> /// Select nodes with the wanted information /// </summary> /// <returns>Returns blast hit object, current element</returns> private void SelectHit(ref BlastHit hit, XmlReader r, ref string curElement) { switch (r.NodeType) { case XmlNodeType.Element: curElement = r.Name; switch (curElement) { case "Hit": hit = new BlastHit(); break; } break; case XmlNodeType.Text: if (curElement.StartsWith("Hit_", StringComparison.OrdinalIgnoreCase)) { ParseHit(curElement, r.Value, hit); } else if (curElement.StartsWith("Hsp_", StringComparison.OrdinalIgnoreCase)) { ParseHsp(curElement, r.Value, hit); } break; } }
/// <summary> /// Fills the blast hit object with hit data /// </summary> /// <returns>Returns blast hit object</returns> private void ParseHit(string element, string value, BlastHit curHit) { switch (element) { case "Hit_num": break; case "Hit_id": break; case "Hit_def": curHit.Def = value; break; case "Hit_accession": curHit.Accession = value; break; case "Hit_len": break; case "Hit_hsps": break; default: throw new FormatException($"Unknown element encountered: {element}"); } }
/// <summary> /// Parses the xml returned from blast, only included the bare minimum and takes the first blast result /// Should take the blast result with the best identity (hsp_identity/hsp /// </summary> /// <returns>Returns first blast hit</returns> public BlastHit ParseBlastXml(Stream blastXml) { List <BlastHit> allHits = new List <BlastHit>(); BlastHit hit = null; try { var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore }; StreamReader sr = null; try { sr = new StreamReader(blastXml); using (XmlReader r = XmlReader.Create(sr, settings)) { string curElement = string.Empty; while (r.Read()) { SelectHit(ref hit, r, ref curElement); if (hit?.Accession != null && hit?.Def != null && hit?.AlignmentLenght != 0 && hit?.Identity != 0) { allHits.Add(hit); hit = null; } } } } finally { if (sr != null) { sr.Dispose(); } } } catch (Exception e) { throw e; } allHits.RemoveAll(h => h.Def.Contains("PREDICTED")); var bestHit = CalculateBestHit(allHits); return(bestHit); }
/// <summary> /// Fills the blast hit object with hsp data /// </summary> /// <returns>Returns blast hit object</returns> private void ParseHsp(string element, string value, BlastHit curHit) { switch (element) { case "Hsp_num": break; case "Hsp_bit-score": break; case "Hsp_score": break; case "Hsp_evalue": break; case "Hsp_query-from": break; case "Hsp_query-to": break; case "Hsp_hit-from": break; case "Hsp_hit-to": break; case "Hsp_query-frame": break; case "Hsp_hit-frame": break; case "Hsp_identity": curHit.Identity = int.Parse(value); break; case "Hsp_positive": break; case "Hsp_align-len": curHit.AlignmentLenght = int.Parse(value); break; case "Hsp_density": break; case "Hsp_qseq": break; case "Hsp_hseq": break; case "Hsp_midline": break; case "Hsp_pattern-from": break; case "Hsp_pattern-to": break; case "Hsp_gaps": break; default: throw new FormatException($"Unknown element encountered: {element}"); } }