Example #1
0
    def test_charge_calculator_selector(self):
        """It checks the charge calculator selector."""
        from peleffy.topology import Molecule
        from peleffy.forcefield import OPLS2005ForceField
        import peleffy

        dummy_mol = Molecule()

        # Check default selection
        oplsff = OPLS2005ForceField()
        calculator = oplsff._get_charge_calculator(None, dummy_mol)

        assert isinstance(
            calculator,
            peleffy.forcefield.calculators.OPLSChargeCalculator), \
            'Invalid default charge calculator: ' \
            + '{}'.format(type(calculator))

        # Check custom selection 1
        oplsff = OPLS2005ForceField()
        calculator = oplsff._get_charge_calculator('gasteiger', dummy_mol)

        assert isinstance(
            calculator,
            peleffy.forcefield.calculators.GasteigerCalculator), \
            'Invalid custom selection 1 for the charge calculator'

        # Check custom selection 1
        oplsff = OPLS2005ForceField()
        calculator = oplsff._get_charge_calculator('am1bcc', dummy_mol)

        assert isinstance(
            calculator,
            peleffy.forcefield.calculators.Am1bccCalculator), \
            'Invalid custom selection 2 for the charge calculator'
Example #2
0
    def test_writer_OPLS(self):
        """
        It tests the writer attribute of the Impact class using OPLS to parameterize.
        """
        from .utils import parameterize_opls2005

        TEMPLATE_METZ_OPLS = get_data_file_path('tests/OPLS_metz')
        TEMPLATE_MALZ_OPLS = get_data_file_path('tests/OPLS_malz')
        TEMPLATE_ETLZ_OPLS = get_data_file_path('tests/OPLS_etlz')

        with tempfile.TemporaryDirectory() as tmpdir:
            with temporary_cd(tmpdir):
                # Generates the template for methane using OPLS
                opls2005 = OPLS2005ForceField()
                pdb_path = get_data_file_path('ligands/methane.pdb')
                molecule = Molecule(pdb_path)
                ffld_file = get_data_file_path('tests/MET_ffld_output.txt')
                parameters = parameterize_opls2005(opls2005, molecule,
                                                   ffld_file)
                topology = Topology(molecule, parameters)

                # Generates the impact template for methane
                impact = Impact(topology)
                impact.to_file('metz')

                # Compare the reference template and the generated template
                compare_files(file1=TEMPLATE_METZ_OPLS, file2='metz')

                # Generates the template for malonate using OPLS
                opls2005 = OPLS2005ForceField()
                pdb_path = get_data_file_path('ligands/malonate.pdb')
                molecule = Molecule(pdb_path)
                ffld_file = get_data_file_path('tests/MAL_ffld_output.txt')
                parameters = parameterize_opls2005(opls2005, molecule,
                                                   ffld_file)
                topology = Topology(molecule, parameters)

                # Generates the impact template for malonate
                impact = Impact(topology)
                impact.to_file('malz')

                # Compare the reference template and the generated template
                compare_files(file1=TEMPLATE_MALZ_OPLS, file2='malz')

                # Generates the template for ethylene using OPLS
                opls2005 = OPLS2005ForceField()
                pdb_path = get_data_file_path('ligands/ethylene.pdb')
                molecule = Molecule(pdb_path, tag='ETL')
                ffld_file = get_data_file_path('tests/ETL_ffld_output.txt')
                parameters = parameterize_opls2005(opls2005, molecule,
                                                   ffld_file)
                topology = Topology(molecule, parameters)

                # Generates the impact template for ethylene
                impact = Impact(topology)
                impact.to_file('etlz')

                # Compare the reference template and the generated template
                compare_files(file1=TEMPLATE_ETLZ_OPLS, file2='etlz')
Example #3
0
    def test_name(self):
        """It checks the name assignment."""

        from peleffy.forcefield import OPLS2005ForceField

        oplsff = OPLS2005ForceField()

        assert oplsff.name == self.FORCE_FIELD_NAME, \
            'Unexpected force field name'
Example #4
0
    def test_type(self):
        """It checks the type assignment."""

        from peleffy.forcefield import OPLS2005ForceField

        oplsff = OPLS2005ForceField()

        assert oplsff.type == 'OPLS2005', \
            'Unexpected force field type'
