import pele.amber.read_amber as ra def cart_to_internal(molecule): """ Converts the coordinates of the molecule from cartesian to internal coordinates. """ # First decide on a starting atom source_node = molecule.atoms.nodes()[0] # Argh, dictionaries aren't sorted... completed_bonds = {source_node: []} for edge in nx.dfs_edges(molecule.atoms, source_node): completed_bonds[edge[1]] = [edge[0]] # completed_bonds[2] += [x for x in completed_bonds[1][1:] if x not in completed_bonds[2]] # completed_bonds[3] += [x for x in completed_bonds[2][1:] if x not in completed_bonds[3]] for entry in completed_bonds: print(entry, completed_bonds[entry]) # completed.append(completed[-1] + [search_tree.neighbors(source_node)[0]]) #completed.append(completed[-1] + [[x for x in search_tree.neighbors(completed[-1][-1]) if x not in completed[-1]][0]]) #completed.append(completed[-1] + [[x for x in search_tree.neighbors(completed[-1][-1]) if x not in completed[-1]][0]]) #print nx.single_source_shortest_path(molecule.atoms, source_node) if __name__ == "__main__": topology_data = ra.read_topology("/home/khs26/coords.prmtop") mol = ra.create_molecule(topology_data) mol.read_coords("/home/khs26/coords.inpcrd") cart_to_internal(mol)
def measure_dihedral(coords, atoms): indices = [3 * atom + k for atom in atoms for k in range(3)] coords_0 = coords[indices[0:3]] coords_1 = coords[indices[3:6]] coords_2 = coords[indices[6:9]] coords_3 = coords[indices[9:12]] b1 = coords_1 - coords_0 b2 = coords_2 - coords_1 b3 = coords_3 - coords_2 b2_b3 = np.cross(b2, b3) b1_b2 = np.cross(b1, b2) angle = np.arctan2( np.linalg.norm(b2) * np.dot(b1, b2_b3), np.dot(b1_b2, b2_b3)) return angle if __name__ == "__main__": import pele.amber.read_amber as amber import playground.group_rotation.amino_acids as amino topology_data = amber.read_topology("/home/khs26/coords.prmtop") parsed = amber.create_atoms_and_residues(topology_data) test_params = amber.group_rotation_dict(parsed, amino.def_parameters) test_coords = np.array( amber.read_amber_coords("/home/khs26/coords.inpcrd")) testGR = GroupRotation(test_params) pre_coords = test_coords.copy() result = testGR.takeStep(test_coords) print test_coords - pre_coords
# Create a filter, telling it to compress with zlib. filters = ts.Filters(complib='zlib') for amino_acid in (amino.amino_acids): table = fileh.create_table("/RotamerGroupTemplates", amino_acid, RotamerGroupTemplate, "Template for {res}".format(res=amino_acid)) # Get the record object associated with the table. group_template = table.row # Read in an appropriate topology file and create a molecular graph. filename = '/scratch/khs26/rotamer_lib_igb2/{res}/{res}/{res}/coords.prmtop'.format(res=amino_acid) topology = ra.read_topology(filename) mol_graph = ra.create_atoms_and_residues(topology) # Get the residue name for the first residue. res = next(residue for residue in mol_graph.residues.nodes() if residue.index == 1) # Get a list of dihedrals we are interested in for this residue. dihedrals = sorted([k[1] for k in amino.def_parameters if k[0] == amino_acid and not ('C' in k[1] and 'CA' in k[1])]) # For each pair of atoms in a dihedral, find their highest-ranked neighbours for defining the dihedral angle. dihedral_atoms = {} dihedral_moving_atoms = {} for atom_pair in dihedrals: atom0 = next(n for n in mol_graph.atoms.nodes() if n.name == atom_pair[0] and n.residue == res) atom1 = next(n for n in mol_graph.atoms.nodes() if n.name == atom_pair[1] and n.residue == res)
# Create a filter, telling it to compress with zlib. filters = ts.Filters(complib='zlib') for amino_acid in (amino.amino_acids): table = fileh.create_table("/RotamerGroupTemplates", amino_acid, RotamerGroupTemplate, "Template for {res}".format(res=amino_acid)) # Get the record object associated with the table. group_template = table.row # Read in an appropriate topology file and create a molecular graph. filename = '/scratch/khs26/rotamer_lib_igb2/{res}/{res}/{res}/coords.prmtop'.format( res=amino_acid) topology = ra.read_topology(filename) mol_graph = ra.create_atoms_and_residues(topology) # Get the residue name for the first residue. res = next(residue for residue in mol_graph.residues.nodes() if residue.index == 1) # Get a list of dihedrals we are interested in for this residue. dihedrals = sorted([ k[1] for k in amino.def_parameters if k[0] == amino_acid and not ('C' in k[1] and 'CA' in k[1]) ]) # For each pair of atoms in a dihedral, find their highest-ranked neighbours for defining the dihedral angle. dihedral_atoms = {} dihedral_moving_atoms = {}
:param sidechains: {Ca: sidechain atoms graph} dictionary :return: residues: residue type :return: mapping: atom mapping dictionary """ residues = {} mapping = {} for sc in sidechains: for res in res_scs.amino_acids: graph_match = nx.algorithms.isomorphism.GraphMatcher(res_scs.amino_acids[res], sidechains[sc], node_match=(lambda x, y: x['element'] == y['element'])) if graph_match.is_isomorphic(): residues[sc.residue] = res mapping[sc.residue] = graph_match.mapping break return residues, mapping if __name__ == "__main__": import os.path topology_data = amber.read_topology(os.path.normpath("B:/flu.prmtop")) molecule = amber.create_molecule(topology_data) cands = chir.tetravalent_atoms(molecule.atoms) chir.multi_bonds(molecule.atoms) cands2 = chir.rankable_neighbours(cands)[0] print len(molecule.atoms), len(cands), len(cands2) # scs = find_sidechains(molecule, [res for res in molecule.residues.nodes()]) # ress, maps = residue_from_sidechain(scs) # for k, v in sorted(ress.items()): # print k, v # for i, j in res_scs.dihedrals: # print i, j, chir.chiral_order(molecule.atoms, maps[k][i]), chir.chiral_order(molecule.atoms, maps[k][j])
dirname = '/scratch/khs26/rotamer_lib_igb2/ARG/TRP/ALA/' # First test the order of atoms in lowest and in the topology file # with open('/scratch/khs26/rotamer_lib_igb2/ALA/ARG/TRP/lowest', 'r') as lowest: # num_atoms = int(lowest.readline()) # energy = float(lowest.readline().split()[4]) # coords = [] # for line in lowest: # if len(coords) >= num_atoms: # break # coords.append(line.split()) # print num_atoms, energy # print coords # Open and parse the topology file topology = ra.read_topology(''.join((dirname, 'coords.prmtop'))) mol_graph = ra.create_atoms_and_residues(topology) # We need to know the residues of interest and their dihedrals res1 = next(residue for residue in mol_graph.residues.nodes() if residue.index == 1) res2 = next(residue for residue in mol_graph.residues.nodes() if residue.index == 2) res3 = next(residue for residue in mol_graph.residues.nodes() if residue.index == 3) # Get the list of possible bonds from the amino_acids module dihedral_bonds = {} for res in (res1, res2, res3): dihedral_bonds[res] = [bond[1] for bond in amino.def_parameters.keys() if bond[0] == res.name] # print chir.chiral_order(mol_graph.atoms, dihedral_bonds[res][0][0], depth=2) print dihedral_bonds # Define all the dihedrals
for dihedral in unmapped_dihedrals: # We need to convert "N-1" to the N neighbouring "C0". The others can be converted using the map. atom_map = residue_atom_map[residue] mapped_dihedral = [[nb for nb in nx.neighbors(residue.molecule.atoms, atom_map["C0"]) if nb.element == "N"][0] if atom_name == "N-1" else atom_map[atom_name] for atom_name in dihedral] mapped_dihedrals.append(mapped_dihedral) dihedral_dict[residue] = mapped_dihedrals return dihedral_dict if __name__ == "__main__": import os.path import numpy as np from playground.rotamer.measure_dihedral import dihedrals_with_symmetry import cProfile # pr = cProfile.Profile() # pr.enable() topology_data = amber.read_topology(os.path.normpath("/home/khs26/flu.prmtop")) coords = np.array(amber.read_amber_coords(os.path.normpath("/home/khs26/flu.inpcrd"))).reshape((-1, 3)) molecule = amber.create_molecule(topology_data) ress, maps = molecule.identify_residues() dihedrals = [] for v in map_dihedrals(ress, maps).values(): for dihedral in v: dihedrals.append(Dihedral(dihedral)) for dihe in sorted(dihedrals, key=lambda x: x.residue.index): if dihe.residue.identity in symmetric_atoms: print dihe.residue, dihe.atoms, dihe.residue.atoms print "Angle:", dihe.measure_dihedral(coords) * 180.0 / np.pi # pr.disable() # pr.print_stats('cumulative')