Java Code Examples for htsjdk.variant.variantcontext.VariantContextBuilder#passFilters()

The following examples show how to use htsjdk.variant.variantcontext.VariantContextBuilder#passFilters() . 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: FilterVariantTranches.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void secondPassApply(VariantContext variant, ReadsContext readsContext, ReferenceContext referenceContext, FeatureContext featureContext) {
    final VariantContextBuilder builder = new VariantContextBuilder(variant);

    if (removeOldFilters) {
        builder.unfiltered();
    }

    if (variant.hasAttribute(infoKey)) {
        final double score = Double.parseDouble((String) variant.getAttribute(infoKey));
        if (variant.isSNP() && snpCutoffs.size() != 0 && isTrancheFiltered(score, snpCutoffs)) {
            builder.filter(filterStringFromScore(SNPString, score, snpTranches, snpCutoffs));
            filteredSnps++;
        } else if (variant.isIndel() && indelCutoffs.size() != 0 && isTrancheFiltered(score, indelCutoffs)) {
            builder.filter(filterStringFromScore(INDELString, score, indelTranches, indelCutoffs));
            filteredIndels++;
        }
    }

    if (builder.getFilters() == null || builder.getFilters().size() == 0){
        builder.passFilters();
    }
    
    vcfWriter.add(builder.make());
}
 
Example 2
Source File: FilterFuncotations.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Mark a variant as matching a set of Funcotation filters, or as matching no filters.
 */
private VariantContext applyFilters(final VariantContext variant, final Set<String> matchingFilters) {
    final VariantContextBuilder variantContextBuilder = new VariantContextBuilder(variant);
    final boolean isSignificant = !matchingFilters.isEmpty();

    // Mark the individual filters that make the variant significant, if any.
    final String clinicalSignificance = isSignificant ?
            String.join(FilterFuncotationsConstants.FILTER_DELIMITER, matchingFilters) :
            FilterFuncotationsConstants.CLINSIG_INFO_NOT_SIGNIFICANT;
    variantContextBuilder.attribute(FilterFuncotationsConstants.CLINSIG_INFO_KEY, clinicalSignificance);

    // Also set the filter field for insignificant variants, to make it easier for
    // downstream tools to extract out the interesting data.
    if (isSignificant) {
        variantContextBuilder.passFilters();
    } else {
        variantContextBuilder.filter(FilterFuncotationsConstants.NOT_CLINSIG_FILTER);
    }

    return variantContextBuilder.make();
}
 
Example 3
Source File: ApplyVQSR.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext ref, final FeatureContext featureContext) {

    final List<VariantContext> recals =  featureContext.getValues(recal, vc.getStart());
    final boolean evaluateThisVariant = useASannotations || VariantDataManager.checkVariationClass( vc, MODE );

    //vc.isNotFiltered is true for PASS; vc.filtersHaveBeenApplied covers PASS and filters
    final boolean variantIsNotFiltered = IGNORE_ALL_FILTERS || vc.isNotFiltered() ||
            (!ignoreInputFilterSet.isEmpty() && ignoreInputFilterSet.containsAll(vc.getFilters()));

    if( evaluateThisVariant && variantIsNotFiltered) {
        String filterString;
        final VariantContextBuilder builder = new VariantContextBuilder(vc);
        if (!useASannotations) {
            filterString = doSiteSpecificFiltering(vc, recals, builder);
        }
        else {  //allele-specific mode
            filterString = doAlleleSpecificFiltering(vc, recals, builder);
        }

        //for both non-AS and AS modes:
        if( filterString.equals(VCFConstants.PASSES_FILTERS_v4) ) {
            builder.passFilters();
        } else if(filterString.equals(VCFConstants.UNFILTERED)) {
            builder.unfiltered();
        } else {
            builder.filters(filterString);
        }

        final VariantContext outputVC = builder.make();
        if( !EXCLUDE_FILTERED || outputVC.isNotFiltered() ) {
            vcfWriter.add( outputVC );
        }
    } else { // valid VC but not compatible with this mode, so just emit the variant untouched
        vcfWriter.add( vc );
    }
}
 
Example 4
Source File: AlleleFilterUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Adds the new allele filters to the existing allele filters in the vc. Computes whether there are
 * new site filters and updates the filter in the vc. If there are no site filters, sets filters to pass
 * Sets the filters for each allele and calculates the intersection of the allele filters to set on the variant.
 * PASS if the intersection is empty.
 * @param vc The variant context to add the filters to, both at the allele and site level
 * @param newAlleleFilters filters to be applied to each allele, the intersection of these filters are applied at the site level
 * @param invalidatePreviousFilters whether existing filters should be removed
 * @return The updated variant context
 */
