Example #1
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
//ORIGINAL LINE: private BCF2FieldEncoder createFieldEncoder(final VCFCompoundHeaderLine line, final BCF2Encoder encoder, final java.util.Map<String, Integer> dict, final boolean createGenotypesEncoders)
        private BCF2FieldEncoder createFieldEncoder(VCFCompoundHeaderLine line, BCF2Encoder encoder, IDictionary <string, int?> dict, bool createGenotypesEncoders)
        {
            if (createGenotypesEncoders && intGenotypeFieldAccessors.getAccessor(line.ID) != null)
            {
                if (GeneralUtils.DEBUG_MODE_ENABLED && line.Type != VCFHeaderLineType.Integer)
                {
                    Console.Error.WriteLine("Warning: field " + line.ID + " expected to encode an integer but saw " + line.Type + " for record " + line);
                }
                return(new BCF2FieldEncoder.IntArray(line, dict));
            }
            else if (createGenotypesEncoders && line.ID.Equals(VCFConstants.GENOTYPE_KEY))
            {
                return(new BCF2FieldEncoder.GenericInts(line, dict));
            }
            else
            {
                switch (line.Type)
                {
                case char?:
                case string:
                    return(new BCF2FieldEncoder.StringOrCharacter(line, dict));

                case Flag:
                    return(new BCF2FieldEncoder.Flag(line, dict));

                case float?:
                    return(new BCF2FieldEncoder.Float(line, dict));

                case int?:
                    if (line.FixedCount && line.Count == 1)
                    {
                        return(new BCF2FieldEncoder.AtomicInt(line, dict));
                    }
                    else
                    {
                        return(new BCF2FieldEncoder.GenericInts(line, dict));
                    }

                default:
                    throw new System.ArgumentException("Unexpected type for field " + line.ID);
                }
            }
        }
Example #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]);
                }
            }
        }