Esempio n. 1
0
    def _parseatoms(self, pdb):
        atoms = []

        # translate Bio.PDB atom objects to MDAnalysis Atom.
        for iatom, atom in enumerate(pdb.get_atoms()):
            residue = atom.parent
            chain_id = residue.parent.id

            atomname = atom.name
            atomtype = guess_atom_type(atomname)
            resname = residue.resname
            resid = int(residue.id[1])
            # no empty segids (or Universe throws IndexError)
            segid = residue.get_segid().strip() or chain_id or "SYSTEM"
            mass = guess_atom_mass(atomname)
            charge = guess_atom_charge(atomname)
            bfactor = atom.bfactor
            # occupancy = atom.occupancy

            atoms.append(
                Atom(iatom,
                     atomname,
                     atomtype,
                     resname,
                     resid,
                     segid,
                     mass,
                     charge,
                     bfactor=bfactor))

        return atoms
Esempio n. 2
0
    def _parseatoms(self, pdb):
        atoms = []

        # translate list of atoms to MDAnalysis Atom.
        for iatom, atom in enumerate(pdb._atoms):
            # ATOM
            if len(atom.__dict__) == 10:
                atomname = atom.name
                atomtype = atom.element or guess_atom_type(atomname)
                resname = atom.resName
                resid = int(atom.resSeq)
                chain = atom.chainID.strip()
                # no empty segids (or Universe throws IndexError)
                segid = atom.segID.strip() or chain or "SYSTEM"
                mass = guess_atom_mass(atomname)
                charge = guess_atom_charge(atomname)
                bfactor = atom.tempFactor
                # occupancy = atom.occupancy
                altLoc = atom.altLoc

                atoms.append(Atom(iatom, atomname, atomtype, resname, resid,
                                  segid, mass, charge,
                                  bfactor=bfactor, serial=atom.serial,
                                  altLoc=altLoc))
            # TER atoms
            #elif len(atom.__dict__) == 5:
            #    pass
            #    #atoms.append(None)
        self.structure["_atoms"] = atoms
Esempio n. 3
0
    def parse(self):
        """Parse CRD file *filename* and return the dict `structure`.

        Only reads the list of atoms.

        :Returns: MDAnalysis internal *structure* dict

        .. SeeAlso:: The *structure* dict is defined in
                     `MDAnalysis.topology`
        """
        extformat = FORTRANReader(
            '2I10,2X,A8,2X,A8,3F20.10,2X,A8,2X,A8,F20.10')
        stdformat = FORTRANReader('2I5,1X,A4,1X,A4,3F10.5,1X,A4,1X,A4,F10.5')

        atoms = []
        atom_serial = 0
        with openany(self.filename) as crd:
            for linenum, line in enumerate(crd):
                # reading header
                if line.split()[0] == '*':
                    continue
                elif line.split()[-1] == 'EXT' and bool(int(
                        line.split()[0])) is True:
                    r = extformat
                    continue
                elif line.split()[0] == line.split(
                )[-1] and line.split()[0] != '*':
                    r = stdformat
                    continue
                # anything else should be an atom
                try:
                    serial, TotRes, resName, name, x, y, z, chainID, resSeq, tempFactor = r.read(
                        line)
                except:
                    raise ValueError("Check CRD format at line {}: {}".format(
                        linenum, line.rstrip()))

                atomtype = guess_atom_type(name)
                mass = guess_atom_mass(name)
                charge = guess_atom_charge(name)
                atoms.append(
                    Atom(atom_serial, name, atomtype, resName, TotRes, chainID,
                         mass, charge))
                atom_serial += 1

        structure = {}
        structure["_atoms"] = atoms

        return structure
Esempio n. 4
0
    def parse(self):
        """Parse CRD file *filename* and return the dict `structure`.

        Only reads the list of atoms.

        :Returns: MDAnalysis internal *structure* dict

        .. SeeAlso:: The *structure* dict is defined in
                     `MDAnalysis.topology`
        """
        extformat = FORTRANReader('2I10,2X,A8,2X,A8,3F20.10,2X,A8,2X,A8,F20.10')
        stdformat = FORTRANReader('2I5,1X,A4,1X,A4,3F10.5,1X,A4,1X,A4,F10.5')

        atoms = []
        atom_serial = 0
        with openany(self.filename) as crd:
            for linenum, line in enumerate(crd):
                # reading header
                if line.split()[0] == '*':
                    continue
                elif line.split()[-1] == 'EXT' and bool(int(line.split()[0])) is True:
                    r = extformat
                    continue
                elif line.split()[0] == line.split()[-1] and line.split()[0] != '*':
                    r = stdformat
                    continue
                # anything else should be an atom
                try:
                    serial, TotRes, resName, name, x, y, z, chainID, resSeq, tempFactor = r.read(line)
                except:
                    raise ValueError("Check CRD format at line {}: {}".format(
                        linenum, line.rstrip()))

                atomtype = guess_atom_type(name)
                mass = guess_atom_mass(name)
                charge = guess_atom_charge(name)
                atoms.append(Atom(atom_serial, name, atomtype, resName, TotRes, chainID, mass, charge))
                atom_serial += 1

        structure = {}
        structure["_atoms"] = atoms

        return structure
