Package htsjdk.variant.variantcontext
Class GenotypesContext
java.lang.Object
htsjdk.variant.variantcontext.GenotypesContext
- All Implemented Interfaces:
Serializable,Iterable<Genotype>,Collection<Genotype>,List<Genotype>,SequencedCollection<Genotype>
- Direct Known Subclasses:
LazyGenotypesContext
Represents an ordered collection of Genotype objects
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final GenotypesContextstatic constant value for an empty GenotypesContext.An ArrayList of genotypes contained in this context WARNING: TO ENABLE THE LAZY VERSION OF THIS CLASS, NO METHODS SHOULD DIRECTLY ACCESS THIS VARIABLE.sampleNamesInOrder a list of sample names, one for each genotype in genotypes, sorted in alphabetical ordera map optimized for efficient lookup.static final long -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate an empty GenotypeContextprotectedGenotypesContext(int n) Create an empty GenotypeContext, with initial capacity for n elementsprotectedGenotypesContext(ArrayList<Genotype> genotypes) Create an GenotypeContext containing genotypesprotectedGenotypesContext(ArrayList<Genotype> genotypes, Map<String, Integer> sampleNameToOffset, List<String> sampleNamesInOrder) Create a fully resolved GenotypeContext containing genotypes, sample lookup table, and sorted sample names -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanAdds a single genotype to this context.booleanaddAll(int i, Collection<? extends Genotype> genotypes) booleanaddAll(Collection<? extends Genotype> genotypes) Adds all of the genotypes to this context Seeadd(Genotype)for important information about this functions constraints and performance costsfinal voidvoidclear()booleanbooleancontainsAll(Collection<?> objects) booleancontainsSample(String sample) booleancontainsSamples(Collection<String> samples) static final GenotypesContextcopy(GenotypesContext toCopy) Create a freshly allocated GenotypeContext containing the genotypes in toCopystatic final GenotypesContextcopy(Collection<Genotype> toCopy) Create a GenotypesContext containing the genotypes in iteration order contained in toCopystatic final GenotypesContextcreate()Basic creation routinestatic final GenotypesContextcreate(int nGenotypes) Basic creation routinestatic final GenotypesContextCreate a fully resolved GenotypeContext containing genotypesstatic final GenotypesContextCreate a fully resolved GenotypeContext containing genotypesstatic final GenotypesContextcreate(ArrayList<Genotype> genotypes, Map<String, Integer> sampleNameToOffset, List<String> sampleNamesInOrder) Create a fully resolved GenotypeContext containing genotypes, sample lookup table, and sorted sample namesprotected voidprotected voidget(int i) Gets sample associated with this sampleName, or null if none is foundintgetMaxPloidy(int defaultPloidy) What is the max ploidy among all samples? Returns defaultPloidy if no genotypes are presentfinal GenotypesContextintprotected voidprotected voidbooleanisEmpty()booleanbooleanIterate over the Genotypes in this context in their sample name order (A, B, C) regardless of the underlying order in the vector of genotypesiterateInSampleNameOrder(Iterable<String> sampleNamesInOrder) Iterate over the Genotypes in this context in the order specified by sampleNamesInOrderiterator()intlistIterator(int i) remove(int i) Note that remove requires us to invalidate our sample -> index cache.booleanSee for important warningremove(int)booleanremoveAll(Collection<?> objects) Replaces the genotype in this context -- note for efficiency reasons we do not add the genotype if it's not present.booleanretainAll(Collection<?> objects) intsize()subList(int i, int i1) subsetToSamples(Set<String> samples) Return a freshly allocated subcontext of this context containing only the samples listed in samples.Object[]toArray()<T> T[]toArray(T[] ts) toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, equals, getFirst, getLast, hashCode, removeFirst, removeLast, replaceAll, reversed, sort, spliterator
-
Field Details
-
serialVersionUID
public static final long serialVersionUID- See Also:
-
NO_GENOTYPES
static constant value for an empty GenotypesContext. Useful since so many VariantContexts have no genotypes -
sampleNamesInOrder
sampleNamesInOrder a list of sample names, one for each genotype in genotypes, sorted in alphabetical order -
sampleNameToOffset
a map optimized for efficient lookup. Each genotype in genotypes must have its sample name in sampleNameToOffset, with a corresponding integer value that indicates the offset of that genotype in the vector of genotypes -
notToBeDirectlyAccessedGenotypes
An ArrayList of genotypes contained in this context WARNING: TO ENABLE THE LAZY VERSION OF THIS CLASS, NO METHODS SHOULD DIRECTLY ACCESS THIS VARIABLE. USE getGenotypes() INSTEAD.
-
-
Constructor Details
-
GenotypesContext
protected GenotypesContext()Create an empty GenotypeContext -
GenotypesContext
protected GenotypesContext(int n) Create an empty GenotypeContext, with initial capacity for n elements -
GenotypesContext
Create an GenotypeContext containing genotypes -
GenotypesContext
protected GenotypesContext(ArrayList<Genotype> genotypes, Map<String, Integer> sampleNameToOffset, List<String> sampleNamesInOrder) Create a fully resolved GenotypeContext containing genotypes, sample lookup table, and sorted sample names- Parameters:
genotypes- our genotypes in arbitrarysampleNameToOffset- map optimized for efficient lookup. Each genotype in genotypes must have its sample name in sampleNameToOffset, with a corresponding integer value that indicates the offset of that genotype in the vector of genotypessampleNamesInOrder- a list of sample names, one for each genotype in genotypes, sorted in alphabetical order.
-
-
Method Details
-
create
Basic creation routine- Returns:
- an empty, mutable GenotypeContext
-
create
Basic creation routine- Returns:
- an empty, mutable GenotypeContext with initial capacity for nGenotypes
-
create
public static final GenotypesContext create(ArrayList<Genotype> genotypes, Map<String, Integer> sampleNameToOffset, List<String> sampleNamesInOrder) Create a fully resolved GenotypeContext containing genotypes, sample lookup table, and sorted sample names- Parameters:
genotypes- our genotypes in arbitrarysampleNameToOffset- map optimized for efficient lookup. Each genotype in genotypes must have its sample name in sampleNameToOffset, with a corresponding integer value that indicates the offset of that genotype in the vector of genotypessampleNamesInOrder- a list of sample names, one for each genotype in genotypes, sorted in alphabetical order.- Returns:
- an mutable GenotypeContext containing genotypes with already present lookup data
-
create
Create a fully resolved GenotypeContext containing genotypes- Parameters:
genotypes- our genotypes in arbitrary- Returns:
- an mutable GenotypeContext containing genotypes
-
create
Create a fully resolved GenotypeContext containing genotypes- Parameters:
genotypes- our genotypes in arbitrary- Returns:
- an mutable GenotypeContext containing genotypes
-
copy
Create a freshly allocated GenotypeContext containing the genotypes in toCopy- Parameters:
toCopy- the GenotypesContext to copy- Returns:
- an mutable GenotypeContext containing genotypes
-
copy
Create a GenotypesContext containing the genotypes in iteration order contained in toCopy- Parameters:
toCopy- the collection of genotypes- Returns:
- an mutable GenotypeContext containing genotypes
-
immutable
-
isMutable
public boolean isMutable() -
checkImmutability
- Throws:
UnsupportedOperationException
-
invalidateSampleNameMap
protected void invalidateSampleNameMap() -
invalidateSampleOrdering
protected void invalidateSampleOrdering() -
ensureSampleOrdering
protected void ensureSampleOrdering() -
ensureSampleNameMap
protected void ensureSampleNameMap() -
isLazyWithData
public boolean isLazyWithData() -
getGenotypes
-
clear
public void clear() -
size
public int size() -
isEmpty
public boolean isEmpty() -
add
Adds a single genotype to this context. There are many constraints on this input, and important impacts on the performance of other functions provided by this context. First, the sample name of genotype must be unique within this context. However, this is not enforced in the code itself, through you will invalid the contract on this context if you add duplicate samples and are running with CoFoJa enabled. Second, adding genotype also updates the sample name -> index map, so add() followed by containsSample and related function is an efficient series of operations. Third, adding the genotype invalidates the sorted list of sample names, to add() followed by any of the SampleNamesInOrder operations is inefficient, as each SampleNamesInOrder must rebuild the sorted list of sample names at an O(n log n) cost.- Specified by:
addin interfaceCollection<Genotype>- Specified by:
addin interfaceList<Genotype>- Parameters:
genotype-- Returns:
- Throws:
UnsupportedOperationException- if the context has been made immutable
-
add
-
addAll
Adds all of the genotypes to this context Seeadd(Genotype)for important information about this functions constraints and performance costs -
addAll
-
contains
-
containsAll
- Specified by:
containsAllin interfaceCollection<Genotype>- Specified by:
containsAllin interfaceList<Genotype>
-
get
-
getMaxPloidy
public int getMaxPloidy(int defaultPloidy) What is the max ploidy among all samples? Returns defaultPloidy if no genotypes are present- Parameters:
defaultPloidy- the default ploidy, if all samples are no-called- Returns:
-
get
Gets sample associated with this sampleName, or null if none is found- Parameters:
sampleName-- Returns:
-
indexOf
-
iterator
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList<Genotype>
-
listIterator
- Specified by:
listIteratorin interfaceList<Genotype>
-
listIterator
- Specified by:
listIteratorin interfaceList<Genotype>
-
remove
Note that remove requires us to invalidate our sample -> index cache. The loop: GenotypesContext gc = ... for ( sample in samples ) if ( gc.containsSample(sample) ) gc.remove(sample) is extremely inefficient, as each call to remove invalidates the cache and containsSample requires us to rebuild it, an O(n) operation. If you must remove many samples from the GC, use either removeAll or retainAll to avoid this O(n * m) operation. -
remove
See for important warningremove(int) -
removeAll
-
retainAll
-
set
-
replace
Replaces the genotype in this context -- note for efficiency reasons we do not add the genotype if it's not present. The return value will be null indicating this happened. Note this operation is preserves the map cache Sample -> Offset but invalidates the sorted list of samples. Using replace within a loop containing any of the SampleNameInOrder operation requires an O(n log n) resorting after each replace operation.- Parameters:
genotype- a non null genotype to bind in this context- Returns:
- null if genotype was not added, otherwise returns the previous genotype
-
subList
-
toArray
-
toArray
public <T> T[] toArray(T[] ts) -
iterateInSampleNameOrder
Iterate over the Genotypes in this context in the order specified by sampleNamesInOrder- Parameters:
sampleNamesInOrder- a Iterable of String, containing exactly one entry for each Genotype sample name in this context- Returns:
- a Iterable over the genotypes in this context.
-
iterateInSampleNameOrder
Iterate over the Genotypes in this context in their sample name order (A, B, C) regardless of the underlying order in the vector of genotypes- Returns:
- a Iterable over the genotypes in this context.
-
getSampleNames
- Returns:
- The set of sample names for all genotypes in this context, in arbitrary order
-
getSampleNamesOrderedByName
- Returns:
- The set of sample names for all genotypes in this context, in their natural ordering (A, B, C)
-
containsSample
-
containsSamples
-
subsetToSamples
Return a freshly allocated subcontext of this context containing only the samples listed in samples. Note that samples can contain names not in this context, they will just be ignored.- Parameters:
samples-- Returns:
-
toString
-