struct BioMarkovChain{S<:DataType, M<:AbstractMatrix, I<:AbstractVector, N<:Integer} <: AbstractBioMarkovChain

A BioMarkovChain represents a Markov chain used in biological sequence analysis. It contains a transition probability matrix (tpm) and an initial distribution of probabilities (inits) and also the order of the Markov chain.


  • alphabet::A: The state space of the sequence whether DNA, RNA AminoAcid DataTypes.
  • tpm::M: The transition probability matrix.
  • inits::I: The initial distribution of probabilities.
  • n::N: The order of the Markov chain.


  • BioMarkovChain(tpm::M, inits::I, n::N=1) where {M<:AbstractMatrix, I<:AbstractVector, N<:Integer}: Constructs a BioMarkovChain object with the provided transition probability matrix, initial distribution, and order.
  • BioMarkovChain(sequence::LongNucOrView{4}, n::Int64=1): Constructs a BioMarkovChain object based on the DNA sequence and transition order.


sequence = LongDNA{4}("ACTACATCTA")

model = BioMarkovChain(sequence, 2)

BioMarkovChain of DNAAlphabet{4}() and order 2:
  - Transition Probability Matrix -> Matrix{Float64}(4 × 4):
   0.4444  0.1111  0.0     0.4444
   0.4444  0.4444  0.0     0.1111
   0.0     0.0     0.0     0.0
   0.1111  0.4444  0.0     0.4444
  - Initial Probabilities -> Vector{Float64}(4 × 1):
   0.3333  0.3333  0.0     0.3333
initials(sequence::SeqOrView{A}) where A

Calculate the estimated initial probabilities for a Markov chain based on a given sequence.

This function takes a sequence of states and calculates the estimated initial probabilities of each state in the sequence for a Markov chain. The initial probabilities are estimated by counting the occurrences of each state at the beginning of the sequence and normalizing the counts to sum up to 1.

\[\begin{align} \pi{i} &= P(X_{i} = i), i \in T \\ \sum_{i=1}^{N} \pi_{i} &= 1 \end{align}\]

Now using the dinucleotides counts estimating the initials would follow:

\[\hat{\pi_{i}} = c_{i} \sum_{k} c_{k}\]


  • sequence::SeqOrView{A}: The sequence of states representing the Markov chain.


An Vector{Flot64} of estimated initial probabilities for each state in the sequence.

log_odds_ratio_matrix(model1::BioMarkovChain, model2::BioMarkovChain)

Calculates the log-odds ratio between the transition probability matrices of two BioMarkovChain models.

\[\beta = \log \frac{P(x|\mathscr{m}_{1})}{P(x|\mathscr{m}_{2})}\]

Where $\mathscr{m}_{1}$ and $\mathscr{m}_{2}$ are the two models transition probability matrices.


  • model1::BioMarkovChain: The first BioMarkovChain model.
  • model2::BioMarkovChain: The second BioMarkovChain model.
log_odds_ratio_score(sequence::SeqOrView{A}, model::BioMarkovChain; b::Number = ℯ)

Compute the log odds ratio score between a given sequence and a BioMarkovChain model.

\[S(x) = \sum_{i=1}^{L} \beta_{x_{i}x} = \sum_{i=1} \log \frac{a^{\mathscr{m}_{1}}_{i-1} x_i}{a^{\mathscr{m}_{2}}_{i-1} x_i}\]


  • sequence::SeqOrView{A}: A sequence of elements of type A.
  • model::BioMarkovChain: A BioMarkovChain model.
  • b::Number = ℯ: The base of the logarithm used to compute the log odds ratio.


The log odds ratio score between the sequence and the model.


markovprobability(sequence::LongNucOrView{4}, model::BioMarkovChain)

Compute the probability of a given sequence using a transition probability matrix and the initial probabilities distributions of a BioMarkovModel.

