示例#1
0
def test_connectivity_filter():

    result = BasicResult(
        record_id=ObjectId("1"),
        cmiles="[Cl:1][Cl:2]",
        inchi_key="KZBUYRJDOAKODT-UHFFFAOYSA-N",
    )
    record = ResultRecord(
        id=ObjectId("1"),
        program="psi4",
        driver=DriverEnum.gradient,
        method="scf",
        basis="sto-3g",
        molecule=ObjectId("1"),
        status=RecordStatusEnum.complete,
    )

    connectivity_filter = ConnectivityFilter()

    molecule: Molecule = Molecule.from_smiles("[Cl:1][Cl:2]")
    molecule.generate_conformers(n_conformers=1)

    assert connectivity_filter._filter_function(result, record, molecule)

    molecule.conformers[0] *= 10.0

    assert not connectivity_filter._filter_function(result, record, molecule)

    connectivity_filter.tolerance = 12.01  # default * 10.0 + 0.01
    assert connectivity_filter._filter_function(result, record, molecule)
示例#2
0
def test_base_validate_record_types():

    records = [
        ResultRecord(
            program="psi4",
            driver=DriverEnum.gradient,
            method="scf",
            basis="sto-3g",
            molecule=ObjectId("1"),
            status=RecordStatusEnum.complete,
        ),
        OptimizationRecord(
            program="psi4",
            qc_spec=QCSpecification(driver=DriverEnum.gradient,
                                    method="scf",
                                    basis="sto-3g",
                                    program="psi4"),
            initial_molecule=ObjectId("1"),
            status=RecordStatusEnum.complete,
        ),
    ]

    _BaseResultCollection._validate_record_types(records[:1], ResultRecord)
    _BaseResultCollection._validate_record_types(records[1:],
                                                 OptimizationRecord)

    with pytest.raises(RecordTypeError,
                       match="The collection contained a records"):
        _BaseResultCollection._validate_record_types(records,
                                                     OptimizationRecord)
示例#3
0
def test_base_n_molecules_property():

    collection = BasicResultCollection(
        entries={
            "http://localhost:442": [
                BasicResult(
                    record_id=ObjectId("1"),
                    cmiles="[He:1]",
                    inchi_key="SWQJXJOGLNCZEY-UHFFFAOYSA-N",
                )
            ],
            "http://localhost:443": [
                BasicResult(
                    record_id=ObjectId("1"),
                    cmiles="[He:1]",
                    inchi_key="SWQJXJOGLNCZEY-UHFFFAOYSA-N",
                ),
                BasicResult(
                    record_id=ObjectId("2"),
                    cmiles="[Cl:1][Cl:2]",
                    inchi_key="KZBUYRJDOAKODT-UHFFFAOYSA-N",
                ),
            ],
        })

    assert collection.n_molecules == 2
示例#4
0
def test_torsion_smirnoff_coverage(public_client, monkeypatch):

    molecule: Molecule = Molecule.from_mapped_smiles(
        "[H:1][C:2]([H:7])([H:8])"
        "[C:3]([H:9])([H:10])"
        "[C:4]([H:11])([H:12])"
        "[C:5]([H:13])([H:14])[H:6]")

    dihedrals = [(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)]

    collection = TorsionDriveResultCollection(
        entries={
            "http://localhost:442": [
                TorsionDriveResult(
                    record_id=ObjectId(str(i + 1)),
                    cmiles=molecule.to_smiles(mapped=True),
                    inchi_key=molecule.to_inchikey(),
                ) for i in range(len(dihedrals))
            ]
        })

    monkeypatch.setattr(
        TorsionDriveResultCollection,
        "to_records",
        lambda self: [(
            TorsionDriveRecord(
                id=entry.record_id,
                qc_spec=QCSpecification(
                    driver=DriverEnum.gradient,
                    method="scf",
                    basis="sto-3g",
                    program="psi4",
                ),
                optimization_spec=OptimizationSpecification(
                    program="geometric", keywords={}),
                initial_molecule=[ObjectId(1)],
                status=RecordStatusEnum.complete,
                client=public_client,
                keywords=TDKeywords(dihedrals=
                                    [dihedrals[int(entry.record_id) - 1]],
                                    grid_spacing=[]),
                final_energy_dict={},
                optimization_history={},
                minimum_positions={},
            ),
            molecule,
        ) for address, entries in self.entries.items() for entry in entries],
    )

    coverage = collection.smirnoff_coverage(ForceField("openff-1.3.0.offxml"),
                                            driven_only=True)

    assert {*coverage} == {"Bonds", "Angles", "ProperTorsions"}

    assert {*coverage["Bonds"].values()} == {3}
    assert {*coverage["Angles"].values()} == {3}

    assert {*coverage["ProperTorsions"].values()} == {1, 3}
