Random Mutator

class RandomMutator(mutators, weights=None, random_seed=None)[source]

Bases: object

Use some other mutator at random.

Examples

Use One of Several Mutators at Random

import stk

random_seed = 12
mutator = stk.RandomMutator(
    mutators=(
        stk.RandomBuildingBlock(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
                stk.BuildingBlock(
                    smiles='BrCCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            is_replaceable=lambda building_block: True,
            random_seed=random_seed,
        ),
        stk.SimilarBuildingBlock(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
                stk.BuildingBlock(
                    smiles='BrCCCCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            is_replaceable=lambda building_block: True,
            random_seed=random_seed,
        ),
        stk.RandomBuildingBlock(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCNCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
                stk.BuildingBlock(
                    smiles='BrCNCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            is_replaceable=lambda building_block: True,
            random_seed=random_seed,
        ),
    ),
)
building_block = stk.BuildingBlock(
    smiles='BrCNNCBr',
    functional_groups=[stk.BromoFactory()],
)
record = stk.MoleculeRecord(
    topology_graph=stk.polymer.Linear(
        building_blocks=(building_block, ),
        repeating_unit='A',
        num_repeating_units=2,
    ),
)
# Use one of the component mutators at random.
mutation_record1 = mutator.mutate(record)
# A different mutator may get selected at random the second,
# third, etc, time.
mutation_record2 = mutator.mutate(record)

Methods

mutate(record)

Return a mutant of record.

__init__(mutators, weights=None, random_seed=None)[source]

Initialize a RandomMutator instance.

Parameters:
  • mutators (tuple) – Holds instances which have mutate() method. The mutate() method must return an instance of MutationRecord.

  • weights (tuple of float, optional) – For each mutator, the probability that it will be chosen whenever mutate() is called. If None all mutators will have equal chance of being selected.

  • random_seed (int, optional) – The random seed to use.

mutate(record)[source]

Return a mutant of record.

Parameters:

record (MoleculeRecord) – The molecule to be mutated.

Returns:

  • MutationRecord – A record of the mutation. The exact subclass of MutationRecord depends on which mutator was used.

  • None (NoneType) – If record cannot be mutated.