Esempio n. 5
0
    def _parseatoms(self, pqr):
        atoms = []

        # translate list of atoms to MDAnalysis Atom.
        for iatom, atom in enumerate(pqr._atoms):
            atomname = atom.name
            atomtype = guess_atom_type(atomname)
            resname = atom.resName
            resid = int(atom.resSeq)
            chain = atom.chainID.strip()
            # no empty segids (or Universe throws IndexError)
            segid = atom.segID.strip() or chain or "SYSTEM"
            mass = guess_atom_mass(atomname)
            charge = float(atom.charge)
            radius = atom.radius

            atoms.append(Atom(iatom, atomname, atomtype, resname, resid,
                              segid, mass, charge, radius=radius))
        return atoms
Esempio n. 6
0
    def _parseatoms(self, pdb):
        atoms = []

        # translate Bio.PDB atom objects to MDAnalysis Atom.
        for iatom, atom in enumerate(pdb.get_atoms()):
            residue = atom.parent
            chain_id = residue.parent.id

            atomname = atom.name
            atomtype = guess_atom_type(atomname)
            resname = residue.resname
            resid = int(residue.id[1])
            # no empty segids (or Universe throws IndexError)
            segid = residue.get_segid().strip() or chain_id or "SYSTEM"
            mass = guess_atom_mass(atomname)
            charge = guess_atom_charge(atomname)
            bfactor = atom.bfactor
            # occupancy = atom.occupancy

            atoms.append(Atom(iatom, atomname, atomtype, resname, resid, segid,
                              mass, charge, bfactor=bfactor))

        return atoms
Esempio n. 7
0
    def _parseatoms(self, pdb):
        atoms = []

        # translate list of atoms to MDAnalysis Atom.
        for iatom, atom in enumerate(pdb._atoms):
            # ATOM
            if len(atom.__dict__) == 10:
                atomname = atom.name
                atomtype = atom.element or guess_atom_type(atomname)
                resname = atom.resName
                resid = int(atom.resSeq)
                chain = atom.chainID.strip()
                # no empty segids (or Universe throws IndexError)
                segid = atom.segID.strip() or chain or "SYSTEM"
                mass = guess_atom_mass(atomname)
                charge = guess_atom_charge(atomname)
                bfactor = atom.tempFactor
                # occupancy = atom.occupancy
                altLoc = atom.altLoc

                atoms.append(
                    Atom(iatom,
                         atomname,
                         atomtype,
                         resname,
                         resid,
                         segid,
                         mass,
                         charge,
                         bfactor=bfactor,
                         serial=atom.serial,
                         altLoc=altLoc))
            # TER atoms
            #elif len(atom.__dict__) == 5:
            #    pass
            #    #atoms.append(None)
        self.structure["_atoms"] = atoms
