Java Code Examples for htsjdk.variant.variantcontext.Genotype#getExtendedAttribute()

The following examples show how to use htsjdk.variant.variantcontext.Genotype#getExtendedAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PonBuilder.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
public void add(@NotNull final VariantContext context) {
    final VariantHotspot hotspot = hotspot(context);
    final Counter counter = map.computeIfAbsent(hotspot, Counter::new);
    final Genotype genotype = context.getGenotype(0);
    if (!hotspot.ref().contains("N") && genotype.hasExtendedAttribute(SageVCF.RAW_ALLELIC_DEPTH)) {
        String rawDepth = (String) genotype.getExtendedAttribute(SageVCF.RAW_ALLELIC_DEPTH);
        int allelicDepth = Integer.parseInt(rawDepth.split(",")[1]);
        if (allelicDepth >= MIN_INPUT_ALLELIC_DEPTH) {
            counter.increment(allelicDepth);
        }
    }
}
 
Example 2
Source File: GATKProtectedVariantContextUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns an attribute as a string.
 * @param genotype the source genotype.
 * @param key the attribute key.
 * @param defaultValue default value to return in case the attribute does not have a value defined.
 * @return the string version of the attribute value.
 * @throws IllegalArgumentException if {@code genotype} or {@code key} is {@code null}.
 */
public static String getAttributeAsString(final Genotype genotype, final String key, final String defaultValue) {
    Utils.nonNull(genotype);
    Utils.nonNull(key);
    final Object value = genotype.getExtendedAttribute(key);
    if (value == null) {
        return defaultValue;
    } else {
        return String.valueOf(value);
    }
}
 
Example 3
Source File: GATKProtectedVariantContextUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns an attribute as an integer.
 * @param genotype the source genotype.
 * @param key the attribute key.
 * @param defaultValue default value to return in case the attribute does not have a value defined.
 * @return the string version of the attribute value.
 * @throws IllegalArgumentException if {@code genotype} or {@code key} is {@code null}.
 * @throws NumberFormatException if the annotation exists, and its string transformation cannot be convert into an integer.
 */
public static int getAttributeAsInt(final Genotype genotype, final String key, final int defaultValue) {
    Utils.nonNull(genotype);
    Utils.nonNull(key);
    final Object value = genotype.getExtendedAttribute(key);
    if (value == null) {
        return defaultValue;
    } else {
        try {
            return Integer.parseInt(String.valueOf(value));
        } catch (final NumberFormatException ex) {
            throw new NumberFormatException(String.format("attribute '%s' does not have a valid integer value: '%s'", key, String.valueOf(value)));
        }
    }
}
 
Example 4
Source File: GATKProtectedVariantContextUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns an attribute as a double.
 * @param genotype the source genotype.
 * @param key the attribute key.
 * @param defaultValue default value to return in case the attribute does not have a value defined.
 * @return the string version of the attribute value.
 * @throws IllegalArgumentException if {@code genotype} or {@code key} is {@code null}.
 * @throws NumberFormatException if the annotation exists, and its string transformation cannot be convert into a double.
 */
public static double getAttributeAsDouble(final Genotype genotype, final String key, final double defaultValue) {
    Utils.nonNull(genotype);
    Utils.nonNull(key);
    final Object value = genotype.getExtendedAttribute(key);
    if (value == null) {
        return defaultValue;
    } else {
        try {
            return Double.parseDouble(String.valueOf(value));
        } catch (final NumberFormatException ex) {
            throw new NumberFormatException(String.format("attribute '%s' does not have a valid double value: '%s'", key, String.valueOf(value)));
        }
    }
}
 
Example 5
Source File: GATKProtectedVariantContextUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Gets an attribute value by transforming the string its string representation using an translation function.
 * @param g the source genotype.
 * @param key the attribute key.
 * @param translate the function to translate from an string to the return class of interest.
 * @param defaultValue the default value to return in case the attribute is not defined or is declared as missing.
 * @param <T> the return object type.
 * @return {@code null} or an instance of the class {@link T}
 */
