def test_ring_center_and_normal(self): mol = parse_smiles("c1ccccc1") R = 1.5 for i in range(6): atom = mol.GetAtom(i+1) atom.SetVector(R*math.cos(2*math.pi*i/6), R*math.sin(2*math.pi*i/6), 0.0) for ring in ob.OBMolRingIter(mol): break center = ob.vector3() norm1 = ob.vector3() norm2 = ob.vector3() ring.findCenterAndNormal(center, norm1, norm2) self.assertZero(center.GetX()) self.assertZero(center.GetY()) self.assertZero(center.GetZ()) self.assertZero(norm1.GetX()) self.assertZero(norm1.GetY()) self.assertClose(norm1.GetZ(), 1.0) self.assertZero(norm2.GetX()) self.assertZero(norm2.GetY()) self.assertClose(norm2.GetZ(), -1.0)
def iter_close(self, molid=0, vdwinc=1.0): s0 = ob.vector3() for i in self.mol_map[molid]: s0 += self.vf[i] slist = list() slist.append(s0) for s in self.iter_symop_t2(): _found = [] I = s.is_identity() for a0idx in self.mol_map[molid]: for molid1 in xrange(len(self.mol_map)): if molid1 in _found: continue if I and molid == molid1: continue for a1idx in self.mol_map[molid1]: vt = self.f2c(s.apply(self.vf[a1idx])) svdw2 = self.vdw[a0idx] + self.vdw[a1idx] + vdwinc svdw2 *= svdw2 if self.vc[a0idx].distSq(vt) < svdw2: s1 = ob.vector3(0, 0, 0) for i in self.mol_map[molid1]: s1 += s.apply(self.vf[i]) _found1 = False for i in slist: if i.distSq(s1) < 0.001: _found1 = True break if _found1: break _found.append(molid1) slist.append(s1) yield s, molid1 break
def center_c(self): c = ob.vector3(0, 0, 0) n = 0 for atom in self.atoms: c += atom.GetVector() n += 1 return ob.vector3(c.GetX() / n, c.GetY() / n, c.GetZ() / n)
def __init__(self,r=ob.transform3d(1),t=ob.vector3(0,0,0)): self.T=ob.vector3(0,0,0) if type(r) is types.StringType: self.set_from_str(r) else: self.R=r self.add_trans(t)
def move_to_center(self): c = self.center_f vc = list() for i in (c.GetX(), c.GetY(), c.GetZ()): if i < 0: i -= 1 vc.append(int(i)) t = ob.vector3(vc[0], vc[1], vc[2]) if not t.IsApprox(ob.vector3(0, 0, 0), 0.001): print "WARNING: moving by [ %4.1f %4.1f %4.1f ]" %(vc[0], vc[1], vc[2]) tc=self.f2c(t) for i in xrange(self.OBMol.NumAtoms()): atom=self.OBMol.GetAtom(i+1) v = ob.vector3(atom.GetX(), atom.GetY(), atom.GetZ()) #wtf! GetVector fails with segfault v -= tc atom.SetVector(v)
def set_from_str(self,s): assert type(s) is types.StringType, 's is not string: %s' %s assert 'x' in s and 'y' in s and 'z' in s, 's sould containt x,y,z: %s' %s (x,y,z)=(0,0,0) s0=eval(s) vt2=[int(i) for i in s0] vt1=[s0[i]-vt2[i] for i in range(3)] r=list() for (x,y,z) in ((1,0,0),(0,1,0),(0,0,1)): s1=eval(s) r.append([s1[i]-s0[i] for i in range(3)]) self.R=ob.transform3d(ob.vector3(r[0][0],r[1][0],r[2][0]), ob.vector3(r[0][1],r[1][1],r[2][1]), ob.vector3(r[0][2],r[1][2],r[2][2]), ob.vector3(vt1[0],vt1[1],vt1[2])) r1=[float(i) for i in self.R.DescribeAsValues().split()] self.T=ob.vector3(s0[0]-r1[3],s0[1]-r1[7],s0[2]-r1[11])
def __repr__(self): #describe as 4x4 matrix v=[float(i) for i in self.R.DescribeAsValues().split()] #construct representation of symmetry operation without translations s=ob.transform3d(ob.vector3(v[0],v[1],v[2]),ob.vector3(v[4],v[5],v[6]), ob.vector3(v[8],v[9],v[10]),ob.vector3(0)).DescribeAsString().split(',') #add translational part from 4x4 matrix to true translation from #symmetry operation t=[v[3]+self.T.GetX(),v[7]+self.T.GetY(),v[11]+self.T.GetZ()] #construct scring for i in xrange(3): t[i]=str(Fraction.from_float(t[i]).limit_denominator(6)) t[i]+='+'+s[i] t[i]=t[i].replace('+-','-') if t[i].startswith('0'): t[i]=t[i][1:] if t[i].startswith('+'): t[i]=t[i][1:] return ','.join(t)
def alignmol(mol): masses = [] coordinates = [a.coords for a in mol.atoms] totalMass = 0 for atom in mol: m = 1 if options.useweights: m = atom.atomicmass masses.append(m) totalMass += m coordinates = numpy.array(coordinates) masses = numpy.array(masses) trans = -numpy.sum(coordinates * masses[:, numpy.newaxis], axis=0) / totalMass mol.OBMol.Translate(openbabel.vector3(*trans)) coordinates += trans #this is shamelessly stolen from the mdanalysis package inertia = calcI(masses, coordinates) eigenval, eigenvec = eig(inertia) # Sort indices = numpy.argsort(eigenval) # Return transposed in more logical form. See Issue 33. principalAxes = eigenvec[:, indices].T if det(principalAxes) < 0: principalAxes = -principalAxes #remove reflection !!Is this correct?? Kabasch wiki says negate just one row, but this is Kabasch... xrot = openbabel.double_array([1, 0, 0, 0, -1, 0, 0, 0, -1]) yrot = openbabel.double_array([-1, 0, 0, 0, 1, 0, 0, 0, -1]) zrot = openbabel.double_array([-1, 0, 0, 0, -1, 0, 0, 0, 1]) ident = openbabel.double_array([1, 0, 0, 0, 1, 0, 0, 0, 1]) rotmat = openbabel.double_array(principalAxes.flatten()) mol.OBMol.Rotate(rotmat) coordinates = [a.coords for a in mol.atoms] #should be modified (a, b) = calcSplitMoments(coordinates, 1) if (a < b): mol.OBMol.Rotate(xrot) # print a,b (a, b) = calcSplitMoments(coordinates, 0) if (a < b): mol.OBMol.Rotate(yrot) # print a,b return
def exchangeAtom(C, H, molTS, molFG, outfile, bondLength): # read write xyz file convTS, convFG = [ob.OBConversion() for i in range(2)] convTS.SetInAndOutFormats("xyz", "xyz") convFG.SetInAndOutFormats("xyz", "xyz") # builder builder = ob.OBBuilder() # define molecules, atoms and bond classes TS, FG = [ob.OBMol() for i in range(2)] atomH = ob.OBAtom() atomC = ob.OBAtom() bond = ob.OBBond() # read in xyz files convTS.ReadFile(TS, molTS) convFG.ReadFile(FG, molFG) # number of atoms in TS molecule numAtoms = TS.NumAtoms() # get hydrogen atom and its vector atomH = TS.GetAtom(H) atomC = TS.GetAtom(C) vec = ob.vector3(float(atomH.GetX()), float(atomH.GetY()), float(atomH.GetZ())) # delete hydrogen atom TS.DeleteAtom(atomH) # put both molecules together (same coord system) TS += FG # making the bond builder.Connect(TS, C, numAtoms, vec, 1) bond = TS.GetBond(C, numAtoms) bond.SetLength(atomC, bondLength) # call opimizer #opt_mmff_FG(TS, numAtoms, outfile) convTS.WriteFile(TS, "single/" + outfile) # clear TS and FG molecule classes TS.Clear() FG.Clear()
def alignmol(mol): masses = [] coordinates = [ a.coords for a in mol.atoms] totalMass = 0 for atom in mol: m = 1 if options.useweights: m = atom.atomicmass masses.append(m) totalMass += m coordinates = numpy.array(coordinates) masses = numpy.array(masses) trans = -numpy.sum(coordinates * masses[:, numpy.newaxis], axis=0) / totalMass mol.OBMol.Translate(openbabel.vector3(*trans)) coordinates += trans #this is shamelessly stolen from the mdanalysis package inertia = calcI(masses, coordinates) eigenval, eigenvec = eig(inertia) # Sort indices = numpy.argsort(eigenval) # Return transposed in more logical form. See Issue 33. principalAxes = eigenvec[:,indices].T if det(principalAxes) < 0: principalAxes = -principalAxes #remove reflection !!Is this correct?? Kabasch wiki says negate just one row, but this is Kabasch... xrot = openbabel.double_array([1,0,0,0,-1,0,0,0,-1]) yrot = openbabel.double_array([-1,0,0,0,1,0,0,0,-1]) zrot = openbabel.double_array([-1,0,0,0,-1,0,0,0,1]) ident = openbabel.double_array([1,0,0,0,1,0,0,0,1]) rotmat = openbabel.double_array(principalAxes.flatten()) mol.OBMol.Rotate(rotmat) coordinates = [ a.coords for a in mol.atoms] #should be modified (a,b) = calcSplitMoments(coordinates, 1) if(a < b): mol.OBMol.Rotate(xrot) # print a,b (a,b) = calcSplitMoments(coordinates, 0) if(a < b): mol.OBMol.Rotate(yrot) # print a,b return
def append_matrix_with_center_of_geometry(matrix, fieldname_cog="cog", fieldname_prefix=""): import numpy ligands= [] for pos in matrix: filename = matrix[pos][fieldname_prefix+"filename"] ligands.append(openbabel.OBMol()) obconversion.SetInFormat(filename[filename.rfind(".")+1:]) if obconversion.ReadFile(ligands[-1], filename): log.info('Successfully read ligand '+str(filename)) else: log.warning('Failed to read ligand '+str(filename)) cog = openbabel.vector3() for atom in openbabel.OBMolAtomIter(ligands[-1]): W = atom.GetVector() cog += W cog /= ligands[-1].NumAtoms() matrix[pos].update({ fieldname_prefix+fieldname_cog : cog }) return
def decode_solution(self, x): fragments_coords = [] fragments_atom_counts = [frag.NumAtoms() for frag in self.fragments] xi = 0 for frag, num_frag, num_atoms in zip(self.fragments, self.nums_fragments, fragments_atom_counts): for i in range(num_frag): cc = ob.OBMol(frag) tx, ty, tz = [t / AtomicRadiusUtils.angstrom2au for t in x[xi: xi + 3]] xi += 3 if num_atoms > 1: theta = x[xi] xi += 1 phi = x[xi] xi += 1 self.rotate(cc, theta, phi) cc.Translate(ob.vector3(tx, ty, tz)) fragments_coords.append(self.get_mol_coords(cc)) return fragments_coords
def to_openbabel_Mol(mol, CalcBondmap = False): obmol = ob.OBMol() if not mol.coords == None: for atom, coord in zip(mol.atoms, mol.coords): obatom = ob.OBAtom() obatom.SetAtomicNum(ob.OBElementTable().GetAtomicNum(atom)) coord_vec = ob.vector3(coord[0], coord[1], coord[2]) obatom.SetVector(coord_vec) obmol.InsertAtom(obatom) else: for atom in mol.atoms: obatom = ob.OBAtom() obatom.SetAtomicNum(ob.OBElementTable().GetAtomicNum(atom)) obmol.InsertAtom(obatom) if CalcBondmap == True: obmol.ConnectTheDots() obmol.PerceiveBondOrders() else: for bond in mol.bondmap: obmol.AddBond(bond[0] + 1, bond[1] + 1, bond[2]) return obmol
def append_matrix_with_center_of_geometry(matrix, fieldname_cog="cog", fieldname_prefix=""): import numpy ligands = [] for pos in matrix: filename = matrix[pos][fieldname_prefix + "filename"] ligands.append(openbabel.OBMol()) obconversion.SetInFormat(filename[filename.rfind(".") + 1:]) if obconversion.ReadFile(ligands[-1], filename): log.info('Successfully read ligand ' + str(filename)) else: log.warning('Failed to read ligand ' + str(filename)) cog = openbabel.vector3() for atom in openbabel.OBMolAtomIter(ligands[-1]): W = atom.GetVector() cog += W cog /= ligands[-1].NumAtoms() matrix[pos].update({fieldname_prefix + fieldname_cog: cog}) return
dx = r1[0] + shift[0] - r2[0] dy = r1[1] + shift[1] - r2[1] dz = r1[2] + shift[2] - r2[2] dd = dx * dx + dy * dy + dz * dz if (dd <= rmax): inner = 1 break if (inner): inc = 1 break if (inc): if (nx == 0) and (ny == 0) and (nz == 0): pass else: newmol = openbabel.OBMol(mol.OBMol) newmol.Translate(openbabel.vector3(*shift)) clust.append(newmol) else: un1 = fr.molunion() un1.construct(dbase, mol) fcom = [] fcrd = [] for f in un1.frags: x = 0.0 y = 0.0 z = 0.0 m = 0.0 fcrd.append([]) for j in f.refndx: at = mol.OBMol.GetAtom(j) dm = at.GetAtomicMass()
def find_PiPi(pdb_file, lig_name, centroid_distance=5.0, dih_parallel=25, dih_tshape=80, verbose=1): """ Find Pi-Pi interactions around the specified ligand residue from the pdb file. :param pdb_file: path of the target file in PDB format. :param lig_name: ligand residue name. :param centroid_distance: Max ring centroid distance :param dih_parallel: Max dihedral (parallel) :param dih_tshape: Min dihedral (T-shaped) :return: number of Pi-Pi interactions found """ # Get ligand residue and print its name. ligAtomList = [] ligAtomIdList = [] mol = next(pybel.readfile('pdb', pdb_file)) if verbose: print("A total of %s residues" % mol.OBMol.NumResidues()) lig = None for res in ob.OBResidueIter(mol.OBMol): # print res.GetName() if res.GetName() == lig_name: lig = res if verbose: print("Ligand residue name is:", lig.GetName()) break if not lig: if verbose: print("No ligand residue %s found, please confirm." % lig_name) return -1 else: for atom in ob.OBResidueAtomIter(lig): # print atom.GetIdx() ligAtomList.append(atom) ligAtomIdList.append(atom.GetIdx()) # Set ring_id i = 0 for ring in mol.sssr: ring.ring_id = i i += 1 # print ring.ring_id # Determine which rings are from ligand. ligRingList = [] ligAroRingList = [] ligRingIdList = [] recRingList = [] recAroRingList = [] for ring in mol.sssr: for atom in ligAtomList: if ring.IsMember(atom): if ring not in ligRingList: ligRingList.append(ring) ligRingIdList.append(ring.ring_id) if verbose: print("ligand ring_ID: ", ring.ring_id, end=' ') if ring.IsAromatic(): if verbose: print("aromatic") ligAroRingList.append(ring) else: if verbose: print("saturated") for ring in mol.sssr: if ring.ring_id not in ligRingIdList: recRingList.append(ring) if ring.IsAromatic(): recAroRingList.append(ring) if verbose: print("\nReceptor has ", len(recRingList), " rings,", end=' ') if verbose: print(" has ", len(recAroRingList), " aromatic rings.") # Find and show the rings ligRingCenter = ob.vector3() recRingCenter = ob.vector3() ligNorm1 = ob.vector3() ligNorm2 = ob.vector3() recNorm1 = ob.vector3() recNorm2 = ob.vector3() count = 0 lig_ring_index = 0 for ligRing in ligAroRingList: lig_ring_index += 1 ligRing.findCenterAndNormal(ligRingCenter, ligNorm1, ligNorm2) rec_ring_index = 0 for recRing in recAroRingList: rec_ring_index += 1 recRing.findCenterAndNormal(recRingCenter, recNorm1, recNorm2) dist = ligRingCenter.distSq(recRingCenter)**0.5 angle = vecAngle(ligNorm1, recNorm1) if (dist < centroid_distance and (angle < dih_parallel or angle > dih_tshape)): # the criteria count += 1 if verbose: print( "Pi-Pi ring pairs: %3s,%3s Angle(deg.): %5.2f Distance(A): %.2f" % (recRing.ring_id, ligRing.ring_id, angle, dist)) if verbose: print("Total Pi-Pi interactions:", count) return count
# generate .top and write fcontent = gen_top(args.force_field, os.path.splitext(args.dfrfile)[0] + ".itp") with open("topology.top", "w") as f: f.write(fcontent) # get the molecule diameter mol.OBMol.Center() maxd = 0.0 for atom in mol: x, y, z = atom.coords r = sqrt(x * x + y * y + z * z) if r > maxd: maxd = r # modify the unit cell and translate the molecule to the center of box boxsize = maxd + 100.0 cell = openbabel.OBUnitCell() cell.SetData(boxsize, boxsize, boxsize, 90.0, 90.0, 90.0) mol.OBMol.CloneData(cell) disp = boxsize / 2.0 arr = openbabel.vector3(openbabel.double_array([disp, disp, disp])) mol.OBMol.Translate(arr) # write .gro mol.write("gro", os.path.splitext(args.txtfile)[0] + ".gro", overwrite=True)
def readstruct(ioa, struct, importers=None): fileadapter = ioadapters.IoAdapterFileReader(ioa) if fileadapter.ext == 'structure': struct.parse(fileadapter) if importers is None: try_importers = ['ase', 'openbabel'] else: try_importers = importers for importer in try_importers: if importer == 'ase': try: import ase.io atoms = ase.io.read(fileadapter.filename_open_workaround()) species = atoms.get_atomic_numbers() coords = atoms.get_positions() basis = atoms.get_cell() return Structure(basis=basis, coords=coords, species=species) except Exception as e: if importers is not None: reraise_from( Exception, "Error while trying ase importer: " + str(info[1]), e) elif importer == 'openbabel': try: import openbabel file = fileadapter.file filename = fileadapter.filename # Use babel to read data from file obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats( obConversion.FormatFromExt(fileadapter.filename), obConversion.FindFormat("pdb")) obmol = openbabel.OBMol() obConversion.ReadString(obmol, file.read()) unitcell = openbabel.toUnitCell( obmol.GetData(openbabel.UnitCell)) unitcell.FillUnitCell(obmol) basisvecs = unitcell.GetCellVectors() basis = array([[ basisvecs[0].GetX(), basisvecs[0].GetY(), basisvecs[0].GetZ() ], [ basisvecs[1].GetX(), basisvecs[1].GetY(), basisvecs[1].GetZ() ], [ basisvecs[2].GetX(), basisvecs[2].GetY(), basisvecs[2].GetZ() ]]) coords = [] species = [] for obatom in openbabel.OBMolAtomIter(obmol): cart = openbabel.vector3(obatom.GetX(), obatom.GetY(), obatom.GetZ()) coords.append([cart.GetX(), cart.GetY(), cart.GetZ()]) species.append(obatom.GetAtomicNum()) return Structure(basis=basis, coords=coords, species=species) except: if importers is not None: info = sys.exc_info() reraise_from( Exception, "Error while trying openbabel importer: " + str(info[1]), info) raise Exception("Could not figure out a way to read structure") return None
def align_ligand(dummies, ligand): # fit dummy atoms of ligand to defined positions log.info('Aligning ligand dummy atoms to desired dummy atoms...') # 0.9 create local copy, as this function would otherwise modify the given ligand aligned_ligand = openbabel.OBMol(ligand) # 1.0 get dummy atoms from ligand log.debug('... get dummy atoms of ligand') ligand_dummies = get_dummies(ligand) # 1.1 get translation vector from read-in position to origin log.info('... determing translation vector from read-in to origin') translation = ligand_dummies.Center(1) ## DEBUG #obconversion = openbabel.OBConversion() #obconversion.SetOutFormat("pdb") #obconversion.WriteFile(ligand_dummies,"ligand_dummies_centered.pdb") # 1.2 initialize OBAlign for alignment to final destination log.info('... doing the alignment for dummy atoms') aligner = openbabel.OBAlign(dummies, ligand_dummies) success=aligner.Align() if success == False: return None, None #log.info('... done.') rmsd=aligner.GetRMSD() log.debug('RMSD of alignment: ' + str(rmsd)) ## 1.2.1 get Rotation Matrix for alignment log.info('... determining the rotation matrix') rotation_matrix = aligner.GetRotMatrix() rot = openbabel.double_array([1,2,3,4,5,6,7,8,9]) rotation_matrix.GetArray(rot) # .. only for debugging: arewedebugging = log.getLogger() if arewedebugging.isEnabledFor(logging.DEBUG): log.debug('--- rotation matrix ---') for i in range(0,9): log.debug(str(i)+ " : " + str(rot[i])) # 1.3 generate positioning vector ## NB: centering would not work because of rotation!! ## update cooordinates to new value aligner.UpdateCoords(ligand_dummies) log.info('... generating positioning vector') positioning = openbabel.vector3() ## calculate the vector for positioning to destination n = 0 for atom in openbabel.OBMolAtomIter(ligand_dummies): n += 1 positioning += atom.GetVector() positioning /= n # 1.4 move all ligand atoms to fit the pose the dummy atoms have been aligned to ## 1.4.1 generate inverted translation vector to translate ligand to origin translation_to_origin = translation translation_to_origin *= -1 ## 1.4.2 generate inverted rotation matrix to reconstruct alignment results rotation_inversion = rotation_matrix.transpose() rot_inv = openbabel.double_array([1,2,3,4,5,6,7,8,9]) rotation_inversion.GetArray(rot_inv) ## 1.4.3 apply translation to origin, rotation, and translation to final destination aligned_ligand.Translate(translation_to_origin) aligned_ligand.Rotate(rot_inv) aligned_ligand.Translate(positioning) ## 1.5 clean output ligand of dummy atoms, if desired if remove_dummies: log.info('Cleaning the ligand of unwanted dummies...') _temp_atom = [] for atom in openbabel.OBMolAtomIter(aligned_ligand): #aligned_ligand.AddResidue(atom.GetResidue()) #aligned_ligand.AddAtom(atom) if atom.GetAtomicNum() == 0: _temp_atom.append(atom) for a in _temp_atom: aligned_ligand.DeleteAtom(a) #else: #aligned_ligand = ligand log.info('... returning the aligned ligand.') return aligned_ligand, rmsd
def generate_dummies(dna_leading_strand_phosphates, dna_lagging_strand_phosphates, start_at_position, stop_at_position, finegraining_step=1, *args): ## this function generates (all) dummy atoms from the given phosphates log.info('generating dummy atom coordinates from phosphates...') ## 0.9 create dummy atom object dummies = openbabel.OBMol() dummies.SetTitle("minor groove dummy atom cloud for "+dna_file) helix = openbabel.OBMol() helix.SetTitle("minor groove dummy atom cloud for "+dna_file) start = 0 stop = 0 step = 0 # 1.0 check operation mode; set start and stop accordingly if start_at_position < stop_at_position: #construct_dummies_from_leading_strand_position > construct_dummies_to_leading_strand_position : log.info("Counting upwards for dummy generation...") step = +1 start = start_at_position stop = stop_at_position+1 elif start_at_position > stop_at_position: log.info("Counting downwards for dummy generation...") step = -1 start = start_at_position stop = stop_at_position-1 # 1.1 get coordinates of relevant phosphates log.info('Getting coordinates of phosphates ...') for i in range(start, stop, step): lead_index = str(i) log.debug(lead_index) progress = i - dna_leading_strand_starts_at_position log.debug(progress) for mode in ["minorgroove", "helix"]: if dna_bp_counting_antiparallel: if mode == "minorgroove": lag_index = str(dna_lagging_strand_position_at_leading_strand_start - progress + dna_leading_strand_basepairs_in_advance_of_lagging_strand_when_looking_at_minor_groove) elif mode == "helix": lag_index = str(dna_lagging_strand_position_at_leading_strand_start - progress) else: if mode == "minorgroove": lag_index = str(dna_lagging_strand_position_at_leading_strand_start + progress - dna_leading_strand_basepairs_in_advance_of_lagging_strand_when_looking_at_minor_groove) elif mode == "helix": lag_index = str(dna_lagging_strand_position_at_leading_strand_start + progress) log.debug(lag_index) log.debug('... Visiting position ' + lead_index + ' in leading strand') _vector_leading = dna_leading_strand_phosphates[lead_index] _vector_lagging = dna_lagging_strand_phosphates[lag_index] ## 1.1 create coordinates of dummy atoms with linear combination of vectors _coord_dummy_atom = openbabel.vector3() _coord_dummy_atom.SetX((_vector_leading.GetX() + _vector_lagging.GetX())/2) _coord_dummy_atom.SetY((_vector_leading.GetY() + _vector_lagging.GetY())/2) _coord_dummy_atom.SetZ((_vector_leading.GetZ() + _vector_lagging.GetZ())/2) log.debug("... Coordinates of dummy: " + str(_coord_dummy_atom.GetX()) + " " + str(_coord_dummy_atom.GetY()) + " " + str(_coord_dummy_atom.GetZ())) ## 1.2 create atom representation in openbabel if mode == "minorgroove": _new_atom = dummies.NewAtom() elif mode == "helix": _new_atom = helix.NewAtom() _new_atom.SetType("Du") _new_atom.SetAtomicNum(0) _new_atom.SetVector(_coord_dummy_atom) ## 1.3 create and add to residue representation if mode == "minorgroove": _new_res = dummies.NewResidue() _new_res.SetName("DUM") elif mode == "helix": _new_res = helix.NewResidue() _new_res.SetName("HDU") _new_res.SetNum(str(float(i))) log.debug("Created atom #"+_new_res.GetNumString()) _new_res.SetChain("W") #_new_res.SetChainNum(99) _new_res.AddAtom(_new_atom) _new_res.SetAtomID(_new_atom, "dummy") _new_res.SetHetAtom(_new_atom, 1) ## 1.4 interpolate between dummies, if wanted (= finegraining) if (finegraining_step < 1) and (i != stop-1): # construct_dummies_to_leading_strand_position ): mantissa = finegraining_step lead_index_next = str(int(lead_index)+step) log.debug(lead_index_next) if dna_bp_counting_antiparallel: lag_index_next = str(int(lag_index)-step) else: lag_index_next = str(int(lag_index)+step) log.debug(lag_index_next) _next_vector_leading = dna_leading_strand_phosphates[lead_index_next] _next_vector_lagging = dna_lagging_strand_phosphates[lag_index_next] while mantissa < 1-finegrain_step: ## 1.4.1 create coordinates of dummy atoms with linear combination of vectors _coord_dummy_atom = openbabel.vector3() _coord_dummy_atom.SetX((1-mantissa)*(_vector_leading.GetX() + _vector_lagging.GetX())/2+(mantissa)*(_next_vector_leading.GetX() + _next_vector_lagging.GetX())/2) _coord_dummy_atom.SetY((1-mantissa)*(_vector_leading.GetY() + _vector_lagging.GetY())/2+(mantissa)*(_next_vector_leading.GetY() + _next_vector_lagging.GetY())/2) _coord_dummy_atom.SetZ((1-mantissa)*(_vector_leading.GetZ() + _vector_lagging.GetZ())/2+(mantissa)*(_next_vector_leading.GetZ() + _next_vector_lagging.GetZ())/2) log.debug("... Coordinates of dummy: " + str(_coord_dummy_atom.GetX()) + " " + str(_coord_dummy_atom.GetY()) + " " + str(_coord_dummy_atom.GetZ())) ## 1.4.2 create atom representation in openbabel if mode == "minorgroove": _new_atom = dummies.NewAtom() elif mode == "helix": _new_atom = helix.NewAtom() _new_atom.SetType("Du") _new_atom.SetAtomicNum(0) _new_atom.SetVector(_coord_dummy_atom) ## 1.4.3 create and add to residue representation if mode == "minorgroove": _new_res = dummies.NewResidue() _new_res.SetName("DUM") elif mode == "helix": _new_res = helix.NewResidue() _new_res.SetName("HDU") if step > 0: _new_res.SetNum(str(i+mantissa)) else: _new_res.SetNum(str(i-mantissa)) log.debug("Created atom #"+_new_res.GetNumString()) _new_res.SetChain("W") #_new_res.SetChainNum(99) _new_res.AddAtom(_new_atom) _new_res.SetAtomID(_new_atom, "dummy") _new_res.SetHetAtom(_new_atom, 1) ## 1.4.4 try if there is a next step to take... mantissa += finegraining_step return dummies, helix
def is_identity(self): v0=ob.vector3(.1,.2,.3) v=self.apply(v0) if v.IsApprox(v0,0.01): return True return False
def PiPi(pdb_file, lig_name, centroid_distance, dih_parallel, dih_tshape): # Get ligand residue and print its name. ligAtomList = [] ligAtomIdList = [] mol = pybel.readfile('pdb', pdb_file).next() print "A total of %s residues" % mol.OBMol.NumResidues() lig = None for res in ob.OBResidueIter(mol.OBMol): # print res.GetName() if res.GetName() == lig_name: lig = res print "Ligand residue name is: ", lig.GetName() break if not lig: print "No ligand residue %s found, please confirm." % lig_name return 0 else: for atom in ob.OBResidueAtomIter(lig): # print atom.GetIdx() ligAtomList.append(atom) ligAtomIdList.append(atom.GetIdx()) # Set ring_id i = 0 for ring in mol.sssr: ring.ring_id = i i += 1 # print ring.ring_id # Determine which rings are from ligand. ligRingList = [] ligAroRingList = [] ligRingIdList = [] recRingList = [] recAroRingList = [] for ring in mol.sssr: for atom in ligAtomList: if ring.IsMember(atom): if ring not in ligRingList: ligRingList.append(ring) ligRingIdList.append(ring.ring_id) print "ligand ring_ID: ", ring.ring_id, if ring.IsAromatic(): print "Aromatic" ligAroRingList.append(ring) else: print "Saturated" for ring in mol.sssr: if ring.ring_id not in ligRingIdList: recRingList.append(ring) if ring.IsAromatic(): recAroRingList.append(ring) print "\nReceptor has ", len(recRingList), " rings,", print " has ", len(recAroRingList), " aromatic rings." # Find and show the rings ligRingCenter = ob.vector3() recRingCenter = ob.vector3() ligNorm1 = ob.vector3() ligNorm2 = ob.vector3() recNorm1 = ob.vector3() recNorm2 = ob.vector3() coord1 = [] coord2 = [] pair = [] # Store the names of the objects pairList = [] psObjectList = [] i = 0 for ligRing in ligAroRingList: ligRing.findCenterAndNormal(ligRingCenter, ligNorm1, ligNorm2) coord1 = [ ligRingCenter.GetX(), ligRingCenter.GetY(), ligRingCenter.GetZ() ] # Create a pseudoatom for the centroid of each ring in the ligand objectName1 = 'psCenter%.2d' % i i += 1 cmd.pseudoatom(object=objectName1, pos=coord1) psObjectList.append(objectName1) for recRing in recAroRingList: recRing.findCenterAndNormal(recRingCenter, recNorm1, recNorm2) dist = ligRingCenter.distSq(recRingCenter) angle = vecAngle(ligNorm1, recNorm1) if (dist**0.5 < centroid_distance and (angle < dih_parallel or angle > dih_tshape)): # the criteria coord2 = [ recRingCenter.GetX(), recRingCenter.GetY(), recRingCenter.GetZ() ] # Create pseudoatom for each lig ring center objectName2 = 'psCenter%.2d' % i i += 1 cmd.pseudoatom(object=objectName2, pos=coord2) psObjectList.append(objectName2) # pair=[coord1,coord2] pair = [objectName1, objectName2] # print "%.4f,%.4f,%.4f" % (pair[1][0],pair[1][1],pair[1][2]) pairList.append(pair) cmd.distance('dist-' + objectName1 + '-' + objectName2, pair[0], pair[1]) print objectName1, objectName2, " angle is : %.2f" % angle
def align(self, inc_hyd = False, symmetry = False, filt = False): """ Align two structures via OpenBabel. :param inc_hyd: include hydrogens :type inc_hyd: bool :param symmetry: consider symmetry of the molecule :type symmetry: bool :param filt: invoke primitive filter :type filt: bool :raises: SetupError """ if not self.ref_file: raise errors.SetupError('reference file not set yet') if self.ref_file == self.mol_file: logger.write('Identical files: will not perform alignment') return conv = ob.OBConversion() conv.SetInAndOutFormats(self.ref_fmt, self.mol_fmt) ref = ob.OBMol() tgt = ob.OBMol() # ignore warning messages about non-standard PDB errlev = ob.obErrorLog.GetOutputLevel() ob.obErrorLog.SetOutputLevel(0) conv.ReadFile(ref, self.ref_file) conv.ReadFile(tgt, self.mol_file) ob.obErrorLog.SetOutputLevel(errlev) if filt: # delete unwanted atoms in reference delat = [] for atom in ob.OBMolAtomIter(ref): resname = atom.GetResidue().GetName() # FIXME: replace with proper function if resname in const.IGNORE_RESIDUES: delat.append(atom) ref.BeginModify() for atom in delat: ref.DeleteAtom(atom, True) ref.EndModify() # copy wanted atoms from target to new OBMol cpy = ob.OBMol() for atom in ob.OBMolAtomIter(tgt): resname = atom.GetResidue().GetName() # FIXME: replace with proper function if resname not in const.IGNORE_RESIDUES: # NOTE: this copies only some info but incl. coordinates cpy.AddAtom(atom) molecs = ob.OBAlign(ref, cpy, inc_hyd, symmetry) else: molecs = ob.OBAlign(ref, tgt, inc_hyd, symmetry) logger.write('Aligning %s with %s as reference' % (self.mol_file, self.ref_file) ) if not molecs.Align(): logger.write('Alignment failed') return logger.write('RMSD is %.2f' % molecs.GetRMSD() ) if filt: rotate = molecs.GetRotMatrix() molecs.UpdateCoords(ref) first = True for atom in ob.OBMolAtomIter(tgt): tmpvec = ob.vector3(atom.GetVector()) tmpvec *= rotate if first: # we obviously can't do this directly: OB's vector3 does not # support the '-' but only the '-=' operator. But the # latter leads to a memory corruption bug... # shift = ref.GetAtom(1).GetVector() # shift -= cpy.GetAtom(1).GetVector() # we assume atoms 1 are equivalent in both molecules... v1 = ref.GetAtom(1).GetVector() v2 = cpy.GetAtom(1).GetVector() x = v1.GetX() - v2.GetX() y = v1.GetY() - v2.GetY() z = v1.GetZ() - v2.GetZ() shift = ob.vector3(x, y, z) first = False tmpvec += shift atom.SetVector(tmpvec) else: if not molecs.UpdateCoords(tgt): logger.write('Coordinate update failed') return try: conv.WriteFile(tgt, self.mol_file) except IOError as why: raise errors.SetupError(why) self.mol_atomtype = 'sybyl'
def iter_trans2(self): d = (0, 1, -1, 2, -2, 3, -3) for i in d: for j in d: for k in d: yield ob.vector3(i, j, k)