annotate tools/rdock/bin/sdtether @ 3:b02d74d22d05 draft default tip

planemo upload
author marpiech
date Mon, 29 Aug 2016 08:23:52 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
1 #! /usr/bin/env python
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
2 #
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
3 # Substitute for rbtether of rDock. Will align input molecules to a reference fragment defined by a smarts string,
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
4 # it will add a TETHERED ATOM property field to the output SDF that is correctly understood by rDock
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
5 # rDock will restrain the matching atom positions to the reference molecule coordinates.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
6 #
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
7 # Initially implemented with a conformational search algorithm to better match target coordinates.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
8 # But had problems with OBabel FF generating non-sense conformers. So in this version the conformer search is commented out.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
9 # Now if the input molecule do not have a good conformation, might not align well with the target. This effect will be
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
10 # dimished or even vanish if the SMARTS string is defined for a rigid region (like a ring).
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
11 # I'm still trying to incorporate somehow this conformational search.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
12 #
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
13 # Script distributed under GNU LGPL 3.0 along rDock software.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
14 #
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
15 # Author: Daniel Alvarez-Garcia
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
16 # Date: 08-11-2013
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
17
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
18 import math
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
19 import pybel
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
20 import numpy as npy
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
21
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
22 def superpose3D(ref, target, weights=None,refmask=None,targetmask=None,returnRotMat=False):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
23 """superpose3D performs 3d superposition using a weighted Kabsch algorithm : http://dx.doi.org/10.1107%2FS0567739476001873 & doi: 10.1529/biophysj.105.066654
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
24 definition : superpose3D(ref, target, weights,refmask,targetmask)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
25 @parameter 1 : ref - xyz coordinates of the reference structure (the ligand for instance)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
26 @type 1 : float64 numpy array (nx3)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
27 ---
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
28 @parameter 2 : target - theoretical target positions to which we should move (does not need to be physically relevant.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
29 @type 2 : float 64 numpy array (nx3)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
30 ---
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
31 @parameter 3: weights - numpy array of atom weights (usuallly between 0 and 1)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
32 @type 3 : float 64 numpy array (n)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
33 @parameter 4: mask - a numpy boolean mask for designating atoms to include
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
34 Note ref and target positions must have the same dimensions -> n*3 numpy arrays where n is the number of points (or atoms)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
35 Returns a set of new coordinates, aligned to the target state as well as the rmsd
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
36 """
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
37 if weights == None :
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
38 weights=1.0
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
39 if refmask == None :
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
40 refmask=npy.ones(len(ref),"bool")
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
41 if targetmask == None :
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
42 targetmask=npy.ones(len(target),"bool")
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
43 #first get the centroid of both states
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
44 ref_centroid = npy.mean(ref[refmask]*weights,axis=0)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
45 #print ref_centroid
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
46 refCenteredCoords=ref-ref_centroid
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
47 #print refCenteredCoords
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
48 target_centroid=npy.mean(target[targetmask]*weights,axis=0)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
49 targetCenteredCoords=target-target_centroid
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
50 #print targetCenteredCoords
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
51 #the following steps come from : http://www.pymolwiki.org/index.php/OptAlign#The_Code and http://en.wikipedia.org/wiki/Kabsch_algorithm
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
52 # Initial residual, see Kabsch.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
53 E0 = npy.sum( npy.sum(refCenteredCoords[refmask] * refCenteredCoords[refmask]*weights,axis=0),axis=0) + npy.sum( npy.sum(targetCenteredCoords[targetmask] * targetCenteredCoords[targetmask]*weights,axis=0),axis=0)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
54 reftmp=npy.copy(refCenteredCoords[refmask])
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
55 targettmp=npy.copy(targetCenteredCoords[targetmask])
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
56 #print refCenteredCoords[refmask]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
57 #single value decomposition of the dotProduct of both position vectors
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
58 try:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
59 dotProd = npy.dot( npy.transpose(reftmp), targettmp* weights)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
60 V, S, Wt = npy.linalg.svd(dotProd )
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
61 except Exception:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
62 try:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
63 dotProd = npy.dot( npy.transpose(reftmp), targettmp)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
64 V, S, Wt = npy.linalg.svd(dotProd )
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
65 except Exception:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
66 print >> sys.stderr,"Couldn't perform the Single Value Decomposition, skipping alignment"
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
67 return ref, 0
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
68 # we already have our solution, in the results from SVD.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
69 # we just need to check for reflections and then produce
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
70 # the rotation. V and Wt are orthonormal, so their det's
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
71 # are +/-1.
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
72 reflect = float(str(float(npy.linalg.det(V) * npy.linalg.det(Wt))))
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
73 if reflect == -1.0:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
74 S[-1] = -S[-1]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
75 V[:,-1] = -V[:,-1]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
76 rmsd = E0 - (2.0 * sum(S))
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
77 rmsd = npy.sqrt(abs(rmsd / len(ref[refmask]))) #get the rmsd
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
78 #U is simply V*Wt
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
79 U = npy.dot(V, Wt) #get the rotation matrix
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
80 # rotate and translate the molecule
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
81 new_coords = npy.dot((refCenteredCoords), U)+ target_centroid #translate & rotate
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
82 #new_coords=(refCenteredCoords + target_centroid)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
83 #print U
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
84 if returnRotMat :
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
85 return U, ref_centroid, target_centroid, rmsd
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
86 return new_coords,rmsd
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
87
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
88
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
89 def squared_distance(coordsA, coordsB):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
90 """Find the squared distance between two 3-tuples"""
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
91 sqrdist = sum( (a-b)**2 for a, b in zip(coordsA, coordsB) )
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
92 return sqrdist
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
93
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
94 def rmsd(allcoordsA, allcoordsB):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
95 """Find the RMSD between two lists of 3-tuples"""
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
96 deviation = sum(squared_distance(atomA, atomB) for
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
97 (atomA, atomB) in zip(allcoordsA, allcoordsB))
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
98 return math.sqrt(deviation / float(len(allcoordsA)))
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
99
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
100 def mapToCrystal(xtal, pose):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
101 """Some docking programs might alter the order of the atoms in the output (like Autodock Vina does...)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
102 this will mess up the rmsd calculation with OpenBabel"""
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
103 query = pybel.ob.CompileMoleculeQuery(xtal.OBMol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
104 mapper=pybel.ob.OBIsomorphismMapper.GetInstance(query)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
105 mappingpose = pybel.ob.vvpairUIntUInt()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
106 exit=mapper.MapUnique(pose.OBMol,mappingpose)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
107 return mappingpose[0]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
108
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
109 def takeCoords(obmol):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
110 """Take coordinates of an OBMol as a npy array"""
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
111 return npy.array([atom.coords for atom in obmol])
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
112
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
113 def updateCoords(obmol, newcoords):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
114 "Update OBMol coordinates. newcoords is a numpy array"
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
115 for i,atom in enumerate(obmol):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
116 atom.OBAtom.SetVector(*newcoords[i])
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
117
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
118 def prepareAtomString(idlist):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
119 s = ""
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
120 n = len(idlist)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
121 for i, id in enumerate(idlist):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
122 s += "%i"%id
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
123 if (i+1) == n: s+="\n"
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
124 elif (i+1)%35 == 0: s+=",\n"
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
125 else: s+=","
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
126 return s
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
127
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
128
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
129 if __name__ == "__main__":
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
130 import sys
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
131
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
132 if len(sys.argv) != 5:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
133 sys.exit("USAGE: %s reference.sdf input.sdf output.sdf 'SMARTS'"%sys.argv[0])
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
134
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
135 refsdf = sys.argv[1]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
136 molsdf = sys.argv[2]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
137 outsdf = sys.argv[3]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
138 smarts = pybel.Smarts(sys.argv[4])
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
139
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
140 # Read reference pose and get atom list matching smarts query
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
141 # if more than 1 match, take the first one
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
142 ref = next(pybel.readfile("sdf", refsdf))
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
143 refMatchIds = smarts.findall(ref)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
144 numRefMatchs = len(refMatchIds)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
145
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
146 if not numRefMatchs:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
147 sys.exit("No match found in the reference structure and the SMARTS string given. Please check it.")
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
148
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
149 if numRefMatchs > 1:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
150 print "More than one match in the reference molecule for the SMARTS string given. Will tether each input molecule all possible ways."
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
151
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
152 refIndxPerMatch = [npy.array(rmi) - 1 for rmi in refMatchIds]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
153
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
154 # Take coordinates for the reference matched atoms
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
155 refCoords = takeCoords(ref)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
156 refMatchCoords = [npy.take(refCoords, refIndx, axis=0) for refIndx in refIndxPerMatch]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
157
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
158 # Do the same for molecule in molsdf
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
159 out=pybel.Outputfile('sdf', outsdf, overwrite=True)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
160 molSupp = pybel.readfile("sdf", molsdf)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
161 ff = pybel.ob.OBForceField_FindForceField('MMFF94')
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
162 for i,mol in enumerate(molSupp):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
163 print "## Molecule %i"%(i+1),
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
164 mol.OBMol.DeleteNonPolarHydrogens()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
165 molMatchAllIds = smarts.findall(mol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
166 numMatchs = len(molMatchAllIds)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
167
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
168 if numMatchs == 0:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
169 print "No_Match",
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
170 continue
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
171 elif numMatchs ==1:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
172 print "Match",
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
173 elif numMatchs > 1:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
174 print "Multiple_Match SMART Matches for this molecule (%d)"%numMatchs,
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
175
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
176 # If more than one match, write an output of the same molecule for each match
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
177 # Start a default bestcoord and rmsd for later looping for each pose
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
178 bestCoordPerMatch = [[0 for i in range(numMatchs)] for i in range(numRefMatchs)]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
179 bestRMSPerMatch = [[999 for i in range(numMatchs)] for i in range(numRefMatchs)]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
180
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
181 # Will do a randomrotorsearch to find conformer with the lower rmsd when superposing
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
182 # At least 20 when possible
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
183 #ff.Setup(mol.OBMol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
184 #numats = mol.OBMol.NumAtoms()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
185 #numrot = mol.OBMol.NumRotors()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
186 #print "Atoms: %i, Rotors: %i"%(numats, numrot)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
187 #geomopt = 300
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
188 #genconf = 100
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
189 # increase iterations if bigger molecule or bigger number of rotatable bonds
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
190 # for allowing better sampling
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
191 #if numats > 40 and numrot > 5:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
192 # geomopt = 300
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
193 # genconf = 150
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
194 #if numats > 55 and numrot > 7:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
195 # genconf = 100
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
196 # geomopt = 500
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
197 #print "\tDoing conformational search with WeightedRotorSearch (%i, %i)..."%(genconf, geomopt),
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
198 #ff.SteepestDescent(500, 1.0e-4)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
199 #ff.WeightedRotorSearch(genconf,geomopt)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
200 #ff.ConjugateGradients(500, 1.0e-6)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
201 #ff.GetConformers(mol.OBMol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
202 #numconf = mol.OBMol.NumConformers()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
203 numconf = 1
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
204 #print "%i conformers generated"%numconf
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
205 if numconf > 1:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
206 # Doing conf search
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
207 #for i in range(numconf):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
208 # mol.OBMol.SetConformer(i)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
209 # confCoords = takeCoords(mol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
210 # print 'coord:',confCoords[0,:]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
211 #
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
212 # for imatch, molMatchIds in enumerate(molMatchAllIds):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
213 # molMatchIndx = npy.array(molMatchIds) - 1
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
214 # confMatchCoords = npy.take(confCoords, molMatchIndx, axis=0)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
215 #
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
216 # # Align: Get rotation matrix between the two sets of coords
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
217 # # Apply rotation to the whole target molecule
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
218 # rotMat, targetCentroid, refCentroid, rmsd = superpose3D(confMatchCoords, refMatchCoords, returnRotMat=True)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
219 # if rmsd < bestRMSPerMatch[imatch]:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
220 # newcoords = npy.dot((confCoords - targetCentroid), rotMat) + refCentroid
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
221 # bestRMSPerMatch[imatch] = rmsd
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
222 # bestCoordPerMatch[imatch] = newcoords
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
223 # #if bestrms < 0.01: break
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
224 pass
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
225 else:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
226 molCoords = takeCoords(mol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
227 for imatch, molMatchIds in enumerate(molMatchAllIds):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
228 # loop in each matching way for the input molecule
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
229 molMatchIndx = npy.array(molMatchIds) - 1
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
230 molMatchCoords = npy.take(molCoords, molMatchIndx, axis=0)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
231
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
232 # Loop over the reference matches
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
233 # Align: Get rotation matrix between the two sets of coords
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
234 # Apply rotation to the whole target molecule
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
235 for ir, refMatchCoord in enumerate(refMatchCoords):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
236 rotMat, targetCentroid, refCentroid, rmsd = superpose3D(molMatchCoords, refMatchCoord, returnRotMat=True)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
237 if rmsd < bestRMSPerMatch[ir][imatch]:
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
238 newcoords = npy.dot((molCoords - targetCentroid), rotMat) + refCentroid
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
239 bestRMSPerMatch[ir][imatch] = rmsd
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
240 bestCoordPerMatch[ir][imatch] = newcoords
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
241
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
242 # Finally update molecule coordinates with the best matching coordinates found
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
243 # change molecule coordinates, set TETHERED ATOMS property and save
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
244 for imatch in range(numMatchs):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
245 for irefmatch in range(numRefMatchs):
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
246 bestCoord = bestCoordPerMatch[irefmatch][imatch]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
247 bestRMS = bestRMSPerMatch[irefmatch][imatch]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
248 print "\tBest RMSD reached (match %d, refmatch %d): %s"%(imatch, irefmatch, bestRMS)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
249 molMatchID = molMatchAllIds[imatch]
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
250 updateCoords(mol, bestCoord)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
251 newData = pybel.ob.OBPairData()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
252 newData.SetAttribute("TETHERED ATOMS")
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
253 newData.SetValue(prepareAtomString(molMatchID))
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
254
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
255 mol.OBMol.DeleteData("TETHERED ATOMS") # Remove Previous DATA
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
256 mol.OBMol.CloneData(newData) # Add new data
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
257 out.write(mol)
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
258
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
259 out.close()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
260
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
261 print "DONE"
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
262 sys.stdout.close()
b02d74d22d05 planemo upload
marpiech
parents:
diff changeset
263 sys.stderr.close()