Example #1
0
		private void fullyDecodeGenotypes (VariantContextBuilder builder, VCFHeader header)
		{
			GenotypesContext gc = new GenotypesContext ();
			foreach (Genotype g in Genotypes) {
				gc.Add (fullyDecodeGenotypes (g, header));
			}
			builder.SetGenotypes (gc, false);
		}
Example #2
0
		/// <summary>
		/// the actual constructor.  Private access only
		/// constructors: see VariantContextBuilder
		/// </summary>
		/// <param name="source">          source </param>
		/// <param name="contig">          the contig </param>
		/// <param name="start">           the start base (one based) </param>
		/// <param name="stop">            the stop reference base (one based) </param>
		/// <param name="alleles">         alleles </param>
		/// <param name="genotypes">       genotypes map </param>
		/// <param name="log10PError">  qual </param>
		/// <param name="filters">         filters: use null for unfiltered and empty set for passes filters </param>
		/// <param name="attributes">      attributes </param>
		/// <param name="validationToPerform">     set of validation steps to take </param>
		protected internal VariantContext (string source, string ID, string contig, long start, long stop, ICollection<Allele> alleles, GenotypesContext genotypes, double log10PError, ISet<string> filters, IDictionary<string, object> attributes, bool fullyDecoded, Validation validationToPerform)
		{
			if (contig == null) {
				throw new System.ArgumentException ("Contig cannot be null");
			}
			this.contig = contig;
			this.start = start;
			this.stop = stop;

			// intern for efficiency.  equals calls will generate NPE if ID is inappropriately passed in as null
			if (ID == null || ID.Equals ("")) {
				throw new System.ArgumentException ("ID field cannot be the null or the empty string");
			}
			this.ID = ID.Equals (VCFConstants.EMPTY_ID_FIELD) ? VCFConstants.EMPTY_ID_FIELD : ID;

			this.CommonInfo = new CommonInfo (source, log10PError, filters, attributes);

			if (alleles == null) {
				throw new System.ArgumentException ("Alleles cannot be null");
			}

			// we need to make this a LinkedHashSet in case the user prefers a given ordering of alleles
			this.alleles = makeAlleles (alleles);

			if (genotypes == null || genotypes == NO_GENOTYPES) {
				this.genotypes = NO_GENOTYPES;
			} else {
				genotypes.Mutable = false;
				this.genotypes = genotypes;
			}

			// cache the REF and ALT alleles
			int nAlleles = alleles.Count;
			foreach (Allele a in alleles) {
				if (a.Reference) {
					REF = a;
				} // only cache ALT when biallelic
				else if (nAlleles == 2) {
					ALT = a;
				}
			}

			this.FullyDecoded = fullyDecoded;
			validate (validationToPerform);
		}
Example #3
0
        /// <summary>
        /// add a record to the file
        /// </summary>
        /// <param name="vc">      the Variant Context object </param>
        public override void add(VariantContext vc)
        {
            if (mHeader == null)
            {
                throw new IllegalStateException("The VCF Header must be written before records can be added: " + StreamName);
            }

            if (doNotWriteGenotypes)
            {
                vc = (new VariantContextBuilder(vc)).noGenotypes().make();
            }

            try
            {
                base.add(vc);

                IDictionary <Allele, string> alleleMap = buildAlleleMap(vc);

                // CHROM
                write(vc.Chr);
                write(VCFConstants.FIELD_SEPARATOR);

                // POS
                write(Convert.ToString(vc.Start));
                write(VCFConstants.FIELD_SEPARATOR);

                // ID
                string ID = vc.ID;
                write(ID);
                write(VCFConstants.FIELD_SEPARATOR);

                // REF
                string refString = vc.Reference.DisplayString;
                write(refString);
                write(VCFConstants.FIELD_SEPARATOR);

                // ALT
                if (vc.Variant)
                {
                    Allele altAllele = vc.getAlternateAllele(0);
                    string alt       = altAllele.DisplayString;
                    write(alt);

                    for (int i = 1; i < vc.AlternateAlleles.Count; i++)
                    {
                        altAllele = vc.getAlternateAllele(i);
                        alt       = altAllele.DisplayString;
                        write(",");
                        write(alt);
                    }
                }
                else
                {
                    write(VCFConstants.EMPTY_ALTERNATE_ALLELE_FIELD);
                }
                write(VCFConstants.FIELD_SEPARATOR);

                // QUAL
                if (!vc.hasLog10PError())
                {
                    write(VCFConstants.MISSING_VALUE_v4);
                }
                else
                {
                    write(formatQualValue(vc.PhredScaledQual));
                }
                write(VCFConstants.FIELD_SEPARATOR);

                // FILTER
                string filters = getFilterString(vc);
                write(filters);
                write(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;
                    }
                }
                writeInfoString(infoFields);

                // FORMAT
                GenotypesContext gc = vc.Genotypes;
                if (gc.LazyWithData && ((LazyGenotypesContext)gc).UnparsedGenotypeData is string)
                {
                    write(VCFConstants.FIELD_SEPARATOR);
                    write(((LazyGenotypesContext)gc).UnparsedGenotypeData.ToString());
                }
                else
                {
                    IList <string> genotypeAttributeKeys = calcVCFGenotypeKeys(vc, mHeader);
                    if (genotypeAttributeKeys.Count > 0)
                    {
                        foreach (String format in genotypeAttributeKeys)
                        {
                            if (!mHeader.hasFormatLine(format))
                            {
                                fieldIsMissingFromHeaderError(vc, format, "FORMAT");
                            }
                        }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String genotypeFormatString = org.broad.tribble.util.ParsingUtils.join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys);
                        string genotypeFormatString = ParsingUtils.join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys);

                        write(VCFConstants.FIELD_SEPARATOR);
                        write(genotypeFormatString);

                        addGenotypeData(vc, alleleMap, genotypeAttributeKeys);
                    }
                }

                write("\n");
                // note that we cannot call flush here if we want block gzipping to work properly
                // calling flush results in all gzipped blocks for each variant
                flushBuffer();
            }
            catch (IOException e)
            {
                throw new Exception("Unable to write the VCF object to " + StreamName, e);
            }
        }
Example #4
0
		/// <summary>
		/// Tells this builder that the resulting VariantContext should not contain any GenotypeContext
		/// </summary>
		public virtual VariantContextBuilder noGenotypes ()
		{
			this.genotypes_Renamed = null;
			return this;
		}
Example #5
0
		/// <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?
		}
Example #6
0
		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);
		}
Example #7
0
		/// <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;
		}