示例#5
0
def test_unperceivable_stereo_filter(toolkits, n_expected, public_client):

    collection = OptimizationResultCollection(
        entries={
            "https://api.qcarchive.molssi.org:443/": [
                OptimizationResult(
                    record_id=ObjectId("19095884"),
                    cmiles=(
                        "[H:37][c:1]1[c:3]([c:8]([c:6]([c:9]([c:4]1[H:40])[S:36]"
                        "(=[O:32])(=[O:33])[N:29]2[C:17]([C:21]([C:18]2([H:53])[H:54])"
                        "([F:34])[F:35])([H:51])[H:52])[H:42])[N:30]([H:66])[c:11]3"
                        "[c:5]([c:2]([c:7]4[c:10]([n:25]3)[N@@:27]([C@:19]([C:12]"
                        "(=[O:31])[N:26]4[C:23]([H:60])([H:61])[H:62])([H:55])[C:22]"
                        "([H:57])([H:58])[H:59])[C:20]5([C:13]([C:15]([N:28]([C:16]"
                        "([C:14]5([H:45])[H:46])([H:49])[H:50])[C:24]([H:63])([H:64])"
                        "[H:65])([H:47])[H:48])([H:43])[H:44])[H:56])[H:38])[H:41])"
                        "[H:39]"
                    ),
                    inchi_key="GMRICROFHKBHBU-MRXNPFEDSA-N"
                )
            ]
        }
    )
    assert collection.n_results == 1

    filtered = collection.filter(UnperceivableStereoFilter(toolkits=toolkits))
    assert filtered.n_results == n_expected
示例#6
0
def test_record_status_filter():

    record = ResultRecord(
        id=ObjectId("1"),
        program="psi4",
        driver=DriverEnum.gradient,
        method="scf",
        basis="sto-3g",
        molecule=ObjectId("1"),
        status=RecordStatusEnum.complete,
    )

    status_filter = RecordStatusFilter(status=RecordStatusEnum.complete)
    assert status_filter._filter_function(None, record, None) is True

    status_filter = RecordStatusFilter(status=RecordStatusEnum.incomplete)
    assert status_filter._filter_function(None, record, None) is False
def mock_optimization_result_collection(
    molecules, monkeypatch
) -> OptimizationResultCollection:

    collection = OptimizationResultCollection(
        entries={
            address: [
                OptimizationResult(
                    record_id=ObjectId(str(i + 1)),
                    cmiles=molecule.to_smiles(mapped=True),
                    inchi_key=molecule.to_inchikey(fixed_hydrogens=True),
                )
                for i, molecule in enumerate(molecules[address])
            ]
            for address in molecules
        }
    )

    monkeypatch.setattr(
        OptimizationResultCollection,
        "to_records",
        lambda self: [
            (
                OptimizationRecord(
                    id=entry.record_id,
                    program="psi4",
                    qc_spec=QCSpecification(
                        driver=DriverEnum.gradient,
                        method="scf",
                        basis="sto-3g",
                        program="psi4",
                    ),
                    initial_molecule=ObjectId(entry.record_id),
                    final_molecule=ObjectId(entry.record_id),
                    status=RecordStatusEnum.complete,
                    energies=[numpy.random.random()],
                    client=_FractalClient(address=address),
                ),
                molecules[address][int(entry.record_id) - 1],
            )
            for address, entries in self.entries.items()
            for entry in entries
        ],
    )

    return collection
示例#8
0
def test_charge_filter():

    record = BasicResult(record_id=ObjectId("1"), cmiles="[N+:1](=[O:2])([O-:3])[O-:4]", inchi_key="NHNBFGGVMKEFGY-UHFFFAOYSA-N")
    charge_filter = ChargeFilter(charges_to_include=[-1, 0])

    assert charge_filter._filter_function(entry=record) is True

    charge_filter = ChargeFilter(charges_to_exclude=[-1])

    assert charge_filter._filter_function(entry=record) is False
