def test_segid_squash(self): segids = np.array(['A', 'A', 'B']) segidx, (new_segids,) = change_squash((segids,), (segids,)) assert_array_equal(segidx, np.array([0, 0, 1])) assert_array_equal(new_segids, np.array(['A', 'B']))
def test_resid_squash(self): # System of 6 atoms in 3 residues # Residues 1 & 2 are Segid A, Residue 3 is Segid B # Resid 2 is repeated twice! Should be detected as 2 distinct residues resids = np.array([2, 2, 3, 3, 2, 2]) resnames = np.array(['RsA', 'RsA', 'RsB', 'RsB', 'RsC', 'RsC']) segids = np.array(['A', 'A', 'A', 'A', 'B', 'B']) residx, (new_resids, new_resnames, new_segids) = change_squash( (resids,), (resids, resnames, segids)) assert_array_equal(residx, np.array([0, 0, 1, 1, 2, 2])) assert_array_equal(new_resids, np.array([2, 3, 2])) assert_array_equal(new_resnames, np.array(['RsA', 'RsB', 'RsC'])) assert_array_equal(new_segids, np.array(['A', 'A', 'B']))
def _apply_map(self, mapping): """Apply the mapping scheme to the beads. Parameters ---------- mapping : dict Mapping definitions per bead/ Returns ------- :class:`~MDAnalysis.core.topology.Topology` defining the new universe. """ # Allocate arrays _beads = [] atomnames = [] atomids = [] resids = [] resnames = [] segids = [] charges = [] masses = [] residues = self.atu.atoms.split("residue") select_residues = enumerate( itertools.product(residues, viewitems(mapping))) for i, (res, (name, selection)) in select_residues: if name != "CB": bead = res.select_atoms(selection) else: bead = res.select_atoms( selection.get(res.resname[0], "hsidechain and not name H*")) if bead: _beads.append(bead) atomnames.append(name) atomids.append(i) resids.append(bead.resids[0]) resnames.append(bead.resnames[0]) segids.append(bead.segids[0].split("_")[-1]) try: charges.append(bead.total_charge()) except AttributeError: charges.append(0.) masses.append(bead.total_mass()) _beads = np.array(_beads) n_atoms = len(_beads) # Atom # _beads = topattrs._Beads(_beads) vdwradii = np.zeros_like(atomids) vdwradii = topologyattrs.Radii(vdwradii) atomids = topologyattrs.Atomids(np.asarray(atomids)) atomnames = topologyattrs.Atomnames( np.asarray(atomnames, dtype=np.object)) atomtypes = topologyattrs.Atomtypes( np.asarray(np.arange(n_atoms) + 100)) charges = topologyattrs.Charges(np.asarray(charges)) masses = topologyattrs.Masses(np.asarray(masses)) # Residue # resids, resnames segids = np.asarray(segids, dtype=np.object) resids = np.asarray(resids) resnames = np.asarray(resnames, dtype=np.object) residx, (new_resids, new_resnames, new_segids) = topbase.change_squash( (resids, ), (resids, resnames, segids)) # transform from atom:Rid to atom:Rix residueids = topologyattrs.Resids(new_resids) residuenums = topologyattrs.Resnums(new_resids.copy()) residuenames = topologyattrs.Resnames(new_resnames) # Segment segidx, (perseg_segids, ) = topbase.change_squash((new_segids, ), (new_segids, )) segids = topologyattrs.Segids(perseg_segids) # Setup topology top = topology.Topology(len(atomids), len(new_resids), len(segids), attrs=[ atomids, atomnames, atomtypes, charges, masses, vdwradii, residueids, residuenums, residuenames, segids ], atom_resindex=residx, residue_segindex=segidx) return top