static void Main(string[] args) { /* * ExACAnnotator.GetVarAnnotation("1", 931393, "G", "T"); * ExACAnnotator.GetVarAnnotation("1-935222-C-A"); * ExACAnnotator.GetVarAnnotation("14-21853913-T-C"); * ExACAnnotator.GetVarAnnotation("22-46615746-A-G"); * ExACAnnotator.GetVarAnnotation("1-6475586-TC-GA"); * List<string> list = new List<string>(); * list.Add("1-931393-G-T"); * list.Add("14-21853913-T-C"); * list.Add("22-46615746-A-G"); * list.Add("1-6475586-TC-GA"); * ExACAnnotator.GetBulkVarAnnotations(list);*/ if (args.Length != 2) { Console.WriteLine("Please use:\ndotnet run yourfile.vcf output.txt"); return; } string vcfFile = args[0]; if (!File.Exists(vcfFile)) { Console.WriteLine("Error! Can not find vcf file to annotate: " + vcfFile); return; } string outputFile = args[1]; IVcfAnnotator annot = new ExACAnnotator(); VCFFile vf = new VCFFile(vcfFile); annot.AnnotateVcfFile(vf, outputFile); vf.Close(); }
public void AnnotateVcfFile(VCFFile file, string outputFile) { string headerLine = "#CHROM\tPOS\tID\tREF\tALT\tTYPE\tDP\tRO\tAO\tARO\t" + "ExAC_Consequence\tExAC_Transcripts\tExAC_rsID\tExAC_AlleleCount\tExAC_AlleleNumber\tExAC_HomozygousNumber\tExAC_AlleleFrequence\tExACBrowser_Link"; using (StreamWriter writer = new StreamWriter(outputFile)) { writer.WriteLine(headerLine); if (useBulkMode) { file.Traverse(BulkLineLimit, delegate(string[] lines) { try { List <string> queries = new List <string>(); Dictionary <string, string> outputinfo1 = new Dictionary <string, string>(); foreach (string line in lines) { string[] vcfvalues = line.Split('\t'); string chrom = vcfvalues[0].Trim(); string pos = vcfvalues[1].Trim(); string id = vcfvalues[2].Trim(); string refAllele = vcfvalues[3].Trim(); string altAllele = vcfvalues[4].Trim(); string info = vcfvalues[7].Trim(); Dictionary <string, string> dict = VCFFile.ParseInfoLine(info); string type = dict.ContainsKey("TYPE") ? dict["TYPE"] : "."; string dp = dict.ContainsKey("DP") ? dict["DP"] : "."; string ro = dict.ContainsKey("RO") ? dict["RO"] : "."; string ao = dict.ContainsKey("AO") ? dict["AO"] : "."; string[] alts = altAllele.Split(','); string[] aos = ao.Split(','); string[] altTypes = type.Split(','); float[] aros = new float[alts.Length]; for (int i = 0; i < alts.Length; i++) { string altAl = alts[i]; float aro = float.Parse(aos[i]) / float.Parse(ro); bool modifyAllele = false; string newRefAl = refAllele; string newAltAl = altAl; int movepos = 0; if (refAllele.Length > 1 && altAl.Length > 1) { modifyAllele = ModifyAlleleForQuery(refAllele, altAl, out newRefAl, out newAltAl, out movepos); } int origpos = int.Parse(pos); int newpos = (movepos > 0)? origpos + movepos : origpos; string query = (modifyAllele) ? chrom + "-" + newpos + "-" + newRefAl + "-" + newAltAl : chrom + "-" + pos + "-" + refAllele + "-" + altAl; queries.Add(query); string info1 = chrom + "\t" + pos + "\t" + id + "\t" + refAllele + "\t" + altAl + "\t" + altTypes[i] + "\t" + dp + "\t" + ro + "\t" + aos[i] + "\t" + aro; outputinfo1[query] = info1; } } Dictionary <string, IVarAnnotation> annots = this.BulkAnnotateVars(queries.ToArray()); foreach (KeyValuePair <string, string> pair in outputinfo1) { if (annots.ContainsKey(pair.Key)) { IVarAnnotation annot = annots[pair.Key]; if (annot == null) { Console.WriteLine("annotation is empty for: " + pair.Key); continue; } else { string httpLink = HttpVarUrl + pair.Key; writer.WriteLine(pair.Value + "\t" + annot.ToString() + "\t" + httpLink); } } else { Console.WriteLine("annotation is empty for: " + pair.Key); continue; } } return(false); } catch (Exception e) { Console.WriteLine(e); } return(true); }); } else { file.Traverse(delegate(string line, long index) { try { string[] vcfvalues = line.Split('\t'); string chrom = vcfvalues[0].Trim(); string pos = vcfvalues[1].Trim(); string id = vcfvalues[2].Trim(); string refAllele = vcfvalues[3].Trim(); string altAllele = vcfvalues[4].Trim(); string info = vcfvalues[7].Trim(); Dictionary <string, string> dict = VCFFile.ParseInfoLine(info); string type = dict.ContainsKey("TYPE") ? dict["TYPE"] : "."; string dp = dict.ContainsKey("DP") ? dict["DP"] : "."; string ro = dict.ContainsKey("RO") ? dict["RO"] : "."; string ao = dict.ContainsKey("AO") ? dict["AO"] : "."; if (altAllele.Contains(',')) { string[] alts = altAllele.Split(','); string[] aos = ao.Split(','); string[] altTypes = type.Split(','); for (int i = 0; i < alts.Length; i++) { string altAl = alts[i]; bool modifyAllele = false; string newRefAl = refAllele; string newAltAl = altAl; int movepos = 0; if (refAllele.Length > 1 && altAl.Length > 1) { modifyAllele = ModifyAlleleForQuery(refAllele, altAl, out newRefAl, out newAltAl, out movepos); } int origpos = int.Parse(pos); int newpos = (movepos > 0)? origpos + movepos : origpos; string query = (modifyAllele) ? chrom + "-" + newpos + "-" + newRefAl + "-" + newAltAl : chrom + "-" + pos + "-" + refAllele + "-" + altAl; IVarAnnotation annot = this.AnnotateVar(query); if (annot == null) { continue; } else { float aro = float.Parse(aos[i]) / float.Parse(ro); string info1 = chrom + "\t" + pos + "\t" + id + "\t" + refAllele + "\t" + altAl + "\t" + altTypes[i] + "\t" + dp + "\t" + ro + "\t" + aos[i] + "\t" + aro; string httpLink = HttpVarUrl + query; writer.WriteLine(info1 + "\t" + annot.ToString() + "\t" + httpLink); } } } else { float aro = float.Parse(ao) / float.Parse(ro); bool modifyAllele = false; string newRefAl = refAllele; string newAltAl = altAllele; int movepos = 0; if (refAllele.Length > 1 && altAllele.Length > 1) { modifyAllele = ModifyAlleleForQuery(refAllele, altAllele, out newRefAl, out newAltAl, out movepos); } int origpos = int.Parse(pos); int newpos = (movepos > 0)? origpos + movepos : origpos; string query = (modifyAllele) ? chrom + "-" + newpos + "-" + newRefAl + "-" + newAltAl : chrom + "-" + pos + "-" + refAllele + "-" + altAllele; IVarAnnotation annot = this.AnnotateVar(query); string info1 = chrom + "\t" + pos + "\t" + id + "\t" + refAllele + "\t" + altAllele + "\t" + type + "\t" + dp + "\t" + ro + "\t" + ao + "\t" + aro; string httpLink = HttpVarUrl + query; writer.WriteLine(info1 + "\t" + annot.ToString() + "\t" + httpLink); } return(false); } catch (Exception e) { Console.WriteLine(e); } return(true); }); } } }