def __init__(self, obj1, obj2, distance, force_constant, nb_exclusion=False): """ :param obj1: the object defining center-of-mass 1 :type obj1: :class:`~MMTK.Collections.GroupOfAtoms` :param obj2: the object defining center-of-mass 2 :type obj2: :class:`~MMTK.Collections.GroupOfAtoms` :param distance: the distance between cm 1 and cm2 at which the restraint is zero :type distance: float :param force_constant: the force constant of the restraint term. The functional form of the restraint is force_constant*((cm1-cm2).length()-distance)**2, where cm1 and cm2 are the centrer-of-mass positions of the two objects. :type force_constant: float :param nb_exclussion: if True, non-bonded interactions between the restrained atoms are suppressed, as for a chemical bond :type nb_exclussion: bool """ if isinstance(obj1, int) and isinstance(obj2, int): # Older MMTK versions admitted only single atoms and # stored single indices. Support this mode for opening # trajectories made with those versions self.atom_indices_1 = [obj1] self.atom_indices_2 = [obj2] if isChemicalObject(obj1) or isCollection(obj1): obj1 = obj1.atomList() if isChemicalObject(obj2) or isCollection(obj2): obj2 = obj2.atomList() self.atom_indices_1 = self.getAtomParameterIndices(obj1) self.atom_indices_2 = self.getAtomParameterIndices(obj2) if nb_exclusion and (len(self.atom_indices_1) > 1 or len(self.atom_indices_2) > 1): raise ValueError("Non-bonded exclusion possible only " "between single-atom objects") self.arguments = (self.atom_indices_1, self.atom_indices_2, distance, force_constant, nb_exclusion) self.distance = distance self.force_constant = force_constant self.nb_exclusion = nb_exclusion ForceField.__init__(self, 'harmonic distance restraint')
def get_chemical_objects_dict(universe): d = {} for obj in universe.objectList(): if isChemicalObject(obj): d.setdefault(obj.name, []).append(obj) return d
def run_step(self, index): """ Runs a single step of the job.\n :Parameters: #. index (int): The index of the step. :Returns: #. index (int): The index of the step. #. None """ # get the Frame index frameIndex = self.configuration['frames']['value'][index] universe = self.configuration['trajectory']['instance'].universe # The configuration corresponding to this index is set to the universe. universe.setFromTrajectory(self.configuration['trajectory']['instance'], frameIndex) # Case of the first frame. if self._refCoords is None: self._refCoords = universe._realToBoxPointArray(universe.configuration().array) for obj in universe.objectList(): if not isChemicalObject(obj): continue if isinstance(obj,Atom): continue if not obj in self._chemicalObjects: continue atoms = obj.atomList()[:] contiguous_configuration(atoms[0],atoms,self._refCoords) # Case of the other frames. else: currentCoords = universe._realToBoxPointArray(universe.configuration().array) diff = currentCoords - self._refCoords self._refCoords = numpy.where(numpy.abs(diff)>0.5,currentCoords-numpy.round(diff),currentCoords) corrCoords = universe._boxToRealPointArray(self._refCoords) universe.configuration().array = corrCoords # The times corresponding to the running index. t = self.configuration['frames']['time'][index] # Write the step. self.snapshot(data = {'time' : t}) return index, None
def atomindex_to_moleculeindex(universe): d = {} for i,obj in enumerate(universe.objectList()): if isChemicalObject(obj): for at in obj.atomList(): d[at.index] = i return d
def __init__(self, obj1, obj2, distance, force_constant, nb_exclusion=False): """ :param obj1: the object defining center-of-mass 1 :type obj1: :class:`~MMTK.Collections.GroupOfAtoms` :param obj2: the object defining center-of-mass 2 :type obj2: :class:`~MMTK.Collections.GroupOfAtoms` :param distance: the distance between cm 1 and cm2 at which the restraint is zero :type distance: float :param force_constant: the force constant of the restraint term. The functional form of the restraint is force_constant*((cm1-cm2).length()-distance)**2, where cm1 and cm2 are the centrer-of-mass positions of the two objects. :type force_constant: float :param nb_exclussion: if True, non-bonded interactions between the restrained atoms are suppressed, as for a chemical bond :type nb_exclussion: bool """ if isinstance(obj1, int) and isinstance(obj2, int): # Older MMTK versions admitted only single atoms and # stored single indices. Support this mode for opening # trajectories made with those versions self.atom_indices_1 = [obj1] self.atom_indices_2 = [obj2] if isChemicalObject(obj1) or isCollection(obj1): obj1 = obj1.atomList() if isChemicalObject(obj2) or isCollection(obj2): obj2 = obj2.atomList() self.atom_indices_1 = self.getAtomParameterIndices(obj1) self.atom_indices_2 = self.getAtomParameterIndices(obj2) if nb_exclusion and (len(self.atom_indices_1) > 1 or len(self.atom_indices_2) > 1): raise ValueError("Non-bonded exclusion possible only " "between single-atom objects") self.arguments = (self.atom_indices_1, self.atom_indices_2, distance, force_constant, nb_exclusion) self.distance = distance self.force_constant = force_constant self.nb_exclusion = nb_exclusion ForceField.__init__(self, 'harmonic distance restraint')
def get_chemical_objects_size(universe): d = {} for obj in universe.objectList(): if isChemicalObject(obj): if d.has_key(obj.name): continue d[obj.name] = obj.numberOfAtoms() return d
def addObject(self, object): """ Add objects to the collection. :param object: the object(s) to be added. If it is another collection or a list, all of its elements are added """ from MMTK.ChemicalObjects import isChemicalObject if isChemicalObject(object): self.addChemicalObject(object) elif isCollection(object): self.addChemicalObjectList(object.objectList()) elif Utility.isSequenceObject(object): if object and isChemicalObject(object[0]): self.addChemicalObjectList(list(object)) else: for o in object: self.addObject(o) else: raise TypeError('Wrong object type in collection')
def addObject(self, object): """ Add objects to the collection. :param object: the object(s) to be added. If it is another collection or a list, all of its elements are added """ from MMTK.ChemicalObjects import isChemicalObject if isChemicalObject(object): self.addChemicalObject(object) elif isCollection(object): self.addChemicalObjectList(object.objectList()) elif Utility.isSequenceObject(object): if object and isChemicalObject(object[0]): self.addChemicalObjectList(list(object)) else: for o in object: self.addObject(o) else: raise TypeError('Wrong object type in collection')
def get_chemical_objects_number(universe): d = {} for obj in universe.objectList(): if isChemicalObject(obj): if d.has_key(obj.name): d[obj.name] += 1 else: d[obj.name] = 1 return d
def map(self, function): """ Apply a function to all objects in the collection and return the list of the results. If the results are chemical objects, a Collection object is returned instead of a list. :param function: the function to be applied :type function: callable :returns: the list or collection of the results """ from MMTK.ChemicalObjects import isChemicalObject list = [function(o) for o in self.objectList()] if list and isChemicalObject(list[0]): return Collection(list) else: return list
def map(self, function): """ Apply a function to all objects in the collection and return the list of the results. If the results are chemical objects, a Collection object is returned instead of a list. :param function: the function to be applied :type function: callable :returns: the list or collection of the results """ from MMTK.ChemicalObjects import isChemicalObject list = [function(o) for o in self.objectList()] if list and isChemicalObject(list[0]): return Collection(list) else: return list
def __init__(self, obj, center, force_constant): """ :param obj: the object on whose center of mass the force field acts :type obj: :class:`~MMTK.Collections.GroupOfAtoms` :param center: the point to which the atom is attached by the harmonic potential :type center: Scientific.Geometry.Vector :param force_constant: the force constant of the harmonic potential :type force_constant: float """ if isChemicalObject(obj): obj = obj.atomList() self.atom_indices = self.getAtomParameterIndices(obj) self.arguments = (self.atom_indices, center, force_constant) ForceField.__init__(self, 'harmonic_trap') self.center = center self.force_constant = force_constant
def __init__(self, obj, center, force_constant): """ :param obj: the object on whose center of mass the force field acts :type obj: :class:`~MMTK.Collections.GroupOfAtoms` :param center: the point to which the atom is attached by the harmonic potential :type center: Scientific.Geometry.Vector :param force_constant: the force constant of the harmonic potential :type force_constant: float """ if isChemicalObject(obj): obj = obj.atomList() self.atom_indices = self.getAtomParameterIndices(obj) self.arguments = (self.atom_indices, center, force_constant) ForceField.__init__(self, 'harmonic_trap') self.center = center self.force_constant = force_constant
def write(self, object, configuration=None, distance=None): from MMTK.ChemicalObjects import isChemicalObject from MMTK.Universe import InfiniteUniverse if distance is None: try: distance = object.universe().distanceVector except AttributeError: distance = InfiniteUniverse().distanceVector if not isChemicalObject(object): for o in object: self.write(o, configuration, distance) else: for bu in object.bondedUnits(): for a in bu.atomList(): self.writeAtom(a, configuration) if hasattr(bu, 'bonds'): for b in bu.bonds: self.writeBond(b, configuration, distance)
def write(self, object, configuration = None, distance = None): from MMTK.ChemicalObjects import isChemicalObject from MMTK.Universe import InfiniteUniverse if distance is None: try: distance = object.universe().distanceVector except AttributeError: distance = InfiniteUniverse().distanceVector if not isChemicalObject(object): for o in object: self.write(o, configuration, distance) else: for bu in object.bondedUnits(): for a in bu.atomList(): self.writeAtom(a, configuration) if hasattr(bu, 'bonds'): for b in bu.bonds: self.writeBond(b, configuration, distance)
def removeObject(self, object): """ Remove an object or a list or collection of objects from the collection. The object(s) to be removed must be elements of the collection. :param object: the object to be removed, or a list or collection of objects whose elements are to be removed :raises ValueError: if the object is not an element of the collection """ from MMTK.ChemicalObjects import isChemicalObject if isChemicalObject(object): self.removeChemicalObject(object) elif isCollection(object) or Utility.isSequenceObject(object): for o in object: self.removeObject(o) else: raise ValueError('Object not in this collection')
def removeObject(self, object): """ Remove an object or a list or collection of objects from the collection. The object(s) to be removed must be elements of the collection. :param object: the object to be removed, or a list or collection of objects whose elements are to be removed :raises ValueError: if the object is not an element of the collection """ from MMTK.ChemicalObjects import isChemicalObject if isChemicalObject(object): self.removeChemicalObject(object) elif isCollection(object) or Utility.isSequenceObject(object): for o in object: self.removeObject(o) else: raise ValueError('Object not in this collection')
def find_atoms_in_molecule(universe, moleculeName, atomNames, indexes=False): molecules = [] for obj in universe.objectList(): if isChemicalObject(obj): if obj.name == moleculeName: molecules.append(obj) match = [] for mol in molecules: atoms = mol.atomList() names = [at.name for at in mol.atomList()] l = [atoms[names.index(atName)] for atName in atomNames] match.append(l) if indexes is True: match = [[at.index for at in atList] for atList in match] return match
def resolve_undefined_molecules_name(universe): for obj in universe.objectList(): if isChemicalObject(obj): if not obj.name.strip(): obj.name = brute_formula(obj,sep="")