def test_create_wrapper_no_reaction():
    tree = ReactionTree()
    mol = Molecule(smiles="CCC")
    tree.graph.add_node(mol)
    tree.root = mol

    wrapper = ReactionTreeWrapper(tree)
    assert wrapper.info["tree count"] == 1
    assert wrapper.info["root"] is mol
    assert len(wrapper.trees) == 1

    wrapper = ReactionTreeWrapper(tree, TreeContent.REACTIONS)
    assert wrapper.info["tree count"] == 0
    assert wrapper.info["root"] is None
    assert len(wrapper.trees) == 0

    wrapper = ReactionTreeWrapper(tree, TreeContent.BOTH)
    assert wrapper.info["tree count"] == 1
    assert wrapper.info["root"] is mol
    assert len(wrapper.trees) == 1
Exemplo n.º 2
0
def _sample_library(library: pd.DataFrame, config: Config,
                    sampler_func: Callable) -> _DfGenerator:
    for _, row in library.iterrows():
        mols = create_reactants_molecules(row.reactants)
        try:
            ref_mol = Molecule(smiles=row.products, sanitize=True)
        except MoleculeException:
            yield None
            continue

        new_product = None
        for template_row in sampler_func(row):
            if row.template_hash == template_row.template_hash:
                continue
            smarts_fwd = reverse_template(template_row.retro_template)
            try:
                new_product = Reaction(mols=mols,
                                       smarts=smarts_fwd).apply()[0][0]
            except (ValueError, IndexError):
                continue
            if new_product.basic_compare(ref_mol):
                continue
            break  # If we have reached here, we have found a match that fits all criteria

        if not new_product:
            yield None
            continue

        # pylint: disable=undefined-loop-variable
        yield _new_dataframe(
            row,
            config,
            reaction_hash=[reaction_hash(row.reactants, new_product)],
            products=[new_product.smiles],
            classification=[""],
            retro_template=[template_row.retro_template],
            template_hash=[template_row.template_hash],
            selectivity=[0],
            outcomes=[1],
            template_code=[template_row.template_code],
        )
Exemplo n.º 3
0
def test_get_actions(
    expansion_policy, mock_policy_model, mock_stock, mocker, shared_datadir
):
    templates_filename = str(shared_datadir / "templates.hdf5")
    expansion_policy.load("dummy.hdf5", templates_filename, "policy1")
    expansion_policy.select("policy1")
    mols = [Molecule(smiles="CCO")]
    mocker.patch("aizynthfinder.context.stock.Stock.__contains__", return_value=False)

    actions, priors = expansion_policy.get_actions(mols)

    assert priors == [0.7, 0.2]
    policy_names = [action.metadata["policy_name"] for action in actions]
    assert policy_names == ["policy1", "policy1"]

    expansion_policy._config.cutoff_cumulative = 1.0
    actions, priors = expansion_policy.get_actions(mols)

    assert priors == [0.7, 0.2, 0.1]

    expansion_policy._config.cutoff_number = 1
    actions, priors = expansion_policy.get_actions(mols)

    assert priors == [0.7]
Exemplo n.º 4
0
def test_basic_equality():
    mol1 = Molecule(smiles="CC[C@@H](C)O")  # R-2-butanol
    mol2 = Molecule(smiles="CC[C@H](C)O")  # S-2-butanol

    assert mol1 != mol2
    assert mol1.basic_compare(mol2)
Exemplo n.º 5
0
def test_no_input():
    with pytest.raises(MoleculeException):
        Molecule()
Exemplo n.º 6
0
def test_equality():
    mol1 = Molecule(smiles="CCCCO")
    mol2 = Molecule(smiles="OCCCC")

    assert mol1 == mol2
Exemplo n.º 7
0
def test_fingerprint():
    mol = Molecule(smiles="O")

    assert sum(mol.fingerprint(2)) == 1

    assert sum(mol.fingerprint(2, 10)) == 1
Exemplo n.º 8
0
def test_inchi_key():
    mol = Molecule(smiles="O")

    assert mol.inchi_key == "XLYOFNOQVPJJNP-UHFFFAOYSA-N"
Exemplo n.º 9
0
def test_price_no_price(setup_stock_with_query):
    stock = setup_stock_with_query()

    with pytest.raises(StockException):
        stock.price(Molecule(smiles="c1ccccc1"))
Exemplo n.º 10
0
def test_add_scorer_to_collection_no_scorer(default_config):
    collection = ScorerCollection(default_config)

    with pytest.raises(ScorerException):
        collection.load(Molecule(smiles="CCC"))
Exemplo n.º 11
0
 def target_smiles(self, smiles: str) -> None:
     self.target_mol = Molecule(smiles=smiles)
Exemplo n.º 12
0
def test_has_atom_mapping():
    mol1 = Molecule(smiles="CCCCO")
    mol2 = Molecule(smiles="C[C:5]CCO")

    assert not mol1.has_atom_mapping()
    assert mol2.has_atom_mapping()
Exemplo n.º 13
0
def test_no_entries_filter(setup_stock_with_query, make_stock_query):
    stock = setup_stock_with_query(make_stock_query([]))

    stock.set_stop_criteria({"size": {"C": 10, "O": 0}})
    assert Molecule(smiles="c1ccccc1") not in stock
Exemplo n.º 14
0
def test_amount_with_amount_raises(setup_stock_with_query, make_stock_query):
    mol = Molecule(smiles="c1ccccc1")
    stock = setup_stock_with_query(make_stock_query([mol]))

    with pytest.raises(StockException):
        stock.amount(Molecule(smiles="c1ccccc1"))
Exemplo n.º 15
0
def test_amount_with_amount(setup_stock_with_query, make_stock_query):
    mol = Molecule(smiles="c1ccccc1")
    amount = {mol: 14}
    stock = setup_stock_with_query(make_stock_query([mol], amount=amount))

    assert stock.amount(mol) == 14
Exemplo n.º 16
0
def test_amount_no_amount(setup_stock_with_query):
    stock = setup_stock_with_query()

    with pytest.raises(StockException):
        stock.amount(Molecule(smiles="c1ccccc1"))
Exemplo n.º 17
0
def test_price_with_price(setup_stock_with_query, make_stock_query):
    mol = Molecule(smiles="c1ccccc1")
    price = {mol: 14}
    stock = setup_stock_with_query(make_stock_query([mol], price=price))

    assert stock.price(mol) == 14
Exemplo n.º 18
0
 def _unique_mol(self, molecule: Molecule) -> UniqueMolecule:
     id_ = id(molecule)
     if id_ not in self._unique_mols:
         self._unique_mols[id_] = molecule.make_unique()
     return self._unique_mols[id_]
Exemplo n.º 19
0
def test_create_with_mol():
    rd_mol = Chem.MolFromSmiles("O")

    mol = Molecule(rd_mol=rd_mol)

    assert mol.smiles == "O"
Exemplo n.º 20
0
 def target_smiles(self, smiles):
     self.target_mol = Molecule(smiles=smiles)
Exemplo n.º 21
0
def test_create_with_smiles():
    mol = Molecule(smiles="O")

    assert Chem.MolToSmiles(mol.rd_mol) == "O"
Exemplo n.º 22
0
def test_create_mol_cost():
    mol = Molecule(smiles="CCCC")

    assert MoleculeCost()(mol) == 0.0
Exemplo n.º 23
0
def test_inchi():
    mol = Molecule(smiles="O")

    assert mol.inchi == "InChI=1S/H2O/h1H2"