コード例 #1
0
ファイル: BCF2Writer.cs プロジェクト: smith-chem-wisc/Bio.VCF
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private byte[] buildSamplesData(final VariantContext vc) throws IOException
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
        private sbyte[] buildSamplesData(VariantContext vc)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.broadinstitute.variant.bcf2.BCF2Codec.LazyData lazyData = getLazyData(vc);
            BCF2Codec.LazyData lazyData = getLazyData(vc);             // has critical side effects
            if (lazyData != null)
            {
                // we never decoded any data from this BCF file, so just pass it back
                return(lazyData.bytes);
            }

            // we have to do work to convert the VC into a BCF2 byte stream
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final List<String> genotypeFields = VCFWriter.calcVCFGenotypeKeys(vc, header);
            IList <string> genotypeFields = VCFWriter.calcVCFGenotypeKeys(vc, header);

            foreach (String field in genotypeFields)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final BCF2FieldWriter.GenotypesWriter writer = fieldManager.getGenotypeFieldWriter(field);
                BCF2FieldWriter.GenotypesWriter writer = fieldManager.getGenotypeFieldWriter(field);
                if (writer == null)
                {
                    errorUnexpectedFieldToWrite(vc, field, "FORMAT");
                }

                Debug.Assert(writer != null);

                writer.start(encoder, vc);
                foreach (String name in sampleNames)
                {
                    Genotype g = vc.getGenotype(name);
                    if (g == null)
                    {
                        g = GenotypeBuilder.createMissing(name, writer.nValuesPerGenotype);
                    }
                    writer.addGenotype(encoder, vc, g);
                }
                writer.done(encoder, vc);
            }
            return(encoder.RecordBytes);
        }
コード例 #2
0
        /// <summary>
        /// add the genotype data
        /// </summary>
        /// <param name="vc">                     the variant context </param>
        /// <param name="genotypeFormatKeys">  Genotype formatting string </param>
        /// <param name="alleleMap">              alleles for this context </param>
        /// <exception cref="IOException"> for writer </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void addGenotypeData(VariantContext vc, Map<Allele, String> alleleMap, List<String> genotypeFormatKeys) throws IOException
        private void addGenotypeData(VariantContext vc, IDictionary <Allele, string> alleleMap, IList <string> genotypeFormatKeys)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int ploidy = vc.getMaxPloidy(2);
            int ploidy = vc.getMaxPloidy(2);

            foreach (string sample in mHeader.GenotypeSampleNames)
            {
                write(VCFConstants.FIELD_SEPARATOR);

                Genotype g = vc.getGenotype(sample);
                if (g == null)
                {
                    g = GenotypeBuilder.createMissing(sample, ploidy);
                }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final List<String> attrs = new ArrayList<String>(genotypeFormatKeys.size());
                IList <string> attrs = new List <string>(genotypeFormatKeys.Count);
                foreach (string field in genotypeFormatKeys)
                {
                    if (field.Equals(VCFConstants.GENOTYPE_KEY))
                    {
                        if (!g.Available)
                        {
                            throw new IllegalStateException("GTs cannot be missing for some samples if they are available for others in the record");
                        }

                        writeAllele(g.getAllele(0), alleleMap);
                        for (int i = 1; i < g.Ploidy; i++)
                        {
                            write(g.Phased ? VCFConstants.PHASED : VCFConstants.UNPHASED);
                            writeAllele(g.getAllele(i), alleleMap);
                        }

                        continue;
                    }
                    else
                    {
                        string outputValue;
                        if (field.Equals(VCFConstants.GENOTYPE_FILTER_KEY))
                        {
                            outputValue = g.Filtered ? g.Filters : VCFConstants.PASSES_FILTERS_v4;
                        }
                        else
                        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final IntGenotypeFieldAccessors.Accessor accessor = intGenotypeFieldAccessors.getAccessor(field);
                            IntGenotypeFieldAccessors.Accessor accessor = intGenotypeFieldAccessors.getAccessor(field);
                            if (accessor != null)
                            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int[] intValues = accessor.getValues(g);
                                int[] intValues = accessor.getValues(g);
                                if (intValues == null)
                                {
                                    outputValue = VCFConstants.MISSING_VALUE_v4;
                                }
                                else if (intValues.Length == 1)                                 // fast path
                                {
                                    outputValue = Convert.ToString(intValues[0]);
                                }
                                else
                                {
                                    StringBuilder sb = new StringBuilder();
                                    sb.Append(intValues[0]);
                                    for (int i = 1; i < intValues.Length; i++)
                                    {
                                        sb.Append(",");
                                        sb.Append(intValues[i]);
                                    }
                                    outputValue = sb.ToString();
                                }
                            }
                            else
                            {
                                object val = g.hasExtendedAttribute(field) ? g.getExtendedAttribute(field) : VCFConstants.MISSING_VALUE_v4;

                                VCFFormatHeaderLine metaData = mHeader.getFormatHeaderLine(field);
                                if (metaData != null)
                                {
                                    int numInFormatField = metaData.getCount(vc);
                                    if (numInFormatField > 1 && val.Equals(VCFConstants.MISSING_VALUE_v4))
                                    {
                                        // If we have a missing field but multiple values are expected, we need to construct a new string with all fields.
                                        // For example, if Number=2, the string has to be ".,."
                                        StringBuilder sb = new StringBuilder(VCFConstants.MISSING_VALUE_v4);
                                        for (int i = 1; i < numInFormatField; i++)
                                        {
                                            sb.Append(",");
                                            sb.Append(VCFConstants.MISSING_VALUE_v4);
                                        }
                                        val = sb.ToString();
                                    }
                                }

                                // assume that if key is absent, then the given string encoding suffices
                                outputValue = formatVCFField(val);
                            }
                        }

                        if (outputValue != null)
                        {
                            attrs.Add(outputValue);
                        }
                    }
                }

                // strip off trailing missing values
                for (int i = attrs.Count - 1; i >= 0; i--)
                {
                    if (isMissingValue(attrs[i]))
                    {
                        attrs.RemoveAt(i);
                    }
                    else
                    {
                        break;
                    }
                }

                for (int i = 0; i < attrs.Count; i++)
                {
                    if (i > 0 || genotypeFormatKeys.Contains(VCFConstants.GENOTYPE_KEY))
                    {
                        write(VCFConstants.GENOTYPE_FIELD_SEPARATOR);
                    }
                    write(attrs[i]);
                }
            }
        }