def test_double_frag_short_bonds(self): # previous bug where if two fragments are given # but all bonds were short, the algorithm didn't # complain self._load_bonds() mdamath.make_whole(self.ag) assert_raises(ValueError, mdamath.make_whole, self.u.atoms)
def test_double_frag_short_bonds(self, universe, ag): # previous bug where if two fragments are given # but all bonds were short, the algorithm didn't # complain mdamath.make_whole(ag) with pytest.raises(ValueError): mdamath.make_whole(universe.atoms)
def test_solve_3(self, universe): # put in a chunk that doesn't need any work refpos = universe.atoms[:1].positions.copy() mdamath.make_whole(universe.atoms[:1]) assert_array_almost_equal(universe.atoms[:1].positions, refpos)
def test_solve_3(self): # put in a chunk that doesn't need any work self._load_bonds() refpos = self.u.atoms[:1].positions.copy() mdamath.make_whole(self.u.atoms[:1]) assert_array_almost_equal(self.u.atoms[:1].positions, refpos)
def test_solve_2(self, universe, ag): # use but specify the center atom refpos = universe.atoms[4:8].positions.copy() mdamath.make_whole(ag, reference_atom=universe.residues[0].atoms[4]) assert_array_almost_equal(universe.atoms[4:8].positions, refpos) assert_array_almost_equal(universe.atoms[0].position, np.array([-20.0, 50.0, 0.0])) assert_array_almost_equal(universe.atoms[1].position, np.array([-10.0, 50.0, 0.0])) assert_array_almost_equal(universe.atoms[2].position, np.array([-10.0, 60.0, 0.0])) assert_array_almost_equal(universe.atoms[3].position, np.array([-10.0, 40.0, 0.0]))
def test_solve_4(self, universe): # Put in only some of a fragment, # check that not everything gets moved chunk = universe.atoms[:7] refpos = universe.atoms[7].position.copy() mdamath.make_whole(chunk) assert_array_almost_equal(universe.atoms[7].position, refpos) assert_array_almost_equal(universe.atoms[4].position, np.array([110.0, 50.0, 0.0])) assert_array_almost_equal(universe.atoms[5].position, np.array([110.0, 60.0, 0.0])) assert_array_almost_equal(universe.atoms[6].position, np.array([110.0, 40.0, 0.0]))
def test_solve_1(self, universe, ag): # regular usage of function refpos = universe.atoms[:4].positions.copy() mdamath.make_whole(ag) assert_array_almost_equal(universe.atoms[:4].positions, refpos) assert_array_almost_equal(universe.atoms[4].position, np.array([110.0, 50.0, 0.0])) assert_array_almost_equal(universe.atoms[5].position, np.array([110.0, 60.0, 0.0])) assert_array_almost_equal(universe.atoms[6].position, np.array([110.0, 40.0, 0.0])) assert_array_almost_equal(universe.atoms[7].position, np.array([120.0, 50.0, 0.0]))
def test_solve_1(self): # regular usage of function self._load_bonds() refpos = self.u.atoms[:4].positions.copy() mdamath.make_whole(self.u.residues[0]) assert_array_almost_equal(self.u.atoms[:4].positions, refpos) assert_array_almost_equal(self.u.atoms[4].position, np.array([110.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[5].position, np.array([110.0, 60.0, 0.0])) assert_array_almost_equal(self.u.atoms[6].position, np.array([110.0, 40.0, 0.0])) assert_array_almost_equal(self.u.atoms[7].position, np.array([120.0, 50.0, 0.0]))
def test_solve_4(self): # Put in only some of a fragment, # check that not everything gets moved self._load_bonds() chunk = self.u.atoms[:7] refpos = self.u.atoms[7].position.copy() mdamath.make_whole(chunk) assert_array_almost_equal(self.u.atoms[7].position, refpos) assert_array_almost_equal(self.u.atoms[4].position, numpy.array([110.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[5].position, numpy.array([110.0, 60.0, 0.0])) assert_array_almost_equal(self.u.atoms[6].position, numpy.array([110.0, 40.0, 0.0]))
def test_solve_2(self): # use but specify the center atom self._load_bonds() refpos = self.u.atoms[4:8].positions.copy() mdamath.make_whole(self.u.residues[0], reference_atom=self.u.residues[0][4]) assert_array_almost_equal(self.u.atoms[4:8].positions, refpos) assert_array_almost_equal(self.u.atoms[0].position, numpy.array([-20.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[1].position, numpy.array([-10.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[2].position, numpy.array([-10.0, 60.0, 0.0])) assert_array_almost_equal(self.u.atoms[3].position, numpy.array([-10.0, 40.0, 0.0]))
def test_solve_1(self): # regular usage of function self._load_bonds() refpos = self.u.atoms[:4].positions.copy() mdamath.make_whole(self.u.residues[0]) assert_array_almost_equal(self.u.atoms[:4].positions, refpos) assert_array_almost_equal(self.u.atoms[4].position, numpy.array([110.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[5].position, numpy.array([110.0, 60.0, 0.0])) assert_array_almost_equal(self.u.atoms[6].position, numpy.array([110.0, 40.0, 0.0])) assert_array_almost_equal(self.u.atoms[7].position, numpy.array([120.0, 50.0, 0.0]))
def test_solve_2(self): # use but specify the center atom self._load_bonds() refpos = self.u.atoms[4:8].positions.copy() mdamath.make_whole(self.u.residues[0], reference_atom=self.u.residues[0][4]) assert_array_almost_equal(self.u.atoms[4:8].positions, refpos) assert_array_almost_equal(self.u.atoms[0].position, np.array([-20.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[1].position, np.array([-10.0, 50.0, 0.0])) assert_array_almost_equal(self.u.atoms[2].position, np.array([-10.0, 60.0, 0.0])) assert_array_almost_equal(self.u.atoms[3].position, np.array([-10.0, 40.0, 0.0]))
def mk_whole(self, sel=None): """ Unwraps all segments in a selection of the MD analysis universe (by default, the selection is the union self.sel1 snd self.sel2) self.unwrap(sel=None) Note that the unwrapping only remains valid while the trajectory remains on the current frame (re-run for every frame) This program needs to be run with MDA2pdb in most cases (automatic) If obtaining vectors, we need to run this if the 'align' option is being used. It is not necessary if we only look at individual bonds without aligning, since we can correct box crossings simply by searching for bonds that are too long. """ "Default selection (segments in self.sel1 and self.sel2)" if sel is None: if self.sel1 is not None: if self.sel2 is not None: sel = self.sel1.union(self.sel2) else: sel = self.sel1 elif self.sel2 is not None: sel = self.sel2 else: sel = self.mda_object.atoms elif isinstance(sel, str): sel = self.mda_object.select_atoms(sel=sel) for seg in sel.segments: try: make_whole(seg.atoms) except: print('Failed to make segment {0} whole'.format(seg.segid))
def PersistenceL(trajFile, top, backboneAtoms, NP, coef=1. / 10., stride=1., chain0Id=0, firstpoint=2): """ get persistence length of chains of indices from chain0Id to chain0Id+NP-1 coef: divide results by 10. to convert to nm since MDAnalysis uses Angstrom """ from MDAnalysis.lib.mdamath import make_whole import MDAnalysis as mda from MDAnalysis.analysis import polymer u = mda.Universe(top, trajFile) for frag in u.atoms.fragments: make_whole(frag) molecules = u.atoms.fragments filter = 'name {}'.format(*backboneAtoms) backbones = [mol.select_atoms(filter) for mol in molecules][chain0Id:chain0Id + NP] sorted_backbones = [polymer.sort_backbone(bb) for bb in backbones] persistence_length = polymer.PersistenceLength(sorted_backbones) persistence_length.run() LB = persistence_length.lb * coef if firstpoint == 0: LP = persistence_length.lp * coef else: LP = polymer.fit_exponential_decay( persistence_length.x[firstpoint:], persistence_length.results[firstpoint:]) persistence_length.fit = np.exp(-persistence_length.x[firstpoint:] / LP) LP *= coef fig, ax = plt.subplots(nrows=1, ncols=1, figsize=[3, 2]) ax.plot(persistence_length.x * coef, persistence_length.results, 'ro', label='Result') ax.plot(persistence_length.x[firstpoint:] * coef, persistence_length.fit, label='Fit') ax.vlines(LP, 0., 1, ls=':', lw=0.75) plt.text(2. * LP, 0.5 * max(persistence_length.fit), '$<cos\\theta> = exp(-n*{}/{})$'.format(round(LB, 3), round(LP, 3)), size=7) ax.set_xlim(-0.1, 1.1 * max(persistence_length.x * coef)) ax.set_xlabel(r'$n*l_b$') ax.set_ylabel(r'$C(n*l_b)$') plt.savefig('persistenceL.png', dpi=500, transparent=True, bbox_inches="tight") return LP, LB, len(sorted_backbones[0])
def test_no_bonds(self): # NoData caused by no bonds universe = mda.Universe(Make_Whole) ag = universe.residues[0].atoms with pytest.raises(NoDataError): mdamath.make_whole(ag)
def test_not_orthogonal(self, universe, ag): # Not an orthogonal unit cell universe.dimensions = [10., 10., 10., 80., 80., 80] with pytest.raises(ValueError): mdamath.make_whole(ag)
def test_zero_box_size(self, universe, ag): universe.dimensions = [0., 0., 0., 90., 90., 90.] with pytest.raises(ValueError): mdamath.make_whole(ag)
def test_too_small_box_size(self, universe, ag): # Set the z dimensions to 0.5, which is small compared to the # bonds (1-2) universe.dimensions = [100.0, 100.0, 0.5, 90., 90., 90.] with pytest.raises(ValueError): mdamath.make_whole(ag)
def test_impossible_solve(self, universe): # check that the algorithm sees the bad walk with pytest.raises(ValueError): mdamath.make_whole(universe.atoms)
def test_wrong_reference_atom(self, universe, ag): # Reference atom not in atomgroup with pytest.raises(ValueError): mdamath.make_whole(ag, reference_atom=universe.atoms[-1])