Exemplo n.º 1
0
    def parse(self):
        """Parse Amber PRMTOP topology file *filename*.
        
        :Returns: MDAnalysis internal *structure* dict.
        """
        formatversion = 10
        with openany(self.filename) as topfile:
            for line in topfile:
                if line.startswith("%FLAG ATOMIC_NUMBER"):
                    formatversion = 12
                    break
        if formatversion == 12:
            sections = [
                ("ATOM_NAME", 1, 20, self._parseatoms, "_name", 0),
                ("CHARGE", 1, 5, self._parsesection, "_charge", 0),
                ("ATOMIC_NUMBER", 1, 10, self._parsesectionint, "_skip", 0),
                ("MASS", 1, 5, self._parsesection, "_mass", 0),
                ("ATOM_TYPE_INDEX", 1, 10, self._parsesectionint, "_atom_type",
                 0),
                ("NUMBER_EXCLUDED_ATOMS", 1, 10, self._parseskip, "_skip", 8),
                ("NONBONDED_PARM_INDEX", 1, 10, self._parseskip, "_skip", 8),
                ("RESIDUE_LABEL", 1, 20, self._parseatoms, "_resname", 11),
                ("RESIDUE_POINTER", 2, 10, self._parsesectionint, "_respoint",
                 11),
            ]
            #("BOND_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
            #("BOND_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
            #("ANGLE_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
            #("ANGLE_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
            #("DIHEDRAL_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
            #("DIHEDRAL_PERIODICITY", 1, 5, self._parseskip,"_skip",8),
            #("DIHEDRAL_PHASE", 1, 5, self._parseskip,"_skip",8),
            #("SOLTY", 1, 5, self._parseskip,"_skip",8),
            #("LENNARD_JONES_ACOEF", 1, 5, self._parseskip,"_skip",8),
            #("LENNARD_JONES_BCOEF", 1, 5, self._parseskip,"_skip",8),
            #("BONDS_INC_HYDROGEN", 2, 4, self._parsebond, "_bonds",2),
            #("ANGLES_INC_HYDROGEN", 3, 3, self._parsesection, "_angles"),
            #("DIHEDRALS_INC_HYDROGEN", 4, 2, self._parsesection, "_dihe"),
            #("NIMPHI", 4, 2, self._parsesection, "_impr"),
            #("NDON", 2, 4, self._parsesection,"_donors"),
            #("NACC", 2, 4, self._parsesection,"_acceptors"),
        elif formatversion == 10:
            sections = [
                ("ATOM_NAME", 1, 20, self._parseatoms, "_name", 0),
                ("CHARGE", 1, 5, self._parsesection, "_charge", 0),
                ("MASS", 1, 5, self._parsesection, "_mass", 0),
                ("ATOM_TYPE_INDEX", 1, 10, self._parsesectionint, "_atom_type",
                 0),
                ("NUMBER_EXCLUDED_ATOMS", 1, 10, self._parseskip, "_skip", 8),
                ("NONBONDED_PARM_INDEX", 1, 10, self._parseskip, "_skip", 8),
                ("RESIDUE_LABEL", 1, 20, self._parseatoms, "_resname", 11),
                ("RESIDUE_POINTER", 2, 10, self._parsesectionint, "_respoint",
                 11),
            ]
            #("BOND_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
            #("BOND_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
            #("ANGLE_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
            #("ANGLE_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
            #("DIHEDRAL_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
            #("DIHEDRAL_PERIODICITY", 1, 5, self._parseskip,"_skip",8),
            #("DIHEDRAL_PHASE", 1, 5, self._parseskip,"_skip",8),
            #("SOLTY", 1, 5, self._parseskip,"_skip",8),
            #("LENNARD_JONES_ACOEF", 1, 5, self._parseskip,"_skip",8),
            #("LENNARD_JONES_BCOEF", 1, 5, self._parseskip,"_skip",8),
            #("BONDS_INC_HYDROGEN", 2, 4, self._parsebond, "_bonds",2),
            #("ANGLES_INC_HYDROGEN", 3, 3, self._parsesection, "_angles"),
            #("DIHEDRALS_INC_HYDROGEN", 4, 2, self._parsesection, "_dihe")]
            #("NIMPHI", 4, 2, self._parsesection, "_impr"),
            #("NDON", 2, 4, self._parsesection,"_donors"),
            #("NACC", 2, 4, self._parsesection,"_acceptors")]

        # Open and check top validity
        # Reading header info POINTERS
        with openany(self.filename) as topfile:
            next_line = topfile.next
            header = next_line()
            if header[:3] != "%VE":
                raise ValueError(
                    "{} is not a valid TOP file. %VE Missing in header".format(
                        topfile))
            title = next_line().split()
            if not (title[1] == "TITLE"):
                raise ValueError(
                    "{} is not a valid TOP file. 'TITLE' missing in header".
                    format(topfile))
            while header[:14] != '%FLAG POINTERS':
                header = next_line()
            header = next_line()

            topremarks = [next_line().strip() for i in xrange(4)]
            sys_info = [int(k) for i in topremarks for k in i.split()]

            structure = {}
            final_structure = {}

            try:
                for info in sections:
                    self._parse_sec(sys_info, info, next_line, structure,
                                    final_structure)
            except StopIteration:
                raise ValueError("The TOP file didn't contain the minimum"
                                 " required section of ATOM_NAME")
            # Completing info respoint to include all atoms in last resid
            structure["_respoint"].append(sys_info[0])
            structure["_respoint"][-1] = structure["_respoint"][-1] + 1

        atoms = [
            None,
        ] * sys_info[0]

        j = 0
        segid = "SYSTEM"
        for i in range(sys_info[0]):
            charge = convert(structure["_charge"][i], 'Amber',
                             flags['charge_unit'])
            if structure["_respoint"][j] <= i + 1 < structure["_respoint"][j +
                                                                           1]:
                resid = j + 1
                resname = structure["_resname"][j]
            else:
                j += 1
                resid = j + 1
                resname = structure["_resname"][j]
            mass = structure["_mass"][i]
            atomtype = structure["_atom_type"][i]
            atomname = structure["_name"][i]
            #segid = 'SYSTEM'  # does not exist in Amber

            atoms[i] = Atom(i, atomname, atomtype, resname, resid, segid, mass,
                            charge)
        final_structure["_atoms"] = atoms
        final_structure["_numatoms"] = sys_info[0]
        return final_structure