Example #5
0
    def test_parameterizer(self):
        """It checks the parameterized method."""

        from peleffy.topology import Molecule
        from peleffy.forcefield import OPLS2005ForceField
        from peleffy.utils import (get_data_file_path,
                                   convert_all_quantities_to_string)
        from .utils import compare_dicts, parameterize_opls2005
        import json

        # Load molecule 1
        molecule = Molecule(get_data_file_path('ligands/methane.pdb'))
        oplsff = OPLS2005ForceField()
        ffld_file = get_data_file_path('tests/MET_ffld_output.txt')

        parameters = parameterize_opls2005(oplsff, molecule, ffld_file)

        writable_parameters = convert_all_quantities_to_string(parameters)

        reference_file = get_data_file_path(
            'tests/MET_opls2005_parameters.json')

        with open(reference_file) as f:
            compare_dicts(writable_parameters, json.load(f))

        # Load molecule 1
        molecule = Molecule(get_data_file_path('ligands/ethylene.pdb'))
        oplsff = OPLS2005ForceField()
        ffld_file = get_data_file_path('tests/ETL_ffld_output.txt')

        parameters = parameterize_opls2005(oplsff, molecule, ffld_file)

        writable_parameters = convert_all_quantities_to_string(parameters)

        reference_file = get_data_file_path(
            'tests/ETL_opls2005_parameters.json')

        with open(reference_file) as f:
            compare_dicts(writable_parameters, json.load(f))
Example #6
0
    def test_multiple_topologies_writer(self):
        """
        It tests the class that generates a OpenFFCompatibleSolvent object for multiple topologies. It compares the outcome of the Solvent writer with
        a reference file.
        """
        from .utils import compare_dicts, parameterize_opls2005
        import json

        TEMPLATE_PARAMS = get_data_file_path('tests/ligandParams.txt')

        with tempfile.TemporaryDirectory() as tmpdir:
            with temporary_cd(tmpdir):
                path_OXO = get_data_file_path('tests/MRO_oleic/OXO.pdb')
                path_OLC = get_data_file_path('tests/MRO_oleic/OLC.pdb')

                ff = OpenForceField('openff_unconstrained-1.2.1.offxml')
                opls2005 = OPLS2005ForceField()

                # Group OXO
                m_OXO = Molecule(path_OXO)
                ffld_file = get_data_file_path('tests/OXO_ffld_output.txt')
                parameters_OXO = parameterize_opls2005(opls2005, m_OXO,
                                                       ffld_file)
                topology_OXO = Topology(m_OXO, parameters_OXO)

                # Acid oleic
                m_OLC = Molecule(path_OLC)
                parameters_OLC = ff.parameterize(m_OLC,
                                                 charge_method='gasteiger')
                topology_OLC = Topology(m_OLC, parameters_OLC)

                # Multiple topologies
                topologies = [topology_OXO, topology_OLC]
                solvent = OBC2(topologies)
                solvent.to_file('OBC_parameters.txt')

                # Loads reference dict from template
                with open(TEMPLATE_PARAMS, 'r') as f:
                    reference_dict = json.load(f)

                # Loads the generated template into a dict
                with open('OBC_parameters.txt', 'r') as f:
                    solvent_dict = json.load(f)

                # Compare the output parameters dict with the reference parameters
                compare_dicts(reference_dict, solvent_dict)
Example #7
0
    def _initialize_from_topology(self):
        """
        Initializes a SolventWrapper object using a peleffy's
        molecular Topology.
        """
        from peleffy.forcefield import OPLS2005ForceField
        from peleffy.forcefield.parameters import OPLS2005ParameterWrapper

        for idx, topology in enumerate(self.topologies):
            # Parameterize with OPLS2005 only if the parameters in topology
            # are not obtained with OPLS2005
            if isinstance(topology.parameters, OPLS2005ParameterWrapper):
                parameters = topology.parameters
            else:
                forcefield = OPLS2005ForceField()
                parameters = forcefield.parameterize(topology.molecule)

            self._radii[idx] = parameters['GBSA_radii']
            self._scales[idx] = parameters['GBSA_scales']
Example #8
0
 def __get_template_and_rot(self, template_path='grw', rot_path='GRW.rot.assign', rot_res=30):
     os.environ['SCHRODINGER'] = self.sch_path
     m = Molecule(self.ligand_pdb, 
                  core_constraints=[' CA ', ' C  ', ' N  '],
                  rotamer_resolution=rot_res)
     if self.__forcefield == 'OPLS2005':
         ff = OPLS2005ForceField()
     if self.__forcefield == 'OpenForceField': # Not tested yet
         ff = OpenForceField('openff_unconstrained-1.2.0.offxml') 
     parameters = ff.parameterize(m)
     topology = Topology(m, parameters)
     impact = Impact(topology)
     impact.to_file(template_path)
     aa_template = self.__create_aa_template_path()  
     cov.correct_template(template_path, aa_template)
     print("Template modified in {}.".format(template_path))
     rotamer_library = RotamerLibrary(m)
     rotamer_library.to_file(rot_path)
     print("Rotamer library stored in {}".format(rot_path))