public static VariantContext addAlleleAndSiteFilters(VariantContext vc, List<Set<String>> newAlleleFilters, boolean invalidatePreviousFilters) {
    if (newAlleleFilters.isEmpty()) {
        return vc;
    }
    List<List<String>> currentAlleleFilters = decodeASFilters(vc);
    if (!currentAlleleFilters.isEmpty() && newAlleleFilters.size() != currentAlleleFilters.size()) {
        // log an error
        return vc;
    }

    if (currentAlleleFilters.isEmpty() || invalidatePreviousFilters) {
        currentAlleleFilters = new ArrayList<>(Collections.nCopies(newAlleleFilters.size(), Collections.singletonList(GATKVCFConstants.SITE_LEVEL_FILTERS)));
    }
    ListIterator<List<String>> currentAlleleFiltersIt = currentAlleleFilters.listIterator();
    List<List<String>> updatedAlleleFilters = newAlleleFilters.stream().map(newfilters -> addAlleleFilters(currentAlleleFiltersIt.next(), newfilters.stream().collect(Collectors.toList()))).collect(Collectors.toList());
    String encodedFilters = encodeASFilters(updatedAlleleFilters);
    VariantContextBuilder vcb = new VariantContextBuilder(vc).attribute(GATKVCFConstants.AS_FILTER_STATUS_KEY, encodedFilters);

    if (invalidatePreviousFilters) {
        vcb.unfiltered();
    }
    Set<String> siteFiltersToAdd = newAlleleFilters.stream().skip(1)
            .collect(()->new HashSet<>(newAlleleFilters.get(0)), Set::retainAll, Set::retainAll);
    siteFiltersToAdd.stream().forEach(filter -> vcb.filter(filter));
    if ((vcb.getFilters() == null || vcb.getFilters().isEmpty()) && !invalidatePreviousFilters) {
        vcb.passFilters();
    }
    return vcb.make();
}
 
