Skip to content

Commit

Permalink
Molecule.center now returns the translation vector. Fixed issue where…
Browse files Browse the repository at this point in the history
… Molecule.append was keeping a reference to the second molecule's arrays instead of copying them when its own arrays were empty
  • Loading branch information
stefdoerr committed Oct 11, 2024
1 parent cf58f81 commit 7f11c45
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions moleculekit/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def insertappend(index, data1, data2, append):
if not isinstance(data2, np.ndarray):
data2 = np.array([data2])
if data1.size == 0:
return data2
return data2.copy() # To not insert a reference of second mol
if data2.size == 0:
return data1
if append: # TODO: Remove this if numpy insert is as fast as append
Expand Down Expand Up @@ -558,7 +558,7 @@ def insertappend(index, data1, data2, append):
self.bondtype = np.append(self.bondtype, mol.bondtype, axis=0)
else:
self.bonds = newbonds
self.bondtype = mol.bondtype
self.bondtype = mol.bondtype.copy()

for k in self._atom_and_coord_fields:
if k == "serial":
Expand Down Expand Up @@ -1290,6 +1290,11 @@ def center(self, loc=(0, 0, 0), sel="all"):
Atom selection string of the atoms whose geometric center we want to center on the `loc` position.
See more `here <http://www.ks.uiuc.edu/Research/vmd/vmd-1.9.2/ug/node89.html>`__
Returns
-------
translation : np.ndarray
3D coordinates of the translation applied to the Molecule
Examples
--------
>>> mol=tryp.copy()
Expand All @@ -1301,9 +1306,9 @@ def center(self, loc=(0, 0, 0), sel="all"):
raise RuntimeError(
f'Atom selection "{sel}" selected 0 atoms. Cannot center Molecule.'
)
com = np.mean(self.coords[selbool, :, self.frame], 0)
self.moveBy(-com)
self.moveBy(loc)
translation = np.mean(self.coords[selbool, :, self.frame], 0)
self.moveBy(-translation + loc)
return -translation + loc

def read(
self,
Expand Down

0 comments on commit 7f11c45

Please sign in to comment.