\[P(X_1 = i_1, \ldots, X_T = i_T) = \pi_{i_1}^{T-1} \prod_{t=1}^{T-1} a_{i_t, i_{t+1}}\]


  • sequence::LongNucOrView{4}: The input sequence of nucleotides.


  • model::BioMarkovChain=ECOLICDS: A given BioMarkovChain model.
  • logscale::Bool=false: If true, the function will return the log2 of the probability.
  • b::Number=2: The base of the logarithm used to compute the log odds ratio.


  • probability::Float64: The probability of the input sequence given the model.


markovprobability(seq, model=CPGPOS, logscale=true)

markovprobability(seq, model=CPGNEG, logscale=true)
perronfrobenius(sequence::SeqOrView{A}, n::Int64=1) where A

Compute the Perron-Frobenius matrix, a column-stochastic version of the transition probability matrix (TPM), for a given nucleotide sequence.

The Perron-Frobenius matrix captures the asymptotic probabilities of transitioning between nucleotides in the sequence over a specified number of steps n. It provides insight into the long-term behavior of a Markov chain or a dynamical system associated with the sequence.


  • sequence::SeqOrView{A}: A nucleotide sequence represented as a NucleicSeqOrView{A} object.
  • n::Int64=1: The number of steps to consider for the transition probability matrix. Default is 1.


A copy of the Perron-Frobenius matrix. Each column of this matrix corresponds to the probabilities of transitioning from the current nucleotide state to all possible nucleotide states after n steps.


sequence = LongSequence{DNAAlphabet{4}}("ACGTCGTCCACTACGACATCAGC")  # Replace with an actual nucleotide sequence
n = 2
pf = perronfrobenius(sequence, n)

Compute the transition count matrix (TCM) of a given DNA sequence.


  • sequence::LongSequence{DNAAlphabet{4}}: a LongSequence{DNAAlphabet{4}} object representing the DNA sequence.


A Matrix object representing the transition count matrix of the sequence.


seq = LongDNA{4}("AGCTAGCTAGCT")

tcm = transition_count_matrix(seq)

4×4 Matrix{Int64}:
 0  0  3  0
 0  0  0  3
 0  3  0  0
 2  0  0  0
transition_probability_matrix(sequence::LongSequence{DNAAlphabet{4}}, n::Int64=1)

Compute the transition probability matrix (TPM) of a given DNA sequence. Formally it construct $\hat{\mathscr{M}}$ where:

\[\mathscr{m}_{ij} = P(X_t = j \mid X_{t-1} = i) = \frac{{P(X_{t-1} = i, X_t = j)}}{{P(X_{t-1} = i)}}\]

The transition matrices of DNA and Amino-Acids are arranged sorted and in row-wise matrices:

First the DNA matrix:

\[\mathscr{M}_{DNA} = \begin{bmatrix} _{AA} & _{AC} & _{AG} & _{AT} \\ _{CA} & _{CC} & _{CG} & _{CT} \\ _{GA} & _{GC} & _{GG} & _{GT} \\ _{TA} & _{TC} & _{TG} & _{TT} \\ \end{bmatrix}\]

And then, the Aminoacids:

\[\mathscr{M}_{AA} = \begin{bmatrix} _{AA} & _{AC} & _{AD} & \dots & _{AW} \\ _{CA} & _{CC} & _{CD} & \dots & _{CW} \\ _{DA} & _{DC} & _{DD} & \dots & _{DW} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ _{WA} & _{WC} & _{WD} & \dots & _{WW} \\ \end{bmatrix}\]


  • sequence::LongNucOrView{4}: a LongNucOrView{4} object representing the DNA sequence.
  • n::Int64=1: The order of the Markov model. That is the $\hat{M}^{n}$


  • extended_alphabet::Bool=false: If true will pass the extended alphabet of DNA to search


A Matrix object representing the transition probability matrix of the sequence.



tpm = transition_probability_matrix(seq)

4×4 Matrix{Float64}:
 0.0  0.0  1.0  0.0
 0.0  0.0  0.0  1.0
 0.0  1.0  0.0  0.0
 1.0  0.0  0.0  0.0