Exemplo n.º 2
0
    def parse(self):
        """Parse Amber PRMTOP topology file *filename*.
        
        :Returns: MDAnalysis internal *structure* dict.
        """
        formatversion = 10
        with openany(self.filename) as topfile:
            for line in topfile:
                if line.startswith("%FLAG ATOMIC_NUMBER"):
                    formatversion = 12
                    break
        if formatversion == 12:
            sections = [
                ("ATOM_NAME", 1, 20, self._parseatoms, "_name", 0),
                ("CHARGE", 1, 5, self._parsesection, "_charge", 0),
                ("ATOMIC_NUMBER", 1, 10, self._parsesectionint, "_skip", 0),
                ("MASS", 1, 5, self._parsesection, "_mass", 0),
                ("ATOM_TYPE_INDEX", 1, 10, self._parsesectionint, "_atom_type", 0),
                ("NUMBER_EXCLUDED_ATOMS", 1, 10, self._parseskip, "_skip", 8),
                ("NONBONDED_PARM_INDEX", 1, 10, self._parseskip, "_skip", 8),
                ("RESIDUE_LABEL", 1, 20, self._parseatoms, "_resname", 11),
                ("RESIDUE_POINTER", 2, 10, self._parsesectionint, "_respoint", 11),
            ]
                #("BOND_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
                #("BOND_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
                #("ANGLE_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
                #("ANGLE_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
                #("DIHEDRAL_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
                #("DIHEDRAL_PERIODICITY", 1, 5, self._parseskip,"_skip",8),
                #("DIHEDRAL_PHASE", 1, 5, self._parseskip,"_skip",8),
                #("SOLTY", 1, 5, self._parseskip,"_skip",8),
                #("LENNARD_JONES_ACOEF", 1, 5, self._parseskip,"_skip",8),
                #("LENNARD_JONES_BCOEF", 1, 5, self._parseskip,"_skip",8),
                #("BONDS_INC_HYDROGEN", 2, 4, self._parsebond, "_bonds",2),
                #("ANGLES_INC_HYDROGEN", 3, 3, self._parsesection, "_angles"),
                #("DIHEDRALS_INC_HYDROGEN", 4, 2, self._parsesection, "_dihe"),
                #("NIMPHI", 4, 2, self._parsesection, "_impr"),
                #("NDON", 2, 4, self._parsesection,"_donors"),
                #("NACC", 2, 4, self._parsesection,"_acceptors"),
        elif formatversion == 10:
            sections = [
                ("ATOM_NAME", 1, 20, self._parseatoms, "_name", 0),
                ("CHARGE", 1, 5, self._parsesection, "_charge", 0),
                ("MASS", 1, 5, self._parsesection, "_mass", 0),
                ("ATOM_TYPE_INDEX", 1, 10, self._parsesectionint, "_atom_type", 0),
                ("NUMBER_EXCLUDED_ATOMS", 1, 10, self._parseskip, "_skip", 8),
                ("NONBONDED_PARM_INDEX", 1, 10, self._parseskip, "_skip", 8),
                ("RESIDUE_LABEL", 1, 20, self._parseatoms, "_resname", 11),
                ("RESIDUE_POINTER", 2, 10, self._parsesectionint, "_respoint", 11),
            ]
                #("BOND_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
                #("BOND_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
                #("ANGLE_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
                #("ANGLE_EQUIL_VALUE", 1, 5, self._parseskip,"_skip",8),
                #("DIHEDRAL_FORCE_CONSTANT", 1, 5, self._parseskip,"_skip",8),
                #("DIHEDRAL_PERIODICITY", 1, 5, self._parseskip,"_skip",8),
                #("DIHEDRAL_PHASE", 1, 5, self._parseskip,"_skip",8),
                #("SOLTY", 1, 5, self._parseskip,"_skip",8),
                #("LENNARD_JONES_ACOEF", 1, 5, self._parseskip,"_skip",8),
                #("LENNARD_JONES_BCOEF", 1, 5, self._parseskip,"_skip",8),
                #("BONDS_INC_HYDROGEN", 2, 4, self._parsebond, "_bonds",2),
                #("ANGLES_INC_HYDROGEN", 3, 3, self._parsesection, "_angles"),
                #("DIHEDRALS_INC_HYDROGEN", 4, 2, self._parsesection, "_dihe")]
                #("NIMPHI", 4, 2, self._parsesection, "_impr"),
                #("NDON", 2, 4, self._parsesection,"_donors"),
                #("NACC", 2, 4, self._parsesection,"_acceptors")]

        # Open and check top validity
        # Reading header info POINTERS
        with openany(self.filename) as topfile:
            next_line = topfile.next
            header = next_line()
            if header[:3] != "%VE":
                raise ValueError("{} is not a valid TOP file. %VE Missing in header".format(topfile))
            title = next_line().split()
            if not (title[1] == "TITLE"):
                raise ValueError("{} is not a valid TOP file. 'TITLE' missing in header".format(topfile))
            while header[:14] != '%FLAG POINTERS':
                header = next_line()
            header = next_line()

            topremarks = [next_line().strip() for i in xrange(4)]
            sys_info = [int(k) for i in topremarks for k in i.split()]

            structure = {}
            final_structure = {}

            try:
                for info in sections:
                    self._parse_sec(sys_info, info, next_line,
                                    structure, final_structure)
            except StopIteration:
                raise ValueError("The TOP file didn't contain the minimum"
                                 " required section of ATOM_NAME")
            # Completing info respoint to include all atoms in last resid
            structure["_respoint"].append(sys_info[0])
            structure["_respoint"][-1] = structure["_respoint"][-1] + 1

        atoms = [None, ]*sys_info[0]

        j = 0
        segid = "SYSTEM"
        for i in range(sys_info[0]):
            charge = convert(structure["_charge"][i],
                             'Amber',
                             flags['charge_unit'])
            if structure["_respoint"][j] <= i+1 < structure["_respoint"][j+1]:
                resid = j + 1
                resname = structure["_resname"][j]
            else:
                j += 1
                resid = j + 1
                resname = structure["_resname"][j]
            mass = structure["_mass"][i]
            atomtype = structure["_atom_type"][i]
            atomname = structure["_name"][i]
            #segid = 'SYSTEM'  # does not exist in Amber

            atoms[i] = Atom(i, atomname, atomtype, resname, resid, segid, mass, charge)
        final_structure["_atoms"] = atoms
        final_structure["_numatoms"] = sys_info[0]
        return final_structure
Exemplo n.º 3
0
 def _assert_almost_equal_convert(value, u1, u2, ref):
     assert_almost_equal(units.convert(value, u1, u2), ref,
                         err_msg="Conversion {0} --> {1} failed".format(u1, u2))