Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(22)  |  Call(19)  |  Derive(0)  |  Import(3)
Generates a random :py:class:`~AChemKit.reactionnet.ReactionNetwork` from molecules that are strings of atoms and can join together or break apart.

Based on the paper Autocatalytic sets of proteins. 1986. Journal of Theoretical Biology 119:1-24 by Kauffman, Stuart A.  but without the explicit catalytic activity.


Arguments:

natoms
    Number of atoms to use. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

.. note::

    :py:class:`AChemKit.reactionnet.ReactionNetwork` tracks molecules by their reactions, so if a molecule is not part of any reaction
    it will not appear at all e.g. in :py:attr:`~AChemKit.reactionnet.ReactionNetwork.seen`.

maxlength
    Maximum number of atoms in a molecule. If this is None, then they are unbounded; this might cause problems with a computational explosion. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

pform
    Probability that a pair of molecules will join together per orientation. Must be between 0 and 1. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

pbreak
    Probability that any pair of atoms will break. Must be between 0 and 1.Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

directed
    If false, molecules have no intrinsic direction so AlphBeta is equivlanet to BetaAlpha.

rates
    Rate of each reaction in the reaction network. Can be a single value, or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

cls
    Alternative class to use for constructing the return rather than :py:class:`AChemKit.reactionnet.ReactionNetwork`.

rng
    Random number generator to use. If not specifed, one will be generated at random.

        def Linear(natoms, maxlength, pform, pbreak, directed = True, rates = 1.0, cls = ReactionNetwork, rng = None):
    """
    Generates a random :py:class:`~AChemKit.reactionnet.ReactionNetwork` from molecules that are strings of atoms and can join together or break apart.

    Based on the paper Autocatalytic sets of proteins. 1986. Journal of Theoretical Biology 119:1-24 by Kauffman, Stuart A.  but without the explicit catalytic activity.


    Arguments:

    natoms
        Number of atoms to use. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

    .. note::

        :py:class:`AChemKit.reactionnet.ReactionNetwork` tracks molecules by their reactions, so if a molecule is not part of any reaction
        it will not appear at all e.g. in :py:attr:`~AChemKit.reactionnet.ReactionNetwork.seen`.

    maxlength
        Maximum number of atoms in a molecule. If this is None, then they are unbounded; this might cause problems with a computational explosion. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

    pform
        Probability that a pair of molecules will join together per orientation. Must be between 0 and 1. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

    pbreak
        Probability that any pair of atoms will break. Must be between 0 and 1.Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

    directed
        If false, molecules have no intrinsic direction so AlphBeta is equivlanet to BetaAlpha.

    rates
        Rate of each reaction in the reaction network. Can be a single value, or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution), or a dict of value:weight which will be sampled from.

    cls
        Alternative class to use for constructing the return rather than :py:class:`AChemKit.reactionnet.ReactionNetwork`.

    rng
        Random number generator to use. If not specifed, one will be generated at random.

    """
    
    if rng is None:
        rng = random.Random(random.random())


    natoms = get_sample(natoms, rng)
    maxlength = get_sample(maxlength, rng)
    pform = get_sample(pform, rng)
    pbreak = get_sample(pbreak, rng)
    assert pform >= 0.0
    assert pform <= 1.0
    assert pbreak >= 0.0
    assert pbreak <= 1.0

    #these are lists not sets because sets have machine-dependant ordering, which prevents reproducibility for the same random seed.
    molecules = []
    new = []

    #create some inital atoms
    #of the form Abc where first letter is capitalized
    alpha = "abcdefghijklmnopqrstuvwxyz"
    assert natoms > 0 
    for i in xrange(natoms):
        name = alpha[i%len(alpha)]
        while i >= len(alpha):
            i /= len(alpha)
            name = alpha[(i%len(alpha))-1] + name
        name = name.strip().capitalize()
        new.append(name)

    outrates = {}
    
    assert maxlength > 0 

    def mol_to_atoms(mol, cache = {}):
        if mol not in cache:
            cache[mol] = tuple(filter(lambda x: len(x) > 0, re.split(r"([A-Z][a-z]*)", mol)))
        return cache[mol] 

    def mol_len(mol):
        #return len(mol)
        return len(mol_to_atoms(mol))

    def mol_reverse(mol):
        rmol = ""
        for atom in mol_to_atoms(mol):
            rmol = atom + rmol
        return rmol

    def mol_order(mol):
        rmol = mol_reverse(mol)
        if mol < rmol:
            return mol
        else:
            return rmol

    def atoms_to_mol(atoms):
        mol = ""
        for atom in atoms:
            mol = mol + atom
        return mol


    while len(new) > 0:
        oldnew = new
        new = []
        #decomposition
        for z in oldnew:
            for i in xrange(1, mol_len(z)):
                if rng.random() < pbreak:
                    a = atoms_to_mol(mol_to_atoms(z)[:i])
                    b = atoms_to_mol(mol_to_atoms(z)[i:])

                    if not directed:
                        a = mol_order(a)
                        b = mol_order(b)

                    reaction = (OrderedFrozenBag((z,)), OrderedFrozenBag(sorted((a,b))))

                    if reaction not in outrates:
                        outrates[reaction] = get_sample(rates, rng)

                    if a not in molecules and a not in new:
                        new.append(a)
                    if b not in molecules and b not in new:
                        new.append(b)

        for a, b in itertools.chain(itertools.product(oldnew, molecules), combinations_with_replacement(oldnew, 2)):
            if maxlength is None or mol_len(a) + mol_len(b) <= maxlength:

                combinations = ((a,b), (b,a))
                if not directed:
                    combinations += ((mol_reverse(a), b), (a, mol_reverse(b)))

                for x,y in combinations:
                    if rng.random() < pform:
                        z = x + y

                        if not directed:
                            z = mol_order(z)

                        #because we may have revereed them, we have to order them again
                        if not directed:
                            reactants = OrderedFrozenBag(sorted((mol_order(x), mol_order(y))))
                        else:
                            reactants = OrderedFrozenBag(sorted((x, y)))
                        products = OrderedFrozenBag((z,))

                        reaction = (reactants, products)

                        if reaction not in outrates:
                            outrates[reaction] = get_sample(rates, rng)

                        if z not in molecules and z not in new:
                            new.append(z)
        for z in oldnew:
            molecules.append(z)

    return cls(outrates)
        


src/a/c/AChemKit-0.3.0/AChemKit/tools/chem_linear.py   AChemKit(Download)
 
import AChemKit
from AChemKit.randomnet import Linear 
 
if __name__=="__main__":
    #import cProfile
    #cProfile.run("""
    net = Linear(args.natoms, args.length, args.pform, args.pbreak, args.directed, rates, rng=rng)
    #""".strip())
 

src/a/c/AChemKit-0.3.0/AChemKit/randomnet_test.py   AChemKit(Download)
import unittest
 
from AChemKit.randomnet import Uniform, Linear
import random
 
    def test_natoms_int(self):
        net = Linear(2, 3, 0.2, 0.2)
        net = Linear(2, 3, 0.2, 0.2, rng = self.rng)
    def test_natoms_tuple(self):
        net = Linear((2,3), 3, 0.2, 0.2)
        net = Linear((2,3), 3, 0.2, 0.2, rng = self.rng)