Example #9
0
        def test_OBCOPLS_writer_ligand(pdbfile, tag_name, ffld_name,
                                       reference_file):
            """
            Given a ligand, it tests that the output parameters file
            corresponds to the refenrece file.

            Parameters
            ----------
            pdbfile : str
                The path to the PDB of the ligand to test
            ffld_name : str
                The path to the ffld_server's output file
            reference_file : str
                The path to reference TXT file compatible with PELE
            """
            with tempfile.TemporaryDirectory() as tmpdir:
                with temporary_cd(tmpdir):

                    # Loads the  molecule
                    molecule = Molecule(get_data_file_path(pdbfile),
                                        tag=tag_name)

                    # Sets forcefield and parameterizes it
                    opls2005 = OPLS2005ForceField()
                    ffld_file = get_data_file_path(ffld_name)
                    parameters = parameterize_opls2005(opls2005,
                                                       molecule,
                                                       ffld_file)

                    # Initializes topology
                    topology = Topology(molecule, parameters)

                    # Initializes solvent and gets parameters file
                    solvent = OPLSOBC(topology)
                    solvent.to_file('OBC_parameters.txt')

                    # Compare the output file with the reference parameters file
                    compare_files_without_order('OBC_parameters.txt',
                                                reference_file)
Example #10
0
    def test_get_Schrodinger_parameters(self):
        """
        It tests the standard methods to obtain Schrodinger parameters
        from an peleffy's Molecule.
        """
        from peleffy.topology import Molecule
        from peleffy.forcefield import OPLS2005ForceField
        from peleffy.utils.toolkits import ToolkitUnavailableException

        # Load benzene ring
        molecule = Molecule(smiles='c1ccccc1', hydrogens_are_explicit=False)

        # Load OPLS2005 force field
        opls2005 = OPLS2005ForceField()

        # Ensure SCHRODINGER is not in the environment
        import os
        if 'SCHRODINGER' in os.environ:
            del(os.environ['SCHRODINGER'])

        with pytest.raises(ToolkitUnavailableException):
            opls2005.parameterize(molecule, charge_method='gasteiger')
    def _retrieve_forcefield(forcefield_name):
        """
        Maps forcefield YAML argument with peleffy classes.

        Parameters
        ----------
        forcefield_name : str
            The force field name defined by the user in args.forcefield

        Returns
        -------
        forcefield_obj : a peleffy.forcefield object
            The corresponding forcefield object from peleffy selected by
            the user
        """
        from peleffy.forcefield import ForceFieldSelector
        from peleffy.forcefield import OPLS2005ForceField

        # If OpenFF extension is missing, add it
        if "openff" in forcefield_name.lower():
            if not forcefield_name.lower().endswith("offxml"):
                forcefield_name += ".offxml"
            if "_unconstrained" not in forcefield_name.lower():
                forcefield_name = forcefield_name.replace(
                    "openff", "openff_unconstrained")

        # Select force field by name
        selector = ForceFieldSelector()
        try:
            forcefield_obj = selector.get_by_name(forcefield_name)
        except ValueError:
            print(f"Warning, invalid force field supplied, using the "
                  f"default one: 'OPLS2005'")
            forcefield_obj = OPLS2005ForceField()

        return forcefield_obj
