Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012 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;
import htsjdk.variant.vcf.VCFConstants;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Common utility routines for VariantContext and Genotype
*
* @author depristo
*/
public final class CommonInfo implements Serializable {
public static final long serialVersionUID = 1L;
public static final double NO_LOG10_PERROR = 1.0;
private static Set NO_FILTERS = Collections.emptySet();
private static Map NO_ATTRIBUTES = Collections.unmodifiableMap(new HashMap());
private double log10PError = NO_LOG10_PERROR;
private String name = null;
private Set filters = null;
private Map attributes = NO_ATTRIBUTES;
public CommonInfo(String name, double log10PError, Set filters, Map attributes) {
this.name = name;
setLog10PError(log10PError);
this.filters = filters;
if ( attributes != null && ! attributes.isEmpty() ) {
this.attributes = attributes;
}
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name
*
* @param name the name associated with this information
*/
public void setName(String name) {
if ( name == null ) throw new IllegalArgumentException("Name cannot be null " + this);
this.name = name;
}
// ---------------------------------------------------------------------------------------------------------
//
// Filter
//
// ---------------------------------------------------------------------------------------------------------
public Set getFiltersMaybeNull() {
return filters;
}
public Set getFilters() {
return filters == null ? NO_FILTERS : Collections.unmodifiableSet(filters);
}
public boolean filtersWereApplied() {
return filters != null;
}
public boolean isFiltered() {
return filters == null ? false : !filters.isEmpty();
}
public boolean isNotFiltered() {
return ! isFiltered();
}
public void addFilter(String filter) {
if ( filters == null ) // immutable -> mutable
filters = new HashSet();
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 ( String f : filters )
addFilter(f);
}
// ---------------------------------------------------------------------------------------------------------
//
// Working with log error rates
//
// ---------------------------------------------------------------------------------------------------------
public boolean hasLog10PError() {
return getLog10PError() != NO_LOG10_PERROR;
}
/**
* @return the -1 * log10-based error estimate
*/
public double getLog10PError() { return log10PError; }
/**
* Floating-point arithmetic allows signed zeros such as +0.0 and -0.0.
* Adding the constant 0.0 to the result ensures that the returned value is never -0.0
* since (-0.0) + 0.0 = 0.0.
*
* When this is set to '0.0', the resulting VCF would be 0 instead of -0.
*
* @return double - Phred scaled quality score
*/
public double getPhredScaledQual() { return (getLog10PError() * -10) + 0.0; }
public void setLog10PError(double log10PError) {
if ( log10PError > 0 && log10PError != NO_LOG10_PERROR)
throw new IllegalArgumentException("BUG: log10PError cannot be > 0 : " + this.log10PError);
if ( Double.isInfinite(this.log10PError) )
throw new IllegalArgumentException("BUG: log10PError should not be Infinity");
if ( Double.isNaN(this.log10PError) )
throw new IllegalArgumentException("BUG: log10PError should not be NaN");
this.log10PError = log10PError;
}
// ---------------------------------------------------------------------------------------------------------
//
// Working with attributes
//
// ---------------------------------------------------------------------------------------------------------
public void clearAttributes() {
attributes = new HashMap();
}
/**
* @return the attribute map
*/
public Map getAttributes() {
return Collections.unmodifiableMap(attributes);
}
// todo -- define common attributes as enum
public void setAttributes(Map map) {
clearAttributes();
putAttributes(map);
}
public void putAttribute(String key, Object value) {
putAttribute(key, value, false);
}
public void putAttribute(String key, Object value, boolean allowOverwrites) {
if ( ! allowOverwrites && hasAttribute(key) )
throw new IllegalStateException("Attempting to overwrite key->value binding: key = " + key + " this = " + this);
if ( attributes == NO_ATTRIBUTES ) // immutable -> mutable
attributes = new HashMap();
attributes.put(key, value);
}
public void removeAttribute(String key) {
if ( attributes == NO_ATTRIBUTES ) // immutable -> mutable
attributes = new HashMap();
attributes.remove(key);
}
public void putAttributes(Map map) {
if ( map != null ) {
// for efficiency, we can skip the validation if the map is empty
if (attributes.isEmpty()) {
if ( attributes == NO_ATTRIBUTES ) // immutable -> mutable
attributes = new HashMap();
attributes.putAll(map);
} else {
for ( Map.Entry elt : map.entrySet() ) {
putAttribute(elt.getKey(), elt.getValue(), false);
}
}
}
}
public boolean hasAttribute(String 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(String key) {
return attributes.get(key);
}
public Object getAttribute(String key, Object defaultValue) {
if ( hasAttribute(key) )
return attributes.get(key);
else
return defaultValue;
}
/**
* Gets the attributes from a key as a list.
*
* Note: int[] and double[] arrays are boxed.
*
* @return empty list if the key was not found; {@link Collections#singletonList(Object)} if
* there is only one value; a list containing the values if the value is a {@link List} or array.
*/
@SuppressWarnings("unchecked")
public List