public void ParseBlastValidation() { var blastFilePath = @"TestUtils\BlastRecords.xml"; var expectedGapCost = "11"; var expectedParameterMatrix = "BLOSUM62"; var expectedResultCount = "9"; var expectedDatabselength = "2974038"; var expectedHitsCount = "1"; var expectedAccession = "NP_010143"; var expectedAlignmentLength = "142"; var expectedBitScore = "33.113"; var expectedHitSequence = "KKIRKRCLAFPISQQIEMKTNIKNAGKLDNNNSPIVQDDS-----HNIIDVDGFLENQIQSNDHMIAADKGSGSLVNIIDTAPLTSTEFKPVEHPPVNY---------------VDLGNGLGLNTYNRGPPSIISDSTDEFY"; // Parse a Blast xml file. BlastXmlParser parser = new BlastXmlParser(); IList <BlastResult> blastResults = parser.Parse(blastFilePath); // Validate Meta data BlastXmlMetadata meta = blastResults[0].Metadata; Assert.AreEqual(meta.ParameterGapOpen.ToString((IFormatProvider)null), expectedGapCost); Assert.AreEqual(meta.ParameterMatrix.ToString((IFormatProvider)null), expectedParameterMatrix); // Validate blast records. BlastSearchRecord record = blastResults[4].Records[0]; Assert.AreEqual(expectedResultCount, blastResults.Count.ToString((IFormatProvider)null)); Assert.AreEqual(expectedDatabselength, record.Statistics.DatabaseLength.ToString((IFormatProvider)null)); Assert.AreEqual(expectedHitsCount, record.Hits.Count.ToString((IFormatProvider)null)); Assert.AreEqual(expectedAccession, record.Hits[0].Accession.ToString((IFormatProvider)null)); Assert.AreEqual(expectedHitsCount, record.Hits[0].Hsps.Count.ToString((IFormatProvider)null)); // Validate bit score. Hsp highScoreSgment = record.Hits[0].Hsps[0]; Assert.AreEqual(expectedAlignmentLength, highScoreSgment.AlignmentLength.ToString( (IFormatProvider)null)); Assert.AreEqual(expectedBitScore, highScoreSgment.BitScore.ToString(( IFormatProvider)null)); Assert.AreEqual(expectedHitSequence, highScoreSgment.HitSequence.ToString( (IFormatProvider)null)); }
public void InValidateHspMetadataGetObjectData() { SerializationInfo info = null; StreamingContext context = new StreamingContext(StreamingContextStates.All); Hsp hspObj = new Hsp(); try { hspObj.GetObjectData(info, context); Assert.Fail(); } catch (ArgumentNullException) { Console.WriteLine("Ncbi Blast P2 : InValidated the GetObjectData() method of Hsp successfully."); ApplicationLog.WriteLine("Ncbi Blast P2 : InValidated the GetObjectData() method of Hsp successfully."); } }
public static List <GenBankItem> filter(IList <BlastResult> blastResults, int maxHits = 1, double maxEvalue = 10, double minPercentId = 0.0, double minQueryCoverage = 0.0) { List <GenBankItem> giList = new List <GenBankItem>(); foreach (BlastResult blastResult in blastResults) { foreach (BlastSearchRecord record in blastResult.Records) { int numHits = 0; for (int i = 0; i < record.Hits.Count; i++) { Hit blastHit = record.Hits[i]; long gi = Convert.ToInt64(blastHit.Id.Split('|')[1]); for (int j = 0; j < blastHit.Hsps.Count; j++) { Hsp blastHsp = blastHit.Hsps[j]; double percentId = (blastHsp.IdentitiesCount / (double)blastHsp.AlignmentLength) * 100; double queryCoverage = ((blastHsp.QueryEnd - blastHsp.QueryStart + 1) / (double)record.IterationQueryLength) * 100; //double queryCoverage = (blastHsp.AlignmentLength / (double)queryLen) * 100; if ((percentId >= minPercentId) && (maxEvalue >= blastHsp.EValue) && (queryCoverage >= minQueryCoverage)) { if (numHits < maxHits) { GenBankItem gitem = new GenBankItem(gi, blastHsp.HitStart, blastHsp.HitEnd); giList.Add(gitem); } else { break; } numHits += 1; } } } } } return(giList); }
public static string ToString(Hit blastHit, int blastHspId) { Hsp hsp = blastHit.Hsps[blastHspId]; string pa = ""; pa += String.Format(">{0} {1}\r\n", blastHit.Id, blastHit.Def); pa += String.Format("Length={0}\r\n\r\n", blastHit.Length); pa += String.Format(" Score = {0} bits ({1}), Expect = {2}\r\n", hsp.BitScore, hsp.Score, hsp.EValue); int percentId = (int)Math.Round(((hsp.IdentitiesCount / (double)hsp.AlignmentLength) * 100.0), 0); int percentPos = (int)Math.Round(((hsp.PositivesCount / (double)hsp.AlignmentLength) * 100.0), 0); int percentGap = (int)Math.Round(((hsp.Gaps / (double)hsp.AlignmentLength) * 100.0), 0); pa += String.Format(" Identities = {0}/{1} ({2}%), Positives = {3}/{4} ({5}%), Gaps = {6}/{7} ({8}%)\r\n", hsp.IdentitiesCount, hsp.AlignmentLength, percentId, hsp.PositivesCount, hsp.AlignmentLength, percentPos, hsp.Gaps, hsp.AlignmentLength, percentGap); pa += String.Format(" Frame = {0}\r\n", hsp.HitFrame); pa += "\r\n"; for (int k = 0; k < hsp.AlignmentLength; k += 60) { long tl = 60; if ((k + tl) > hsp.AlignmentLength) { tl = hsp.AlignmentLength - k; } pa += String.Format("Query {1,-5} {0} {2,-5}\r\n", hsp.QuerySequence.Substring(k, (int)tl), hsp.QueryStart + k, hsp.QueryStart + k + tl - 1); pa += String.Format(" {1,-5} {0} {2,-5}\r\n", hsp.Midline.Substring(k, (int)tl), "", ""); pa += String.Format("Sbjct {1,-5} {0} {2,-5}\r\n", hsp.HitSequence.Substring(k, (int)tl), hsp.HitStart + k, hsp.HitStart + k + tl - 1); pa += "\r\n"; } return(pa); }
public static Facet CreateGBErrorFacet(string fName, string fType, BlastSearchRecord rec, int hitId, int hspId, Item item, string url, int rank) { Hit hit = rec.Hits[hitId]; Hsp hsp = hit.Hsps[hspId]; switch (fName) { case "QueryName": return(new Facet(fName, fType, item.Name)); case "QueryLen": return(new Facet(fName, fType, rec.IterationQueryLength)); case "Rank": return(new Facet(fName, fType, rank)); case "Score": return(new Facet(fName, fType, Math.Round(hsp.BitScore, 1))); case "Identity": double pi = (hsp.IdentitiesCount / (double)hsp.AlignmentLength) * 100.0; return(new Facet(fName, fType, Math.Round(pi, 0))); case "Span": double sp = ((hsp.QueryEnd - hsp.QueryStart + 1) / (double)rec.IterationQueryLength) * 100.0; return(new Facet(fName, fType, Math.Round(sp, 0))); case "SubjStart": double subjStart = hsp.HitStart; return(new Facet(fName, fType, Math.Round(subjStart, 0))); case "SubjLen": double subjLen = hit.Length; return(new Facet(fName, fType, Math.Round(subjLen, 0))); case "Strand": string strand = FrameToStrand(hsp.QueryFrame) + "/" + FrameToStrand(hsp.HitFrame); return(new Facet(fName, fType, strand)); case "Species": return(new Facet(fName, fType, "N/A")); case "Lineage": return(new Facet(fName, fType, "N/A")); case "Kingdom": return(new Facet(fName, fType, "N/A")); case "Phylum": return(new Facet(fName, fType, "N/A")); case "Class": return(new Facet(fName, fType, "N/A")); case "Order": return(new Facet(fName, fType, "N/A")); case "Family": return(new Facet(fName, fType, "N/A")); case "Organism": return(new Facet(fName, fType, "N/A")); case "Genus": return(new Facet(fName, fType, "N/A")); case "Gene": return(new Facet(fName, fType, "N/A", null)); case "GI": return(new Facet(fName, fType, "N/A", url)); case "Accession": return(new Facet(fName, fType, "N/A", url)); case "Definition": return(new Facet(fName, fType, "N/A")); case "EValue": return(new Facet(fName, fType, "N/A")); case "AlignLen": return(new Facet(fName, fType, hsp.AlignmentLength, @"txt\" + item.Id + ".txt")); case "RefCount": int i = 0; return(new Facet(fName, fType, i)); case "References": return(new Facet(fName, fType)); case "SubmissionDate": return(new Facet(fName, fType)); case "Product": return(new Facet(fName, fType, "N/A")); case "Function": return(new Facet(fName, fType, "N/A")); default: throw (new Exception("Facet category with name = " + fName + " does not exist.")); } }
//public static Facet CreateFacet(string fName, string fType, BlastSearchRecord rec, int hitId, int hspId, ISequence gb, Item item, string NCBIurl, CodingSequence bestCds, int rank) public static Facet CreateFacet(string fName, string fType, BlastSearchRecord rec, int hitId, int hspId, ISequence gb, Item item, string NCBIurl, FeatureItem bestItem, int rank) { Hit hit = rec.Hits[hitId]; Hsp hsp = hit.Hsps[hspId]; GenBankMetadata gbMeta = (GenBankMetadata)gb.Metadata["GenBank"]; string[] classLevels; switch (fName) { case "QueryName": return(new Facet(fName, fType, item.Name)); case "QueryLen": return(new Facet(fName, fType, rec.IterationQueryLength)); case "Rank": return(new Facet(fName, fType, rank)); case "Score": return(new Facet(fName, fType, Math.Round(hsp.BitScore, 1))); case "Identity": double pi = (hsp.IdentitiesCount / (double)hsp.AlignmentLength) * 100.0; return(new Facet(fName, fType, Math.Round(pi, 0))); case "Span": double sp = ((hsp.QueryEnd - hsp.QueryStart + 1) / (double)rec.IterationQueryLength) * 100.0; return(new Facet(fName, fType, Math.Round(sp, 0))); case "SubjStart": double subjStart = hsp.HitStart; return(new Facet(fName, fType, Math.Round(subjStart, 0))); case "SubjLen": double subjLen = hit.Length; return(new Facet(fName, fType, Math.Round(subjLen, 0))); case "Strand": string strand = FrameToStrand(hsp.QueryFrame) + "/" + FrameToStrand(hsp.HitFrame); return(new Facet(fName, fType, strand)); case "Species": int index = gbMeta.Source.Organism.Species.IndexOf(" ", StringComparison.Ordinal); if (index > 0) { return(new Facet(fName, fType, gbMeta.Source.Organism.Genus + " " + gbMeta.Source.Organism.Species.Substring(0, index))); } else { return(new Facet(fName, fType, gbMeta.Source.Organism.Genus + " " + gbMeta.Source.Organism.Species)); } case "Kingdom": classLevels = gbMeta.Source.Organism.ClassLevels.Split(';'); if (classLevels.Length >= 1) { return(new Facet(fName, fType, classLevels[0])); } else { return(new Facet(fName, fType, "N/A")); } case "Phylum": classLevels = gbMeta.Source.Organism.ClassLevels.Split(';'); if (classLevels.Length >= 2) { return(new Facet(fName, fType, classLevels[1])); } else { return(new Facet(fName, fType, "N/A")); } case "Class": classLevels = gbMeta.Source.Organism.ClassLevels.Split(';'); if (classLevels.Length >= 3) { return(new Facet(fName, fType, classLevels[2])); } else { return(new Facet(fName, fType, "N/A")); } case "Order": classLevels = gbMeta.Source.Organism.ClassLevels.Split(';'); if (classLevels.Length >= 4) { return(new Facet(fName, fType, classLevels[3])); } else { return(new Facet(fName, fType, "N/A")); } case "Family": classLevels = gbMeta.Source.Organism.ClassLevels.Split(';'); if (classLevels.Length >= 5) { return(new Facet(fName, fType, classLevels[4])); } else { return(new Facet(fName, fType, "N/A")); } case "Lineage": return(new Facet(fName, fType, gbMeta.Source.Organism.ClassLevels.ToString())); case "Organism": return(new Facet(fName, fType, gbMeta.Source.CommonName)); // return new Facet(fName, fType, gbMeta.Source.Organism.Genus + " " + gbMeta.Source.Organism.Species); case "Genus": return(new Facet(fName, fType, gbMeta.Source.Organism.ClassLevels.Split(';').Last().Trim().TrimEnd('.'))); case "Gene": string name = "N/A"; //if (bestCds != null) if (bestItem != null) { //CodingSequence feature = bestCds; FeatureItem feature = bestItem; String geneSym = "N/A"; foreach (KeyValuePair <string, List <String> > qualifier in feature.Qualifiers) { if (qualifier.Key == "gene") { geneSym = qualifier.Value[0].ToString().Trim('"'); } } if (geneSym != "") { name = geneSym; string url2 = System.Web.HttpUtility.HtmlEncode("http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=search&db=gene&term=" + name + "%5Bsym%5D"); return(new Facet(fName, fType, name, url2)); } } return(new Facet(fName, fType, name, null)); case "GI": return(new Facet(fName, fType, gbMeta.Version.GINumber, NCBIurl + gbMeta.Version.GINumber)); case "Accession": return(new Facet(fName, fType, gbMeta.Version.CompoundAccession, NCBIurl + gbMeta.Version.CompoundAccession)); case "Definition": return(new Facet(fName, fType, gbMeta.Definition)); case "EValue": return(new Facet(fName, fType, String.Format("{0:#e+00}", hsp.EValue))); case "AlignLen": return(new Facet(fName, fType, hsp.AlignmentLength, @"txt\" + item.Id + ".txt")); case "RefCount": int i = 0; foreach (CitationReference r in gbMeta.References) { if ((r.Title != "Direct Submission") && (r.Journal != "Unpublished")) { i++; } } return(new Facet(fName, fType, i)); case "References": if (gbMeta.References.Count() == 0) { return(new Facet(fName, fType)); } string url = CreateReferenceURL(gbMeta.References[0]); Facet f = new Facet(fName, fType); if (gbMeta.References.Count() > 0) { int j = 1; foreach (CitationReference r in gbMeta.References) { if (r.Title != "Direct Submission" && (r.Journal != "Unpublished")) { url = CreateReferenceURL(r); f.Add(new FacetValue(f.Type, String.Format("{0}. {1}. {2}.", j, r.Title, r.Journal), url)); j++; } } } return(f); case "SubmissionDate": DateTime dt = new DateTime(gbMeta.Locus.Date.Year, gbMeta.Locus.Date.Month, gbMeta.Locus.Date.Day); return(new Facet(fName, fType, dt.ToUniversalTime().ToString("o"))); case "Product": Facet productFacet = new Facet(fName, fType, GetQualifierString("Protein", "product", gbMeta)); if (productFacet[0].Value == "N/A") { Console.WriteLine(productFacet[0].Value + "!!!!!!!!!!!!!!!!!!!!!!!!!!***********"); if (bestItem != null) { productFacet = new Facet(fName, fType, GetQualifierStringFromCDS(bestItem, "product")); } Console.WriteLine(productFacet[0].Value + "!!!!!!!!!!!!!!!!!!!!!!!!&&&&&&&&&&&&&"); } return(productFacet); case "Function": Facet funcFacet = new Facet(fName, fType, GetQualifierString("Protein", "function", gbMeta)); if (funcFacet[0].Value == "N/A") { if (bestItem != null) { funcFacet = new Facet(fName, fType, GetQualifierStringFromCDS(bestItem, "function")); } } return(funcFacet); default: throw (new Exception("Facet category with name = " + fName + " does not exist.")); } }
public static int CreateItems(UIParameters Up, ISequence rec, int itemId, int seqPos, Collection collection) { string queryName = rec.DisplayID.ToString().Split(' ')[0]; // BLAST reports are saved in individual files by query and // numbered in the same order as they appear in the input FASTA file. string blastFile = Up.ProjectDir + "\\xml\\" + seqPos + ".xml"; if (!File.Exists(blastFile)) { throw new Exception("File does not exist."); } BlastXmlParser blastParser = new BlastXmlParser(); IList <BlastResult> blastResults = blastParser.Parse(blastFile); GenBankParser gbParser = new GenBankParser(); int[] annotatedIndex = GetBestAnnotatedIndex(Up, seqPos); // iterate through the BLAST results. foreach (BlastResult blastResult in blastResults) { foreach (BlastSearchRecord record in blastResult.Records) { int hitsProcessed = 0; // If there are not hits in the BLAST result ... int rank = 0; if (record.Hits.Count() > 0) { // For each hit for (int i = 0; i < record.Hits.Count(); i++) { Hit blastHit = record.Hits[i]; // For each HSP for (int j = 0; j < blastHit.Hsps.Count(); j++) { Hsp blastHsp = blastHit.Hsps[j]; double percentId = (blastHsp.IdentitiesCount / (double)blastHsp.AlignmentLength) * 100; double queryCoverage = ((double)(blastHsp.QueryEnd - blastHsp.QueryStart + 1) / record.IterationQueryLength) * 100; string txt = String.Format("{0} {1} {2} {3} {4} {5} {6} {7}", percentId, Up.BlastMinPercentIdentity, Up.BlastMaxEvalue, blastHsp.EValue, queryCoverage, Up.BlastMinPercentQueryCoverage, hitsProcessed, Up.BlastMaxNumHits); // if HSP passes user-defined thresholds if ((percentId >= Up.BlastMinPercentIdentity) && (Up.BlastMaxEvalue >= blastHsp.EValue) && (queryCoverage >= Up.BlastMinPercentQueryCoverage) && (hitsProcessed < Up.BlastMaxNumHits)) { rank += 1; string nextScore = "no"; if ((i + 1) < record.Hits.Count()) { if (blastHsp.Score > record.Hits[i + 1].Hsps[0].Score) { nextScore = "less than"; } else { nextScore = "equal"; } } else { nextScore = "non existent"; } // parse GI numner from hit long gi = Convert.ToInt64(blastHit.Id.Split('|')[1]); GenBankItem gitem = new GenBankItem(gi, blastHsp.HitStart, blastHsp.HitEnd); string gbFile = Up.ProjectDir + "\\gb\\" + gitem.Id.ToString(); gbFile += "_" + gitem.HitStart.ToString(); gbFile += "_" + gitem.HitEnd.ToString(); gbFile += ".gb"; // init item string img = "#" + itemId.ToString(); Item item = new Item(itemId, img); string[] headerTokens = parseFastaHeader(rec.DisplayID.ToString()); item.Name = headerTokens[0]; item.Description = headerTokens[1]; // write pairwise alignment writePairwiseAlignment(Up, blastHit, j, itemId); // try to parse the GB record associated with the hit and set facet values to data from BLAST/GB record try { Console.WriteLine("GB OK: " + record.Hits[0].Id + " " + i.ToString() + " " + j.ToString()); ISequence gbRecord = gbParser.ParseOne(gbFile); item.Href = GetNCBIUrl(Up.BlastProgram) + GetGenBankIdentifier(gbRecord); GenBankMetadata gbMeta = (GenBankMetadata)gbRecord.Metadata["GenBank"]; CodingSequence bestCds = null; IList <FeatureItem> features = gbMeta.Features.All; FeatureItem bestItem = getBestFeatureItem(features); if (gbMeta.Features.CodingSequences.Count > 0) { bestCds = gbMeta.Features.CodingSequences[0]; } for (int k = 1; k < gbMeta.Features.CodingSequences.Count; k++) { CodingSequence cds = gbMeta.Features.CodingSequences[k]; //int bestSize = Math.Abs(bestCds.Location.End - bestCds.Location.Start); int bestSize = Math.Abs(bestItem.Location.End - bestItem.Location.Start); int cdsSize = Math.Abs(cds.Location.End - cds.Location.Start); if (cdsSize > bestSize) { bestCds = cds; } } foreach (FacetCategory f in Up.FacetCategories) { Facet facet = new Facet(); switch (f.Name) { case "InputOrder": facet = new Facet(f.Name, f.Type, seqPos); break; case "QuerySequence": facet = new Facet(f.Name, f.Type, rec.ToString()); break; case "NextScore": facet = new Facet(f.Name, f.Type, nextScore); break; case "Annotated": string value = "na"; if ((annotatedIndex[0] == i) && (annotatedIndex[1] == j)) { value = "top_annotated"; } else { if ((i == 0) && (j == 0) && (annotatedIndex[0] == -1) && (annotatedIndex[1] == -1)) { value = "top_unannotated"; } else { if (bestItem != null) { value = "annotated"; } else { value = "unannotated"; } } } facet = new Facet(f.Name, f.Type, value); break; default: //facet = CreateFacet(f.Name, f.Type, record, i, j, gbRecord, item, GetNCBIUrl(Up.BlastProgram), bestCds, rank); facet = CreateFacet(f.Name, f.Type, record, i, j, gbRecord, item, GetNCBIUrl(Up.BlastProgram), bestItem, rank); break; } /* * if (f.Name == "InputOrder") * { * facet = new Facet(f.Name, f.Type, seqPos); * } * * else * { * facet = CreateFacet(f.Name, f.Type, record, i, j, gbRecord, item); * } */ item.Facets.Add(facet); } } //catch (System.NullReferenceException e) // if parsing failed init the item w/ default values (similar to 'no hit' above) catch { Console.WriteLine("GB ERROR: " + record.Hits[0].Id + " " + i.ToString() + " " + j.ToString()); item.Href = "#"; foreach (FacetCategory f in Up.FacetCategories) { Facet facet = new Facet(); switch (f.Name) { case ("InputOrder"): facet = new Facet(f.Name, f.Type, seqPos); break; case "QuerySequence": facet = new Facet(f.Name, f.Type, rec.ToString()); break; case ("NextScore"): facet = new Facet(f.Name, f.Type, "no"); break; case "Annotated": string value = "na"; if ((annotatedIndex[0] == i) && (annotatedIndex[1] == j)) { value = "top_annotated"; } else { if ((i == 0) && (j == 0) && (annotatedIndex[0] == -1) && (annotatedIndex[1] == -1)) { value = "top_unannotated"; } else { value = "unannotated"; } } facet = new Facet(f.Name, f.Type, value); break; default: facet = CreateGBErrorFacet(f.Name, f.Type, record, i, j, item, GetNCBIUrl(Up.BlastProgram), rank); break; } item.Facets.Add(facet); } //throw (e); } // Add item to collection, increment to next item, collection.Items.Add(item); hitsProcessed += 1; itemId += 1; } } } } if ((record.Hits.Count()) == 0 || (hitsProcessed == 0)) { // Init Pivot item string img = "#" + itemId.ToString(); Item item = new Item(itemId, img); item.Href = "#"; string[] headerTokens = parseFastaHeader(rec.DisplayID.ToString()); item.Name = headerTokens[0]; item.Description = headerTokens[1]; // Write pairwise alignment to file. writePairwiseAlignment(Up, itemId); // Set facet values for each facet category to default values foreach (FacetCategory f in Up.FacetCategories) { Facet facet = new Facet(); switch (f.Name) { case ("InputOrder"): facet = new Facet(f.Name, f.Type, seqPos); break; case ("QuerySequence"): facet = new Facet(f.Name, f.Type, rec.ToString()); break; default: facet = CreateFacet(f.Name, f.Type, record, item, 0); break; } item.Facets.Add(facet); } // Add item to collection, increment to next item, skip remaining code collection.Items.Add(item); itemId += 1; hitsProcessed += 1; } } } return(itemId); }
public static int[] GetBestAnnotatedIndex(UIParameters Up, int seqPos) { // BLAST reports are saved in individual files by query and // numbered in the same order as they appear in the input FASTA file. int[] annotatedIndex = new int[2]; annotatedIndex[0] = -1; annotatedIndex[1] = -1; string blastFile = Up.ProjectDir + "\\xml\\" + seqPos + ".xml"; if (!File.Exists(blastFile)) { throw new Exception("File does not exist."); } BlastXmlParser blastParser = new BlastXmlParser(); IList <BlastResult> blastResults = blastParser.Parse(blastFile); GenBankParser gbParser = new GenBankParser(); // iterate through the BLAST results. foreach (BlastResult blastResult in blastResults) { foreach (BlastSearchRecord record in blastResult.Records) { int hitsProcessed = 0; // If there are not hits in the BLAST result ... int rank = 0; if (record.Hits.Count() > 0) { // For each hit for (int i = 0; i < record.Hits.Count(); i++) { Hit blastHit = record.Hits[i]; for (int j = 0; j < blastHit.Hsps.Count(); j++) { Hsp blastHsp = blastHit.Hsps[j]; double percentId = (blastHsp.IdentitiesCount / (double)blastHsp.AlignmentLength) * 100; double queryCoverage = ((double)(blastHsp.QueryEnd - blastHsp.QueryStart + 1) / record.IterationQueryLength) * 100; if ((percentId >= Up.BlastMinPercentIdentity) && (Up.BlastMaxEvalue >= blastHsp.EValue) && (queryCoverage >= Up.BlastMinPercentQueryCoverage) && (hitsProcessed < Up.BlastMaxNumHits)) { rank += 1; long gi = Convert.ToInt64(blastHit.Id.Split('|')[1]); GenBankItem gitem = new GenBankItem(gi, blastHsp.HitStart, blastHsp.HitEnd); string gbFile = Up.ProjectDir + "\\gb\\" + gitem.Id.ToString(); gbFile += "_" + gitem.HitStart.ToString(); gbFile += "_" + gitem.HitEnd.ToString(); gbFile += ".gb"; try { Console.WriteLine("GB OK: " + record.Hits[0].Id + " " + i.ToString() + " " + j.ToString()); ISequence gbRecord = gbParser.ParseOne(gbFile); GenBankMetadata gbMeta = (GenBankMetadata)gbRecord.Metadata["GenBank"]; IList <FeatureItem> features = gbMeta.Features.All; FeatureItem bestItem = getBestFeatureItem(features); if (bestItem != null) { annotatedIndex[0] = i; annotatedIndex[1] = j; return(annotatedIndex); } } catch { Console.WriteLine("ISANNOTATED: " + record.Hits[0].Id + " " + i.ToString() + " " + j.ToString()); } hitsProcessed += 1; } } } } } } return(annotatedIndex); }
protected void butBLAST_Click(object sender, EventArgs e) { string baseDir = ConfigurationManager.AppSettings["BLASTbase"]; string exomeRef = ConfigurationManager.AppSettings["RefName"]; string file = baseDir + @"temp\test" + DateTime.Now.ToString("yyyyMMddhhmmss"); string ls = Environment.NewLine.Normalize(); if (!txtBlast.Text.StartsWith(">")) { txtBlast.Text = @">No definition line" + ls + txtBlast.Text; } using (MemoryStream mstrm = new MemoryStream(Encoding.UTF8.GetBytes(txtBlast.Text))) { ISequenceParser parser = SequenceParsers.Fasta; ISequenceFormatter formatter = SequenceFormatters.Fasta; try { Sequence seq = (Sequence)parser.ParseOne(mstrm); formatter.Format(seq, file + ".in"); txtBlast.Text = @">" + seq.ID + ls + seq.ConvertToString(0); ErrorMessage.Text = ""; } catch (Exception ex) { ErrorMessage.Text = ex.Message; txtRes.Text = ""; return; } parser.Close(); formatter.Close(); } Session["blast"] = txtBlast.Text; using (Process blast = new Process()) { blast.StartInfo.FileName = baseDir + @"bin\blastn.exe"; blast.StartInfo.UseShellExecute = false; blast.StartInfo.Arguments = @"-task blastn -db " + baseDir + @"db\" + exomeRef + " -evalue 0.1 -outfmt 11 -query " + file + ".in -out " + file + ".asn -max_target_seqs 5"; blast.Start(); blast.WaitForExit(); } if (File.Exists(file + ".asn")) { using (Process form = new Process()) { form.StartInfo.FileName = baseDir + @"bin\blast_formatter.exe"; form.StartInfo.UseShellExecute = false; form.StartInfo.Arguments = @"-archive " + file + ".asn" + @" -outfmt 5 -out " + file + ".xml"; form.Start(); form.WaitForExit(); form.StartInfo.Arguments = @"-archive " + file + ".asn" + @" -outfmt 0 -out " + file + ".txt"; form.Start(); form.WaitForExit(); } using (StreamReader sr = new StreamReader(file + ".xml")) { IBlastParser blastParser = new BlastXmlParser(); List <BlastResult> blastres = blastParser.Parse(sr.BaseStream).ToList(); ChartArea area = chartBlast.ChartAreas.FindByName("ChartArea1"); if (blastres.Count > 0) { BlastXmlMetadata meta = blastres[0].Metadata; chartBlast.Titles.FindByName("blastTitle").Text = meta.QueryDefinition; area.AxisY.Maximum = Math.Floor(meta.QueryLength + 5.0); Series series = chartBlast.Series["Series1"]; series.Points.Clear(); int i = 0; List <BlastHit> blasts = new List <BlastHit>(); chartBlast.Height = 30 * blastres[0].Records[0].Hits.Count + 50; foreach (Hit hit in blastres[0].Records[0].Hits) { string contig = hit.Def; for (int j = 0; j < hit.Hsps.Count; j++) { Hsp hsp = hit.Hsps[j]; int k = series.Points.AddXY(i, hsp.QueryStart, hsp.QueryEnd); if (j < 1) { series.Points[k].Label = contig; } BlastHit bhit = new BlastHit(); bhit.seqID = Convert.ToInt64(hit.Accession); bhit.Contig = contig; bhit.Descr = hit.Def; bhit.Score = hsp.BitScore.ToString("N2"); bhit.Evalue = hsp.EValue.ToString("E2"); bhit.HitStart = hsp.HitStart.ToString(); bhit.HitEnd = hsp.HitEnd.ToString(); bhit.Align = hsp.AlignmentLength.ToString(); bhit.Frame = hsp.QueryFrame > 0?"+":"-"; bhit.Frame += @"/"; bhit.Frame += hsp.HitFrame > 0 ? "+" : "-"; blasts.Add(bhit); } i++; } gridBLAST.DataSource = blasts; } else { gridBLAST.DataSource = null; chartBlast.Height = 1; } gridBLAST.DataBind(); } using (StreamReader sr = new StreamReader(file + ".txt", Encoding.UTF8, true)) { txtRes.Text = sr.ReadToEnd(); } } else { txtRes.Text = "Nothing found."; } string[] tmpFiles = Directory.GetFiles(baseDir + @"temp\"); foreach (string filePath in tmpFiles) { if (File.GetLastWriteTime(filePath).CompareTo(DateTime.Now.AddDays(-28)) < 0) { File.Delete(filePath); } } }