Esempio n. 8
0
    def parse(self):
        """Parse DMS file *filename* and return the dict `structure`.
                
        Only reads the list of atoms.
                
        :Returns: MDAnalysis internal *structure* dict, which contains
        Atom and Bond objects
                
        .. SeeAlso:: The *structure* dict is defined in
        `MDAnalysis.topology`.

        """
        # Fix by SB: Needed because sqlite3.connect does not raise anything if file is not there
        if not os.path.isfile(self.filename):
            raise IOError("No such file: {}".format(self.filename))

        def dict_factory(cursor, row):
            """
            Fetch SQL records as dictionaries, rather than the default tuples.
            """
            d = {}
            for idx, col in enumerate(cursor.description):
                d[col[0]] = row[idx]
            return d

        with sqlite3.connect(self.filename) as con:
            try:
                # This will return dictionaries instead of tuples,
                # when calling cur.fetch() or fetchall()
                con.row_factory = dict_factory
                cur = con.cursor()
                cur.execute('SELECT * FROM particle')
                particles = cur.fetchall()
            except sqlite3.DatabaseError:
                raise IOError("Failed reading the atoms from DMS Database")
            else:
                # p["anum"] contains the atomic number
                try:
                    atoms = [
                        Atom(p["id"], p["name"].strip(),
                             guess_atom_type(p["name"].strip()),
                             p["resname"].strip(), p["resid"],
                             p["segid"].strip(), p["mass"], p["charge"])
                        for p in particles
                    ]
                except KeyError:
                    raise ValueError("Failed reading atom information")
            try:
                cur.execute('SELECT * FROM bond')
                bonds = cur.fetchall()
            except sqlite3.DatabaseError:
                raise IOError("Failed reading the bonds from DMS Database")
            else:
                bondlist = []
                bondorder = {}
                for b in bonds:
                    desc = tuple(sorted([b['p0'], b['p1']]))
                    bondlist.append(desc)
                    bondorder[desc] = b['order']

        # All the records below besides donors and acceptors can be contained in a DMS file.
        # In addition to the coordinates and bonds, DMS may contain the entire force-field
        # information (terms+parameters),
        structure = {
            "_atoms": atoms,
            "_bonds": tuple(bondlist),
            "_bondorder": bondorder
        }

        return structure
Esempio n. 9
0
def guess_types(names):
    """GRO doesn't supply types, this returns an Attr"""
    return Atomtypes(
        np.array([guess_atom_type(name) for name in names], dtype=object))
Esempio n. 10
0
def check_atom_type(atype, aname):
    assert_equal(guess_atom_type(aname), atype)
Esempio n. 11
0
def check_atom_type(atype, aname):
    assert_equal(guess_atom_type(aname), atype)
Esempio n. 12
0
    def parse(self, filename=None):
        """Parse MOL2 file *filename* and return the dict `structure`.

        Only reads the list of atoms.

        :Returns: MDAnalysis internal *structure* dict

        .. SeeAlso:: The *structure* dict is defined in
                     :func:`MDAnalysis.topology.PSFParser.PSFParser`.

        """
        if not filename:
            filename = self.filename

        blocks = []

        with openany(filename) as f:
            for i, line in enumerate(f):
                # found new molecules
                if "@<TRIPOS>MOLECULE" in line:
                    if len(blocks):
                        break
                    blocks.append({"start_line": i, "lines": []})
                blocks[-1]["lines"].append(line)

        if not len(blocks):
            raise ValueError("The mol2 file '{}' needs to have at least one"
                             " @<TRIPOS>MOLECULE block".format(filename))
        block = blocks[0]

        sections = {}
        cursor = None

        for line in block["lines"]:
            if "@<TRIPOS>" in line:
                cursor = line.split("@<TRIPOS>")[1].strip().lower()
                sections[cursor] = []
                continue
            elif line.startswith("#") or line == "\n":
                continue
            sections[cursor].append(line)

        atom_lines, bond_lines = sections["atom"], sections["bond"]

        if not len(atom_lines):
            raise ValueError("The mol2 block ({}:{}) has no atoms".format(
                os.path.basename(filename), block["start_line"]))
        if not len(bond_lines):
            raise ValueError("The mol2 block ({}:{}) has no bonds".format(
                os.path.basename(filename), block["start_line"]))

        atoms = []
        for a in atom_lines:
            aid, name, x, y, z, atom_type, resid, resname, charge = a.split()
            aid = int(aid) - 1
            #x, y, z = float(x), float(y), float(z)
            resid = int(resid)
            charge = float(charge)
            element = guess_atom_type(name)
            mass = guess_atom_mass(element)
            # atom type is sybl atom type
            atoms.append(
                Atom(aid, name, atom_type, resname, resid, "X", mass, charge))
            #guess_atom_type(a.split()[1]

        bonds = []
        bondorder = {}
        for b in bond_lines:
            # bond_type can be: 1, 2, am, ar
            bid, a0, a1, bond_type = b.split()
            a0, a1 = int(a0) - 1, int(a1) - 1
            bond = tuple(sorted([a0, a1]))
            bondorder[bond] = bond_type
            bonds.append(bond)
        structure = {"_atoms": atoms, "_bonds": bonds, "_bondorder": bondorder}
        return structure
Esempio n. 13
0
def guess_types(names):
    """GRO doesn't supply types, this returns an Attr"""
    return Atomtypes(np.array([guess_atom_type(name) for name in names], dtype=object))
