def load_mol2(filename): """Load a TRIPOS mol2 file from disk. Parameters ---------- filename : str Path to the prmtop file on disk. Returns ------- traj : md.Trajectory The resulting topology, as an md.Topology object. Notes ----- This function should work on GAFF and sybyl style MOL2 files, but has been primarily tested on GAFF mol2 files. This function does NOT accept multi-structure MOL2 files!!! The elements are guessed using GAFF atom types or via the atype string. Examples -------- >>> traj = md.load_mol2('mysystem.mol2') """ from mdtraj.core.trajectory import Trajectory from mdtraj.core.topology import Topology atoms, bonds = mol2_to_dataframes(filename) atoms_mdtraj = atoms[["name", "resName"]].copy() atoms_mdtraj["serial"] = atoms.index #Figure out 1 letter element names # IF this is a GAFF mol2, this line should work without issues atoms_mdtraj["element"] = atoms.atype.map(gaff_elements) # If this is a sybyl mol2, there should be NAN (null) values if atoms_mdtraj.element.isnull().any(): # If this is a sybyl mol2, I think this works generally. atoms_mdtraj["element"] = atoms.atype.apply(lambda x: x.strip(".")[0]) atoms_mdtraj["resSeq"] = np.ones(len(atoms), 'int') atoms_mdtraj["chainID"] = np.ones(len(atoms), 'int') if bonds is not None: bonds_mdtraj = bonds[["id0", "id1"]].values offset = bonds_mdtraj.min() # Should this just be 1??? bonds_mdtraj -= offset else: bonds_mdtraj = None top = Topology.from_dataframe(atoms_mdtraj, bonds_mdtraj) xyzlist = np.array([atoms[["x", "y", "z"]].values]) xyzlist /= 10.0 # Convert from angstrom to nanometer traj = Trajectory(xyzlist, top) return traj
def load_mol2(filename): """Load a TRIPOS mol2 file from disk. Parameters ---------- filename : str Path to the prmtop file on disk. Returns ------- traj : md.Trajectory The resulting topology, as an md.Topology object. Notes ----- This function should work on GAFF and sybyl style MOL2 files, but has been primarily tested on GAFF mol2 files. This function does NOT accept multi-structure MOL2 files!!! The elements are guessed using GAFF atom types or via the atype string. Examples -------- >>> traj = md.load_mol2('mysystem.mol2') """ from mdtraj.core.trajectory import Trajectory from mdtraj.core.topology import Topology atoms, bonds = mol2_to_dataframes(filename) atoms_mdtraj = atoms[["name", "resName"]].copy() atoms_mdtraj["serial"] = atoms.index #Figure out 1 letter element names # IF this is a GAFF mol2, this line should work without issues atoms_mdtraj["element"] = atoms.atype.map(gaff_elements) # If this is a sybyl mol2, there should be NAN (null) values if atoms_mdtraj.element.isnull().any(): # If this is a sybyl mol2, I think this works generally. atoms_mdtraj["element"] = atoms.atype.apply(lambda x: x.strip(".")[0]) atoms_mdtraj["resSeq"] = np.ones(len(atoms), 'int') atoms_mdtraj["chainID"] = np.ones(len(atoms), 'int') if bonds is not None: bonds_mdtraj = bonds[["id0", "id1"]].values offset = bonds_mdtraj.min() # Should this just be 1??? bonds_mdtraj -= offset else: bonds_mdtraj = None top = Topology.from_dataframe(atoms_mdtraj, bonds_mdtraj) xyzlist = np.array([atoms[["x", "y", "z"]].values]) xyzlist /= 10.0 # Convert from angstrom to nanometer traj = Trajectory(xyzlist, top) return traj
def load_mol2(filename): """Load a TRIPOS mol2 file from disk. Parameters ---------- filename : str Path to the prmtop file on disk. Returns ------- traj : md.Trajectory The resulting topology, as an md.Topology object. Notes ----- This function should work on GAFF and sybyl style MOL2 files, but has been primarily tested on GAFF mol2 files. This function does NOT accept multi-structure MOL2 files!!! The elements are guessed using GAFF atom types or via the atype string. Examples -------- >>> traj = md.load_mol2('mysystem.mol2') """ from mdtraj.core.trajectory import Trajectory from mdtraj.core.topology import Topology, Single, Double, Triple, Aromatic, Amide atoms, bonds = mol2_to_dataframes(filename) atoms_mdtraj = atoms[["name", "resName"]].copy() atoms_mdtraj["serial"] = atoms.index #Figure out 1 letter element names # IF this is a GAFF mol2, this line should work without issues atoms_mdtraj["element"] = atoms.atype.map(gaff_elements) # If this is a sybyl mol2, there should be NAN (null) values if atoms_mdtraj.element.isnull().any(): # If this is a sybyl mol2, I think this works generally. atoms_mdtraj["element"] = atoms.atype.apply(lambda x: x.strip(".")[0]) atoms_mdtraj["resSeq"] = np.ones(len(atoms), 'int') atoms_mdtraj["chainID"] = np.ones(len(atoms), 'int') bond_type_map = { '1': Single, '2': Double, '3': Triple, 'am': Amide, 'ar': Aromatic } if bonds is not None: bonds_mdtraj = bonds[["id0", "id1"]].values offset = bonds_mdtraj.min() # Should this just be 1??? bonds_mdtraj -= offset # Create the bond augment information n_bonds = bonds_mdtraj.shape[0] bond_augment = np.zeros([n_bonds, 2], dtype=float) # Add bond type information bond_augment[:, 0] = [float(bond_type_map[bond_value]) for bond_value in bonds["bond_type"].values] # Add Bond "order" information, this is not known from Mol2 files bond_augment[:, 1] = [0.0 for _ in range(n_bonds)] # Augment array, dtype is cast to minimal representation of float bonds_mdtraj = np.append(bonds_mdtraj, bond_augment, axis=-1) else: bonds_mdtraj = None top = Topology.from_dataframe(atoms_mdtraj, bonds_mdtraj) xyzlist = np.array([atoms[["x", "y", "z"]].values]) xyzlist /= 10.0 # Convert from angstrom to nanometer traj = Trajectory(xyzlist, top) return traj
def load_mol2(filename): """Load a TRIPOS mol2 file from disk. Parameters ---------- filename : str Path to the prmtop file on disk. Returns ------- traj : md.Trajectory The resulting topology, as an md.Topology object. Notes ----- This function should work on GAFF and sybyl style MOL2 files, but has been primarily tested on GAFF mol2 files. This function does NOT accept multi-structure MOL2 files!!! The elements are guessed using GAFF atom types or via the atype string. Examples -------- >>> traj = md.load_mol2('mysystem.mol2') """ from mdtraj.core.trajectory import Trajectory from mdtraj.core.topology import Topology, Single, Double, Triple, Aromatic, Amide atoms, bonds = mol2_to_dataframes(filename) atoms_mdtraj = atoms[["name", "resName"]].copy() atoms_mdtraj["serial"] = atoms.index #Figure out 1 letter element names # IF this is a GAFF mol2, this line should work without issues atoms_mdtraj["element"] = atoms.atype.map(gaff_elements) # If this is a sybyl mol2, there should be NAN (null) values if atoms_mdtraj.element.isnull().any(): # If this is a sybyl mol2, I think this works generally. # Argument x is being passed as a list with only one element. def to_element(x): if isinstance(x, (list, tuple)): assert len(x) == 1 x = x[0] if '.' in x: # orbital-hybridizations in SYBL return x.split('.')[0] try: # check if we can convert the whole str to an Element, # if not, we only pass the first letter. from mdtraj.core.element import Element Element.getBySymbol(x) except KeyError: return to_element(x[0]) return x atoms_mdtraj["element"] = atoms.atype.apply(to_element) atoms_mdtraj["resSeq"] = np.ones(len(atoms), 'int') atoms_mdtraj["chainID"] = np.ones(len(atoms), 'int') bond_type_map = { '1': Single, '2': Double, '3': Triple, 'am': Amide, 'ar': Aromatic } if bonds is not None: bonds_mdtraj = bonds[["id0", "id1"]].values offset = bonds_mdtraj.min() # Should this just be 1??? bonds_mdtraj -= offset # Create the bond augment information n_bonds = bonds_mdtraj.shape[0] bond_augment = np.zeros([n_bonds, 2], dtype=float) # Add bond type information bond_augment[:, 0] = [float(bond_type_map[str(bond_value)]) for bond_value in bonds["bond_type"].values] # Add Bond "order" information, this is not known from Mol2 files bond_augment[:, 1] = [0.0 for _ in range(n_bonds)] # Augment array, dtype is cast to minimal representation of float bonds_mdtraj = np.append(bonds_mdtraj, bond_augment, axis=-1) else: bonds_mdtraj = None top = Topology.from_dataframe(atoms_mdtraj, bonds_mdtraj) xyzlist = np.array([atoms[["x", "y", "z"]].values]) xyzlist /= 10.0 # Convert from angstrom to nanometer traj = Trajectory(xyzlist, top) return traj
def load_mol2(filename): """Load a TRIPOS mol2 file from disk. Parameters ---------- filename : path-like Path to the prmtop file on disk. Returns ------- traj : md.Trajectory The resulting topology, as an md.Topology object. Notes ----- This function should work on GAFF and sybyl style MOL2 files, but has been primarily tested on GAFF mol2 files. This function does NOT accept multi-structure MOL2 files!!! The elements are guessed using GAFF atom types or via the atype string. Examples -------- >>> traj = md.load_mol2('mysystem.mol2') """ from mdtraj.core.trajectory import Trajectory from mdtraj.core.topology import Topology, Single, Double, Triple, Aromatic, Amide atoms, bonds = mol2_to_dataframes(filename) atoms_mdtraj = atoms[["name", "resName"]].copy() atoms_mdtraj["serial"] = atoms.index #Figure out 1 letter element names # IF this is a GAFF mol2, this line should work without issues atoms_mdtraj["element"] = atoms.atype.map(gaff_elements) # If this is a sybyl mol2, there should be NAN (null) values if atoms_mdtraj.element.isnull().any(): # If this is a sybyl mol2, I think this works generally. # Argument x is being passed as a list with only one element. def to_element(x): if isinstance(x, (list, tuple)): assert len(x) == 1 x = x[0] if '.' in x: # orbital-hybridizations in SYBL return x.split('.')[0] try: # check if we can convert the whole str to an Element, # if not, we only pass the first letter. from mdtraj.core.element import Element Element.getBySymbol(x) except KeyError: return x[0] return x atoms_mdtraj["element"] = atoms.atype.apply(to_element) # Check if elements inferred from atoms.atype are valid # If not, try to infer elements from atoms.name try: atoms_mdtraj['element'].apply(elem.get_by_symbol) except KeyError: try: atoms_mdtraj["element"] = atoms.name.apply(to_element) atoms_mdtraj['element'].apply(elem.get_by_symbol) except KeyError: raise KeyError('Invalid element passed to atoms DataFrame') atoms_mdtraj['resSeq'] = atoms['code'] atoms_mdtraj["chainID"] = np.ones(len(atoms), 'int') bond_type_map = { '1': Single, '2': Double, '3': Triple, 'am': Amide, 'ar': Aromatic } if bonds is not None: bonds_mdtraj = bonds[["id0", "id1"]].values offset = bonds_mdtraj.min() # Should this just be 1??? bonds_mdtraj -= offset # Create the bond augment information n_bonds = bonds_mdtraj.shape[0] bond_augment = np.zeros([n_bonds, 2], dtype=float) # Add bond type information bond_augment[:, 0] = [ float(bond_type_map[str(bond_value)]) for bond_value in bonds["bond_type"].values ] # Add Bond "order" information, this is not known from Mol2 files bond_augment[:, 1] = [0.0 for _ in range(n_bonds)] # Augment array, dtype is cast to minimal representation of float bonds_mdtraj = np.append(bonds_mdtraj, bond_augment, axis=-1) else: bonds_mdtraj = None top = Topology.from_dataframe(atoms_mdtraj, bonds_mdtraj) xyzlist = np.array([atoms[["x", "y", "z"]].values]) xyzlist /= 10.0 # Convert from angstrom to nanometer traj = Trajectory(xyzlist, top) return traj