public void ConvertToMatrix(string inputPath, string outputPath, VariantFileType intputFileType, VariantEncoding sourceEncoding, VariantEncoding destinationEncoding, string snpPath = null, IProgressReporter progress = null) { /* string[] sampleIds = null; var variants = new List<VariantCall>(); StringSplitter splitter = null; var variantHeader = new StringBuilder(); using (var reader = _fileReaderFactory.GetFileReader(inputPath)) { foreach (var line in reader) { if (intputFileType == VariantFileType.Vcf) { //Check for comment headers if (line.Length < 2) { continue; } if (line.Substring(0, 2) == "##") { continue; } if (line[0] == '#') { var header = line.Split('\t'); sampleIds = new string[header.Length - 9]; for (var i = 9; i < header.Length; i++) { sampleIds[i - 9] = header[i]; } variantHeader.Append("SampleId"); continue; } if (splitter == null) { var testSplit = line.Split('\t'); splitter = new StringSplitter(testSplit.Length); } //Split the line out //var lineSplit = line.Split('\t'); splitter.Split(line, '\t'); var lineSplit = splitter.Results; //Check if we passed QC if (lineSplit[6] != "PASS") { continue; } //Check if id starts with rs if (lineSplit[2][0] != 'r') { continue; } if (lineSplit[2][1] != 's') { continue; } //Create variant var newVar = new VariantCall(); //newVar.Chromosome = lineSplit[0]; //newVar.ChromPos = Convert.ToInt32(lineSplit[1]); newVar.Id = lineSplit[2]; newVar.dbSNPRef = lineSplit[3]; newVar.VcfAlt = lineSplit[4]; var tempBuilder = new StringBuilder(); var callA = lineSplit[9][0] == '0' ? newVar.dbSNPRef : newVar.VcfAlt; var callB = lineSplit[9][2] == '0' ? newVar.dbSNPRef : newVar.VcfAlt; tempBuilder.Append(callA); tempBuilder.Append("_"); tempBuilder.Append(callB); tempBuilder.Append("\t"); for (var i = 10; i < lineSplit.Length; i++) { callA = lineSplit[9][0] == '0' ? newVar.dbSNPRef : newVar.VcfAlt; callB = lineSplit[9][2] == '0' ? newVar.dbSNPRef : newVar.VcfAlt; tempBuilder.Append(callA); tempBuilder.Append("_"); tempBuilder.Append(callB); tempBuilder.Append("\t"); } newVar.OutputCalls = tempBuilder.ToString(); variants.Add(newVar); variantHeader.Append("\t"); variantHeader.Append(newVar.Id); if (progress != null) { var perc = (double) reader.Stream.Position/reader.Stream.Length; if (perc > 0.05) { break; } progress.Update(perc/2); } } } } using (var writer = new StreamWriter(File.Open(outputPath, FileMode.Create))) { writer.WriteLine(variantHeader); var totalSamples = sampleIds.Length; var totalVariants = variants.Count; splitter = new StringSplitter(totalSamples); for (var i = 0; i < totalSamples; i++) { var lineBuilder = new StringBuilder(); lineBuilder.Append(sampleIds[i]); for (var j = 0; j < totalVariants; j++) { splitter.Split(variants[j].OutputCalls, "\t"); lineBuilder.Append("\t"); lineBuilder.Append(splitter.Results[i]); } writer.WriteLine(lineBuilder); if (progress != null) { var perc = (double)i / totalSamples; progress.Update((perc / 2) + 0.5); } } }*/ }
private VariantCall[] LoadVariant(VariantFileType fileType, string line) { if (fileType == VariantFileType.Vcf) { return LoadVcfVariant(line); } if (fileType == VariantFileType.IlluminiaFinalReport) { return LoadIlluminaFinalReportVariant(line); } if (fileType == VariantFileType.IlluminaMatrix) { return LoadIlluminaMatrixReportVariant(line); } return null; }
public void AnalyseVcfFile(string variantPath, VariantFileType fileType, string snpPath = null, ConsoleLogger logger = null) { ProgressReporter pr = null; logger?.LogInfo(LogName, "Analysing variants"); //Do we need to load snps? if (snpPath != null) { pr = null; if (logger != null) { pr = new ProgressReporter(logger, 1); } logger?.StartTask(1, "Load SNP file"); LoadUcscSnps(snpPath, true, pr); logger?.EndTask(1); logger?.LogInfo(LogName, "Loaded " + Snps.Count + " SNPs"); } //Init _splitter = null; var variantCount = 0; var unmatchedCount = 0; //Var vcf var calledForwardOnPlus = 0; var calledForwardOnNeg = 0; //Var final report var matchedFwdStrand = 0; var unMatchedFwdStrand = 0; //Logging pr = null; if (logger != null) { pr = new ProgressReporter(logger, 1); } logger?.StartTask(1, "Processing variants"); //Read file using (var reader = _fileReaderFactory.GetFileReader(variantPath)) { foreach (var line in reader) { //Load variants var variants = LoadVariant(fileType, line); //Continue if not a real variants if (variants == null) { continue; } for (var i = 0; i < variants.Length; i++) { //Get current variant var variant = variants[i]; //Try and match to snp's loads Snp matchSnp; Snps.TryGetValue(variant.Id, out matchSnp); //Check we have a match if (matchSnp != null) { if (fileType == VariantFileType.Vcf) { if (matchSnp.Strand == "+" && matchSnp.SnpRef == variant.VcfRef) { calledForwardOnPlus++; } else if (matchSnp.Strand == "-" && matchSnp.SnpRef == variant.VcfRef) { calledForwardOnNeg++; } } else if (fileType == VariantFileType.IlluminiaFinalReport) { if (variant.AlleleA != "-" && variant.AlleleB != "-") { if ((variant.AlleleA == matchSnp.SnpRef || variant.AlleleA == matchSnp.SnpAlt) && (variant.AlleleB == matchSnp.SnpRef || variant.AlleleB == matchSnp.SnpAlt)) { matchedFwdStrand++; } else { if (matchSnp.Observed.Length <= 3) { unMatchedFwdStrand++; } } } } else if (fileType == VariantFileType.IlluminaMatrix) { if (variant.AlleleA != "-" && variant.AlleleB != "-") { if ((variant.AlleleA == matchSnp.SnpRef || variant.AlleleA == matchSnp.SnpAlt) && (variant.AlleleB == matchSnp.SnpRef || variant.AlleleB == matchSnp.SnpAlt)) { matchedFwdStrand++; } else { if (matchSnp.Observed.Length <= 3) { unMatchedFwdStrand++; } } } } } else { unmatchedCount++; } variantCount++; if (logger != null) { var perc = (double) reader.Stream.Position/reader.Stream.Length; pr.Update(perc); if (fileType == VariantFileType.Vcf) { pr.Update( $" TV:{variantCount} UTV:{unmatchedCount} :CallFwd+:{calledForwardOnPlus} CallFwd-:{calledForwardOnNeg}"); } else if (fileType == VariantFileType.IlluminiaFinalReport) { pr.Update( $" TV:{variantCount} UTV:{unmatchedCount} MatchedFwd:{matchedFwdStrand} UnMatchedFwd:{unMatchedFwdStrand}"); } else if (fileType == VariantFileType.IlluminaMatrix) { pr.Update( $" TV:{variantCount} UTV:{unmatchedCount} MatchedFwd:{matchedFwdStrand} UnMatchedFwd:{unMatchedFwdStrand}"); } } } } } logger?.EndTask(1); }