Deleted MutableVariantContext

-- All methods that used this capable now use VariantContext directly instead
This commit is contained in:
Mark DePristo 2011-11-14 14:19:06 -05:00
parent b11c535527
commit 077397cb4b
5 changed files with 22 additions and 233 deletions

View File

@ -39,7 +39,6 @@ import org.broadinstitute.sting.utils.codecs.vcf.VCFHeader;
import org.broadinstitute.sting.utils.codecs.vcf.VCFHeaderLine;
import org.broadinstitute.sting.utils.codecs.vcf.VCFUtils;
import org.broadinstitute.sting.utils.codecs.vcf.VCFWriter;
import org.broadinstitute.sting.utils.variantcontext.MutableVariantContext;
import org.broadinstitute.sting.utils.variantcontext.VariantContext;
import org.broadinstitute.sting.utils.variantcontext.VariantContextUtils;
@ -466,9 +465,7 @@ public class GenotypeAndValidateWalker extends RodWalker<GenotypeAndValidateWalk
if (vcfWriter != null && writeVariant) {
if (!vcComp.hasAttribute("callStatus")) {
MutableVariantContext mvc = new MutableVariantContext(vcComp);
mvc.putAttribute("callStatus", call.isCalledAlt(callConf) ? "ALT" : "REF" );
vcfWriter.add(mvc);
vcfWriter.add(VariantContext.modifyAttribute(vcComp, "callStatus", call.isCalledAlt(callConf) ? "ALT" : "REF"));
}
else
vcfWriter.add(vcComp);

View File

@ -1,212 +0,0 @@
package org.broadinstitute.sting.utils.variantcontext;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* Mutable version of VariantContext
*
* @author depristo
*/
public class MutableVariantContext extends VariantContext {
// ---------------------------------------------------------------------------------------------------------
//
// constructors
//
// ---------------------------------------------------------------------------------------------------------
public MutableVariantContext(String source, String contig, long start, long stop, Collection<Allele> alleles, Collection<Genotype> genotypes, double negLog10PError, Set<String> filters, Map<String, Object> attributes) {
super(source, contig, start, stop, alleles, genotypes, negLog10PError, filters, attributes);
}
public MutableVariantContext(String source, String contig, long start, long stop, Collection<Allele> alleles, GenotypeMap genotypes, double negLog10PError, Set<String> filters, Map<String, Object> attributes) {
super(source, contig, start, stop, alleles, genotypes, negLog10PError, filters, attributes);
}
public MutableVariantContext(String source, String contig, long start, long stop, Collection<Allele> alleles) {
super(source, contig, start, stop, alleles, NO_GENOTYPES, InferredGeneticContext.NO_NEG_LOG_10PERROR, null, null);
}
public MutableVariantContext(String source, String contig, long start, long stop, Collection<Allele> alleles, Collection<Genotype> genotypes) {
super(source, contig, start, stop, alleles, genotypes, InferredGeneticContext.NO_NEG_LOG_10PERROR, null, null);
}
public MutableVariantContext(VariantContext parent) {
super(parent.getSource(), parent.contig, parent.start, parent.stop, parent.getAlleles(), parent.getGenotypes(), parent.getNegLog10PError(), parent.getFilters(), parent.getAttributes(), parent.getReferenceBaseForIndel());
}
/**
* Sets the alleles segregating in this context to the collect of alleles. Each of which must be unique according
* to equals() in Allele. Validate() should be called when you are done modifying the context.
*
* @param alleles
*/
public void setAlleles(Collection<Allele> alleles) {
this.alleles.clear();
for ( Allele a : alleles )
addAllele(a);
}
/**
* Adds allele to the segregating allele list in this context to the collection of alleles. The new
* allele must be be unique according to equals() in Allele.
* Validate() should be called when you are done modifying the context.
*
* @param allele
*/
public void addAllele(Allele allele) {
final boolean allowDuplicates = false; // used to be a parameter
type = null;
for ( Allele a : alleles ) {
if ( a.basesMatch(allele) && ! allowDuplicates )
throw new IllegalArgumentException("Duplicate allele added to VariantContext" + this);
}
// we are a novel allele
alleles.add(allele);
}
public void clearGenotypes() {
genotypes = GenotypeMap.create();
}
/**
* Adds this single genotype to the context, not allowing duplicate genotypes to be added
* @param genotype
*/
public void addGenotypes(Genotype genotype) {
putGenotype(genotype.getSampleName(), genotype, false);
}
/**
* Adds these genotypes to the context, not allowing duplicate genotypes to be added
* @param genotypes
*/
public void addGenotypes(Collection<Genotype> genotypes) {
for ( Genotype g : genotypes ) {
addGenotype(g);
}
}
/**
* Adds these genotype to the context, not allowing duplicate genotypes to be added.
* @param genotypes
*/
public void addGenotypes(Map<String, Genotype> genotypes) {
for ( Map.Entry<String, Genotype> elt : genotypes.entrySet() ) {
addGenotype(elt.getValue());
}
}
/**
* Adds these genotypes to the context.
*
* @param genotypes
*/
public void putGenotypes(Map<String, Genotype> genotypes) {
for ( Map.Entry<String, Genotype> g : genotypes.entrySet() )
putGenotype(g.getKey(), g.getValue());
}
/**
* Adds these genotypes to the context.
*
* @param genotypes
*/
public void putGenotypes(Collection<Genotype> genotypes) {
for ( Genotype g : genotypes )
putGenotype(g);
}
/**
* Adds this genotype to the context, throwing an error if it's already bound.
*
* @param genotype
*/
public void addGenotype(Genotype genotype) {
addGenotype(genotype.getSampleName(), genotype);
}
/**
* Adds this genotype to the context, throwing an error if it's already bound.
*
* @param genotype
*/
public void addGenotype(String sampleName, Genotype genotype) {
putGenotype(sampleName, genotype, false);
}
/**
* Adds this genotype to the context.
*
* @param genotype
*/
public void putGenotype(Genotype genotype) {
putGenotype(genotype.getSampleName(), genotype);
}
/**
* Adds this genotype to the context.
*
* @param genotype
*/
public void putGenotype(String sampleName, Genotype genotype) {
putGenotype(sampleName, genotype, true);
}
private void putGenotype(String sampleName, Genotype genotype, boolean allowOverwrites) {
if ( hasGenotype(sampleName) && ! allowOverwrites )
throw new IllegalStateException("Attempting to overwrite sample->genotype binding: " + sampleName + " this=" + this);
if ( ! sampleName.equals(genotype.getSampleName()) )
throw new IllegalStateException("Sample name doesn't equal genotype.getSample(): " + sampleName + " genotype=" + genotype);
this.genotypes.put(sampleName, genotype);
}
/**
* Removes the binding from sampleName to genotype. If this doesn't exist, throws an IllegalArgumentException
* @param sampleName
*/
public void removeGenotype(String sampleName) {
if ( ! this.genotypes.containsKey(sampleName) )
throw new IllegalArgumentException("Sample name isn't contained in genotypes " + sampleName + " genotypes =" + genotypes);
this.genotypes.remove(sampleName);
}
/**
* Removes genotype from the context. If this doesn't exist, throws an IllegalArgumentException
* @param genotype
*/
public void removeGenotype(Genotype genotype) {
removeGenotype(genotype.getSampleName());
}
// todo -- add replace genotype routine
// ---------------------------------------------------------------------------------------------------------
//
// InferredGeneticContext mutation operators
//
// ---------------------------------------------------------------------------------------------------------
public void setSource(String source) { commonInfo.setName(source); }
public void addFilter(String filter) { commonInfo.addFilter(filter); }
public void addFilters(Collection<String> filters) { commonInfo.addFilters(filters); }
public void clearFilters() { commonInfo.clearFilters(); }
public void setFilters(Collection<String> filters) { commonInfo.setFilters(filters); }
public void setAttributes(Map<String, ?> map) { commonInfo.setAttributes(map); }
public void clearAttributes() { commonInfo.clearAttributes(); }
public void putAttribute(String key, Object value) { commonInfo.putAttribute(key, value); }
public void removeAttribute(String key) { commonInfo.removeAttribute(key); }
public void putAttributes(Map<String, ?> map) { commonInfo.putAttributes(map); }
public void setNegLog10PError(double negLog10PError) { commonInfo.setNegLog10PError(negLog10PError); }
public void putAttribute(String key, Object value, boolean allowOverwrites) { commonInfo.putAttribute(key, value, allowOverwrites); }
public void setID(String id) { putAttribute(ID_KEY, id, true); }
}

View File

@ -399,6 +399,12 @@ public class VariantContext implements Feature { // to enable tribble intergrati
return new VariantContext(vc.getSource(), vc.getChr(), vc.getStart(), vc.getEnd(), vc.getAlleles(), vc.genotypes, vc.getNegLog10PError(), vc.filtersWereApplied() ? vc.getFilters() : null, attributes, vc.getReferenceBaseForIndel(), true);
}
public static VariantContext modifyAttribute(VariantContext vc, final String key, final Object value) {
Map<String, Object> attributes = new HashMap<String, Object>(vc.getAttributes());
attributes.put(key, value);
return new VariantContext(vc.getSource(), vc.getChr(), vc.getStart(), vc.getEnd(), vc.getAlleles(), vc.genotypes, vc.getNegLog10PError(), vc.filtersWereApplied() ? vc.getFilters() : null, attributes, vc.getReferenceBaseForIndel(), true);
}
public static VariantContext modifyReferencePadding(VariantContext vc, Byte b) {
return new VariantContext(vc.getSource(), vc.getChr(), vc.getStart(), vc.getEnd(), vc.getAlleles(), vc.genotypes, vc.getNegLog10PError(), vc.filtersWereApplied() ? vc.getFilters() : null, vc.getAttributes(), b, true);
}
@ -407,6 +413,10 @@ public class VariantContext implements Feature { // to enable tribble intergrati
return new VariantContext(vc.getSource(), vc.getChr(), vc.getStart(), vc.getEnd(), vc.getAlleles(), vc.genotypes, negLog10PError, filters, attributes, vc.getReferenceBaseForIndel(), true);
}
public static VariantContext modifyID(final VariantContext vc, final String id) {
return modifyAttribute(vc, ID_KEY, id);
}
// ---------------------------------------------------------------------------------------------------------
//
// Selectors

View File

@ -11,10 +11,7 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.Assert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
public class VariantContextUnitTest extends BaseTest {
@ -402,29 +399,26 @@ public class VariantContextUnitTest extends BaseTest {
List<Allele> alleles = Arrays.asList(Aref, T, del);
Genotype g1 = new Genotype("AA", Arrays.asList(Aref, Aref), 10);
Genotype g2 = new Genotype("AT", Arrays.asList(Aref, T), 10);
MutableVariantContext vc = new MutableVariantContext("test", snpLoc,snpLocStart, snpLocStop, alleles, Arrays.asList(g1,g2));
VariantContext vc = new VariantContext("test", snpLoc,snpLocStart, snpLocStop, alleles, Arrays.asList(g1,g2));
Assert.assertTrue(vc.isNotFiltered());
Assert.assertFalse(vc.isFiltered());
Assert.assertEquals(0, vc.getFilters().size());
vc.addFilter("BAD_SNP_BAD!");
Set<String> filters = new HashSet<String>(Arrays.asList("BAD_SNP_BAD!"));
vc = new VariantContext("test", snpLoc,snpLocStart, snpLocStop, alleles, Arrays.asList(g1,g2), VariantContext.NO_NEG_LOG_10PERROR, filters, null);
Assert.assertFalse(vc.isNotFiltered());
Assert.assertTrue(vc.isFiltered());
Assert.assertEquals(1, vc.getFilters().size());
vc.addFilters(Arrays.asList("REALLY_BAD_SNP", "CHRIST_THIS_IS_TERRIBLE"));
filters = new HashSet<String>(Arrays.asList("BAD_SNP_BAD!", "REALLY_BAD_SNP", "CHRIST_THIS_IS_TERRIBLE"));
vc = new VariantContext("test", snpLoc,snpLocStart, snpLocStop, alleles, Arrays.asList(g1,g2), VariantContext.NO_NEG_LOG_10PERROR, filters, null);
Assert.assertFalse(vc.isNotFiltered());
Assert.assertTrue(vc.isFiltered());
Assert.assertEquals(3, vc.getFilters().size());
vc.clearFilters();
Assert.assertTrue(vc.isNotFiltered());
Assert.assertFalse(vc.isFiltered());
Assert.assertEquals(0, vc.getFilters().size());
}
@Test

View File

@ -245,13 +245,13 @@ public class VariantContextUtilsUnitTest extends BaseTest {
@Test(dataProvider = "simplemergersiddata")
public void testRSIDMerge(SimpleMergeRSIDTest cfg) {
final VariantContext snpVC1 = makeVC("snpvc1", Arrays.asList(Aref, T));
VariantContext snpVC1 = makeVC("snpvc1", Arrays.asList(Aref, T));
final List<VariantContext> inputs = new ArrayList<VariantContext>();
for ( final String id : cfg.inputs ) {
MutableVariantContext vc = new MutableVariantContext(snpVC1);
if ( ! id.equals(".") ) vc.setID(id);
inputs.add(vc);
if ( id.equals(".") )
snpVC1 = VariantContext.modifyID(snpVC1, id);
inputs.add(snpVC1);
}
final VariantContext merged = VariantContextUtils.simpleMerge(genomeLocParser,