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

The following examples show how to use htsjdk.variant.variantcontext.Genotype#hasAnyAttribute() . 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: StrandBiasTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
//template method for calculating strand bias annotations using the three different methods
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    Utils.nonNull(vc);
    if ( !vc.isVariant() ) {
        return Collections.emptyMap();
    }

    // if the genotype and strand bias are provided, calculate the annotation from the Genotype (GT) field
    if ( vc.hasGenotypes() ) {
        for (final Genotype g : vc.getGenotypes()) {
            if (g.hasAnyAttribute(GATKVCFConstants.STRAND_BIAS_BY_SAMPLE_KEY)) {
                return calculateAnnotationFromGTfield(vc.getGenotypes());
            }
        }
    }

    if (likelihoods != null) {
        return calculateAnnotationFromLikelihoods(likelihoods, vc);
    }
    return Collections.emptyMap();
}
 
Example 2
Source File: StrandBiasTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create the contingency table by retrieving the per-sample strand bias annotation and adding them together
 * @param genotypes the genotypes from which to pull out the per-sample strand bias annotation
 * @param minCount minimum threshold for the sample strand bias counts for each ref and alt.
 *                 If both ref and alt counts are above minCount the whole sample strand bias is added to the resulting table
 * @return the table used for several strand bias tests, will be null if none of the genotypes contain the per-sample SB annotation
 */
protected int[][] getTableFromSamples( final GenotypesContext genotypes, final int minCount ) {
    if( genotypes == null ) {
        return null;
    }

    final int[] sbArray = {0,0,0,0}; // reference-forward-reverse -by- alternate-forward-reverse
    boolean foundData = false;

    for( final Genotype g : genotypes ) {
        if( ! g.hasAnyAttribute(GATKVCFConstants.STRAND_BIAS_BY_SAMPLE_KEY) ) {
            continue;
        }

        foundData = true;
        int[] data = getStrandCounts(g);

        if ( passesMinimumThreshold(data, minCount) ) {
            for( int index = 0; index < sbArray.length; index++ ) {
                sbArray[index] += data[index];
            }
        }
    }

    return foundData ? decodeSBBS(sbArray) : null ;
}
 
Example 3
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 4
Source File: CustomMafFuncotationCreator.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Create the MAF count fields (See {@link CustomMafFuncotationCreator#COUNT_FIELD_NAMES}) for a single pair in the given variant.
 *
 * If no samples can be linked to a tumor and normal combination, then blank values are generated for the count fields.
 * @param variant Never {@code null}
 * @param tnPair Never {@code null}
 * @return List of funcotations for the pair.  This list will usually be of length one, unless multiallelics are involved.
 *  Never {@code null}  Field values can be blank:
 *  - if there are no AD or AF format fields
 *  - if the specified pair is not in the variant sample list.
 */
private static List<Funcotation> createCustomMafCountFields(final VariantContext variant, final TumorNormalPair tnPair) {
    Utils.nonNull(variant);
    Utils.nonNull(tnPair);
    final List<Funcotation> result = new ArrayList<>();

    for (int i = 0; i < variant.getAlternateAlleles().size(); i++) {
        final Allele allele = variant.getAlternateAllele(i);
        final String tumorSampleName = tnPair.getTumor();
        final String normalSampleName = tnPair.getNormal();

        // This code assumes that the AD field is of Type "R"
        final Genotype tumorGenotype = variant.getGenotype(tumorSampleName);
        final boolean hasTumorAD = (tumorGenotype != null) && (tumorGenotype.hasAD());
        final boolean hasTumorAF = (tumorGenotype != null) && (tumorGenotype.hasAnyAttribute(GATKVCFConstants.ALLELE_FRACTION_KEY));

        // Just make a quick check that the tumor genotype has allelic depth for both a ref and alt
        // This is REQUIRED, but can be missing and our validations seem to miss it:
        if ( hasTumorAD && tumorGenotype.getAD().length < 2 ) {
            throw new UserException("Allelic Depth (AD field) for Variant[" +
                    variant.getContig() + ":" + variant.getStart() + "_" +
                    variant.getReference().toString() + "->" + variant.getAlternateAlleles().get(0).toString() + ']' +
                    " Does not contain both a REF and an ALT value (only one value is present)!");
        }

        final String tAltCount = hasTumorAD ? Integer.toString(tumorGenotype.getAD()[i + 1]) : "";
        final String tRefCount = hasTumorAD ? Integer.toString(tumorGenotype.getAD()[0]) : "";
        final String tumorFAllAlleles = hasTumorAF ? tumorGenotype.getAnyAttribute(GATKVCFConstants.ALLELE_FRACTION_KEY).toString() : "";
        final String tumorF = hasTumorAF ? StringUtils.split(tumorFAllAlleles, ",")[i] : "";

        final Genotype normalGenotype = variant.getGenotype(normalSampleName);
        final boolean hasNormalAD = (normalGenotype != null) && (normalGenotype.hasAD());
        final String nAltCount = hasNormalAD ? Integer.toString(normalGenotype.getAD()[i + 1]) : "";
        final String nRefCount = hasNormalAD ? Integer.toString(normalGenotype.getAD()[0]) : "";

        final List<String> fieldValues = Arrays.asList(
                tAltCount,
                tRefCount,
                nAltCount,
                nRefCount,
                tumorF);

        result.add(TableFuncotation.create(COUNT_FIELD_NAMES, fieldValues, allele, MafOutputRendererConstants.MAF_COUNT_RENDERING_DATASOURCE_DUMMY_NAME, createCustomMafCountFieldsMetadata()));
    }

    return result;
}