示例#9
0
def test_base_molecule_property():

    record = BasicResult(
        record_id=ObjectId("1"),
        cmiles="[Cl:2][H:1]",
        inchi_key="VEXZGXHMUGYJMC-UHFFFAOYSA-N",
    )
    molecule = record.molecule

    assert molecule.atoms[0].atomic_number == 1
    assert molecule.atoms[1].atomic_number == 17
示例#10
0
def test_base_n_results_property():

    collection = BasicResultCollection(
        entries={
            "http://localhost:442": [
                BasicResult(
                    record_id=ObjectId("1"),
                    cmiles="[He:1]",
                    inchi_key="SWQJXJOGLNCZEY-UHFFFAOYSA-N",
                )
            ],
            "http://localhost:443": [
                BasicResult(
                    record_id=ObjectId("1"),
                    cmiles="[He:1]",
                    inchi_key="SWQJXJOGLNCZEY-UHFFFAOYSA-N",
                )
            ],
        })

    assert collection.n_results == 2
示例#11
0
def test_cached_query_torsion_drive_results(public_client):

    assert len(_grid_id_cache) == 0

    result = TorsionDriveResult(
        record_id=ObjectId("36633243"),
        cmiles="[H:6][N:5]([H:7])[C:3](=[O:4])[C:1]#[N:2]",
        inchi_key="",
    )

    expected_molecule = Molecule.from_mapped_smiles(result.cmiles)

    records = cached_query_torsion_drive_results(public_client.address,
                                                 [result])
    assert len(records) == 1

    record, molecule = records[0]

    assert molecule.n_conformers == 24

    assert "grid_ids" in molecule.properties

    expected_qc_molecules = {
        json.dumps(grid_id): expected_qc_molecule
        for grid_id, expected_qc_molecule in
        record.get_final_molecules().items()
    }

    for grid_id, conformer in zip(molecule.properties["grid_ids"],
                                  molecule.conformers):

        assert numpy.allclose(
            conformer.value_in_unit(unit.bohr),
            expected_qc_molecules[grid_id].geometry.reshape(
                (molecule.n_atoms, 3)))

    assert len(molecule.properties["grid_ids"]) == 24

    are_isomorphic, _ = Molecule.are_isomorphic(molecule, expected_molecule)
    assert are_isomorphic

    assert len(_grid_id_cache) == 24

    # The request mocker would raise an exception if the client tries to reach out
    # to the server.
    with requests_mock.Mocker():
        cached_query_torsion_drive_results(public_client.address, [result])
示例#12
0
    def mock_query_results(*args, **kwargs):

        assert "program" in kwargs and kwargs["program"] == "psi4"
        assert "method" in kwargs and kwargs["method"] == "scf"
        assert "basis" in kwargs and kwargs["basis"] == "sto-3g"
        assert "driver" in kwargs and kwargs["driver"] == "hessian"

        return [
            ResultRecord(
                id=ObjectId("1"),
                program=kwargs["program"],
                driver=getattr(DriverEnum, kwargs["driver"]),
                method=kwargs["method"],
                basis=kwargs["basis"],
                molecule=kwargs["molecule"][0],
                status=RecordStatusEnum.complete,
            )
        ]
示例#13
0
def test_base_smirnoff_coverage():

    collection = TorsionDriveResultCollection(
        entries={
            "http://localhost:442": [
                TorsionDriveResult(
                    record_id=ObjectId(str(i + 1)),
                    cmiles=smiles,
                    inchi_key=Molecule.from_smiles(smiles).to_inchikey(),
                ) for i, smiles in enumerate([
                    "[H:1][C:2]([H:3])([H:4])([H:5])",
                    "[C:1]([H:2])([H:3])([H:4])([H:5])",
                ])
            ]
        })

    coverage = collection.smirnoff_coverage(ForceField("openff-1.3.0.offxml"))

    assert {*coverage} == {"Bonds", "Angles", "vdW", "Constraints"}
    assert all(count == 1 for counts in coverage.values()
               for count in counts.values())
