def _initialize(self, simulation): """Deferred initialization of the reporter, which happens before processing the first report. At the time that the first report is processed, we now have access to the simulation object, which we don't have at the point when the reporter is instantiated Parameters ---------- simulation : simtk.openmm.app.Simulation The Simulation to generate a report for """ if self._atomSubset is not None: if not min(self._atomSubset) >= 0: raise ValueError('atomSubset must be zero indexed. ' 'the smallest allowable value is zero') if not max(self._atomSubset) < simulation.system.getNumParticles(): raise ValueError( 'atomSubset must be zero indexed. ' 'the largest value must be less than the number ' 'of particles in the system') if not all(a == int(a) for a in self._atomSubset): raise ValueError( 'all of the indices in atomSubset must be integers') self._atomSlice = self._atomSubset if hasattr(self._traj_file, 'topology'): self._traj_file.topology = _topology_from_subset( Topology.from_openmm(simulation.topology), self._atomSubset) else: self._atomSlice = slice(None) if hasattr(self._traj_file, 'topology'): self._traj_file.topology = Topology.from_openmm( simulation.topology) system = simulation.system if self._temperature: # Compute the number of degrees of freedom. dof = 0 for i in range(system.getNumParticles()): if system.getParticleMass(i) > 0 * units.dalton: dof += 3 dof -= system.getNumConstraints() if any( type(system.getForce(i)) == mm.CMMotionRemover for i in range(system.getNumForces())): dof -= 3 self._dof = dof if simulation.topology.getUnitCellDimensions() is None: self._cell = False
def _initialize(self, simulation): """Deferred initialization of the reporter, which happens before processing the first report. At the time that the first report is processed, we now have access to the simulation object, which we don't have at the point when the reporter is instantiated Parameters ---------- simulation : simtk.openmm.app.Simulation The Simulation to generate a report for """ if self._atomSubset is not None: if not min(self._atomSubset) >= 0: raise ValueError('atomSubset must be zero indexed. ' 'the smallest allowable value is zero') if not max(self._atomSubset) < simulation.system.getNumParticles(): raise ValueError('atomSubset must be zero indexed. ' 'the largest value must be less than the number ' 'of particles in the system') if not all(a==int(a) for a in self._atomSubset): raise ValueError('all of the indices in atomSubset must be integers') self._atomSlice = self._atomSubset if hasattr(self._traj_file, 'topology'): self._traj_file.topology = _topology_from_subset( Topology.from_openmm(simulation.topology), self._atomSubset) else: self._atomSlice = slice(None) if hasattr(self._traj_file, 'topology'): self._traj_file.topology = Topology.from_openmm(simulation.topology) system = simulation.system if self._temperature: # Compute the number of degrees of freedom. dof = 0 for i in range(system.getNumParticles()): if system.getParticleMass(i) > 0*units.dalton: dof += 3 dof -= system.getNumConstraints() if any(type(system.getForce(i)) == mm.CMMotionRemover for i in range(system.getNumForces())): dof -= 3 self._dof = dof if simulation.topology.getUnitCellDimensions() is None: self._cell = False
def to_mdtraj_Topology(item, atom_indices='all', check=True): if check: try: is_openmm_Topology(item) except: raise WrongFormError('openmm.Topology') try: atom_indices = digest_atom_indices(atom_indices) except: raise WrongAtomIndicesError() try: from mdtraj.core.topology import Topology as mdtraj_Topology except: raise LibraryNotFoundError('MDTraj') from . import extract tmp_item = extract(item, atom_indices=atom_indices, copy_if_all=False, check=False) tmp_item = mdtraj_Topology.from_openmm(tmp_item) return tmp_item
def to_mdtraj_Topology(item, molecular_system=None, atom_indices='all', structure_indices='all'): from mdtraj.core.topology import Topology tmp_item, tmp_molecular_system = to_openmm_Topology( item, molecular_system=molecular_system, atom_indices=atom_indices, structure_indices=structure_indices) tmp_item = Topology.from_openmm(tmp_item) if tmp_molecular_system is not None: tmp_molecular_system = tmp_molecular_system.combine_with_items( tmp_item) return tmp_item, tmp_molecular_system
def topology(self, topology_object): """Set the topology in the file Parameters ---------- topology_object : mdtraj.Topology A topology object """ # we want to be able to handle the simtk.openmm Topology object # here too, so if it's not an mdtraj topology we'll just guess # that it's probably an openmm topology and convert if not isinstance(topology_object, Topology): topology_object = Topology.from_openmm(topology_object) try: topology_dict = {"chains": [], "bonds": []} for chain in topology_object.chains: chain_dict = {"residues": [], "index": int(chain.index)} for residue in chain.residues: residue_dict = { "index": int(residue.index), "name": str(residue.name), "atoms": [], "resSeq": int(residue.resSeq), } for atom in residue.atoms: try: element_symbol_string = str(atom.element.symbol) except AttributeError: element_symbol_string = "" residue_dict["atoms"].append( {"index": int(atom.index), "name": str(atom.name), "element": element_symbol_string} ) chain_dict["residues"].append(residue_dict) topology_dict["chains"].append(chain_dict) for atom1, atom2 in topology_object.bonds: topology_dict["bonds"].append([int(atom1.index), int(atom2.index)]) except AttributeError as e: raise AttributeError( "topology_object fails to implement the" "chains() -> residue() -> atoms() and bond() protocol. " "Specifically, we encountered the following %s" % e ) # actually set the tables try: self._remove_node(where="/", name="topology") except self.tables.NoSuchNodeError: pass data = json.dumps(topology_dict) if not isinstance(data, bytes): data = data.encode("ascii") if self.tables.__version__ >= "3.0.0": self._handle.create_array(where="/", name="topology", obj=[data]) else: self._handle.createArray(where="/", name="topology", object=[data])
def topology(self, topology_object): """Set the topology in the file Parameters ---------- topology_object : mdtraj.Topology A topology object """ _check_mode(self.mode, ('w', 'a')) # we want to be able to handle the simtk.openmm Topology object # here too, so if it's not an mdtraj topology we'll just guess # that it's probably an openmm topology and convert if not isinstance(topology_object, Topology): topology_object = Topology.from_openmm(topology_object) try: topology_dict = { 'chains': [], 'bonds': [] } for chain in topology_object.chains: chain_dict = { 'residues': [], 'index': int(chain.index) } for residue in chain.residues: residue_dict = { 'index': int(residue.index), 'name': str(residue.name), 'atoms': [], "resSeq": int(residue.resSeq), "segmentID": str(residue.segment_id) } for atom in residue.atoms: try: element_symbol_string = str(atom.element.symbol) except AttributeError: element_symbol_string = "" residue_dict['atoms'].append({ 'index': int(atom.index), 'name': str(atom.name), 'element': element_symbol_string }) chain_dict['residues'].append(residue_dict) topology_dict['chains'].append(chain_dict) for atom1, atom2 in topology_object.bonds: topology_dict['bonds'].append([ int(atom1.index), int(atom2.index) ]) except AttributeError as e: raise AttributeError('topology_object fails to implement the' 'chains() -> residue() -> atoms() and bond() protocol. ' 'Specifically, we encountered the following %s' % e) # actually set the tables try: self._remove_node(where='/', name='topology') except self.tables.NoSuchNodeError: pass data = json.dumps(topology_dict) if not isinstance(data, bytes): data = data.encode('ascii') if self.tables.__version__ >= '3.0.0': self._handle.create_array(where='/', name='topology', obj=[data]) else: self._handle.createArray(where='/', name='topology', object=[data])