def test_logp_reverse(): """ Make sure logp_reverse and logp_forward are consistent """ molecule_name_1 = 'erlotinib' molecule_name_2 = 'imatinib' #molecule_name_1 = 'benzene' #molecule_name_2 = 'biphenyl' molecule1 = generate_initial_molecule(molecule_name_1) molecule2 = generate_initial_molecule(molecule_name_2) new_to_old_atom_mapping = align_molecules(molecule1, molecule2) sys1, pos1, top1 = oemol_to_openmm_system(molecule1, molecule_name_1) sys2, pos2, top2 = oemol_to_openmm_system(molecule2, molecule_name_2) test_pdb_file = open("reverse_test1.pdb", 'w') app.PDBFile.writeFile(top1, pos1, file=test_pdb_file) test_pdb_file.close() import perses.rjmc.geometry as geometry import perses.rjmc.topology_proposal as topology_proposal sm_top_proposal = topology_proposal.TopologyProposal(new_topology=top2, new_system=sys2, old_topology=top1, old_system=sys1, logp_proposal=0.0, new_to_old_atom_map=new_to_old_atom_mapping, new_chemical_state_key="CCC", old_chemical_state_key="CC", metadata={'test':0.0}) geometry_engine = geometry.FFAllAngleGeometryEngine({'test': 'true'}) new_positions, logp_proposal = geometry_engine.propose(sm_top_proposal, pos1, beta) logp_reverse = geometry_engine.logp_reverse(sm_top_proposal, pos2, pos1, beta) print(logp_proposal) print(logp_reverse) print(logp_reverse-logp_proposal)
def make_geometry_proposal_array(smiles_list, forcefield=['data/gaff.xml']): """ Make an array of topology_proposals for each molecule to each other in the smiles_list. Includes self-proposals so as to test that. Parameters ---------- smiles_list : list of str list of smiles Returns ------- list of proposal_test namedtuple """ topology_proposals = [] #make oemol array: oemols = OrderedDict() syspostop = OrderedDict() for smiles in smiles_list: oemols[smiles] = generate_molecule_from_smiles(smiles) for smiles in oemols.keys(): print("Generating %s" % smiles) syspostop[smiles] = oemol_to_openmm_system(oemols[smiles], forcefield=forcefield) #get a list of all the smiles in the appropriate order smiles_pairs = list() for smiles1 in smiles_list: for smiles2 in smiles_list: if smiles1==smiles2: continue smiles_pairs.append([smiles1, smiles2]) for i, pair in enumerate(smiles_pairs): #print("preparing pair %d" % i) smiles_1 = pair[0] smiles_2 = pair[1] new_to_old_atom_mapping = align_molecules(oemols[smiles_1], oemols[smiles_2]) sys1, pos1, top1 = syspostop[smiles_1] sys2, pos2, top2 = syspostop[smiles_2] import perses.rjmc.topology_proposal as topology_proposal sm_top_proposal = topology_proposal.TopologyProposal(new_topology=top2, new_system=sys2, old_topology=top1, old_system=sys1, old_chemical_state_key='',new_chemical_state_key='', logp_proposal=0.0, new_to_old_atom_map=new_to_old_atom_mapping, metadata={'test':0.0}) sm_top_proposal._beta = beta proposal_tuple = proposal_test(sm_top_proposal, pos1) topology_proposals.append(proposal_tuple) return topology_proposals
def test_mutate_from_every_amino_to_every_other(): """ Make sure mutations are successful between every possible pair of before-and-after residues Mutate Ecoli F-ATPase alpha subunit to all 20 amino acids (test going FROM all possibilities) Mutate each residue to all 19 alternatives """ import perses.rjmc.topology_proposal as topology_proposal aminos = [ 'ALA', 'ARG', 'ASN', 'ASP', 'CYS', 'GLN', 'GLU', 'GLY', 'HIS', 'ILE', 'LEU', 'LYS', 'MET', 'PHE', 'PRO', 'SER', 'THR', 'TRP', 'TYR', 'VAL' ] failed_mutants = 0 pdbid = "2A7U" topology, positions = load_pdbid_to_openmm(pdbid) modeller = app.Modeller(topology, positions) for chain in modeller.topology.chains(): pass modeller.delete([chain]) ff_filename = "amber99sbildn.xml" max_point_mutants = 1 ff = app.ForceField(ff_filename) system = ff.createSystem(modeller.topology) chain_id = 'A' metadata = dict() system_generator = topology_proposal.SystemGenerator([ff_filename]) pm_top_engine = topology_proposal.PointMutationEngine( modeller.topology, system_generator, chain_id, proposal_metadata=metadata, max_point_mutants=max_point_mutants, always_change=True) current_system = system current_topology = modeller.topology current_positions = modeller.positions pm_top_engine._allowed_mutations = list() for k, proposed_amino in enumerate(aminos): pm_top_engine._allowed_mutations.append((str(k + 2), proposed_amino)) pm_top_proposal = pm_top_engine.propose(current_system, current_topology) current_system = pm_top_proposal.new_system current_topology = pm_top_proposal.new_topology for chain in current_topology.chains(): if chain.id == chain_id: # num_residues : int num_residues = len(chain._residues) break new_sequence = list() for residue in current_topology.residues(): if residue.index == 0: continue if residue.index == (num_residues - 1): continue if residue.name in ['HID', 'HIE']: residue.name = 'HIS' new_sequence.append(residue.name) for i in range(len(aminos)): assert new_sequence[i] == aminos[i] pm_top_engine = topology_proposal.PointMutationEngine( current_topology, system_generator, chain_id, proposal_metadata=metadata, max_point_mutants=max_point_mutants) from perses.rjmc.topology_proposal import append_topology old_topology = app.Topology() append_topology(old_topology, current_topology) new_topology = app.Topology() append_topology(new_topology, current_topology) old_chemical_state_key = pm_top_engine.compute_state_key(old_topology) for chain in new_topology.chains(): if chain.id == chain_id: # num_residues : int num_residues = len(chain._residues) break for proposed_location in range(1, num_residues - 1): print('Making mutations at residue %s' % proposed_location) original_residue_name = chain._residues[proposed_location].name matching_amino_found = 0 for proposed_amino in aminos: pm_top_engine._allowed_mutations = [(str(proposed_location + 1), proposed_amino)] new_topology = app.Topology() append_topology(new_topology, current_topology) old_system = current_system old_topology_natoms = sum([1 for atom in old_topology.atoms()]) old_system_natoms = old_system.getNumParticles() if old_topology_natoms != old_system_natoms: msg = 'PolymerProposalEngine: old_topology has %d atoms, while old_system has %d atoms' % ( old_topology_natoms, old_system_natoms) raise Exception(msg) metadata = dict() for atom in new_topology.atoms(): atom.old_index = atom.index index_to_new_residues, metadata = pm_top_engine._choose_mutant( new_topology, metadata) if len(index_to_new_residues) == 0: matching_amino_found += 1 continue print('Mutating %s to %s' % (original_residue_name, proposed_amino)) residue_map = pm_top_engine._generate_residue_map( new_topology, index_to_new_residues) for res_pair in residue_map: residue = res_pair[0] name = res_pair[1] assert residue.index in index_to_new_residues.keys() assert index_to_new_residues[residue.index] == name assert residue.name + '-' + str( residue.id) + '-' + name in metadata['mutations'] new_topology, missing_atoms = pm_top_engine._delete_excess_atoms( new_topology, residue_map) new_topology = pm_top_engine._add_new_atoms( new_topology, missing_atoms, residue_map) for res_pair in residue_map: residue = res_pair[0] name = res_pair[1] assert residue.name == name atom_map = pm_top_engine._construct_atom_map( residue_map, old_topology, index_to_new_residues, new_topology) templates = pm_top_engine._ff.getMatchingTemplates(new_topology) assert [ templates[index].name == residue.name for index, (residue, name) in enumerate(residue_map) ] new_chemical_state_key = pm_top_engine.compute_state_key( new_topology) new_system = pm_top_engine._system_generator.build_system( new_topology) pm_top_proposal = topology_proposal.TopologyProposal( new_topology=new_topology, new_system=new_system, old_topology=old_topology, old_system=old_system, old_chemical_state_key=old_chemical_state_key, new_chemical_state_key=new_chemical_state_key, logp_proposal=0.0, new_to_old_atom_map=atom_map) assert matching_amino_found == 1
def test_run_geometry_engine(index=0): """ Run the geometry engine a few times to make sure that it actually runs without exceptions. Convert n-pentane to 2-methylpentane """ import logging logging.basicConfig(level=logging.DEBUG) import copy molecule_name_1 = 'benzene' molecule_name_2 = 'biphenyl' #molecule_name_1 = 'imatinib' #molecule_name_2 = 'erlotinib' molecule1 = generate_initial_molecule(molecule_name_1) molecule2 = generate_initial_molecule(molecule_name_2) new_to_old_atom_mapping = align_molecules(molecule1, molecule2) sys1, pos1, top1 = oemol_to_openmm_system(molecule1, molecule_name_1) sys2, pos2, top2 = oemol_to_openmm_system(molecule2, molecule_name_2) import perses.rjmc.geometry as geometry import perses.rjmc.topology_proposal as topology_proposal from perses.tests.utils import compute_potential_components sm_top_proposal = topology_proposal.TopologyProposal(new_topology=top2, new_system=sys2, old_topology=top1, old_system=sys1, old_chemical_state_key='',new_chemical_state_key='', logp_proposal=0.0, new_to_old_atom_map=new_to_old_atom_mapping, metadata={'test':0.0}) sm_top_proposal._beta = beta geometry_engine = geometry.FFAllAngleGeometryEngine(metadata={}) # Turn on PDB file writing. geometry_engine.write_proposal_pdb = True geometry_engine.pdb_filename_prefix = 't13geometry-proposal' test_pdb_file = open("%s_to_%s_%d.pdb" % (molecule_name_1, molecule_name_2, index), 'w') valence_system = copy.deepcopy(sys2) valence_system.removeForce(3) valence_system.removeForce(3) integrator = openmm.VerletIntegrator(1*unit.femtoseconds) integrator_1 = openmm.VerletIntegrator(1*unit.femtoseconds) ctx_1 = openmm.Context(sys1, integrator_1) ctx_1.setPositions(pos1) ctx_1.setVelocitiesToTemperature(300*unit.kelvin) integrator_1.step(1000) pos1_new = ctx_1.getState(getPositions=True).getPositions(asNumpy=True) context = openmm.Context(sys2, integrator) context.setPositions(pos2) state = context.getState(getEnergy=True) print("Energy before proposal is: %s" % str(state.getPotentialEnergy())) openmm.LocalEnergyMinimizer.minimize(context) new_positions, logp_proposal = geometry_engine.propose(sm_top_proposal, pos1_new, beta) logp_reverse = geometry_engine.logp_reverse(sm_top_proposal, new_positions, pos1, beta) print(logp_reverse) app.PDBFile.writeFile(top2, new_positions, file=test_pdb_file) test_pdb_file.close() context.setPositions(new_positions) state2 = context.getState(getEnergy=True) print("Energy after proposal is: %s" %str(state2.getPotentialEnergy())) print(compute_potential_components(context)) valence_integrator = openmm.VerletIntegrator(1*unit.femtoseconds) platform = openmm.Platform.getPlatformByName("Reference") valence_ctx = openmm.Context(valence_system, valence_integrator, platform) valence_ctx.setPositions(new_positions) vstate = valence_ctx.getState(getEnergy=True) print("Valence energy after proposal is %s " % str(vstate.getPotentialEnergy())) final_potential = state2.getPotentialEnergy() return final_potential / final_potential.unit