Skip to content

Sequences

Sequences shared by the biology packages.

They are sampled row by row, such as the sequences of RNA walks and the conformations of proteins.

RowScoreSpec(function, proposals=1) dataclass

Bases: BaseSpec

Spec of an objective that scores a whole row of a lattice state.

The row is the values along the last axis.

Parameters:

Name Type Description Default
function Callable[..., float]

The function function(row, **parameters) of the values of a row and the parameters of the system that it names, which returns one number.

required
proposals int

The number of moves proposed per row and unit of time.

1

Raises:

Type Description
ValidationError

The function is not callable, or the number of proposals is not positive.

Examples:

>>> from skcomplex.systems.biology.common.sequences import RowScoreSpec
>>> RowScoreSpec(sum, proposals=2).proposals
2

SequenceSamplingInteraction(label, *, target, state, objective, move='flip', intensity=1.0, rule='metropolis', values=(), width=1.0, cooling=None)

Bases: Interaction

Monte Carlo sampling of whole rows of a state on a LatticeSpace, scored row by row.

The objective is a RowScoreSpec that scores whole rows along the last axis. Every unit of time proposes its proposals moves per row, each accepted from the change of the score of the row, and a run records the score of every row as <label>.score. A named move changes one random site of the row, a function move receives the row, and the heat-bath rule draws the value of one random site among values by the weights exp(-intensity * score). An infinite intensity accepts a move that keeps the objective with probability 1 under the Metropolis rule and 1/2 under the Glauber rule, and it never accepts a move that raises the objective. It samples the walks of a sequence on a fitness landscape, the folds of a protein chain, or the cells of the residues of a protein.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
target Role

The path of the group on the sites of a lattice, whose rows are the sequences.

required
state str

The name of the state the moves change.

required
objective RowScoreSpec

The score of a row.

required
move str | Callable[..., object]

'flip' to the negative of the value, 'random-value' to one of values, 'rotate' by a uniform step of at most width, 'gaussian' by a normal step of deviation width, or a function move(row, random) that returns the proposed row.

'flip'
intensity Annotated[float, POSITIVE_VALUES]

The factor of the change of the score in the acceptance exp(-intensity * change), positive and possibly infinite.

1.0
rule str

The acceptance, 'metropolis', 'glauber', or 'heat-bath'.

'metropolis'
values Sequence[float]

The values a random-value move or the heat-bath rule draws from.

()
width float

The largest change of a rotation, or the standard deviation of a Gaussian move.

1.0
cooling Callable[[Expression | float], object] | None

The function of the time that multiplies one over the intensity, or None for a constant intensity.

None

Raises:

Type Description
ValidationError

The move or the rule is unknown, or the objective is not a row score the sampling runs, raised by prepare.

Examples:

>>> import numpy as np
>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.components.spaces import LatticeSpace
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.biology.common.sequences import RowScoreSpec, SequenceSamplingInteraction
>>> def flip_one(row, random):
...     row[random.integers(row.size)] *= -1
...     return row
>>> walk = SequenceSamplingInteraction(
...     'walk', target='spins', state='s', objective=RowScoreSpec(np.sum), move=flip_one, intensity=float('inf')
... )
>>> spins = Element('spins', values={'s': 1.0}, space=LatticeSpace((2, 8)))
>>> system = System(elements=[spins], interactions=[walk]).prepare()
>>> system.simulate(3, random_state=0).get_values('walk.score').shape
(4, 2)
Source code in src/skcomplex/systems/biology/common/sequences/_sequence_sampling.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def __init__(
    self,
    label: str,
    *,
    target: Role,
    state: str,
    objective: RowScoreSpec,
    move: str | Callable[..., object] = 'flip',
    intensity: Annotated[float, POSITIVE_VALUES] = 1.0,
    rule: str = 'metropolis',
    values: Sequence[float] = (),
    width: float = 1.0,
    cooling: Callable[[Expression | float], object] | None = None,
) -> None:
    super().__init__(label)
    self.target = target
    self.state = state
    self.objective = objective
    self.move = move
    self.intensity = intensity
    self.rule = rule
    self.values = values
    self.width = width
    self.cooling = cooling