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

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