/// <summary> /// Tells this builder that the resulting VariantContext should use this genotypes GenotypeContext /// /// Note that genotypes can be null -> meaning there are no genotypes /// </summary> /// <param name="genotypes"> </param> public void SetGenotypes(GenotypesContext genotypes, bool requireValidation = true) { this.genotypes_Renamed = genotypes; if (genotypes != null && requireValidation) { toValidate = toValidate | VariantContext.Validation.GENOTYPES; } //TODO: Perhaps if the genotypes are reset we should remove the validation flag if it was not required but was already present? }
public VariantContextBuilder(VariantContextBuilder parent) : this() { if (parent == null) { throw new System.ArgumentException("BUG: VariantContext parent argument cannot be null in VariantContextBuilder"); } this.alleles_Renamed = parent.alleles_Renamed; this.attributesCanBeModified = false; this.Contig = parent.Contig; this.genotypes_Renamed = parent.genotypes_Renamed; this.ID = parent.ID; this.Log10PError = parent.Log10PError; this.Source = parent.Source; this.start_Renamed = parent.start_Renamed; this.stop_Renamed = parent.stop_Renamed; this.FullyDecoded = parent.FullyDecoded; this.Attributes = parent.attributes_Renamed; this.SetFilters(parent.filters_Renamed); }
/// <summary> /// Returns a new builder based on parent -- the new VC will have all fields initialized /// to their corresponding values in parent. This is the best way to create a derived VariantContext /// </summary> /// <param name="parent"> Cannot be null </param> public VariantContextBuilder(VariantContext parent) : this() { if (parent == null) { throw new System.ArgumentException("BUG: VariantContextBuilder parent argument cannot be null in VariantContextBuilder"); } this.alleles_Renamed = parent.alleles; this.attributes_Renamed = (IDictionary <string, object>)parent.Attributes; this.attributesCanBeModified = false; this.Contig = parent.contig; this.filters_Renamed = (ISet <string>)parent.FiltersMaybeNull; this.genotypes_Renamed = parent.genotypes; this.ID = parent.ID; this.Log10PError = parent.Log10PError; this.Source = parent.Source; this.start_Renamed = parent.Start; this.stop_Renamed = parent.End; this.FullyDecoded = parent.FullyDecoded; }
/// <summary> /// Return a freshly allocated subcontext of this context containing only the samples /// listed in samples. Note that samples can contain names not in this context, they /// will just be ignored. /// </summary> /// <param name="samples"> /// @return </param> public GenotypesContext subsetToSamples(ISet <string> samples) { int nSamples = samples.Count; if (nSamples == 0) { return(NO_GENOTYPES); } else // nGenotypes < nSamples { GenotypesContext subset = create(samples.Count); foreach (String sample in samples) { Genotype g = Get(sample); if (g != null) { subset.Add(g); } } return(subset); } }
/// <summary> /// This method subsets down to a set of samples. /// /// At the same time returns the alleles to just those in use by the samples, /// if rederiveAllelesFromGenotypes is true, otherwise the full set of alleles /// in this VC is returned as the set of alleles in the subContext, even if /// some of those alleles aren't in the samples /// /// WARNING: BE CAREFUL WITH rederiveAllelesFromGenotypes UNLESS YOU KNOW WHAT YOU ARE DOING /// </summary> /// <param name="sampleNames"> the sample names </param> /// <param name="rederiveAllelesFromGenotypes"> if true, returns the alleles to just those in use by the samples, true should be default </param> /// <returns> new VariantContext subsetting to just the given samples </returns> public VariantContext SubContextFromSamples(ISet <string> sampleNames, bool rederiveAllelesFromGenotypes) { if (sampleNames.SetEquals(SampleNames) && !rederiveAllelesFromGenotypes) { return(this); // fast path when you don't have any work to do } else { VariantContextBuilder builder = new VariantContextBuilder(this); GenotypesContext newGenotypes = genotypes.subsetToSamples(sampleNames); if (rederiveAllelesFromGenotypes) { builder.SetAlleles(allelesOfGenotypes(newGenotypes)); } else { builder.SetAlleles(alleles); } builder.SetGenotypes(newGenotypes); return(builder.make()); } }
/// <summary> /// Add a record to the file /// </summary> /// <param name="vc">The Variant Context object </param> protected string getVariantLinetoWrite(VariantContext vc) { if (doNotWriteGenotypes) { vc = (new VariantContextBuilder(vc)).noGenotypes().make(); } try { //Convert alleles to 1,2,3,etc. numbering IDictionary <Allele, string> alleleMap = buildAlleleMap(vc); // CHROM StringBuilder lineToWrite = new StringBuilder(); //Add chr, pos, id, ref lineToWrite.Append(String.Join(VCFConstants.FIELD_SEPARATOR, vc.Chr, vc.Start.ToString(), vc.ID, vc.Reference.DisplayString)); // ALT if (vc.Variant) { Allele altAllele = vc.GetAlternateAllele(0); string alt = altAllele.DisplayString; lineToWrite.Append(alt); for (int i = 1; i < vc.AlternateAlleles.Count; i++) { altAllele = vc.GetAlternateAllele(i); alt = altAllele.DisplayString; lineToWrite.Append(","); lineToWrite.Append(alt); } } else { lineToWrite.Append(VCFConstants.EMPTY_ALTERNATE_ALLELE_FIELD); } lineToWrite.Append(VCFConstants.FIELD_SEPARATOR); // QUAL if (!vc.HasLog10PError) { lineToWrite.Append(VCFConstants.MISSING_VALUE_v4); } else { lineToWrite.Append(formatQualValue(vc.PhredScaledQual)); } lineToWrite.Append(VCFConstants.FIELD_SEPARATOR); // FILTER string filters = getFilterString(vc); lineToWrite.Append(filters); lineToWrite.Append(VCFConstants.FIELD_SEPARATOR); // INFO IDictionary <string, string> infoFields = new SortedDictionary <string, string>(); foreach (KeyValuePair <string, object> field in vc.Attributes) { string key = field.Key; if (!mHeader.hasInfoLine(key)) { fieldIsMissingFromHeaderError(vc, key, "INFO"); } string outputValue = formatVCFField(field.Value); if (outputValue != null) { infoFields[key] = outputValue; } } lineToWrite.Append(getInfoString(infoFields));; // FORMAT GenotypesContext gc = vc.Genotypes; if (gc.LazyWithData && ((LazyGenotypesContext)gc).UnparsedGenotypeData is string) { lineToWrite.Append(VCFConstants.FIELD_SEPARATOR); lineToWrite.Append(((LazyGenotypesContext)gc).UnparsedGenotypeData.ToString()); } else { IList <string> genotypeAttributeKeys = calcVCFGenotypeKeys(vc); if (genotypeAttributeKeys.Count > 0) { foreach (String format in genotypeAttributeKeys) { if (!mHeader.hasFormatLine(format)) { fieldIsMissingFromHeaderError(vc, format, "FORMAT"); } } string genotypeFormatString = String.Join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys); lineToWrite.Append(VCFConstants.FIELD_SEPARATOR); lineToWrite.Append(genotypeFormatString); lineToWrite.Append(getGenotypeDataText(vc, alleleMap, genotypeAttributeKeys)); } } lineToWrite.Append("\n"); return(lineToWrite.ToString()); } catch (IOException e) { throw new Exception("Unable to write the VCF object:\n " + vc.ToString() + "\n", e); } }
/// <summary> /// Tells this builder that the resulting VariantContext should not contain any GenotypeContext /// </summary> public virtual VariantContextBuilder noGenotypes() { this.genotypes_Renamed = null; return(this); }
/// <summary> /// Tells this builder that the resulting VariantContext should use a GenotypeContext containing genotypes </summary> /// <param name="genotypes"> </param> public void SetGenotypes(params Genotype[] genotypes) { this.SetGenotypes(GenotypesContext.copy(genotypes)); }
/// <summary> /// Tells this builder that the resulting VariantContext should use a GenotypeContext containing genotypes /// /// Note that genotypes can be null -> meaning there are no genotypes /// </summary> /// <param name="genotypes"> </param> public void SetGenotypes(ICollection <Genotype> genotypes) { this.SetGenotypes(GenotypesContext.copy(genotypes)); }
/// <summary> /// Create a freshly allocated GenotypeContext containing the genotypes in toCopy /// </summary> /// <param name="toCopy"> the GenotypesContext to copy </param> /// <returns> an mutable GenotypeContext containing genotypes </returns> public static GenotypesContext copy(GenotypesContext toCopy) { return(create(new List <Genotype>(toCopy.Genotypes))); }