codeine.CodingSpace

class codeine.CodingSpace(aa_seq: str, *, translation_table: TranslationTable | None = None, rna: bool | None = None, codon_restrictions: Dict[int, str | Sequence[str]] | None = None, forbidden_motifs: str | RestrictionSite | Sequence[str | RestrictionSite] | None = None, max_homopolymer: int | None = None, context_l: str = '', context_r: str = '', codon_weights: CodonWeights | None = None, seed: None | int | float | str | bytes | bytearray = None)[source]

Bases: object

Represents a space of valid coding sequences for a protein under constraints.

Parameters:
  • aa_seq – The amino acid sequence.

  • translation_table – The translation table to use. Leave blank to use standard table.

  • rna – Whether to use RNA. If false or blank, use DNA.

  • codon_restrictions – Any codon restrictions in the format e.g. {4: 'TCC'} or {5: ['AGT', 'AGC']}. Positions are 1-based.

  • forbidden_motifs – Forbidden motifs, either as strings or as codeine.RestrictionSite.

  • max_homopolymer – The maximum allowed length of nucleotide homopolymer

  • context_l – The context sequence to the left of the coding sequence.

  • context_r – The context sequence to the right of the coding sequence.

  • codon_weights – The codon weights to use. Leave blank to sample uniformly.

  • seed – Seed used to initialise the random number generator for sampling.

Properties

aa_seq

The amino acid sequence for this coding space.

codon_restrictions

The fixed codon restrictions from the underlying graph.

codon_weights

The codon weights being used in this space.

context_l

The left context sequence from the underlying graph.

context_r

The right context sequence from the underlying graph.

n_valid_sequences

The number of valid sequences in this space.

pinned_codons

Temporary codon pins currently applied to this coding space.

translation_table

The translation table being used in this space.

Methods

clear_forbidden_motifs()

Remove all forbidden motifs from this coding space.

clear_max_homopolymer()

Remove the maximum homopolymer constraint from this coding space.

clear_pins()

Remove all temporary codon pins from this coding space.

contains(seq)

Check whether a coding sequence is contained in this coding space.

enumerate()

Generate all sequences in this space.

load(path)

Load a coding space from disc.

mutants(cds[, free_positions, min_nts, ...])

Return a space of mutants relative to a given coding sequence, i.e. a space derived from this one but which fixes the sequence on all but the specified positions.

pin_codons(pinned_codons)

Pin temporary codons in this coding space.

sample([n])

Sample one or more variants from this coding space.

save(path)

Save this coding space to disc.

set_forbidden_motifs(forbidden_motifs)

Set the forbidden motifs for this coding space.

set_max_homopolymer(max_homopolymer)

Set the maximum allowed homopolymer length.

set_pinned_codons(pinned_codons)

Replace all temporary codon pins on this coding space.

unpin_codons(positions)

Remove temporary codon pins by position.

CodingSpace.clear_forbidden_motifs() None[source]

Remove all forbidden motifs from this coding space.

CodingSpace.clear_max_homopolymer() None[source]

Remove the maximum homopolymer constraint from this coding space.

CodingSpace.clear_pins() None[source]

Remove all temporary codon pins from this coding space.

CodingSpace.contains(seq: str) bool[source]

Check whether a coding sequence is contained in this coding space.

Parameters:

seq – The sequence to check.

Return type:

True if and only if the sequence is contained in this coding space.

CodingSpace.enumerate() Generator[str, None, None][source]

Generate all sequences in this space. If there are many (and often there are astronomically many), one would not expect to reach the ‘end’. However for smaller sequence spaces, such as mutation spaces, it’s quite possible to get there.

Yields:

str – A valid coding sequence.

classmethod CodingSpace.load(path) CodingSpace[source]

Load a coding space from disc.

CodingSpace.mutants(cds: str, free_positions: Sequence[int] | None = None, min_nts: int | None = None, max_nts: int | None = None, min_codons: int | None = None, max_codons: int | None = None) MutationSpace[source]

Return a space of mutants relative to a given coding sequence, i.e. a space derived from this one but which fixes the sequence on all but the specified positions.

Parameters:
  • cds – The sequence to mutate.

  • free_positions – The positions that are allowed to vary.

  • min_nts – The min nucleotide (Hamming) distance relative to the reference sequence.

  • max_nts – The max nucleotide (Hamming) distance relative to the reference sequence.

  • min_codons – The min number of changed codons relative to the reference sequence.

  • max_codons – The max number of changed codons relative to the reference sequence.

CodingSpace.pin_codons(pinned_codons: Dict[int, str]) None[source]

Pin temporary codons in this coding space.

Parameters:

pinned_codons – A dict specifying which codons to pin, by position.

CodingSpace.sample(n: int | None = None) str[source]

Sample one or more variants from this coding space.

Parameters:

n – Number of sequences to sample. If omitted, return a single sequence.

Return type:

A sampled string sequence from this coding space.

CodingSpace.save(path) None[source]

Save this coding space to disc.

CodingSpace.set_forbidden_motifs(forbidden_motifs: str | RestrictionSite | Sequence[str | RestrictionSite]) None[source]

Set the forbidden motifs for this coding space.

Parameters:

forbidden_motifs – Motifs that should be forbidden in generated sequences.

CodingSpace.set_max_homopolymer(max_homopolymer: int | None) None[source]

Set the maximum allowed homopolymer length.

Parameters:

max_homopolymer – The longest allowed repeated run of one nucleotide, or None for no limit.

CodingSpace.set_pinned_codons(pinned_codons: Dict[int, str]) None[source]

Replace all temporary codon pins on this coding space.

Parameters:

pinned_codons – A dict specifying which codons to pin, by position.

CodingSpace.unpin_codons(positions: Sequence[int]) None[source]

Remove temporary codon pins by position.

Parameters:

positions – Positions to unpin.