Bug fix for incorrect logic in subsetSamples

-- Now properly handles the case where a sample isn't present (no longer adds a null to the genotypes list)
-- Fix for logic failure where if the number of requested samples equals the number of known genotypes then all of the records were returned, which isn't correct when there are missing samples.
-- Unit tests added to handle these cases
This commit is contained in:
Mark DePristo 2011-12-14 09:58:23 -05:00
parent 7ac8966184
commit 71b4bb12b7
2 changed files with 13 additions and 5 deletions

View File

@ -410,6 +410,12 @@ public class GenotypesContext implements List<Genotype> {
return getGenotypes().get(i);
}
/**
* Gets sample associated with this sampleName, or null if none is found
*
* @param sampleName
* @return
*/
public Genotype get(final String sampleName) {
Integer offset = getSampleI(sampleName);
return offset == null ? null : getGenotypes().get(offset);
@ -648,16 +654,15 @@ public class GenotypesContext implements List<Genotype> {
@Ensures("result != null")
public GenotypesContext subsetToSamples( final Set<String> samples ) {
final int nSamples = samples.size();
final int nGenotypes = size();
if ( nSamples == nGenotypes )
return this;
else if ( nSamples == 0 )
if ( nSamples == 0 )
return NO_GENOTYPES;
else { // nGenotypes < nSamples
final GenotypesContext subset = create(samples.size());
for ( final String sample : samples ) {
subset.add(get(sample));
final Genotype g = get(sample);
if ( g != null )
subset.add(g);
}
return subset;
}

View File

@ -704,11 +704,14 @@ public class VariantContextUnitTest extends BaseTest {
public Object[][] MakeSubContextTest() {
for ( boolean updateAlleles : Arrays.asList(true, false)) {
new SubContextTest(Collections.<String>emptySet(), updateAlleles);
new SubContextTest(Collections.singleton("MISSING"), updateAlleles);
new SubContextTest(Collections.singleton("AA"), updateAlleles);
new SubContextTest(Collections.singleton("AT"), updateAlleles);
new SubContextTest(Collections.singleton("TT"), updateAlleles);
new SubContextTest(Arrays.asList("AA", "AT"), updateAlleles);
new SubContextTest(Arrays.asList("AA", "AT", "TT"), updateAlleles);
new SubContextTest(Arrays.asList("AA", "AT", "MISSING"), updateAlleles);
new SubContextTest(Arrays.asList("AA", "AT", "TT", "MISSING"), updateAlleles);
}
return SubContextTest.getTests(SubContextTest.class);