//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: protected GenotypesWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) protected internal GenotypesWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { if (fieldEncoder.hasConstantNumElements()) { nValuesPerGenotype = FieldEncoder.numElements(); } }
// ----------------------------------------------------------------- // // Master routine to look at the header, a specific line, and // build an appropriate Genotypes for that header element // // ----------------------------------------------------------------- //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: private BCF2FieldWriter.GenotypesWriter createGenotypesWriter(final VCFHeader header, final VCFFormatHeaderLine line, final BCF2Encoder encoder, final java.util.Map<String, Integer> dict) private BCF2FieldWriter.GenotypesWriter createGenotypesWriter(VCFHeader header, VCFFormatHeaderLine line, BCF2Encoder encoder, IDictionary <string, int?> dict) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final String field = line.getID(); string field = line.ID; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final BCF2FieldEncoder fieldEncoder = createFieldEncoder(line, encoder, dict, true); BCF2FieldEncoder fieldEncoder = createFieldEncoder(line, encoder, dict, true); if (field.Equals(VCFConstants.GENOTYPE_KEY)) { return(new BCF2FieldWriter.GTWriter(header, fieldEncoder)); } else if (line.ID.Equals(VCFConstants.GENOTYPE_FILTER_KEY)) { return(new BCF2FieldWriter.FTGenotypesWriter(header, fieldEncoder)); } else if (intGenotypeFieldAccessors.getAccessor(field) != null) { return(new BCF2FieldWriter.IGFGenotypesWriter(header, fieldEncoder, intGenotypeFieldAccessors.getAccessor(field))); } else if (line.Type == VCFHeaderLineType.Integer) { return(new BCF2FieldWriter.IntegerTypeGenotypesWriter(header, fieldEncoder)); } else { return(new BCF2FieldWriter.StaticallyTypeGenotypesWriter(header, fieldEncoder)); } }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public static VCFHeader writeHeader(VCFHeader header, final Writer writer, final boolean doNotWriteGenotypes, final String versionLine, final String streamNameForError) public static VCFHeader writeHeader(VCFHeader header, Writer writer, bool doNotWriteGenotypes, string versionLine, string streamNameForError) { header = doNotWriteGenotypes ? new VCFHeader(header.MetaDataInSortedOrder) : header; try { // the file format field needs to be written first writer.write(versionLine + "\n"); foreach (VCFHeaderLine line in header.MetaDataInSortedOrder) { if (VCFHeaderVersion.isFormatString(line.Key)) { continue; } writer.write(VCFHeader.METADATA_INDICATOR); writer.write(line.ToString()); writer.write("\n"); } // write out the column line writer.write(VCFHeader.HEADER_INDICATOR); bool isFirst = true; foreach (string field in VCFHeader.HEADER_FIELDS) { if (isFirst) { isFirst = false; // don't write out a field separator } else { writer.write(VCFConstants.FIELD_SEPARATOR); } writer.write(field.ToString()); } if (header.hasGenotypingData()) { writer.write(VCFConstants.FIELD_SEPARATOR); writer.write("FORMAT"); foreach (string sample in header.GenotypeSampleNames) { writer.write(VCFConstants.FIELD_SEPARATOR); writer.write(sample); } } writer.write("\n"); writer.flush(); // necessary so that writing to an output stream will work } catch (IOException e) { throw new Exception("IOException writing the VCF header to " + streamNameForError, e); } return(header); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public IGFGenotypesWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder, final IntGenotypeFieldAccessors.Accessor ige) public IGFGenotypesWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder, IntGenotypeFieldAccessors.Accessor ige) : base(header, fieldEncoder) { this.ige = ige; if (!(fieldEncoder is BCF2FieldEncoder.IntArray)) { throw new System.ArgumentException("BUG: IntGenotypesWriter requires IntArray encoder for field " + Field); } }
/// <summary> /// Can we safely write on the raw (undecoded) genotypes of an input VC? /// /// The cache depends on the undecoded lazy data header == lastVCFHeaderOfUnparsedGenotypes, in /// which case we return the previous result. If it's not cached, we use the BCF2Util to /// compare the VC header with our header (expensive) and cache it. /// </summary> /// <param name="lazyData"> /// @return </param> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: private boolean canSafelyWriteRawGenotypesBytes(final org.broadinstitute.variant.bcf2.BCF2Codec.LazyData lazyData) private bool canSafelyWriteRawGenotypesBytes(BCF2Codec.LazyData lazyData) { if (lazyData.header != lastVCFHeaderOfUnparsedGenotypes) { // result is already cached canPassOnUnparsedGenotypeDataForLastVCFHeader = BCF2Utils.headerLinesAreOrderedConsistently(this.header, lazyData.header); lastVCFHeaderOfUnparsedGenotypes = lazyData.header; } return(canPassOnUnparsedGenotypeDataForLastVCFHeader); }
public override void writeHeader(VCFHeader header) { // note we need to update the mHeader object after this call because they header // may have genotypes trimmed out of it, if doNotWriteGenotypes is true try { mHeader = writeHeader(header, writer, doNotWriteGenotypes, VersionLine, StreamName); flushBuffer(); } catch (IOException e) { throw new Exception("Couldn't write file " + StreamName, e); } }
/// <summary> /// Setup the FieldWriters appropriate to each INFO and FORMAT in the VCF header /// /// Must be called before any of the getter methods will work /// </summary> /// <param name="header"> a VCFHeader containing description for every INFO and FORMAT field we'll attempt to write out to BCF </param> /// <param name="encoder"> the encoder we are going to use to write out the BCF2 data </param> /// <param name="stringDictionary"> a map from VCFHeader strings to their offsets for encoding </param> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public void setup(final VCFHeader header, final BCF2Encoder encoder, final java.util.Map<String, Integer> stringDictionary) public virtual void setup(VCFHeader header, BCF2Encoder encoder, IDictionary <string, int?> stringDictionary) { foreach (VCFInfoHeaderLine line in header.InfoHeaderLines) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final String field = line.getID(); string field = line.ID; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final BCF2FieldWriter.SiteWriter writer = createInfoWriter(header, line, encoder, stringDictionary); BCF2FieldWriter.SiteWriter writer = createInfoWriter(header, line, encoder, stringDictionary); add(siteWriters, field, writer); } foreach (VCFFormatHeaderLine line in header.FormatHeaderLines) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final String field = line.getID(); string field = line.ID; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final BCF2FieldWriter.GenotypesWriter writer = createGenotypesWriter(header, line, encoder, stringDictionary); BCF2FieldWriter.GenotypesWriter writer = createGenotypesWriter(header, line, encoder, stringDictionary); add(genotypesWriters, field, writer); } }
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); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public StaticallyTypeGenotypesWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) public StaticallyTypeGenotypesWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { encodingType = FieldEncoder.StaticType; }
public static VCFCompoundHeaderLine GetMetaDataForField (VCFHeader header, string field) { VCFCompoundHeaderLine metaData = header.getFormatHeaderLine (field); if (metaData == null) { metaData = header.getInfoHeaderLine (field); } if (metaData == null) { throw new VCFParsingError ("Fully decoding VariantContext requires header line for all fields, but none was found for " + field); } return metaData; }
private void fullyDecodeInfo (VariantContextBuilder builder, VCFHeader header, bool lenientDecoding) { builder.Attributes = fullyDecodeAttributes (Attributes, header, lenientDecoding); }
// -------------------------------------------------------------------------------- // // Interface functions // // -------------------------------------------------------------------------------- public override void writeHeader(VCFHeader header) { // make sure the header is sorted correctly header = new VCFHeader(header.MetaDataInSortedOrder, header.GenotypeSampleNames); // create the config offsets map if (header.ContigLines.Count == 0) { if (ALLOW_MISSING_CONTIG_LINES) { if (GeneralUtils.DEBUG_MODE_ENABLED) { Console.Error.WriteLine("No contig dictionary found in header, falling back to reference sequence dictionary"); } createContigDictionary(VCFUtils.makeContigHeaderLines(RefDict, null)); } else { throw new IllegalStateException("Cannot write BCF2 file with missing contig lines"); } } else { createContigDictionary(header.ContigLines); } // set up the map from dictionary string values -> offset //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ArrayList<String> dict = org.broadinstitute.variant.bcf2.BCF2Utils.makeDictionary(header); List<string> dict = BCF2Utils.makeDictionary(header); for (int i = 0; i < dict.Count; i++) { stringDictionaryMap[dict[i]] = i; } sampleNames = header.GenotypeSampleNames.ToArray(); // setup the field encodings fieldManager.setup(header, encoder, stringDictionaryMap); try { // write out the header into a byte stream, get it's length, and write everything to the file //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ByteArrayOutputStream capture = new ByteArrayOutputStream(); ByteArrayOutputStream capture = new ByteArrayOutputStream(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final OutputStreamWriter writer = new OutputStreamWriter(capture); OutputStreamWriter writer = new OutputStreamWriter(capture); this.header = VCFWriter.writeHeader(header, writer, doNotWriteGenotypes, VCFWriter.VersionLine, "BCF2 stream"); writer.append('\0'); // the header is null terminated by a byte writer.close(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final byte[] headerBytes = capture.toByteArray(); sbyte[] headerBytes = capture.toByteArray(); (new BCFVersion(MAJOR_VERSION, MINOR_VERSION)).write(outputStream); BCF2Type.INT32.write(headerBytes.Length, outputStream); outputStream.write(headerBytes); } catch (IOException e) { throw new Exception("BCF2 stream: Got IOException while trying to write BCF2 header", e); } }
// ----------------------------------------------------------------- // // Master routine to look at the header, a specific line, and // build an appropriate SiteWriter for that header element // // ----------------------------------------------------------------- //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: private BCF2FieldWriter.SiteWriter createInfoWriter(final VCFHeader header, final VCFInfoHeaderLine line, final BCF2Encoder encoder, final java.util.Map<String, Integer> dict) private BCF2FieldWriter.SiteWriter createInfoWriter(VCFHeader header, VCFInfoHeaderLine line, BCF2Encoder encoder, IDictionary <string, int?> dict) { return(new BCF2FieldWriter.GenericSiteWriter(header, createFieldEncoder(line, encoder, dict, false))); }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public GenericSiteWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) public GenericSiteWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: protected SiteWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) protected internal SiteWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Requires({"header != null", "fieldEncoder != null"}) protected BCF2FieldWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: protected internal BCF2FieldWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) { this.header = header; this.fieldEncoder = fieldEncoder; }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public GTWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) public GTWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public FTGenotypesWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) public FTGenotypesWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { }
void writeHeader(VCFHeader header) { header = doNotWriteGenotypes ? new VCFHeader(header.MetaDataInSortedOrder) : header; try { // the file format field needs to be written first writer.Write(VERSION_LINE + "\n"); foreach (VCFHeaderLine line in header.MetaDataInSortedOrder) { if (VCFHeaderVersion.IsFormatString(line.Key)) { continue; } writer.Write(VCFHeader.METADATA_INDICATOR); writer.Write(line.ToString()); writer.Write("\n"); } // write out the column line writer.Write(VCFHeader.HEADER_INDICATOR); bool isFirst = true; foreach (string field in VCFHeader.HEADER_FIELDS) { if (isFirst) { isFirst = false; // don't write out a field separator } else { writer.Write(VCFConstants.FIELD_SEPARATOR); } writer.Write(field.ToString()); } if (header.hasGenotypingData()) { writer.Write(VCFConstants.FIELD_SEPARATOR); writer.Write("FORMAT"); foreach (string sample in header.GenotypeSampleNames) { writer.Write(VCFConstants.FIELD_SEPARATOR); writer.Write(sample); } } writer.Write("\n"); } catch (IOException e) { throw new Exception("IOException writing the VCF header." , e); } }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public static VCFHeader writeHeader(VCFHeader header, final Writer writer, final boolean doNotWriteGenotypes, final String versionLine, final String streamNameForError) public static VCFHeader writeHeader(VCFHeader header, Writer writer, bool doNotWriteGenotypes, string versionLine, string streamNameForError) { header = doNotWriteGenotypes ? new VCFHeader(header.MetaDataInSortedOrder) : header; try { // the file format field needs to be written first writer.write(versionLine + "\n"); foreach (VCFHeaderLine line in header.MetaDataInSortedOrder) { if (VCFHeaderVersion.isFormatString(line.Key)) { continue; } writer.write(VCFHeader.METADATA_INDICATOR); writer.write(line.ToString()); writer.write("\n"); } // write out the column line writer.write(VCFHeader.HEADER_INDICATOR); bool isFirst = true; foreach (string field in VCFHeader.HEADER_FIELDS) { if (isFirst) { isFirst = false; // don't write out a field separator } else { writer.write(VCFConstants.FIELD_SEPARATOR); } writer.write(field.ToString()); } if (header.hasGenotypingData()) { writer.write(VCFConstants.FIELD_SEPARATOR); writer.write("FORMAT"); foreach (string sample in header.GenotypeSampleNames) { writer.write(VCFConstants.FIELD_SEPARATOR); writer.write(sample); } } writer.write("\n"); writer.flush(); // necessary so that writing to an output stream will work } catch (IOException e) { throw new Exception("IOException writing the VCF header to " + streamNameForError, e); } return header; }
/// <summary> /// Determine which genotype fields are in use in the genotypes in VC </summary> /// <param name="vc"> </param> /// <returns> an ordered list of genotype fields in use in VC. If vc has genotypes this will always include GT first </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public static List<String> calcVCFGenotypeKeys(final VariantContext vc, final VCFHeader header) public static IList <string> calcVCFGenotypeKeys(VariantContext vc, VCFHeader header) { Set <string> keys = new HashSet <string>(); bool sawGoodGT = false; bool sawGoodQual = false; bool sawGenotypeFilter = false; bool sawDP = false; bool sawAD = false; bool sawPL = false; foreach (Genotype g in vc.Genotypes) { keys.addAll(g.ExtendedAttributes.Keys); if (g.Available) { sawGoodGT = true; } if (g.hasGQ()) { sawGoodQual = true; } if (g.hasDP()) { sawDP = true; } if (g.hasAD()) { sawAD = true; } if (g.hasPL()) { sawPL = true; } if (g.Filtered) { sawGenotypeFilter = true; } } if (sawGoodQual) { keys.add(VCFConstants.GENOTYPE_QUALITY_KEY); } if (sawDP) { keys.add(VCFConstants.DEPTH_KEY); } if (sawAD) { keys.add(VCFConstants.GENOTYPE_ALLELE_DEPTHS); } if (sawPL) { keys.add(VCFConstants.GENOTYPE_PL_KEY); } if (sawGenotypeFilter) { keys.add(VCFConstants.GENOTYPE_FILTER_KEY); } IList <string> sortedList = ParsingUtils.sortList(new List <string>(keys)); // make sure the GT is first if (sawGoodGT) { IList <string> newList = new List <string>(sortedList.Count + 1); newList.Add(VCFConstants.GENOTYPE_KEY); newList.AddRange(sortedList); sortedList = newList; } if (sortedList.Count == 0 && header.hasGenotypingData()) { // this needs to be done in case all samples are no-calls return(Collections.singletonList(VCFConstants.GENOTYPE_KEY)); } else { return(sortedList); } }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public IntegerTypeGenotypesWriter(final Bio.VCF.VCFHeader header, final BCF2FieldEncoder fieldEncoder) public IntegerTypeGenotypesWriter(VCFHeader header, BCF2FieldEncoder fieldEncoder) : base(header, fieldEncoder) { }
public abstract void writeHeader(VCFHeader header);
/// <summary> /// create a VCF header from a set of header record lines /// </summary> /// <param name="headerStrings"> a list of strings that represent all the ## and # entries </param> /// <returns> a VCFHeader object </returns> protected internal virtual VCFHeader parseHeaderFromLines (IList<string> headerStrings, VCFHeaderVersion version) { this.version = version; ISet<VCFHeaderLine> metaData = new LinkedHashSet<VCFHeaderLine> (); ISet<string> sampleNames = new LinkedHashSet<string> (); int contigCounter = 0; // iterate over all the passed in strings foreach (string str in headerStrings) { if (!str.StartsWith (VCFHeader.METADATA_INDICATOR)) {//presumably the #CHROM POS ID REF ALT QUAL FILTER INFO etc. line string[] strings = str.Substring (1).Split (VCFConstants.FIELD_SEPARATOR_CHAR); //check for null last string, grrr... if (String.IsNullOrEmpty (strings.Last ())) { strings = strings.Take (strings.Length - 1).ToArray (); } if (strings.Length < VCFHeader.HEADER_FIELDS.Length) { throw new VCFParsingError ("There are not enough columns present in the header line: " + str); } //Verify Arrays var misMatchedColumns = Enumerable.Range (0, VCFHeader.HEADER_FIELDS.Length).Where (x => VCFHeader.HEADER_FIELDS [x] != strings [x]).Select (x => strings [x]).ToArray (); if (misMatchedColumns.Length > 0) { throw new VCFParsingError ("We were not expecting column name '" + misMatchedColumns [0] + " in that position"); } int arrayIndex = VCFHeader.HEADER_FIELDS.Length;//start after verified columns bool sawFormatTag = false; if (arrayIndex < strings.Length) { if (!strings [arrayIndex].Equals ("FORMAT")) { throw new VCFParsingError ("we were expecting column name 'FORMAT' but we saw '" + strings [arrayIndex] + "'"); } sawFormatTag = true; arrayIndex++; } while (arrayIndex < strings.Length) { sampleNames.Add (strings [arrayIndex++]); } if (sawFormatTag && sampleNames.Count == 0) { throw new VCFParsingError ("The FORMAT field was provided but there is no genotype/sample data"); } } else { if (str.StartsWith (VCFConstants.INFO_HEADER_START)) { VCFInfoHeaderLine info = new VCFInfoHeaderLine (str.Substring (7), version); metaData.Add (info); } else if (str.StartsWith (VCFConstants.FILTER_HEADER_START)) { VCFFilterHeaderLine filter = new VCFFilterHeaderLine (str.Substring (9), version); metaData.Add (filter); } else if (str.StartsWith (VCFConstants.FORMAT_HEADER_START)) { VCFFormatHeaderLine format = new VCFFormatHeaderLine (str.Substring (9), version); metaData.Add (format); } else if (str.StartsWith (VCFConstants.CONTIG_HEADER_START)) { VCFContigHeaderLine contig = new VCFContigHeaderLine (str.Substring (9), version, VCFConstants.CONTIG_HEADER_START.Substring (2), contigCounter++); metaData.Add (contig); } else if (str.StartsWith (VCFConstants.ALT_HEADER_START)) { //TODO: Consider giving Alt header lines their own class VCFSimpleHeaderLine alt = new VCFSimpleHeaderLine (str.Substring (6), version, VCFConstants.ALT_HEADER_START.Substring (2), "ID", "Description"); metaData.Add (alt); } else { int equals = str.IndexOf ("="); if (equals != -1) { metaData.Add (new VCFHeaderLine (str.Substring (2, equals - 2), str.Substring (equals + 1))); } } } } this.header = new VCFHeader (metaData, sampleNames); if (doOnTheFlyModifications) { this.header = VCFStandardHeaderLines.repairStandardHeaderLines (this.header); } return this.header; }
/// <summary> /// Determine which genotype fields are in use in the genotypes in VC </summary> /// <param name="vc"> </param> /// <returns> an ordered list of genotype fields in use in VC. If vc has genotypes this will always include GT first </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public static List<String> calcVCFGenotypeKeys(final VariantContext vc, final VCFHeader header) public static IList<string> calcVCFGenotypeKeys(VariantContext vc, VCFHeader header) { Set<string> keys = new HashSet<string>(); bool sawGoodGT = false; bool sawGoodQual = false; bool sawGenotypeFilter = false; bool sawDP = false; bool sawAD = false; bool sawPL = false; foreach (Genotype g in vc.Genotypes) { keys.addAll(g.ExtendedAttributes.Keys); if (g.Available) { sawGoodGT = true; } if (g.hasGQ()) { sawGoodQual = true; } if (g.hasDP()) { sawDP = true; } if (g.hasAD()) { sawAD = true; } if (g.hasPL()) { sawPL = true; } if (g.Filtered) { sawGenotypeFilter = true; } } if (sawGoodQual) { keys.add(VCFConstants.GENOTYPE_QUALITY_KEY); } if (sawDP) { keys.add(VCFConstants.DEPTH_KEY); } if (sawAD) { keys.add(VCFConstants.GENOTYPE_ALLELE_DEPTHS); } if (sawPL) { keys.add(VCFConstants.GENOTYPE_PL_KEY); } if (sawGenotypeFilter) { keys.add(VCFConstants.GENOTYPE_FILTER_KEY); } IList<string> sortedList = ParsingUtils.sortList(new List<string>(keys)); // make sure the GT is first if (sawGoodGT) { IList<string> newList = new List<string>(sortedList.Count + 1); newList.Add(VCFConstants.GENOTYPE_KEY); newList.AddRange(sortedList); sortedList = newList; } if (sortedList.Count == 0 && header.hasGenotypingData()) { // this needs to be done in case all samples are no-calls return Collections.singletonList(VCFConstants.GENOTYPE_KEY); } else { return sortedList; } }
/// <summary> /// Can we safely write on the raw (undecoded) genotypes of an input VC? /// /// The cache depends on the undecoded lazy data header == lastVCFHeaderOfUnparsedGenotypes, in /// which case we return the previous result. If it's not cached, we use the BCF2Util to /// compare the VC header with our header (expensive) and cache it. /// </summary> /// <param name="lazyData"> /// @return </param> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: private boolean canSafelyWriteRawGenotypesBytes(final org.broadinstitute.variant.bcf2.BCF2Codec.LazyData lazyData) private bool canSafelyWriteRawGenotypesBytes(BCF2Codec.LazyData lazyData) { if (lazyData.header != lastVCFHeaderOfUnparsedGenotypes) { // result is already cached canPassOnUnparsedGenotypeDataForLastVCFHeader = BCF2Utils.headerLinesAreOrderedConsistently(this.header,lazyData.header); lastVCFHeaderOfUnparsedGenotypes = lazyData.header; } return canPassOnUnparsedGenotypeDataForLastVCFHeader; }
// --------------------------------------------------------------------------------------------------------- // // Fully decode // // --------------------------------------------------------------------------------------------------------- /// <summary> /// Return a VC equivalent to this one but where all fields are fully decoded /// /// See VariantContext document about fully decoded /// </summary> /// <param name="header"> containing types about all fields in this VC </param> /// <returns> a fully decoded version of this VC </returns> public VariantContext FullyDecode (VCFHeader header, bool lenientDecoding) { if (FullyDecoded) { return this; } else { // TODO -- warning this is potentially very expensive as it creates copies over and over VariantContextBuilder builder = new VariantContextBuilder (this); fullyDecodeInfo (builder, header, lenientDecoding); fullyDecodeGenotypes (builder, header); builder.FullyDecoded = true; return builder.make (); } }
private IDictionary<string, object> fullyDecodeAttributes (IDictionary<string, object> attributes, VCFHeader header, bool lenientDecoding) { IDictionary<string, object> newAttributes = new Dictionary<string, object> (10); foreach (KeyValuePair<string, object> attr in attributes) { string field = attr.Key; if (field.Equals (VCFConstants.GENOTYPE_FILTER_KEY)) { continue; // gross, FT is part of the extended attributes } VCFCompoundHeaderLine format = VariantContextUtils.GetMetaDataForField (header, field); object decoded = decodeValue (field, attr.Value, format); if (decoded != null && !lenientDecoding && format.CountType != VCFHeaderLineCount.UNBOUNDED && format.Type != VCFHeaderLineType.Flag) { // we expect exactly the right number of elements int obsSize = decoded is IList ? ((IList)decoded).Count : 1; int expSize = format.getCount (this); if (obsSize != expSize) { throw new VCFParsingError ("Discordant field size detected for field " + field + " at " + Chr + ":" + Start + ". Field had " + obsSize + " values " + "but the header says this should have " + expSize + " values based on header record " + format); } } newAttributes [field] = decoded; } return newAttributes; }
public void WriteVariants(string outFileName, IEnumerable<VariantContext> variants,VCFHeader header) { System.Text.Encoding enc = System.Text.Encoding.GetEncoding(encodingName); writer = new StreamWriter(outFileName, false, enc); writeHeader(header); foreach (var vc in variants) { writer.Write(getVariantLinetoWrite(vc)); } writer.Close(); }
private Genotype fullyDecodeGenotypes (Genotype g, VCFHeader header) { IDictionary<string, object> map = fullyDecodeAttributes (g.ExtendedAttributes, header, true); var g2 = new GenotypeBuilder (g); g2.AddAttributes (map); return g2.Make (); }
// -------------------------------------------------------------------------------- // // Interface functions // // -------------------------------------------------------------------------------- public override void writeHeader(VCFHeader header) { // make sure the header is sorted correctly header = new VCFHeader(header.MetaDataInSortedOrder, header.GenotypeSampleNames); // create the config offsets map if (header.ContigLines.Count == 0) { if (ALLOW_MISSING_CONTIG_LINES) { if (GeneralUtils.DEBUG_MODE_ENABLED) { Console.Error.WriteLine("No contig dictionary found in header, falling back to reference sequence dictionary"); } createContigDictionary(VCFUtils.makeContigHeaderLines(RefDict, null)); } else { throw new IllegalStateException("Cannot write BCF2 file with missing contig lines"); } } else { createContigDictionary(header.ContigLines); } // set up the map from dictionary string values -> offset //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ArrayList<String> dict = org.broadinstitute.variant.bcf2.BCF2Utils.makeDictionary(header); List <string> dict = BCF2Utils.makeDictionary(header); for (int i = 0; i < dict.Count; i++) { stringDictionaryMap[dict[i]] = i; } sampleNames = header.GenotypeSampleNames.ToArray(); // setup the field encodings fieldManager.setup(header, encoder, stringDictionaryMap); try { // write out the header into a byte stream, get it's length, and write everything to the file //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ByteArrayOutputStream capture = new ByteArrayOutputStream(); ByteArrayOutputStream capture = new ByteArrayOutputStream(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final OutputStreamWriter writer = new OutputStreamWriter(capture); OutputStreamWriter writer = new OutputStreamWriter(capture); this.header = VCFWriter.writeHeader(header, writer, doNotWriteGenotypes, VCFWriter.VersionLine, "BCF2 stream"); writer.append('\0'); // the header is null terminated by a byte writer.close(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final byte[] headerBytes = capture.toByteArray(); sbyte[] headerBytes = capture.toByteArray(); (new BCFVersion(MAJOR_VERSION, MINOR_VERSION)).write(outputStream); BCF2Type.INT32.write(headerBytes.Length, outputStream); outputStream.write(headerBytes); } catch (IOException e) { throw new Exception("BCF2 stream: Got IOException while trying to write BCF2 header", e); } }