Exemple #1
0
        /// <summary>
        /// Add a record to the file
        /// </summary>
        /// <param name="vc">The Variant Context object </param>
        protected string getVariantLinetoWrite(VariantContext vc)
        {
            if (doNotWriteGenotypes)
            {
                vc = (new VariantContextBuilder(vc)).noGenotypes().make();
            }
            try
            {
                //Convert alleles to 1,2,3,etc. numbering
                IDictionary <Allele, string> alleleMap = buildAlleleMap(vc);
                // CHROM
                StringBuilder lineToWrite = new StringBuilder();
                //Add chr, pos, id, ref
                lineToWrite.Append(String.Join(VCFConstants.FIELD_SEPARATOR, vc.Chr, vc.Start.ToString(), vc.ID, vc.Reference.DisplayString));
                // ALT
                if (vc.Variant)
                {
                    Allele altAllele = vc.GetAlternateAllele(0);
                    string alt       = altAllele.DisplayString;
                    lineToWrite.Append(alt);

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

                // QUAL
                if (!vc.HasLog10PError)
                {
                    lineToWrite.Append(VCFConstants.MISSING_VALUE_v4);
                }
                else
                {
                    lineToWrite.Append(formatQualValue(vc.PhredScaledQual));
                }
                lineToWrite.Append(VCFConstants.FIELD_SEPARATOR);

                // FILTER
                string filters = getFilterString(vc);
                lineToWrite.Append(filters);
                lineToWrite.Append(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;
                    }
                }
                lineToWrite.Append(getInfoString(infoFields));;

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

                        string genotypeFormatString = String.Join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys);
                        lineToWrite.Append(VCFConstants.FIELD_SEPARATOR);
                        lineToWrite.Append(genotypeFormatString);
                        lineToWrite.Append(getGenotypeDataText(vc, alleleMap, genotypeAttributeKeys));
                    }
                }
                lineToWrite.Append("\n");
                return(lineToWrite.ToString());
            }
            catch (IOException e)
            {
                throw new Exception("Unable to write the VCF object:\n " + vc.ToString() + "\n", e);
            }
        }
Exemple #2
0
        /// <summary>
        /// Add a record to the file
        /// </summary>
        /// <param name="vc">The Variant Context object </param>
        protected string getVariantLinetoWrite(VariantContext vc)
        {
     
            if (doNotWriteGenotypes)
            {
                vc = (new VariantContextBuilder(vc)).noGenotypes().make();
            }
            try
            {
                //Convert alleles to 1,2,3,etc. numbering
                IDictionary<Allele, string> alleleMap = buildAlleleMap(vc);
                // CHROM
                StringBuilder lineToWrite = new StringBuilder();
                //Add chr, pos, id, ref
                lineToWrite.Append(String.Join(VCFConstants.FIELD_SEPARATOR, vc.Chr, vc.Start.ToString(), vc.ID, vc.Reference.DisplayString));
                // ALT
                if (vc.Variant)
                {
                    Allele altAllele = vc.GetAlternateAllele(0);
                    string alt = altAllele.DisplayString;
                    lineToWrite.Append(alt);

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

                // QUAL
                if (!vc.HasLog10PError)
                {
                    lineToWrite.Append(VCFConstants.MISSING_VALUE_v4);
                }
                else
                {
                    lineToWrite.Append(formatQualValue(vc.PhredScaledQual));
                }
                lineToWrite.Append(VCFConstants.FIELD_SEPARATOR);

                // FILTER
                string filters = getFilterString(vc);
                lineToWrite.Append(filters);
                lineToWrite.Append(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;
                    }
                }
                lineToWrite.Append(getInfoString(infoFields)); ;

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

                        string genotypeFormatString = String.Join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys);
                        lineToWrite.Append(VCFConstants.FIELD_SEPARATOR);
                        lineToWrite.Append(genotypeFormatString);
                        lineToWrite.Append(getGenotypeDataText(vc, alleleMap, genotypeAttributeKeys));
                    }
                }
                lineToWrite.Append("\n");
                return lineToWrite.ToString();
            }
            catch (IOException e)
            {
                throw new Exception("Unable to write the VCF object:\n " + vc.ToString() + "\n", e);
            }
        }