예제 #1
0
def _test_get_all(
    database: stk.ConstructedMoleculeDatabase,
    expected_molecules: dict[str, stk.ConstructedMolecule],
) -> None:
    """
    Test iteration over all entries.

    Parameters:

        database: A database to test.

        expected_molecules: The expected molecules to get from the
            databases using their smiles as the key.

    """

    smiles = stk.Smiles()
    for i, retrieved in enumerate(database.get_all()):
        expected = expected_molecules[smiles.get_key(retrieved)]
        is_equivalent_constructed_molecule(
            constructed_molecule1=(expected.with_canonical_atom_ordering()),
            constructed_molecule2=(retrieved.with_canonical_atom_ordering()),
        )

    assert i + 1 == len(expected_molecules)
예제 #2
0
def _test_database(database, molecule, key):
    """
    Test a molecule or constructed molecule database.

    Parameters
    ----------
    database : :class:`.MoleculeCache`
        The database to test.

    molecule : :class:`.Molecule`
        The molecule to put and get from the `database`.

    key : :class:`object`
        The key used to retrieve `molecule` from the database.

    Returns
    -------
    None : :class:`NoneType`

    """

    database.put(molecule)
    retrieved = database.get(key)
    is_equivalent_constructed_molecule(
        molecule.with_canonical_atom_ordering(),
        retrieved.with_canonical_atom_ordering(),
    )
예제 #3
0
def test_get_all():
    """
    Test iteration over all molecules.

    """

    database_name = '_test_get_entries_constructed_molecule'
    client = pymongo.MongoClient()
    client.drop_database(database_name)

    key_maker = stk.Inchi()
    jsonizer = stk.ConstructedMoleculeJsonizer(key_makers=(key_maker, ))

    database = stk.ConstructedMoleculeMongoDb(
        mongo_client=client,
        database=database_name,
        jsonizer=jsonizer,
        put_lru_cache_size=0,
        get_lru_cache_size=0,
    )

    molecules = [
        stk.ConstructedMolecule(topology_graph=stk.polymer.Linear(
            building_blocks=(stk.BuildingBlock(
                smiles='BrCCCBr', functional_groups=[stk.BromoFactory()]), ),
            repeating_unit='A',
            num_repeating_units=3,
        ), ),
        stk.ConstructedMolecule(topology_graph=stk.polymer.Linear(
            building_blocks=(
                stk.BuildingBlock(smiles='BrCCBr',
                                  functional_groups=[stk.BromoFactory()]),
                stk.BuildingBlock(smiles='BrCNCBr',
                                  functional_groups=[stk.BromoFactory()]),
            ),
            repeating_unit='AB',
            num_repeating_units=2,
        ), ),
    ]
    molecules_by_key = {
        key_maker.get_key(molecule): molecule
        for molecule in molecules
    }

    for molecule in molecules:
        database.put(molecule)

    for i, retrieved in enumerate(database.get_all()):
        key = key_maker.get_key(retrieved)
        molecule = molecules_by_key[key]
        is_equivalent_constructed_molecule(
            molecule.with_canonical_atom_ordering(),
            retrieved.with_canonical_atom_ordering(),
        )

    # Check number of molecules.
    assert i + 1 == len(molecules)
예제 #4
0
def is_clone_constructed_molecule(
    constructed_molecule1,
    constructed_molecule2,
):
    assert constructed_molecule1 is not constructed_molecule2
    is_equivalent_constructed_molecule(
        constructed_molecule1=constructed_molecule1,
        constructed_molecule2=constructed_molecule2,
    )