Example #1
0
        // --------------------------------------------------------------------------------
        //
        // implicit block
        //
        // The first four records of BCF are inline untype encoded data of:
        //
        // 4 byte integer chrom offset
        // 4 byte integer start
        // 4 byte integer ref length
        // 4 byte float qual
        //
        // --------------------------------------------------------------------------------
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private byte[] buildSitesData(VariantContext vc) throws IOException
        private sbyte[] buildSitesData(VariantContext vc)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int contigIndex = contigDictionary.get(vc.getChr());
            int contigIndex = contigDictionary[vc.Chr];

            if (contigIndex == -1)
            {
                throw new IllegalStateException(string.Format("Contig {0} not found in sequence dictionary from reference", vc.Chr));
            }

            // note use of encodeRawValue to not insert the typing byte
            encoder.encodeRawValue(contigIndex, BCF2Type.INT32);

            // pos.  GATK is 1 based, BCF2 is 0 based
            encoder.encodeRawValue(vc.Start - 1, BCF2Type.INT32);

            // ref length.  GATK is closed, but BCF2 is open so the ref length is GATK end - GATK start + 1
            // for example, a SNP is in GATK at 1:10-10, which has ref length 10 - 10 + 1 = 1
            encoder.encodeRawValue(vc.End - vc.Start + 1, BCF2Type.INT32);

            // qual
            if (vc.hasLog10PError())
            {
                encoder.encodeRawFloat((float)vc.PhredScaledQual);
            }
            else
            {
                encoder.encodeRawMissingValue(BCF2Type.FLOAT);
            }

            // info fields
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nAlleles = vc.getNAlleles();
            int nAlleles = vc.NAlleles;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nInfo = vc.getAttributes().size();
            int nInfo = vc.Attributes.Count;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nGenotypeFormatFields = getNGenotypeFormatFields(vc);
            int nGenotypeFormatFields = getNGenotypeFormatFields(vc);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nSamples = header.getNGenotypeSamples();
            int nSamples = header.NGenotypeSamples;

            encoder.encodeRawInt((nAlleles << 16) | (nInfo & 0x0000FFFF), BCF2Type.INT32);
            encoder.encodeRawInt((nGenotypeFormatFields << 24) | (nSamples & 0x00FFFFF), BCF2Type.INT32);

            buildID(vc);
            buildAlleles(vc);
            buildFilter(vc);
            buildInfo(vc);

            return(encoder.RecordBytes);
        }
Example #2
0
		// --------------------------------------------------------------------------------
		//
		// implicit block
		//
		// The first four records of BCF are inline untype encoded data of:
		//
		// 4 byte integer chrom offset
		// 4 byte integer start
		// 4 byte integer ref length
		// 4 byte float qual
		//
		// --------------------------------------------------------------------------------
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private byte[] buildSitesData(VariantContext vc) throws IOException
		private sbyte[] buildSitesData(VariantContext vc)
		{
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int contigIndex = contigDictionary.get(vc.getChr());
			int contigIndex = contigDictionary[vc.Chr];
			if (contigIndex == -1)
			{
				throw new IllegalStateException(string.Format("Contig {0} not found in sequence dictionary from reference", vc.Chr));
			}

			// note use of encodeRawValue to not insert the typing byte
			encoder.encodeRawValue(contigIndex, BCF2Type.INT32);

			// pos.  GATK is 1 based, BCF2 is 0 based
			encoder.encodeRawValue(vc.Start - 1, BCF2Type.INT32);

			// ref length.  GATK is closed, but BCF2 is open so the ref length is GATK end - GATK start + 1
			// for example, a SNP is in GATK at 1:10-10, which has ref length 10 - 10 + 1 = 1
			encoder.encodeRawValue(vc.End - vc.Start + 1, BCF2Type.INT32);

			// qual
			if (vc.hasLog10PError())
			{
				encoder.encodeRawFloat((float) vc.PhredScaledQual);
			}
			else
			{
				encoder.encodeRawMissingValue(BCF2Type.FLOAT);
			}

			// info fields
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nAlleles = vc.getNAlleles();
			int nAlleles = vc.NAlleles;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nInfo = vc.getAttributes().size();
			int nInfo = vc.Attributes.Count;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nGenotypeFormatFields = getNGenotypeFormatFields(vc);
			int nGenotypeFormatFields = getNGenotypeFormatFields(vc);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nSamples = header.getNGenotypeSamples();
			int nSamples = header.NGenotypeSamples;

			encoder.encodeRawInt((nAlleles << 16) | (nInfo & 0x0000FFFF), BCF2Type.INT32);
			encoder.encodeRawInt((nGenotypeFormatFields << 24) | (nSamples & 0x00FFFFF), BCF2Type.INT32);

			buildID(vc);
			buildAlleles(vc);
			buildFilter(vc);
			buildInfo(vc);

			return encoder.RecordBytes;
		}
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);
            }
        }