public static <T> T getAttributeAsObject(final Genotype g, final String key, final Function<String, T> translate,
                                                                          final T defaultValue) {
    Utils.nonNull(g);
    Utils.nonNull(key);
    final Object value = g.getExtendedAttribute(key);
    if (value == null || VCFConstants.MISSING_VALUE_v4.equals(value)) {
        return defaultValue;
    } else {
        return translate.apply(String.valueOf(value));
    }
}
 
Example 6
Source File: HaplotypeMap.java    From picard with MIT License 5 votes vote down vote up
static private String anchorFromVc(final VariantContext vc) {
    final Genotype genotype = vc.getGenotype(0);

    if (genotype == null || !genotype.hasExtendedAttribute(VCFConstants.PHASE_SET_KEY)) {
        return SYNTHETIC_PHASESET_PREFIX + "_" + vc.getContig() + "_" + vc.getStart();
    } else {
        return PHASESET_PREFIX + "_" + vc.getContig() + "_" + genotype.getExtendedAttribute(VCFConstants.PHASE_SET_KEY);
    }
}
 
Example 7
Source File: VariantContextGetters.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns an attribute as an integer.
 * @param genotype the source genotype.
 * @param key the attribute key.
 * @param defaultValue default value to return in case the attribute does not have a value defined.
 * @return the string version of the attribute value.
 * @throws IllegalArgumentException if {@code genotype} or {@code key} is {@code null}.
 * @throws NumberFormatException if the annotation exists, and its string transformation cannot be convert into an integer.
 */
public static int getAttributeAsInt(final Genotype genotype, final String key, final int defaultValue) {
    Utils.nonNull(genotype);
    Utils.nonNull(key);
    final Object value = genotype.getExtendedAttribute(key);
    if (value == null) {
        return defaultValue;
    } else {
        try {
            return Integer.parseInt(String.valueOf(value));
        } catch (final NumberFormatException ex) {
            throw new NumberFormatException(String.format("attribute '%s' does not have a valid integer value: '%s'", key, String.valueOf(value)));
        }
    }
}
 
Example 8
Source File: VariantContextGetters.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns an attribute as a double.
 * @param genotype the source genotype.
 * @param key the attribute key.
 * @param defaultValue default value to return in case the attribute does not have a value defined.
 * @return the string version of the attribute value.
 * @throws IllegalArgumentException if {@code genotype} or {@code key} is {@code null}.
 * @throws NumberFormatException if the annotation exists, and its string transformation cannot be convert into a double.
 */
public static double getAttributeAsDouble(final Genotype genotype, final String key, final double defaultValue) {
    Utils.nonNull(genotype);
    Utils.nonNull(key);
    final Object value = genotype.getExtendedAttribute(key);
    if (value == null) {
        return defaultValue;
    } else {
        try {
            return Double.parseDouble(String.valueOf(value));
        } catch (final NumberFormatException ex) {
            throw new NumberFormatException(String.format("attribute '%s' does not have a valid double value: '%s'", key, String.valueOf(value)));
        }
    }
}
 
Example 9
Source File: VariantContextGetters.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns an attribute as a string.
 * @param genotype the source genotype.
 * @param key the attribute key.
 * @param defaultValue default value to return in case the attribute does not have a value defined.
 * @return the string version of the attribute value.
 * @throws IllegalArgumentException if {@code genotype} or {@code key} is {@code null}.
 */
public static String getAttributeAsString(final Genotype genotype, final String key, final String defaultValue) {
    Utils.nonNull(genotype);
    Utils.nonNull(key);
    final Object value = genotype.getExtendedAttribute(key);
    if (value == null) {
        return defaultValue;
    } else {
        return String.valueOf(value);
    }
}
 