Example #12
0
    def test_opls2005_parameterizer(self):
        """
        It checks the behaviour of the Topology with the OPLS2005
        parameters.
        """

        from peleffy.topology import Molecule
        from peleffy.topology import Topology
        from peleffy.forcefield import OPLS2005ForceField
        from peleffy.utils import get_data_file_path
        from .utils import check_parameters, parameterize_opls2005

        # Load molecule
        opls2005 = OPLS2005ForceField()
        molecule = Molecule(get_data_file_path('ligands/methane.pdb'))
        ffld_file = get_data_file_path('tests/MET_ffld_output.txt')

        # Workaround to parameterize with OPLS2005 without the Schrodinger
        # dependency
        parameters = parameterize_opls2005(opls2005, molecule, ffld_file)

        # Generate molecular topology
        topology = Topology(molecule, parameters)

        # Define expected parameters
        expected_nonbonding = [[
            1, 0, 'M', 'CT', '_C1_', 0, 3.5, 0.066, -0.24, 1.975, 1.75, 0.005,
            -0.74168571
        ],
                               [
                                   2, 1, 'M', 'HC', '_H1_', 0, 2.5, 0.03, 0.06,
                                   1.425, 1.25, 0.00859824, 0.268726247
                               ],
                               [
                                   3, 1, 'M', 'HC', '_H2_', 0, 2.5, 0.03, 0.06,
                                   1.425, 1.25, 0.00859824, 0.268726247
                               ],
                               [
                                   4, 1, 'M', 'HC', '_H3_', 0, 2.5, 0.03, 0.06,
                                   1.425, 1.25, 0.00859824, 0.268726247
                               ],
                               [
                                   5, 1, 'M', 'HC', '_H4_', 0, 2.5, 0.03, 0.06,
                                   1.425, 1.25, 0.00859824, 0.268726247
                               ]]

        expected_bonds = [[1, 2, 340.0, 1.09], [1, 3, 340.0, 1.09],
                          [1, 4, 340.0, 1.09], [1, 5, 340.0, 1.09]]

        expected_angles = [[2, 1, 3, 33.0, 107.8], [2, 1, 4, 33.0, 107.8],
                           [2, 1, 5, 33.0, 107.8], [3, 1, 4, 33.0, 107.8],
                           [3, 1, 5, 33.0, 107.8], [4, 1, 5, 33.0, 107.8]]

        # Check it up
        check_parameters(topology,
                         expected_nonbonding=expected_nonbonding,
                         expected_bonds=expected_bonds,
                         expected_angles=expected_angles)

        # Load molecule
        opls2005 = OPLS2005ForceField()
        molecule = Molecule(get_data_file_path('ligands/ethylene.pdb'))
        ffld_file = get_data_file_path('tests/ETL_ffld_output.txt')

        # Workaround to parameterize with OPLS2005 without the Schrodinger
        # dependency
        parameters = parameterize_opls2005(opls2005, molecule, ffld_file)

        # Generate molecular topology
        topology = Topology(molecule, parameters)

        # Define expected parameters
        expected_propers = [[3, 1, 2, 5, 7.0, -1, 2, 0.0],
                            [3, 1, 2, 6, 7.0, -1, 2, 0.0],
                            [4, 1, 2, 5, 7.0, -1, 2, 0.0],
                            [4, 1, 2, 6, 7.0, -1, 2, 0.0]]

        expected_impropers = [[3, 4, 1, 2, 15.0, -1, 2],
                              [5, 6, 2, 1, 15.0, -1, 2]]

        # Check it up
        check_parameters(topology,
                         expected_propers=expected_propers,
                         expected_impropers=expected_impropers)
Example #13
0
    def test_datalocal_paths_for_opls(self):
        """It tests the datalocal paths assignment for OPLS2005."""
        from peleffy.utils import OutputPathHandler
        from peleffy.forcefield import OPLS2005ForceField

        # Load benzene molecule
        molecule = Molecule(smiles='c1ccccc1', name='benzene', tag='BNZ',
                            hydrogens_are_explicit=False)

        # Load force field
        opls2005 = OPLS2005ForceField()

        # Molecule's tag
        tag = molecule.tag

        # Initialize output handler without output_path
        output_handler = OutputPathHandler(molecule, opls2005,
                                           as_datalocal=True)

        # Validate output paths
        assert output_handler.get_rotamer_library_path(
            create_missing_folders=False) == \
            './DataLocal/LigandRotamerLibs/' \
            + '{}.rot.assign'.format(tag.upper()), \
            'Unexpected default rotamer library path'
        assert output_handler.get_impact_template_path(
            create_missing_folders=False) == \
            './DataLocal/Templates/OPLS2005/HeteroAtoms/' \
            + '{}z'.format(tag.lower()), \
            'Unexpected default Impact template path'
        assert output_handler.get_solvent_template_path(
            create_missing_folders=False) == \
            './DataLocal/OBC/ligandParams.txt', \
            'Unexpected default solvent parameters path'
        assert output_handler.get_conformation_library_path(
            create_missing_folders=False) == \
            './DataLocal/Conformations/' \
            + '{}.conformation'.format(tag.upper()), \
            'Unexpected default conformation library path'

        # Initialize output handler with an output_path set
        with tempfile.TemporaryDirectory() as tmpdir:
            output_handler = OutputPathHandler(
                molecule, opls2005, as_datalocal=True,
                output_path=os.path.join(tmpdir, 'output'))

            assert output_handler.get_rotamer_library_path(
                create_missing_folders=False) == \
                os.path.join(tmpdir, 'output', 'DataLocal/LigandRotamerLibs/'
                             + '{}.rot.assign'.format(tag.upper())), \
                'Unexpected default rotamer library path'
            assert output_handler.get_impact_template_path(
                create_missing_folders=False) == \
                os.path.join(tmpdir, 'output', 'DataLocal/Templates/OPLS2005/'
                             + 'HeteroAtoms/{}z'.format(tag.lower())), \
                'Unexpected default Impact template path'
            assert output_handler.get_solvent_template_path(
                create_missing_folders=False) == \
                os.path.join(tmpdir, 'output',
                             'DataLocal/OBC/ligandParams.txt'), \
                'Unexpected default solvent parameters path'
            assert output_handler.get_conformation_library_path(
                create_missing_folders=False) == \
                os.path.join(tmpdir, 'output/DataLocal/Conformations/' \
                + '{}.conformation'.format(tag.upper())), \
                'Unexpected default conformation library path'