use of htsjdk.samtools.util.ListMap in project gatk by broadinstitute.
the class FilterApplyingVariantIterator method next.
/**
* Provides the next record from the underlying iterator after applying filter strings generated
* by the set of filters in use by the iterator.
*/
@Override
public VariantContext next() {
final VariantContext ctx = this.iterator.next();
final Set<String> filterStrings = new HashSet<>();
// Collect variant level filters
for (final VariantFilter filter : this.filters) {
final String val = filter.filter(ctx);
if (val != null)
filterStrings.add(val);
}
// Collect genotype level filters in a Map of Sample -> List<filter string>
final ListMap<String, String> gtFilterStrings = new ListMap<>();
final Set<String> variantSamples = new HashSet<>();
for (final Genotype gt : ctx.getGenotypes()) {
if (gt.isCalled() && !gt.isHomRef())
variantSamples.add(gt.getSampleName());
for (final GenotypeFilter filter : gtFilters) {
final String filterString = filter.filter(ctx, gt);
if (filterString != null)
gtFilterStrings.add(gt.getSampleName(), filterString);
}
}
// If all genotypes are filtered apply a site level filter
if (gtFilterStrings.keySet().containsAll(variantSamples)) {
filterStrings.add(ALL_GTS_FILTERED);
}
// Make a builder and set the site level filter appropriately
final VariantContextBuilder builder = new VariantContextBuilder(ctx);
if (filterStrings.isEmpty()) {
builder.passFilters();
} else {
builder.filters(filterStrings);
}
// Apply filters to the necessary genotypes
builder.noGenotypes();
final List<Genotype> newGenotypes = new ArrayList<>(ctx.getNSamples());
for (final Genotype gt : ctx.getGenotypes()) {
final GenotypeBuilder gtBuilder = new GenotypeBuilder(gt);
final List<String> filters = gtFilterStrings.get(gt.getSampleName());
if (filters == null || filters.isEmpty()) {
gtBuilder.filter(PASS_FILTER);
} else {
gtBuilder.filters(filters);
}
newGenotypes.add(gtBuilder.make());
}
builder.genotypes(newGenotypes);
return builder.make();
}
Aggregations