Example 10
Source File: VcfToVariant.java    From genomewarp with Apache License 2.0 4 votes vote down vote up
private static boolean parseOtherGenotypeFields(String field, VariantContext vc,
    ListValue.Builder lvBuilder, Genotype g, VCFHeader header) {

  if (!g.hasAnyAttribute(field)) {
    return false;
  }

  final VCFFormatHeaderLine metaData = header.getFormatHeaderLine(field);
  if (metaData == null) {
    logger.log(Level.WARNING, String.format("Could not find matching VCF header field for "
        + "genotype field %s", field));
    return false;
  }

  VCFHeaderLineType type = metaData.getType();
  Object value = g.getExtendedAttribute(field);
  final int fieldCount = metaData.getCount(vc);
  if (fieldCount == 1) {
    lvBuilder.addValues(createTypedValue(type, value));
    return true;
  }

  if (!(value instanceof String)) {
    throw new IllegalStateException("received non-Flag genotype field as non-String type");
  }
  String[] valueArray = ((String) value).split(",");
  if (valueArray.length == 1) {
    throw new IllegalStateException(String.format("header indicating a count greater than 1 "
        + "with non-List type found for field %s",
        field));
  }

  boolean allFalse = true;
  for (int i = 0; i < valueArray.length; i++) {
    VCFHeaderLineType thisType = VCFHeaderLineType.String;
    if (!valueArray[i].equals(VCFConstants.MISSING_VALUE_v4)) {
      thisType = type;
      allFalse = false;
    }

    lvBuilder.addValues(createTypedValue(thisType, valueArray[i]));
  }
  // We only add the lvBuilder if there is at least one non-missing value
  return !allFalse;
}
 
Example 11
Source File: ArraysCallingMetricAccumulator.java    From picard with MIT License 4 votes vote down vote up
private void updateDetailMetric(final CollectArraysVariantCallingMetrics.ArraysVariantCallingDetailMetrics metric,
                                final Genotype genotype,
                                final VariantContext vc,
                                final boolean hasSingletonSample) {
    metric.NUM_ASSAYS++;
    if (!vc.isFiltered() || vc.getCommonInfo().getFilters().contains(InfiniumVcfFields.DUPE)) {
        metric.NUM_NON_FILTERED_ASSAYS++;
        if (genotype.isCalled()) {
            metric.NUM_CALLS++;
            String gtA = (String) genotype.getExtendedAttribute(InfiniumVcfFields.GTA, genotype.getGenotypeString());
            if (!gtA.equals(VCFConstants.EMPTY_GENOTYPE)) {
                metric.NUM_AUTOCALL_CALLS++;
            }
        } else {
            metric.NUM_NO_CALLS++;
        }

        if (vc.isSNP()) {
            // Biallelic SNPs
            final boolean isInDbSnp = dbsnp.snps.isDbSnpSite(vc.getContig(), vc.getStart());

            metric.NUM_SNPS++;

            if (isInDbSnp) {
                metric.NUM_IN_DB_SNP++;
            }
        } else if (vc.isIndel()) {
            metric.NUM_INDELS++;
        }

        if (hasSingletonSample) {
            metric.NUM_SINGLETONS++;
        }

        if (genotype.isHet()) {
            metric.numHets++;
        } else if (genotype.isHomVar()) {
            metric.numHomVar++;
        }
    } else {
        metric.NUM_FILTERED_ASSAYS++;
        if (vc.getCommonInfo().getFilters().contains(InfiniumVcfFields.ZEROED_OUT_ASSAY)) {
            // A "zeroed-out SNP".  Marked as unusable/uncallable
            metric.NUM_ZEROED_OUT_ASSAYS++;
        }
    }
}
 
Example 12
Source File: FilteredHaplotypeFilter.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static Optional<String> makePhasingString(final Genotype genotype) {
    final String pgt = (String) genotype.getExtendedAttribute(GATKVCFConstants.HAPLOTYPE_CALLER_PHASING_GT_KEY, null);
    final String pid = (String) genotype.getExtendedAttribute(GATKVCFConstants.HAPLOTYPE_CALLER_PHASING_ID_KEY, null);
    return (pgt == null || pid == null) ? Optional.empty() : Optional.of(pgt + pid);
}
 
Example 13
Source File: OrientationBiasUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 *  Get field value as a string.  This method can be slow.  If the field value is empty or the field does not exist,
 *   returns {@link VCFConstants#MISSING_VALUE_v4}.
 *
 * @param g genotype Never {@code null}
 * @param fieldName Name of the field (format field).
 * @return field value or {@link VCFConstants#MISSING_VALUE_v4}
 */
public static String getGenotypeString(final Genotype g, final String fieldName) {
    Utils.nonNull(g);
    final Object genotypeExtendedAttribute = g.getExtendedAttribute(fieldName, VCFConstants.MISSING_VALUE_v4);
    return String.valueOf(genotypeExtendedAttribute);
}