示例#14
0
def mock_basic_result_collection(molecules, monkeypatch) -> BasicResultCollection:

    collection = BasicResultCollection(
        entries={
            address: [
                BasicResult(
                    record_id=ObjectId(str(i + 1)),
                    cmiles=molecule.to_smiles(mapped=True),
                    inchi_key=molecule.to_inchikey(fixed_hydrogens=True),
                )
                for i, molecule in enumerate(molecules[address])
            ]
            for address in molecules
        }
    )

    monkeypatch.setattr(
        BasicResultCollection,
        "to_records",
        lambda self: [
            (
                ResultRecord(
                    id=entry.record_id,
                    program="psi4",
                    driver=DriverEnum.gradient,
                    method="scf",
                    basis="sto-3g",
                    molecule=entry.record_id,
                    status=RecordStatusEnum.complete,
                    client=_FractalClient(address=address),
                ),
                molecules[address][int(entry.record_id) - 1]
            )
            for address, entries in self.entries.items()
            for entry in entries
        ],
    )

    return collection
示例#15
0
    assert {molecule.id for molecule in molecules} == {*molecule_ids}

    assert len(_molecule_cache) == 2

    # The request mocker would raise an exception if the client tries to reach out
    # to the server.
    with requests_mock.Mocker():
        cached_query_molecules(public_client.address, molecule_ids)


@pytest.mark.parametrize(
    "result, query_function",
    [
        (
            BasicResult(
                record_id=ObjectId("32651863"),
                cmiles="[H:3][C:1]([H:4])([H:5])[O:2][H:6]",
                inchi_key="",
            ),
            cached_query_basic_results,
        ),
        (
            OptimizationResult(
                record_id=ObjectId("25724668"),
                cmiles="[C:1]([H:2])([H:3])([H:4])[H:5]",
                inchi_key="",
            ),
            cached_query_optimization_results,
        ),
    ],
)
示例#16
0
def mock_torsion_drive_result_collection(
    molecules, monkeypatch
) -> TorsionDriveResultCollection:

    collection = TorsionDriveResultCollection(
        entries={
            address: [
                TorsionDriveResult(
                    record_id=ObjectId(str(i + 1)),
                    cmiles=molecule.to_smiles(mapped=True),
                    inchi_key=molecule.to_inchikey(fixed_hydrogens=True),
                )
                for i, molecule in enumerate(molecules[address])
            ]
            for address in molecules
        }
    )

    monkeypatch.setattr(
        TorsionDriveResultCollection,
        "to_records",
        lambda self: [
            (
                TorsionDriveRecord(
                    id=entry.record_id,
                    qc_spec=QCSpecification(
                        driver=DriverEnum.gradient,
                        method="scf",
                        basis="sto-3g",
                        program="psi4",
                    ),
                    optimization_spec=OptimizationSpecification(
                        program="geometric", keywords={}
                    ),
                    initial_molecule=[
                        ObjectId(i + 1)
                        for i in range(
                            molecules[address][int(entry.record_id) - 1].n_conformers
                        )
                    ],
                    status=RecordStatusEnum.complete,
                    client=_FractalClient(address=address),
                    keywords=TDKeywords(dihedrals=[], grid_spacing=[]),
                    final_energy_dict={},
                    optimization_history={},
                    minimum_positions={},
                ),
                molecules[address][int(entry.record_id) - 1],
            )
            for address, entries in self.entries.items()
            for entry in entries
        ],
    )

    def get_molecules(self):

        return_value = []

        for molecule_id in self.initial_molecule:

            molecule = copy.deepcopy(molecules[self.client.address][int(self.id) - 1])
            molecule._conformers = [molecule.conformers[int(molecule_id) - 1]]

            return_value.append(molecule.to_qcschema())

        return return_value

    monkeypatch.setattr(
        TorsionDriveRecord, "get_final_molecules", lambda self: get_molecules(self)
    )

    return collection
示例#17
0
        cmiles="[Cl:2][H:1]",
        inchi_key="VEXZGXHMUGYJMC-UHFFFAOYSA-N",
    )
    molecule = record.molecule

    assert molecule.atoms[0].atomic_number == 1
    assert molecule.atoms[1].atomic_number == 17


@pytest.mark.parametrize(
    "entries, expected_raises",
    [
        (
            [
                BasicResult(
                    record_id=ObjectId("1"),
                    cmiles="[He:1]",
                    inchi_key="SWQJXJOGLNCZEY-UHFFFAOYSA-N",
                )
            ],
            does_not_raise(),
        ),
        (
            [
                BasicResult(
                    record_id=ObjectId("1"),
                    cmiles="[He:1]",
                    inchi_key="SWQJXJOGLNCZEY-UHFFFAOYSA-N",
                )
            ] * 2,
            pytest.raises(ValidationError,