Example #1
0
def refTop():
    return topology.Topology(
        3, 2, 2,
        attrs = [
            ta.Atomnames(TA_FILLER[object]),
            ta.Masses(TA_FILLER[float]),
            ta.Resids(TA_FILLER[int]),
            ta.Bonds(TA_FILLER['bond']),
        ],
        atom_resindex=np.array([0, 0, 1]),
        residue_segindex=np.array([0, 1])
    )
Example #2
0
def make_masses(size):
    """Atom masses (5.1, 4.2, 3.3, 1.5, 0.5) repeated"""
    import MDAnalysis.core.topologyattrs as ta
    na, nr, ns = size
    masses = itertools.cycle([5.1, 4.2, 3.3, 1.5, 0.5])
    return ta.Masses(np.array([next(masses) for _ in range(na)]))
Example #3
0
    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
Example #4
0
    def __new__(cls,
                have_bonds=True,
                have_masses=True,
                have_molnums=True,
                have_charges=True,
                is_triclinic=False):
        # box:
        a = 10.0  # edge length
        tfac = 0.1  # factor for box vector shift of triclinic boxes (~84°)
        if is_triclinic:
            box = triclinic_box([a, 0.0, 0.0], [a * tfac, a, 0.0],
                                [a * tfac, a * tfac, a])
        else:
            box = np.array([a, a, a, 90.0, 90.0, 90.0], dtype=np.float32)

        # number of atoms, residues, and segments:
        n_atoms = 47
        n_residues = 15
        n_segments = 6

        # resindices:
        residx = np.empty(n_atoms, dtype=np.int64)
        # type A
        rix = 0
        for i in range(0, 3, 1):
            residx[i] = rix
            rix += 1
        # type B
        for i in range(3, 15, 3):
            residx[i:i + 3] = rix
            rix += 1
        # type C & D
        for i in range(15, 47, 4):
            residx[i:i + 4] = rix
            rix += 1

        # segindices:
        segidx = np.empty(n_residues, dtype=np.int64)
        segidx[0:3] = 0
        segidx[3:7] = 1
        segidx[7:9] = 2
        segidx[9:11] = 3
        segidx[11:13] = 4
        segidx[13:15] = 5

        # universe:
        u = mda.Universe.empty(
            # topology things
            n_atoms=n_atoms,
            n_residues=n_residues,
            n_segments=n_segments,
            atom_resindex=residx,
            residue_segindex=segidx,
            # trajectory things
            trajectory=True,
            velocities=False,
            forces=False,
        )

        # resnames: we always want those for selection purposes
        resnames = ['A'] * 3
        resnames += ['B'] * 4
        resnames += ['C'] * 2
        resnames += ['D1', 'D2'] * 3
        u.add_TopologyAttr(topologyattrs.Resnames(resnames))

        # moltypes: we always want those for selection purposes
        moltypes = ['A'] * 3
        moltypes += ['B'] * 4
        moltypes += ['C'] * 2
        moltypes += ['D'] * 6
        u.add_TopologyAttr(topologyattrs.Moltypes(moltypes))

        # trajectory:
        ts = u.trajectory.ts
        ts.frame = 0
        ts.dimensions = box

        # positions:
        relpos = np.empty((n_atoms, 3), dtype=np.float32)
        # type A
        relpos[0:3, :] = np.array(
            [[0.5, 0.5, 0.5], [1.4, 0.5, 0.5], [2.1, 0.5, 0.5]],
            dtype=np.float32)
        # type B
        relpos[3:15, :] = np.array(
            [[0.1, 0.1, 0.2], [0.1, 0.1, 0.1], [0.2, 0.1, 0.1],
             [-0.05, 0.2, 0.05], [0.05, 0.2, 0.05], [0.05, 0.2, 0.95],
             [-0.2, -0.9, 1.05], [-0.2, 0.1, -0.05], [-0.1, 0.1, -0.05],
             [0.95, 0.2, 0.25], [0.95, 0.2, 0.15], [1.05, 0.2, 0.15]],
            dtype=np.float32)
        # type C
        relpos[15:23, :] = np.array(
            [[0.4, 0.95, 1.05], [0.4, 0.95, 0.95], [0.4, 0.05, 0.95],
             [0.4, 0.05, 1.05], [0.6, 0.05, 0.25], [0.6, 0.05, 0.15],
             [0.6, 0.15, 0.15], [0.6, 0.15, 0.25]],
            dtype=np.float32)
        # type D
        relpos[23:47, :] = np.array(
            [[0.2, 0.7, 0.8], [0.3, 0.7, 0.8], [0.4, 0.7, 0.8],
             [0.5, 0.7, 0.8], [0.6, 0.7, 0.8], [0.7, 0.7, 0.8],
             [0.8, 0.7, 0.8], [0.9, 0.7, 0.8], [0.66, 0.75, 0.7],
             [0.76, 0.75, 0.7], [0.86, 0.75, 0.7], [0.96, 0.75, 0.7],
             [0.06, 0.75, 0.7], [0.16, 0.75, 0.7], [0.26, 0.75, 0.7],
             [0.36, 0.75, 0.7], [1.14, 0.65, -0.4], [1.04, 0.65, -0.4],
             [0.94, 0.65, -0.4], [0.84, 0.65, -0.4], [0.74, 0.65, -0.4],
             [0.64, 0.65, -0.4], [0.54, 0.65, -0.4], [0.44, 0.65, -0.4]],
            dtype=np.float32)
        # make a copy, we need the original later
        _relpos = relpos.copy()
        # apply y- and z-dependent shift of x and y coords for triclinic boxes:
        if is_triclinic:
            # x-coord shift depends on y- and z-coords
            _relpos[:, 0] += tfac * _relpos[:, 1:].sum(axis=1)
            # y-coord shift depends on z-coords only
            _relpos[:, 1] += tfac * _relpos[:, 2]
        # scale relative to absolute positions:
        ts.positions = (_relpos * np.array([a, a, a])).astype(np.float32)

        # bonds:
        if have_bonds:
            bonds = []
            # type A has no bonds
            #type B
            for base in range(3, 15, 3):
                for i in range(2):
                    bonds.append((base + i, base + i + 1))
            # type C
            for base in range(15, 23, 4):
                for i in range(3):
                    bonds.append((base + i, base + i + 1))
                bonds.append((0 + base, 3 + base))
            # type D
            for base in range(23, 47, 8):
                for i in range(7):
                    bonds.append((base + i, base + i + 1))
            u.add_TopologyAttr(topologyattrs.Bonds(bonds))

        # masses:
        if have_masses:
            # masses are all set to 1 so that one can cross-check the results of
            # reference='com' with reference='cog' unwrapping
            masses = np.ones(n_atoms)
            u.add_TopologyAttr(topologyattrs.Masses(masses))

        # molnums:
        if have_molnums:
            molnums = [0, 1, 2]
            molnums += [3, 4, 5, 6]
            molnums += [7, 8]
            molnums += [9, 9, 10, 10, 11, 11]
            u.add_TopologyAttr(topologyattrs.Molnums(molnums))

        # charges:
        if have_charges:
            # type A
            charges = [2] * 3
            # type B
            charges += [-0.5] * 12
            # type C
            charges += [-1.5] * 8
            # type C
            charges += [0.5] * 24
            u.add_TopologyAttr(topologyattrs.Charges(charges))

        # shamelessly monkey-patch some custom universe attributes:
        u._is_triclinic = is_triclinic
        u._relpos = relpos
        u._tfac = tfac
        u._box_edge = a
        # bind custom methods to universe:
        u.unwrapped_coords = cls.unwrapped_coords.__get__(u)
        u.wrapped_coords = cls.wrapped_coords.__get__(u)
        u.center = cls.center.__get__(u)

        return u