Example 5
Source File: FuncotatorTestUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Create a variant context with the following fields.  Note that the genotype will be empty, as will
 *  INFO annotations.
 *
 * @param reference E.g. {@link FuncotatorReferenceTestUtils#retrieveHg19Chr3Ref}
 * @param contig Never {@code null}
 * @param start Must be positive.
 * @param end Must be positive.
 * @param refAlleleString Valid {@link String} for an allele.  Do not include "*".  Never {@code null}
 * @param altAlleleString Valid {@link String} for an allele.  Do not include "*".  Never {@code null}
 * @param filterString Valid {@link String} for filters (See VCF 4.2 spec).  May be {@code null}.
 * @return a simple, biallelic variant context
 */
public static VariantContext createSimpleVariantContext(final String reference, final String contig,
                                                        final int start,
                                                        final int end,
                                                        final String refAlleleString,
                                                        final String altAlleleString,
                                                        final String filterString) {
    Utils.nonNull(contig);
    Utils.nonNull(refAlleleString);
    Utils.nonNull(altAlleleString);
    ParamUtils.isPositive(start, "Invalid start position: " + start);
    ParamUtils.isPositive(end, "Invalid end position: " + end);

    final Allele refAllele = Allele.create(refAlleleString, true);
    final Allele altAllele = Allele.create(altAlleleString);

    final VariantContextBuilder variantContextBuilder = new VariantContextBuilder(
            reference,
            contig,
            start,
            end,
            Arrays.asList(refAllele, altAllele)
    );

    if ( filterString != null ) {
        variantContextBuilder.filter(filterString);
    }
    else {
        variantContextBuilder.passFilters();
    }

    final VariantContext vc = variantContextBuilder.make();

    return vc;
}
 
Example 6
Source File: VariantToVcf.java    From genomewarp with Apache License 2.0 4 votes vote down vote up
private static VariantContext getContextFromVariant(VCFHeader header, Variant in) {
  // Create allele list
  String refBases = in.getReferenceBases();
  List<String> altBases = in.getAlternateBasesList();
  List<Allele> alleleList = new ArrayList<>();
  alleleList.add(Allele.create(refBases.getBytes(StandardCharsets.UTF_8), true));
  for (String base : altBases) {
    alleleList.add(Allele.create(base.getBytes(StandardCharsets.UTF_8), false));
  }

  // Note htsjdk start/end are both 1-based closed
  VariantContextBuilder builder = new VariantContextBuilder(SOURCE, in.getReferenceName(),
      in.getStart() + 1, in.getEnd(), alleleList);

  // Set Quality
  if (in.getQuality() != 0.0) {
    builder.log10PError(-in.getQuality() / 10);
  }

  // Set names
  int numNames = in.getNamesCount();
  int i = 0;
  String namesString = "";
  for (String name : in.getNamesList()) {
    if (i == numNames - 1) {
      break;
    }
    namesString += name + ",";
  }
  if (numNames == 0) {
    builder.noID();
  } else {
    // Also handle fence post
    builder.id(namesString + in.getNames(numNames - 1));
  }

  // Set filters
  boolean hadFilter = false;
  for (String filter : in.getFilterList()) {
    hadFilter = true;
    if (filter.equals(VCFConstants.PASSES_FILTERS_v4)) {
      builder.passFilters();
      break;
    }
    if (filter.equals(VCFConstants.MISSING_VALUE_v4)) {
      builder.unfiltered();
      break;
    }

    builder.filter(filter);
  }
  if (!hadFilter) {
    builder.unfiltered();
  }

  // Set info
  Map<String, Object> toSet = new HashMap<>();
  for (Map.Entry<String, ListValue> entry : in.getInfo().entrySet()) {
    String currKey = entry.getKey();
    List<Value> currValue = entry.getValue().getValuesList();
    int currSize = currValue.size();

    if (currSize == 0) {
      // If the key is present in the info map, there must
      // be non-zero attributes associated with it
      throw new IllegalStateException(String.format("info field %s has length 0", currKey));
    }

    VCFHeaderLineType type = header.getInfoHeaderLine(currKey).getType();
    if (currSize == 1) {
      toSet.put(currKey, parseObject(currKey, currValue.get(0), type));
      continue;
    }

    List<Object> objectList = new ArrayList<>();
    for (Value val : currValue) {
      objectList.add(parseObject(currKey, val, type));
    }
    toSet.put(currKey, objectList);
  }
  builder.attributes(toSet);

  // Set calls
  List<Genotype> genotypes = new ArrayList<>();
  for (VariantCall vc : in.getCallsList()) {
    genotypes.add(getGenotypeFromVariantCall(header, vc, alleleList));
  }
  if (genotypes.size() == 0) {
    builder.noGenotypes();
  } else {
    builder.genotypes(genotypes);
  }

  return builder.make();
}
 
Example 7
Source File: VariantContextCodec.java    From Hadoop-BAM with MIT License 4 votes vote down vote up
public static VariantContext read(final DataInput in) throws IOException {
	final VariantContextBuilder builder = new VariantContextBuilder();

	int count, len;
	byte[] b;

	len = in.readInt();
	b = new byte[len];
	in.readFully(b);
	final String chrom = new String(b, "UTF-8");
	builder.chr(chrom);

	final int start = in.readInt();
	builder.start(start);
	builder.stop (in.readInt());

	len = in.readInt();
	if (len == 0)
		builder.noID();
	else {
		if (len > b.length) b = new byte[len];
		in.readFully(b, 0, len);
		builder.id(new String(b, 0, len, "UTF-8"));
	}

	count = in.readInt();
	final List<Allele> alleles = new ArrayList<Allele>(count);
	for (int i = 0; i < count; ++i) {
		len = in.readInt();
		if (len > b.length) b = new byte[len];
		in.readFully(b, 0, len);
		alleles.add(Allele.create(Arrays.copyOf(b, len), i == 0));
	}
	builder.alleles(alleles);

	final int qualInt = in.readInt();
	builder.log10PError(
		qualInt == 0x7f800001
			? VariantContext.NO_LOG10_PERROR
			: Float.intBitsToFloat(qualInt));

	count = in.readInt();
	switch (count) {
	case -2: builder.unfiltered(); break;
	case -1: builder.passFilters(); break;
	default:
		while (count-- > 0) {
			len = in.readInt();
			if (len > b.length) b = new byte[len];
			in.readFully(b, 0, len);
			builder.filter(new String(b, 0, len, "UTF-8"));
		}
		break;
	}

	count = in.readInt();
	final Map<String,Object> attrs = new HashMap<String,Object>(count, 1);
	while (count-- > 0) {
		len = in.readInt();
		if (len > b.length) b = new byte[len];
		in.readFully(b, 0, len);
		attrs.put(new String(b, 0, len, "UTF-8"), decodeAttrVal(in));
	}
	builder.attributes(attrs);

	count = in.readInt();
	final byte genoType = in.readByte();
	len = in.readInt();

	// Resize b even if it's already big enough, minimizing the amount of
	// memory LazyGenotypesContext hangs on to.
	b = new byte[len];
	in.readFully(b);

	switch (genoType) {
	case 0:
		builder.genotypesNoValidation(
			new LazyVCFGenotypesContext(alleles, chrom, start, b, count));
		break;

	case 1:
		builder.genotypesNoValidation(
			new LazyBCFGenotypesContext(alleles, in.readInt(), b, count));
		break;

	default:
		throw new IOException(
			"Invalid genotypes type identifier: cannot decode");
	}

	return builder.make();
}