-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adding a filtering iterator on variantContexts and
- an implementation of a filter that looks for het sites. - and a function to find the offset into a read from genomic position - small change in behavior of new function (optionally let the position of a deletion be the last aligned base. - added tests for new and old function - added more tests. - changed functionality so that getGenotype(int) returns null (rather than throws an OOB exception) - responded to review comments fixed documentation - cleaned up some documentation issues that were making the log to long for travis - reduced verbosity in TestNG
- Loading branch information
Showing
12 changed files
with
660 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/java/htsjdk/variant/variantcontext/filter/FilteringIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015 The Broad Institute | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package htsjdk.variant.variantcontext.filter; | ||
|
||
import htsjdk.samtools.util.CloseableIterator; | ||
import htsjdk.samtools.util.CloserUtil; | ||
import htsjdk.variant.variantcontext.VariantContext; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* A filtering iterator for VariantContexts that takes a base iterator and a VariantContextFilter. | ||
* | ||
* The iterator returns all the variantcontexts for which the filter's function "pass" returns true (and only those) | ||
*/ | ||
public class FilteringIterator implements CloseableIterator<VariantContext>, Iterable<VariantContext>{ | ||
private final Iterator<VariantContext> iterator; | ||
private final VariantContextFilter filter; | ||
private VariantContext next = null; | ||
|
||
/** | ||
* Constructor of an iterator based on the provided iterator and predicate. The resulting | ||
* records will be all those VariantContexts from iterator for which filter.pass( . ) is true | ||
* | ||
* @param iterator the backing iterator | ||
* @param filter the filter | ||
*/ | ||
public FilteringIterator(final Iterator<VariantContext> iterator, final VariantContextFilter filter) { | ||
this.iterator = iterator; | ||
this.filter = filter; | ||
next = getNextVC(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
CloserUtil.close(iterator); | ||
} | ||
|
||
/** | ||
* Returns true if the iteration has more elements. | ||
* | ||
* @return true if the iteration has more elements. Otherwise returns false. | ||
*/ | ||
@Override | ||
public boolean hasNext() { | ||
return next != null; | ||
} | ||
|
||
/** | ||
* Returns the next element in the iteration. | ||
* | ||
* @return the next element in the iteration | ||
* @throws NoSuchElementException if there are no more elements to return | ||
* | ||
*/ | ||
@Override | ||
public VariantContext next() throws NoSuchElementException { | ||
if (next == null) { | ||
throw new NoSuchElementException("Iterator has no more elements."); | ||
} | ||
final VariantContext result = next; | ||
next = getNextVC(); | ||
return result; | ||
} | ||
|
||
/** | ||
* Required method for Iterator API. | ||
* | ||
* @throws UnsupportedOperationException since it is unsupported here. | ||
*/ | ||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("Remove() not supported by FilteringIterator"); | ||
} | ||
|
||
/** | ||
* Gets the next record from the underlying iterator that passes the filter | ||
* | ||
* @return VariantContext the next filter-passing record | ||
*/ | ||
private VariantContext getNextVC() { | ||
|
||
while (iterator.hasNext()) { | ||
final VariantContext record = iterator.next(); | ||
|
||
if (filter.pass(record)) { | ||
return record; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* function to satisfy the Iterable interface | ||
* | ||
* @return itself since the class inherits from Iterator | ||
*/ | ||
@Override | ||
public Iterator<VariantContext> iterator() { | ||
return this; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/java/htsjdk/variant/variantcontext/filter/HeterozygosityFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015 The Broad Institute | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package htsjdk.variant.variantcontext.filter; | ||
|
||
import htsjdk.variant.variantcontext.Genotype; | ||
import htsjdk.variant.variantcontext.VariantContext; | ||
|
||
/** | ||
* A Predicate on VariantContexts that either returns true at heterozygous sites (invertible to false). | ||
* if optional "sample" argument to constructor is given, the genotype of that sample will be examined, | ||
* otherwise first genotype will be used. | ||
* | ||
* Missing sample, or no genotype will result in an exception being thrown. | ||
*/ | ||
public class HeterozygosityFilter implements VariantContextFilter { | ||
|
||
final private String sample; | ||
final private boolean keepHets; | ||
|
||
/** | ||
* Constructor for a filter that will keep (or remove, if keepHets is false) VC for which the | ||
* genotype of sample is heterozygous. If sample is null, the first genotype in the | ||
* variant context will be used. | ||
* | ||
* @param keepHets determine whether to keep the het sites (true) or filter them out (false) | ||
* @param sample the name of the sample in the variant context whose genotype should be examined. | ||
*/ | ||
public HeterozygosityFilter(final boolean keepHets, final String sample) { | ||
this.keepHets = keepHets; | ||
this.sample = sample; | ||
} | ||
|
||
/** | ||
* Constructor as above that doesn't take a sample, instead it will look at the first genotype of the variant context. | ||
* @param keepHets if true, the heterozygous variant contexts will pass the filter, otherwise they will fail. | ||
*/ | ||
public HeterozygosityFilter(final boolean keepHets) { | ||
this(keepHets, null); | ||
} | ||
|
||
/** | ||
* @return true if variantContext is to be kept, otherwise false | ||
* Assumes that this.sample is a sample in the variantContext, if not null, | ||
* otherwise looks for the first genotype (and assumes it exists). | ||
* @param variantContext the record to examine for heterozygosity | ||
*/ | ||
@Override | ||
public boolean pass(final VariantContext variantContext) { | ||
final Genotype gt = (sample == null) ? variantContext.getGenotype(0) : variantContext.getGenotype(sample); | ||
|
||
if (gt == null) { | ||
throw new IllegalArgumentException((sample == null) ? | ||
"Cannot find any genotypes in VariantContext: " + variantContext : | ||
"Cannot find sample requested: " + sample); | ||
} | ||
|
||
//XOR operator to reverse behaviour if keepHets is true. | ||
return gt.isHet() ^ !keepHets; | ||
} | ||
} |
Oops, something went wrong.