本文整理汇总了Python中mdtraj.core.topology.Topology.from_dataframe方法的典型用法代码示例。如果您正苦于以下问题:Python Topology.from_dataframe方法的具体用法?Python Topology.from_dataframe怎么用?Python Topology.from_dataframe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mdtraj.core.topology.Topology
的用法示例。
在下文中一共展示了Topology.from_dataframe方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_mol2
# 需要导入模块: from mdtraj.core.topology import Topology [as 别名]
# 或者: from mdtraj.core.topology.Topology import from_dataframe [as 别名]
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
示例2: load_mol2
# 需要导入模块: from mdtraj.core.topology import Topology [as 别名]
# 或者: from mdtraj.core.topology.Topology import from_dataframe [as 别名]
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