Esempio n. 14
0
    def parse(self):
        """Parse DMS file *filename* and return the dict `structure`.
                
        Only reads the list of atoms.
                
        :Returns: MDAnalysis internal *structure* dict, which contains
        Atom and Bond objects
                
        .. SeeAlso:: The *structure* dict is defined in
        `MDAnalysis.topology`.

        """
        # Fix by SB: Needed because sqlite3.connect does not raise anything if file is not there
        if not os.path.isfile(self.filename):
            raise IOError("No such file: {}".format(self.filename))

        def dict_factory(cursor, row):
            """
            Fetch SQL records as dictionaries, rather than the default tuples.
            """
            d = {}
            for idx, col in enumerate(cursor.description):
                d[col[0]] = row[idx]
            return d

        with sqlite3.connect(self.filename) as con:
            try:
                # This will return dictionaries instead of tuples,
                # when calling cur.fetch() or fetchall()
                con.row_factory = dict_factory
                cur = con.cursor()
                cur.execute('SELECT * FROM particle')
                particles = cur.fetchall()
            except sqlite3.DatabaseError:
                raise IOError("Failed reading the atoms from DMS Database")
            else:
                # p["anum"] contains the atomic number
                try:
                    atoms = [Atom(p["id"], p["name"].strip(),
                                  guess_atom_type(p["name"].strip()),
                                  p["resname"].strip(), p["resid"],
                                  p["segid"].strip(), p["mass"], p["charge"])
                             for p in particles]
                except KeyError:
                    raise ValueError("Failed reading atom information")
            try:
                cur.execute('SELECT * FROM bond')
                bonds = cur.fetchall()
            except sqlite3.DatabaseError:
                raise IOError("Failed reading the bonds from DMS Database")
            else:
                bondlist = []
                bondorder = {}
                for b in bonds:
                    desc = tuple(sorted([b['p0'], b['p1']]))
                    bondlist.append(desc)
                    bondorder[desc] = b['order']

        # All the records below besides donors and acceptors can be contained in a DMS file.
        # In addition to the coordinates and bonds, DMS may contain the entire force-field
        # information (terms+parameters),
        structure = {"_atoms": atoms, "_bonds": tuple(bondlist), "_bondorder": bondorder}

        return structure
Esempio n. 15
0
    def parse(self, filename=None):
        """Parse MOL2 file *filename* and return the dict `structure`.

        Only reads the list of atoms.

        :Returns: MDAnalysis internal *structure* dict

        .. SeeAlso:: The *structure* dict is defined in
                     :func:`MDAnalysis.topology.PSFParser.PSFParser`.

        """
        if not filename:
            filename = self.filename

        blocks = []

        with openany(filename) as f:
            for i, line in enumerate(f):
                # found new molecules
                if "@<TRIPOS>MOLECULE" in line:
                    if len(blocks):
                        break
                    blocks.append({"start_line": i, "lines": []})
                blocks[-1]["lines"].append(line)

        if not len(blocks):
            raise ValueError("The mol2 file '{}' needs to have at least one"
                             " @<TRIPOS>MOLECULE block".format(filename))
        block = blocks[0]

        sections = {}
        cursor = None

        for line in block["lines"]:
            if "@<TRIPOS>" in line:
                cursor = line.split("@<TRIPOS>")[1].strip().lower()
                sections[cursor] = []
                continue
            elif line.startswith("#") or line == "\n":
                continue
            sections[cursor].append(line)

        atom_lines, bond_lines = sections["atom"], sections["bond"]

        if not len(atom_lines):
            raise ValueError("The mol2 block ({}:{}) has no atoms".format(
                os.path.basename(filename), block["start_line"]))
        if not len(bond_lines):
            raise ValueError("The mol2 block ({}:{}) has no bonds".format(
                os.path.basename(filename), block["start_line"]))

        atoms = []
        for a in atom_lines:
            aid, name, x, y, z, atom_type, resid, resname, charge = a.split()
            aid = int(aid) - 1
            #x, y, z = float(x), float(y), float(z)
            resid = int(resid)
            charge = float(charge)
            element = guess_atom_type(name)
            mass = guess_atom_mass(element)
            # atom type is sybl atom type
            atoms.append(Atom(aid, name, atom_type, resname,
                              resid, "X", mass, charge))
            #guess_atom_type(a.split()[1]

        bonds = []
        bondorder = {}
        for b in bond_lines:
            # bond_type can be: 1, 2, am, ar
            bid, a0, a1, bond_type = b.split()
            a0, a1 = int(a0) - 1, int(a1) - 1
            bond = tuple(sorted([a0, a1]))
            bondorder[bond] = bond_type
            bonds.append(bond)
        structure = {"_atoms": atoms,
                     "_bonds": bonds,
                     "_bondorder": bondorder}
        return structure