This code defines a class SFS
that simulates the evolution of genetic sequences under a beta coalescent model and calculates the Site Frequency Spectrum (SFS) from the generated genealogical trees. The SFS represents the distribution of allele frequencies in a population sample.
The SFS
class inherits from the betatree
class, which is responsible for generating the beta coalescent trees. The SFS
class extends this functionality by accumulating allele frequency information from multiple trees and calculating the SFS. It also provides methods for binning and saving the calculated SFS.
This code references the betatree
class from the same package, specifically the coalesce
method for generating the trees and accessing tree nodes for allele frequency calculations. It also uses the Bio.Phylo
module for working with phylogenetic trees.
SFS
This class simulates the evolution of genetic sequences under a beta coalescent model and calculates the Site Frequency Spectrum (SFS).
Name | Type | Description |
---|---|---|
sample_size | int | The number of individuals in the sample. |
alpha | float | The alpha parameter of the beta coalescent model (default: 2). |
The class does not have a direct return value. It stores the calculated SFS and related data in its internal attributes.
SFS
object by calling the constructor of the parent class betatree
and initializing internal variables to store allele frequencies and the SFS.glob_trees
method generates multiple beta coalescent trees using the inherited coalesce
method. For each tree, it extracts the branch lengths and corresponding allele counts (weights) and stores them. The getSFS
method then uses this accumulated data to calculate the average SFS over all generated trees.binSFS
method allows for binning the calculated SFS using different modes (linear, log, logit) and binning schemes. It calculates the bin edges, bin widths, and the binned SFS values.saveSFS
method saves a calculated SFS to a file, while the loadSFS
method loads a previously saved SFS from a file.The glob_trees
and getSFS
methods modify the internal state of the SFS
object by generating trees, accumulating allele frequencies, and calculating the SFS.
logit
This function calculates the logit of a given value.
Name | Type | Description |
---|---|---|
x | float | The input value. |
Name | Type | Description |
---|---|---|
logit(x) | float | The logit of the input value. |
The function calculates the logit using the formula: log(x / (1 - x))
.
Dependency | Purpose |
---|---|
numpy | Numerical operations, array handling. |
scipy.special | Special functions, including the gamma function used in beta coalescent calculations. |
Bio.Phylo | Working with phylogenetic trees. |
matplotlib.pyplot | Plotting the SFS (only in the example usage). |
The code includes basic error handling when loading an SFS from a file. It checks if the file exists and if the loaded data has the expected one-dimensional shape. If an error occurs, it prints an error message but does not raise an exception.
The code does not implement any specific logging mechanisms. It uses print
statements for informational output.
This code does not define an explicit API. It provides a class SFS
with methods for simulating the evolutionary process, calculating the SFS, and interacting with SFS data.
There are no explicit TODOs or notes left in the code.
This code defines a class SFS
that simulates the evolution of genetic sequences under a beta coalescent model and calculates the Site Frequency Spectrum (SFS) from the generated genealogical trees. The SFS represents the distribution of allele frequencies in a population sample.
The SFS
class inherits from the betatree
class, which is responsible for generating the beta coalescent trees. The SFS
class extends this functionality by accumulating allele frequency information from multiple trees and calculating the SFS. It also provides methods for binning and saving the calculated SFS.
This code references the betatree
class from the same package, specifically the coalesce
method for generating the trees and accessing tree nodes for allele frequency calculations. It also uses the Bio.Phylo
module for working with phylogenetic trees.
SFS
This class simulates the evolution of genetic sequences under a beta coalescent model and calculates the Site Frequency Spectrum (SFS).
Name | Type | Description |
---|---|---|
sample_size | int | The number of individuals in the sample. |
alpha | float | The alpha parameter of the beta coalescent model (default: 2). |
The class does not have a direct return value. It stores the calculated SFS and related data in its internal attributes.
SFS
object by calling the constructor of the parent class betatree
and initializing internal variables to store allele frequencies and the SFS.glob_trees
method generates multiple beta coalescent trees using the inherited coalesce
method. For each tree, it extracts the branch lengths and corresponding allele counts (weights) and stores them. The getSFS
method then uses this accumulated data to calculate the average SFS over all generated trees.binSFS
method allows for binning the calculated SFS using different modes (linear, log, logit) and binning schemes. It calculates the bin edges, bin widths, and the binned SFS values.saveSFS
method saves a calculated SFS to a file, while the loadSFS
method loads a previously saved SFS from a file.The glob_trees
and getSFS
methods modify the internal state of the SFS
object by generating trees, accumulating allele frequencies, and calculating the SFS.
logit
This function calculates the logit of a given value.
Name | Type | Description |
---|---|---|
x | float | The input value. |
Name | Type | Description |
---|---|---|
logit(x) | float | The logit of the input value. |
The function calculates the logit using the formula: log(x / (1 - x))
.
Dependency | Purpose |
---|---|
numpy | Numerical operations, array handling. |
scipy.special | Special functions, including the gamma function used in beta coalescent calculations. |
Bio.Phylo | Working with phylogenetic trees. |
matplotlib.pyplot | Plotting the SFS (only in the example usage). |
The code includes basic error handling when loading an SFS from a file. It checks if the file exists and if the loaded data has the expected one-dimensional shape. If an error occurs, it prints an error message but does not raise an exception.
The code does not implement any specific logging mechanisms. It uses print
statements for informational output.
This code does not define an explicit API. It provides a class SFS
with methods for simulating the evolutionary process, calculating the SFS, and interacting with SFS data.
There are no explicit TODOs or notes left in the code.