Site stratification#
A Stratification splits sites into categories so the Parser counts a separate spectrum per stratum, for instance neutral versus selected sites.
Abstract class for Stratifying the SFS by determining a site's type based on its properties. |
|
Abstract class for stratifications that can only handle SNPs. |
|
Stratify the SFS by the base context of the mutation. |
|
Stratify the SFS by the base transition of the mutation, i.e., |
|
Stratify the SFS by whether we have a transition or transversion. |
|
Stratify the SFS by the base context of the mutation: the reference base. |
|
Stratify SFS by degeneracy. |
|
Stratify SFS by synonymy (neutral or selected). |
|
Stratify SFS by synonymy (neutral or selected) based on annotation provided by VEP. |
|
Stratify SFS by synonymy (neutral or selected) based on annotation provided by SnpEff. |
|
Base class for stratifications that derive the type from a site's genomic position. |
|
Stratify SFS by contig. |
|
Stratify SFS by creating |
|
Stratify the SFS randomly into a fixed number of bins. |
Stratification#
- class Stratification[source]#
Bases:
ABCAbstract class for Stratifying the SFS by determining a site’s type based on its properties.
Methods:
- abstractmethod get_type(variant: Site)[source]#
Get type of given Variant. Only the types given by
get_types()are valid, orNoneif no type could be determined.
SNPStratification#
- class SNPStratification[source]#
Bases:
Stratification,ABCAbstract class for stratifications that can only handle SNPs. We need to issue a warning in this case.
Methods:
- __init__()#
Create instance.
- abstractmethod get_type(variant: Site)#
Get type of given Variant. Only the types given by
get_types()are valid, orNoneif no type could be determined.
BaseContextStratification#
- class BaseContextStratification(fasta: str, n_flanking: int = 1, aliases: Dict[str, List[str]] = {}, cache: bool = True)[source]#
Bases:
Stratification,FASTAHandlerStratify the SFS by the base context of the mutation. The number of flanking bases can be configured. Note that we attempt to take the ancestral allele as the middle base. If
skip_non_polarizedis set toFalse, we use the reference allele as the middle base.Methods:
Create instance.
Get the base context for a given mutation
Create all possible base contexts.
Download a file from a URL.
Download the file if it is a URL.
Get all aliases for the given contig alias including the primary alias.
Get the contig from the FASTA file.
Get the names of the contigs in the FASTA file.
Return the file extension of a URL.
Return a truncated SHA1 hash of a string.
Check if the given path is a URL.
Load a FASTA file into a dictionary.
If the given file is gzipped, unzip it and return the path to the unzipped file.
- __init__(fasta: str, n_flanking: int = 1, aliases: Dict[str, List[str]] = {}, cache: bool = True)[source]#
Create instance. Note that we require a fasta file to be specified for base context to be able to be inferred
- Parameters:
fasta (
str) – The fasta file path, possibly gzipped or a URLn_flanking (
int) – The number of flanking basesaliases (
Dict[str,List[str]]) – Dictionary of aliases for the contigs in the input, e.g.{'chr1': ['1']}. This is used to match the contig names in the input with the contig names in the FASTA file and GFF file.cache (
bool) – Whether to cache files that are downloaded from URLs
- classmethod download_file(url: str, cache: bool = True, desc: str = 'Downloading file')#
Download a file from a URL.
- get_contig(aliases, rewind: bool = True, notify: bool = True)#
Get the contig from the FASTA file. The contig is looked up in the header index and read by seeking to it, so the sites may visit the contigs in any order at the same cost.
Note that
pyfaidxwould be more efficient here, but there were problems when running it in parallel.- Parameters:
- Return type:
SeqRecord- Returns:
The contig.
- Raises:
LookupError – Where the FASTA carries none of the aliases.
- get_contig_names()#
Get the names of the contigs in the FASTA file.
- static get_filename(url: str)#
Return the file extension of a URL.
- Parameters:
url (
str) – The URL to get the file extension from.- Returns:
The file extension.
- load_fasta(file: str)#
Load a FASTA file into a dictionary.
- Parameters:
file (
str) – The path to The FASTA file path, possibly gzipped or a URL- Return type:
FastaIterator- Returns:
Iterator over the sequences.
- static unzip_if_zipped(file: str)#
If the given file is gzipped, unzip it and return the path to the unzipped file. If the file is not gzipped, return the path to the original file.
- Parameters:
file (
str) – The path to the file.- Returns:
The path to the unzipped file, or the original file if it was not gzipped.
- aliases#
The contig mappings
BaseTransitionStratification#
- class BaseTransitionStratification[source]#
Bases:
SNPStratificationStratify the SFS by the base transition of the mutation, i.e.,
A>T.Warning
This stratification only works for SNPs. You thus need to update the number of mono-allelic sites manually.
Methods:
Get the base transition for the given variant.
Get all possible base transitions.
Create instance.
- get_type(variant: Site)[source]#
Get the base transition for the given variant.
- Parameters:
variant (
Site) – The site- Return type:
- Returns:
Base transition
- Raises:
NoTypeException – if not type could be determined
- __init__()#
Create instance.
TransitionTransversionStratification#
- class TransitionTransversionStratification[source]#
Bases:
BaseTransitionStratificationStratify the SFS by whether we have a transition or transversion.
Warning
This stratification only works for SNPs. You thus need to update the number of mono-allelic sites manually.
Methods:
Get the mutation type (transition or transversion) for a given mutation.
All possible mutation types (transition and transversion).
Create instance.
- get_type(variant: Site)[source]#
Get the mutation type (transition or transversion) for a given mutation.
- __init__()#
Create instance.
AncestralBaseStratification#
- class AncestralBaseStratification[source]#
Bases:
StratificationStratify the SFS by the base context of the mutation: the reference base. If
skip_non_polarizedis set toFalse, we use the reference allele as ancestral base. By default, we use theAAtag to determine the ancestral allele.Any subclass of
AncestralAlleleAnnotationcan be used to annotate the ancestral allele.Methods:
Get the type which is the reference allele.
The possible base types.
Create instance.
- __init__()#
Create instance.
DegeneracyStratification#
- class DegeneracyStratification(custom_callback: Callable[[cyvcf2.Variant], str] = None)[source]#
Bases:
StratificationStratify SFS by degeneracy. We only consider sides which 4-fold degenerate (neutral) or 0-fold degenerate (selected) which facilitates counting.
DegeneracyAnnotationcan be used to annotate the degeneracy of a site.Methods:
Initialize the stratification.
Get the degeneracy.
Get all possible degeneracy type (
neutralandselected).- __init__(custom_callback: Callable[[cyvcf2.Variant], str] = None)[source]#
Initialize the stratification.
- get_degeneracy#
Custom callback to determine the degeneracy of mutation
- get_type(variant: Site)[source]#
Get the degeneracy.
- Parameters:
variant (
Site) – The site- Return type:
Literal['neutral','selected']- Returns:
Type of the mutation
- Raises:
NoTypeException – If the mutation is not synonymous or non-synonymous
SynonymyStratification#
- class SynonymyStratification[source]#
Bases:
SNPStratificationStratify SFS by synonymy (neutral or selected).
SynonymyAnnotationcan be used to annotate the synonymy of a site.Warning
This stratification only works for SNPs. You thus need to update the number of mono-allelic sites manually.
Methods:
Get all possible synonymy types (
neutralandselected).Get the synonymy using the custom synonymy annotation.
Create instance.
- __init__()#
Create instance.
VEPStratification#
- class VEPStratification[source]#
Bases:
SynonymyStratificationStratify SFS by synonymy (neutral or selected) based on annotation provided by VEP.
Warning
This stratification only works for SNPs. You thus need to update the number of mono-allelic sites manually.
Methods:
Get all possible synonymy types (
neutralandselected).Get the synonymy of a site.
Create instance.
- __init__()#
Create instance.
SnpEffStratification#
- class SnpEffStratification[source]#
Bases:
VEPStratificationStratify SFS by synonymy (neutral or selected) based on annotation provided by SnpEff.
Warning
This stratification only works for SNPs. You thus need to update the number of mono-allelic sites manually.
Methods:
Create instance.
Get the synonymy of a site.
Get all possible synonymy types (
neutralandselected).- __init__()#
Create instance.
- get_types()#
Get all possible synonymy types (
neutralandselected).
GenomePositionDependentStratification#
- class GenomePositionDependentStratification[source]#
Bases:
Stratification,ABCBase class for stratifications that derive the type from a site’s genomic position.
Methods:
- __init__()#
Create instance.
- abstractmethod get_type(variant: Site)#
Get type of given Variant. Only the types given by
get_types()are valid, orNoneif no type could be determined.
ContigStratification#
- class ContigStratification(contigs: List[str] = None)[source]#
Bases:
GenomePositionDependentStratificationStratify SFS by contig.
Methods:
Initialize the stratification.
Get the contig.
Get all possible contig type.
ChunkedStratification#
- class ChunkedStratification(n_chunks: int)[source]#
Bases:
GenomePositionDependentStratificationStratify SFS by creating
ncontiguous chunks of roughly equal size.Note
Since the total number of sites is not known in advance, we cannot create contiguous chunks of exactly equal size.
Warning
Chunk boundaries are sized from the raw input record count (
parser.n_sites), but a site is assigned to a chunk only once it has survived filtration and down-projection. When any filtration or projection drops sites, all included sites fall within the firstn_included / n_sitesfraction of the record range, so they concentrate in the leading chunks and the trailing chunks come out under-filled or empty. The included count is not known at setup without an extra pass over the data, so the chunks cannot be pre-balanced; a warning is logged at setup when the parser carries filtrations.The first pass assigns sites by counting them and records the genomic position at which each chunk begins. Any further pass over the same input (the
TargetSiteCountersampling pass, which visits a different number of sites) is assigned by position instead, so a sampled site lands in the same chunk as the variants surrounding it.Methods:
Initialize the stratification.
Get all possible window types.
Get the type.
- __init__(n_chunks: int)[source]#
Initialize the stratification.
- Parameters:
n_chunks (
int) – Number of sites per window
RandomStratification#
- class RandomStratification(n_bins: int, seed: int | None = 0)[source]#
Bases:
StratificationStratify the SFS randomly into a fixed number of bins. Can be used to analyze expected sampling variance between different stratifications.
Methods:
Initialize random stratification.
Assign the variant to a random bin.
Get all possible bin labels.
- rng#
Random generator instance