Package model

Class Variant


public class Variant extends Attributes
Represents a nucleotide variant.

This class represents a nucleotide variant, including its reference base content, type (e.g., SNV, insertion, deletion), and occurrences in samples and alleles. It provides methods to determine the type of the variant, check its canonical or padded canonical status. It extends the Attributes class to inherit functionality for managing attributes associated with the variant.

In contrast to other entities in the model, this class does not implement an identifier, but the combination of position, reference, and alternative is used as such. Variants are stored in the Contig.variants property of the model.

  • Field Details

    • position

      public final int position
      The 1-based position of this variant on a contig.
    • reference

      public final String reference
      The reference base content of this variant.
    • alternative

      public final String alternative
      The alternative base content of this variant.
    • type

      public final Variant.Type type
      The type of this variant (e.g., SNV, insertion, deletion).
    • active

      protected transient boolean active
      Indicates whether this variant is active.

      This boolean flag is used to mark variants that are newly identified and not present in an existing storage or modified. It is set to true for newly created or touched entries, but will not be serialized. During deserialization, it is assumed that all variants are idle and should be set to false (see Storage.typeAdapter()).

  • Constructor Details

    • Variant

      protected Variant(int position, String reference, String alternative)
      Constructs a new Variant instance, based on the provided position, reference, and alternative content.

      The constructor determines the type of variant based on the reference and alternative content as well as if the reference and alternative content match any padded canonical content type. If they do not, an IllegalArgumentException is thrown.

      Parameters:
      position - The 1-based position of the variant on a contig.
      reference - The reference base content of the variant.
      alternative - The alternative base content of the variant.
      Throws:
      IllegalArgumentException - If the reference and alternative content do not match any padded canonical content type.
  • Method Details

    • ofSample

      public boolean ofSample(String sampleIdentifier)
      Checks if the variant is associated with a specific sample.
      Parameters:
      sampleIdentifier - The unique identifier of the sample to check.
      Returns:
      true if the variant is associated with the given sample identifier, false otherwise.
    • ofSamples

      public boolean ofSamples(Collection<String> sampleIdentifiers)
      Checks if the variant is associated with any of the specified samples.
      Parameters:
      sampleIdentifiers - A collection of sample identifiers to check.
      Returns:
      true if the variant is associated with at least one of the given sample identifiers, false otherwise.
    • ofFeature

      public boolean ofFeature(String featureIdentifier)
      Checks if the variant is associated with a specific feature.

      This method determines whether the given feature identifier exists in the features map. The features map contains associations between feature identifiers and their related alleles.

      Parameters:
      featureIdentifier - The unique identifier of the feature to check.
      Returns:
      true if the feature identifier exists in the features map; false otherwise.
    • ofFeatures

      public boolean ofFeatures(Collection<String> featureIdentifiers)
      Checks if the variant is associated with any of the specified features.
      Parameters:
      featureIdentifiers - A collection of feature identifiers to check.
      Returns:
      true if the variant is associated with at least one of the given feature identifiers, false otherwise.
    • ofAllele

      public boolean ofAllele(String alleleIdentifier)
      Checks if the variant is associated with a specific allele.
      Parameters:
      alleleIdentifier - The unique identifier of the allele to check.
      Returns:
      true if the variant is associated with the given allele identifier, false otherwise.
    • ofAlleles

      public boolean ofAlleles(Collection<String> alleleIdentifiers)
      Checks if the variant is associated with any of the specified alleles.
      Parameters:
      alleleIdentifiers - A collection of allele identifiers to check.
      Returns:
      true if the variant is associated with at least one of the given allele identifiers, false otherwise.
    • isFiltered

      public boolean isFiltered(String sampleIdentifier)
      Checks if the variant is filtered with respect to a specific sample.

      Retrieves the variant call string associated with the given sample identifier from the `samples` map. It then checks if the variant call is filtered using the VariantCall.isFiltered(String) method. If the sample identifier does not exist in the map, the method returns false, indicating that the variant is considered not filtered.

      Parameters:
      sampleIdentifier - The identifier of the sample to check.
      Returns:
      true if the variant is filtered for the specified sample; false otherwise.
    • isFiltered

      public boolean isFiltered(Collection<String> sampleIdentifiers)
      Checks if the variant is filtered with respect to all specified samples.

      See isFiltered(String).

      Parameters:
      sampleIdentifiers - A collection of sample identifiers to check.
      Returns:
      true if the variant is filtered for all specified samples; false otherwise.
    • isFiltered

      public boolean isFiltered()
      Checks if all variant calls for this variant are filtered.

      This method iterates through all variant calls in the `samples` map and checks if each call is filtered using the VariantCall.isFiltered(String) method. If all calls are filtered, it returns true; otherwise, it returns false.

      Returns:
      true if all variant calls for this variant are filtered; false otherwise.
    • getRelatedSamples

      public Set<htsjdk.samtools.util.Tuple<String,String>> getRelatedSamples()
      Retrieves a set of sample identifiers that have occurrences of this variant.

      This method returns an unmodifiable set of sample identifiers associated with this variant. The collection ensures that external modifications are not allowed, preserving the integrity of the data.

      Returns:
      A set of sample identifiers that have occurrences of this variant.
    • getSampleRelation

      public String getSampleRelation(String sampleIdentifier)
      Retrieves the variant call string associated with a specific sample.

      This method fetches the variant call string for the given sample identifier from the `samples` map. If the sample identifier does not exist in the map, it returns null.

      Parameters:
      sampleIdentifier - The unique identifier of the sample whose relation is to be retrieved.
      Returns:
      The variant call string associated with the given sample identifier, or null if the sample is not found.
    • getFeatureRelation

      public Set<String> getFeatureRelation(String featureIdentifier)
      Retrieves a set of allele identifiers associated with a specific feature for this variant.

      This method fetches the set of allele identifiers for the given feature identifier from the `features` map. If the feature identifier does not exist in the map, it returns an empty set.

      Parameters:
      featureIdentifier - The unique identifier of the feature whose allele relations are to be retrieved.
      Returns:
      A set of allele identifiers associated with the given feature identifier, or an empty set if the feature is not found.
    • getRelatedAlleles

      public Set<htsjdk.samtools.util.Tuple<String,String>> getRelatedAlleles()
      Retrieves a set of tuples representing the feature and allele occurrences associated with this variant.
      Returns:
      A set of tuples where each tuple contains:
      • The feature identifier as a String.
      • The allele identifier as a String.
    • addSampleRelation

      public void addSampleRelation(String sampleIdentifier, Set<VariantCall> variantCalls)
      Associates a sample with this variant by adding the sample identifier and its associated variant calls.

      This method updates the `samples` map by associating the given sample identifier with a string representation of the provided variant calls. The variant calls are converted to strings using their `toString` method and concatenated with a pipe ('|') delimiter.

      This will also mark the variant as active by setting the active property to true, indicating that the variant has been modified. This will overwrite any existing association for the given sample identifier.

      Parameters:
      sampleIdentifier - The unique identifier of the sample to associate with this variant.
      variantCalls - A set of VariantCall objects representing the variant calls to associate with the sample. Each variant call is converted to its string representation.
    • addAlleleRelation

      public void addAlleleRelation(String featureIdentifier, String alleleIdentifier)
      Associates an allele and its parent feature with this variant.
      Parameters:
      featureIdentifier - The identifier of the feature to associate with this variant.
      alleleIdentifier - The identifier of the allele to associate with the feature.
    • asStub

      public Variant.Stub asStub()
      Converts this variant to a simplified stub representation.

      This method creates a Variant.Stub object that encapsulates the position and alternative allele of this variant. The stub serves as a simplified representation of the variant.

      Returns:
      A Variant.Stub object containing the position and alternative allele of this variant.
    • asMaskedStub

      public Variant.Stub asMaskedStub()
      Converts this variant to a masked stub representation.

      This method creates a Variant.Stub object that encapsulates the position of this variant and a masked alternative allele represented by Constants.ANY_NUCLEOTIDE.

      Returns:
      A Variant.Stub object containing the position of this variant and a masked alternative allele.
    • toString

      public String toString()
      Converts the variant to its string representation.

      This method generates a string representation of the variant based on its type:

      • For Variant.Type.SNV, the format is: "prefix + position + reference + '>' + alternative".
      • For Variant.Type.INSERTION, the format is: "prefix + position + '_' + (position + 1) + 'ins' + alternative".
      • For Variant.Type.DELETION, the format is: "prefix + position + '_' + (position + reference.length() - 1) + 'del'".
      Overrides:
      toString in class Object
      Returns:
      A String representing the variant.
    • hashCode

      public int hashCode()
      Computes the hash code for this variant.

      This method calculates the hash code of the variant based on its string representation.

      Overrides:
      hashCode in class Object
      Returns:
      The hash code of the variant.
    • equals

      public boolean equals(Object obj)
      Compares this variant to another object for equality.

      This method checks if the provided object is the same instance as this object. If not, it verifies that the object is of the same class and compares their string representations for equality.

      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare with this Variant instance.
      Returns:
      true if the objects are the same instance or if their string representations are equal; false otherwise.