gatk-3.8/java/src/org/broadinstitute/sting/oneoffprojects/variantcontext/AttributedObject.java

187 lines
6.1 KiB
Java
Raw Normal View History

package org.broadinstitute.sting.oneoffprojects.variantcontext;
import org.broadinstitute.sting.utils.GenomeLoc;
import org.broadinstitute.sting.utils.StingException;
import java.util.*;
/**
* Common superclass of VariantContext and Genotype
*
* @author depristo
*/
public abstract class AttributedObject {
public static final double NO_NEG_LOG_10PERROR = 0.0;
private double negLog10PError = NO_NEG_LOG_10PERROR;
private Set<Object> filters = new HashSet<Object>();
private Map<Object, Object> attributes = new HashMap<Object, Object>();
public AttributedObject() { }
public AttributedObject(double negLog10PError) {
setNegLog10PError(negLog10PError);
}
public AttributedObject(Map<? extends Object, ? extends Object> attributes) {
setAttributes(attributes);
}
public AttributedObject(Map<? extends Object, ? extends Object> attributes, double negLog10PError) {
this(attributes);
setNegLog10PError(negLog10PError);
}
// ---------------------------------------------------------------------------------------------------------
//
// Filter
//
// ---------------------------------------------------------------------------------------------------------
public Set<Object> getFilters() {
return filters;
}
public boolean isFiltered() {
return filters.size() > 0;
}
public boolean isNotFiltered() {
return ! isFiltered();
}
public void addFilter(Object filter) {
if ( filter == null ) throw new IllegalArgumentException("BUG: Attempting to add null filter " + this);
if ( getFilters().contains(filter) ) throw new IllegalArgumentException("BUG: Attempting to add duplicate filter " + filter + " at " + this);
filters.add(filter);
}
public void addFilters(Collection<?> filters) {
if ( filters == null ) throw new IllegalArgumentException("BUG: Attempting to add null filters at" + this);
for ( Object f : filters )
addFilter(f);
}
public void clearFilters() {
filters.clear();
}
public void setFilters(Collection<?> filters) {
clearFilters();
addFilters(filters);
}
// ---------------------------------------------------------------------------------------------------------
//
// Working with log error rates
//
// ---------------------------------------------------------------------------------------------------------
public boolean hasNegLog10PError() {
return getNegLog10PError() == NO_NEG_LOG_10PERROR;
}
/**
* @return the -1 * log10-based error estimate
*/
public double getNegLog10PError() { return negLog10PError; }
public void setNegLog10PError(double negLog10PError) {
if ( negLog10PError < 0 && negLog10PError != NO_NEG_LOG_10PERROR ) throw new IllegalArgumentException("BUG: negLog10PError cannot be < than 0 : " + negLog10PError);
if ( Double.isInfinite(negLog10PError) ) throw new IllegalArgumentException("BUG: negLog10PError should not be Infinity");
if ( Double.isNaN(negLog10PError) ) throw new IllegalArgumentException("BUG: negLog10PError should not be NaN");
this.negLog10PError = negLog10PError;
}
// ---------------------------------------------------------------------------------------------------------
//
// Working with attributes
//
// ---------------------------------------------------------------------------------------------------------
public void clearAttributes() {
this.attributes.clear();
}
/**
* @return the attribute map
*/
public Map<Object, Object> getAttributes() {
return attributes;
}
// todo -- define common attributes as enum
public void setAttributes(Map<? extends Object, ? extends Object> map) {
this.attributes.clear();
putAttributes(map);
}
public void putAttribute(Object key, Object value) {
putAttribute(key, value, false);
}
public void putAttribute(Object key, Object value, boolean allowOverwrites) {
if ( hasAttribute(key) && ! allowOverwrites )
throw new StingException("Attempting to overwrite key->value binding: key = " + key + " this = " + this);
this.attributes.put(key, value);
}
public void removeAttribute(Object key) {
this.attributes.remove(key);
}
public void putAttributes(Map<? extends Object, ? extends Object> map) {
if ( map != null ) {
for ( Map.Entry<? extends Object, ? extends Object> elt : map.entrySet() ) {
putAttribute(elt.getKey(), elt.getValue());
}
}
}
public boolean hasAttribute(Object key) {
return attributes.containsKey(key);
}
public int getNumAttributes() {
return attributes.size();
}
/**
* @param key the attribute key
*
* @return the attribute value for the given key (or null if not set)
*/
public Object getAttribute(Object key) {
return attributes.get(key);
}
public Object getAttribute(Object key, Object defaultValue) {
if ( hasAttribute(key) )
return attributes.get(key);
else
return defaultValue;
}
// public AttributedObject getAttributes(Collection<Object> keys) {
// AttributedObject selected = new AttributedObject();
//
// for ( Object key : keys )
// selected.putAttribute(key, this.getAttribute(key));
//
// return selected;
// }
public String getAttributeAsString(Object key) { return (String)getAttribute(key); }
public int getAttributeAsInt(Object key) { return (Integer)getAttribute(key); }
public double getAttributeAsDouble(Object key) { return (Double)getAttribute(key); }
public String getAttributeAsString(Object key, String defaultValue) { return (String)getAttribute(key, defaultValue); }
public int getAttributeAsInt(Object key, int defaultValue) { return (Integer)getAttribute(key, defaultValue); }
public double getAttributeAsDouble(Object key, double defaultValue) { return (Double)getAttribute(key, defaultValue); }
}