def add_parm(self, parm, name=None): """ Add a parm to the list """ # Make sure this parm is not part of the list already if isinstance(parm, string_types): name = parm if name in self._parm_names: raise DuplicateParm('%s already in ParmList' % name) try: parm = load_file(name, structure=True) except FormatNotFound: raise ParmError('Could not determine file type of %s' % name) if not isinstance(parm, Structure): raise ParmError('Added parm must be Structure or a subclass') elif not isinstance(parm, Structure): raise ParmError('Added parm must be Structure or a subclass') else: name = name or str(parm) if name in self._parm_names: raise DuplicateParm('%s already in ParmList' % parm) # Otherwise, add in the new parm's name self._parm_names.append(name) self._parm_instances.append(parm) # A newly added topology file is the currently active parm self.current_index = len(self._parm_instances) - 1 self.parm = parm
def __init__(self, fname, seq=None): super(XyzFile, self).__init__() if isinstance(fname, string_types): fxyz = genopen(fname, 'r') own_handle_xyz = True else: fxyz = fname own_handle_xyz = False if seq is not None: seqstruct = load_file(seq) # Now parse the file try: natom = int(fxyz.readline().split()[0]) except (ValueError, IndexError): raise TinkerError('Bad XYZ file format; first line') if seq is not None and natom != len(seqstruct.atoms): raise ValueError( 'Sequence file %s # of atoms does not match the # ' 'of atoms in the XYZ file' % seq) words = fxyz.readline().split() if len(words) == 6 and not XyzFile._check_atom_record(words): self.box = [float(w) for w in words] words = fxyz.readline().split() atom = Atom(atomic_number=AtomicNum[element_by_name(words[1])], name=words[1], type=words[5]) atom.xx, atom.xy, atom.xz = [float(w) for w in words[2:5]] residue = Residue('SYS') residue.number = 1 residue._idx = 0 if seq is not None: residue = seqstruct.residues[0] self.add_atom(atom, residue.name, residue.number, residue.chain, residue.insertion_code, residue.segid) bond_ids = [[int(w) for w in words[6:]]] for i, line in enumerate(fxyz): words = line.split() atom = Atom(atomic_number=AtomicNum[element_by_name(words[1])], name=words[1], type=words[5]) atom.xx, atom.xy, atom.xz = [float(w) for w in words[2:5]] if seq is not None: residue = seqstruct.atoms[i + 1].residue self.add_atom(atom, residue.name, residue.number, residue.chain, residue.insertion_code, residue.segid) bond_ids.append([int(w) for w in words[6:]]) # All of the bonds are stored now -- go ahead and make them now for atom, bonds in zip(self.atoms, bond_ids): i = atom.idx + 1 for idx in bonds: if idx > i: self.bonds.append(Bond(atom, self.atoms[idx - 1])) if own_handle_xyz: fxyz.close()
def _amber_to_gromacs(self): # Load prmtop and inpcrd as a Structure parmstruct = load_file(self.outprefix + ".prmtop", xyz=self.outprefix + ".inpcrd", structure=True) # Save .gro coordinate file GromacsGroFile.write(struct=parmstruct, dest=self.outprefix + ".gro") # Save .top topology and parameter file grotop = GromacsTopologyFile.from_structure(parmstruct, copy=False) grotop.write(dest=self.outprefix + ".top", parameters="inline")
def __init__(self, fname, seq=None): super(XyzFile, self).__init__() if isinstance(fname, string_types): fxyz = genopen(fname, 'r') own_handle_xyz = True else: fxyz = fname own_handle_xyz = False if seq is not None: seqstruct = load_file(seq) # Now parse the file try: natom = int(fxyz.readline().split()[0]) except (ValueError, IndexError): raise TinkerError('Bad XYZ file format; first line') if seq is not None and natom != len(seqstruct.atoms): raise ValueError('Sequence file %s # of atoms does not match the # ' 'of atoms in the XYZ file' % seq) words = fxyz.readline().split() if len(words) == 6 and not XyzFile._check_atom_record(words): self.box = [float(w) for w in words] words = fxyz.readline().split() atom = Atom(atomic_number=AtomicNum[element_by_name(words[1])], name=words[1], type=words[5]) atom.xx, atom.xy, atom.xz = [float(w) for w in words[2:5]] residue = Residue('SYS') residue.number = 1 residue._idx = 0 if seq is not None: residue = seqstruct.residues[0] self.add_atom(atom, residue.name, residue.number, residue.chain, residue.insertion_code, residue.segid) bond_ids = [[int(w) for w in words[6:]]] for i, line in enumerate(fxyz): words = line.split() atom = Atom(atomic_number=AtomicNum[element_by_name(words[1])], name=words[1], type=words[5]) atom.xx, atom.xy, atom.xz = [float(w) for w in words[2:5]] if seq is not None: residue = seqstruct.atoms[i+1].residue self.add_atom(atom, residue.name, residue.number, residue.chain, residue.insertion_code, residue.segid) bond_ids.append([int(w) for w in words[6:]]) # All of the bonds are stored now -- go ahead and make them now for atom, bonds in zip(self.atoms, bond_ids): i = atom.idx + 1 for idx in bonds: if idx > i: self.bonds.append(Bond(atom, self.atoms[idx-1])) if own_handle_xyz: fxyz.close()
def _prmtop_to_charmm(self): """ Converts an AMBER prmtop with AMBER parameters to a psf file, using ParmEd. """ # Save PSF topology and parameter file parmstruct = load_file(self.outprefix + ".prmtop", xyz=self.outprefix + ".inpcrd", structure=True) parmstruct.save(self.outprefix + ".psf", format="psf") # Save PDB file with coordinates m = molecule.load("parm7", self.outprefix + ".prmtop", "rst7", self.outprefix + ".inpcrd") atomsel("all", m).write("pdb", self.outprefix + ".pdb") molecule.delete(m)
def test_gromacs_amber(tmpdir): from dabble.param import GromacsWriter p = str(tmpdir) molid = molecule.load("mae", os.path.join(dir, "prepped.mae")) w = GromacsWriter(molid, tmp_dir=p, forcefield="amber", extra_topos=[ os.path.join(dir, "glx.off"), os.path.join(dir, "lyx.off") ], extra_params=[ os.path.join(dir, "join.frcmod"), os.path.join(dir, "analogies.frcmod") ], override_defaults=False) w.write(os.path.join(p, "test")) # Load and check the output file m2 = molecule.load("gro", os.path.join(p, "test.gro")) molecule.set_top(m2) # Check the two custom residues are present assert len(atomsel("resname GLX")) == 7 assert len(atomsel("resname LYX")) == 20 # Check that the isopeptide bond is there lybonds = [] for x in atomsel("resname LYX").bonds: lybonds.extend(x) assert any(x in lybonds for x in atomsel("resname GLX").index) # Check the parameterized file with parmed API from parmed.formats.registry import load_file f = load_file(os.path.join(p, "test.top")) assert f.residues[153].name == "GLX" assert "n2" in [_.type for _ in f.residues[153].atoms] assert "n2" not in [_.type for _ in f.residues[152].atoms] assert "N" in [_.type for _ in f.residues[152].atoms]