def __init__(self, reference_system, receptor_atoms=[], ligand_atoms=[]): """ Initialize absolute alchemical intermediate factory with reference system. ARGUMENTS reference_system (System) - reference system containing receptor and ligand ligand_atoms (list) - list of atoms to be designated 'ligand' -- everything else in system is considered the 'environment' receptor_atoms (list) - list of atoms to be considered in softening specific 'receptor' degrees of freedom -- shouldn't be the whole receptor, but a subset of atoms in binding site """ # Create pyopenmm System object. self.reference_system = pyopenmm.System(reference_system) # Store copy of atom sets. self.receptor_atoms = copy.deepcopy(receptor_atoms) self.ligand_atoms = copy.deepcopy(ligand_atoms) # Store atom sets self.ligand_atomset = Set(self.ligand_atoms) self.receptor_atomset = Set(self.receptor_atoms) # Make sure intersection of ligand and receptor atomsets is null. intersection = Set.intersection(self.ligand_atomset, self.receptor_atomset) if (len(intersection) > 0): raise ParameterException("receptor and ligand atomsets must not overlap.") return
def __init__(self, reference_system, receptor_atoms=[], ligand_atoms=[]): """ Initialize absolute alchemical intermediate factory with reference system. ARGUMENTS reference_system (System) - reference system containing receptor and ligand ligand_atoms (list) - list of atoms to be designated 'ligand' -- everything else in system is considered the 'environment' receptor_atoms (list) - list of atoms to be considered in softening specific 'receptor' degrees of freedom -- shouldn't be the whole receptor, but a subset of atoms in binding site """ # Create pyopenmm System object. self.reference_system = pyopenmm.System(reference_system) # Store copy of atom sets. self.receptor_atoms = copy.deepcopy(receptor_atoms) self.ligand_atoms = copy.deepcopy(ligand_atoms) # Store atom sets self.ligand_atomset = Set(self.ligand_atoms) self.receptor_atomset = Set(self.receptor_atoms) # Make sure intersection of ligand and receptor atomsets is null. intersection = Set.intersection(self.ligand_atomset, self.receptor_atomset) if (len(intersection) > 0): raise ParameterException( "receptor and ligand atomsets must not overlap.") return
def _is_restraint(self, valence_atoms): """ Determine whether specified valence term connects the ligand with its environment. Parameters ---------- valence_atoms : list of int Atom indices involved in valence term (bond, angle or torsion). Returns ------- is_restraint : bool True if the set of atoms includes at least one ligand atom and at least one non-ligand atom; False otherwise Examples -------- Various tests for a simple system. >>> # Create a reference system. >>> from repex import testsystems >>> alanine_dipeptide = testsystems.AlanineDipeptideImplicit() >>> [reference_system, positions] = [alanine_dipeptide.system, alanine_dipeptide.positions] >>> # Create a factory. >>> factory = AbsoluteAlchemicalFactory(reference_system, ligand_atoms=[0, 1, 2]) >>> factory._is_restraint([0,1,2]) False >>> factory._is_restraint([1,2,3]) True >>> factory._is_restraint([3,4]) False >>> factory._is_restraint([2,3,4,5]) True """ valence_atomset = Set(valence_atoms) intersection = Set.intersection(valence_atomset, self.ligand_atomset) if (len(intersection) >= 1) and (len(intersection) < len(valence_atomset)): return True return False
def _is_restraint(self, valence_atoms): """ Determine whether specified valence term connects the ligand with its environment. ARGUMENTS valence_atoms (list of int) - atom indices involved in valence term (bond, angle or torsion) RETURNS True if the set of atoms includes at least one ligand atom and at least one non-ligand atom; False otherwise EXAMPLES Various tests. >>> # Create a reference system. >>> import testsystems >>> [reference_system, coordinates] = testsystems.AlanineDipeptideImplicit() >>> # Create a factory. >>> factory = AbsoluteAlchemicalFactory(reference_system, ligand_atoms=[0, 1, 2]) >>> factory._is_restraint([0,1,2]) False >>> factory._is_restraint([1,2,3]) True >>> factory._is_restraint([3,4]) False >>> factory._is_restraint([2,3,4,5]) True """ valence_atomset = Set(valence_atoms) intersection = Set.intersection(valence_atomset, self.ligand_atomset) if (len(intersection) >= 1) and (len(intersection) < len(valence_atomset)): return True return False
def _is_restraint(self, valence_atoms): """ Determine whether specified valence term connects the ligand with its environment. ARGUMENTS valence_atoms (list of int) - atom indices involved in valence term (bond, angle or torsion) RETURNS True if the set of atoms includes at least one ligand atom and at least one non-ligand atom; False otherwise EXAMPLES Various tests. >>> # Create a reference system. >>> import testsystems >>> [reference_system, coordinates] = testsystems.AlanineDipeptideImplicit() >>> # Create a factory. >>> factory = AbsoluteAlchemicalFactory(reference_system, alchemical_atoms=[0, 1, 2]) >>> factory._is_restraint([0,1,2]) False >>> factory._is_restraint([1,2,3]) True >>> factory._is_restraint([3,4]) False >>> factory._is_restraint([2,3,4,5]) True """ valence_atomset = Set(valence_atoms) intersection = Set.intersection(valence_atomset, self.alchemical_atomset) if (len(intersection) >= 1) and (len(intersection) < len(valence_atomset)): return True return False
def findNeighbors(self) : neighbors = [] nEdges = 0 for i in range(self.numElems()) : allNeighbors = Set() for v in self.elemVerts_[i] : allNeighbors = Set.union(allNeighbors, self.vertToElemMap_[v]) # get rid of self-references allNeighbors.discard(i) fullNeighbors = [] for j in allNeighbors : numCommonNodes = Set.intersection(self.elemVerts_[i], self.elemVerts_[j]) if len(numCommonNodes) == self.dim_ : fullNeighbors.append(j) nEdges = nEdges + len(fullNeighbors) neighbors.append(fullNeighbors) nEdges = nEdges/2 return (neighbors, nEdges)
def findNeighbors(self): neighbors = [] nEdges = 0 for i in range(self.numElems()): allNeighbors = Set() for v in self.elemVerts_[i]: allNeighbors = Set.union(allNeighbors, self.vertToElemMap_[v]) # get rid of self-references allNeighbors.discard(i) fullNeighbors = [] for j in allNeighbors: numCommonNodes = Set.intersection(self.elemVerts_[i], self.elemVerts_[j]) if len(numCommonNodes) == self.dim_: fullNeighbors.append(j) nEdges = nEdges + len(fullNeighbors) neighbors.append(fullNeighbors) nEdges = nEdges / 2 return (neighbors, nEdges)
def findspikes(xin, vin, thresh, t0=None, t1=None, dt=1.0, mode=None, interpolate=False, debug=False): """ findspikes identifies the times of action potential in the trace v, with the times in t. An action potential is simply timed at the first point that exceeds the threshold... or is the peak. 4/1/11 - added peak mode if mode is none or schmitt, we work as in the past. if mode is peak, we return the time of the peak of the AP instead 7/15/11 - added interpolation flag if True, the returned time is interpolated, based on a spline fit if False, the returned time is just taken as the data time. 2012/10/9: Removed masked arrays and forced into ndarray from start (metaarrays were really slow...) """ # if debug: # # this does not work with pyside... # import matplotlib # matplotlib.use('Qt4Agg') # import pylab # from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas # from matplotlib.figure import Figure # # #MP.rcParams['interactive'] = False st = numpy.array([]) spk = [] if xin is None: return (st, spk) xt = xin.view(numpy.ndarray) v = vin.view(numpy.ndarray) if t1 is not None and t0 is not None: it0 = int(t0 / dt) it1 = int(t1 / dt) if not isinstance(xin, numpy.ndarray): xt = xt[it0:it1] v = v[it0:it1] else: xt = xt[it0:it1] v = v[it0:it1] # if debug: # f = pylab.figure(1) # print "xt: ", xt # print "v: ", v # pylab.plot(numpy.array(xt), v, 'k-') # pylab.draw() # pylab.show() dv = numpy.diff(v, axis=0) # compute slope try: dv = numpy.insert(dv, 0, dv[0]) except: pass # print 'dv: ', dv dv /= dt st = numpy.array([]) spk = [] spv = numpy.where(v > thresh)[0].tolist() # find points above threshold sps = numpy.where( dv > 0.0)[0].tolist() # find points where slope is positive sp = list(Set.intersection( Set(spv), Set(sps))) # intersection defines putative spikes sp.sort() # make sure all detected events are in order (sets is unordered) sp = tuple(sp) # convert to tuple if sp is (): return (st, spk) # nothing detected dx = 1 mingap = int(0.0005 / dt) # 0.5 msec between spikes (a little unphysiological...) # normal operating mode is fixed voltage threshold # for this we need to just get the FIRST positive crossing, if mode is 'schmitt': sthra = list(numpy.where(numpy.diff(sp) > mingap)) sthr = [sp[x] for x in sthra[0]] # bump indices by 1 #print 'findspikes: sthr: ', len(sthr), sthr for k in sthr: if k == 0: continue x = xt[k - 1:k + 1] y = v[k - 1:k + 1] if interpolate: dx = 0 m = (y[1] - y[0]) / dt # local slope b = y[0] - (x[0] * m) s0 = (thresh - b) / m else: s0 = x[1] st = numpy.append(st, x[1]) elif mode is 'peak': pkwidth = 1.0e-3 # in same units as dt - usually msec kpkw = int(pkwidth / dt) z = (numpy.array(numpy.where(numpy.diff(spv) > 1)[0]) + 1).tolist() z.insert(0, 0) # first element in spv is needed to get starting AP spk = [] #print 'findspikes peak: ', len(z) for k in z: zk = spv[k] spkp = numpy.argmax(v[zk:zk + kpkw]) + zk # find the peak position x = xt[spkp - 1:spkp + 2] y = v[spkp - 1:spkp + 2] if interpolate: try: # mimic Igor FindPeak routine with B = 1 m1 = (y[1] - y[0]) / dt # local slope to left of peak b1 = y[0] - (x[0] * m1) m2 = (y[2] - y[1]) / dt # local slope to right of peak b2 = y[1] - (x[1] * m2) mprime = ( m2 - m1 ) / dt # find where slope goes to 0 by getting the line bprime = m2 - ((dt / 2.0) * mprime) st = numpy.append(st, -bprime / mprime + x[1]) spk.append(spkp) except: continue else: st = numpy.append(st, x[1]) # always save the first one spk.append(spkp) return (st, spk)
def makeChacoGraphFile(filename) : f = file(filename + '.ele') nodeToEleMap = {} elemVerts = [] # read header while 1 : line = f.readline() if line[0]=='#': continue header = line.split() nElems = int(header[0]) d = int(header[1])-1 break # read lines, building elements and the element-to-node map while 1: line = f.readline() if not line : break if line[0]=='#': continue toks = line.split() ele = int(toks[0]) verts = Set() for i in range(d+1) : node = int(toks[i+1]) verts.add(node) if nodeToEleMap.has_key(node) : nodeToEleMap[node].add(ele) else : nodeToEleMap[node] = Set() nodeToEleMap[node].add(ele) elemVerts.append(verts) # For each node, assign one of the adjoining elements as its "owner." # The node will later be assigned to the same processer as the owner. # The choice of owner is arbitrary; here, we simply choose the # adjoining element having the largest index. # # We write the ownership information to a file, with the format: # line 1: <num nodes> # line 2: <node 1 number> <node 1 owner> # etc. nodeOwnerFile = file(filename + '.owner', 'w') nodeOwnerFile.write('%d\n' % len(nodeToEleMap.keys())) for node in nodeToEleMap.keys() : owner = max(nodeToEleMap[node]) nodeOwnerFile.write('%d %d\n' % (node, owner)) # determine lists of neighbors for each element neighbors = [] nEdges = 0 for i in range(nElems) : allNeighbors = Set() for v in elemVerts[i] : allNeighbors = Set.union(allNeighbors, nodeToEleMap[v]) # get rid of self-references allNeighbors.discard(i) fullNeighbors = [] for j in allNeighbors : numCommonNodes = Set.intersection(elemVerts[i], elemVerts[j]) if len(numCommonNodes) == d : fullNeighbors.append(j) nEdges = nEdges + len(fullNeighbors) neighbors.append(fullNeighbors) nEdges = nEdges/2 graphFile = file(filename + '.graph', 'w') graphFile.write('%d %d\n' % (nElems, nEdges)) for i in range(nElems) : line = '' for j in neighbors[i] : line = line + '%d ' % (j+1) graphFile.write(line + '\n'); graphFile.flush() return (elemVerts, nodeToEleMap)
def findspikes(xin, vin, thresh, t0=None, t1= None, dt=1.0, mode=None, interpolate=False, debug=False): """ findspikes identifies the times of action potential in the trace v, with the times in t. An action potential is simply timed at the first point that exceeds the threshold... or is the peak. 4/1/11 - added peak mode if mode is none or schmitt, we work as in the past. if mode is peak, we return the time of the peak of the AP instead 7/15/11 - added interpolation flag if True, the returned time is interpolated, based on a spline fit if False, the returned time is just taken as the data time. 2012/10/9: Removed masked arrays and forced into ndarray from start (metaarrays were really slow...) """ # if debug: # # this does not work with pyside... # import matplotlib # matplotlib.use('Qt4Agg') # import pylab # from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas # from matplotlib.figure import Figure # # #MP.rcParams['interactive'] = False st=numpy.array([]) spk = [] if xin is None: return(st, spk) xt = xin.view(numpy.ndarray) v = vin.view(numpy.ndarray) if t1 is not None and t0 is not None: it0 = int(t0/dt) it1 = int(t1/dt) if not isinstance(xin, numpy.ndarray): xt = xt[it0:it1] v = v[it0:it1] else: xt = xt[it0:it1] v = v[it0:it1] # if debug: # f = pylab.figure(1) # print "xt: ", xt # print "v: ", v # pylab.plot(numpy.array(xt), v, 'k-') # pylab.draw() # pylab.show() dv = numpy.diff(v, axis=0) # compute slope dv /= dt st=numpy.array([]) spk = [] spv = numpy.where(v > thresh)[0].tolist() # find points above threshold sps = numpy.where(dv > 0.0)[0].tolist() # find points where slope is positive sp = list(Set.intersection(Set(spv),Set(sps))) # intersection defines putative spikes sp.sort() # make sure all detected events are in order (sets is unordered) sp = tuple(sp) # convert to tuple if sp is (): return(st, spk) # nothing detected dx = 1 mingap = int(0.0005/dt) # 0.5 msec between spikes (a little unphysiological...) # normal operating mode is fixed voltage threshold # for this we need to just get the FIRST positive crossing, if mode is 'schmitt': sthra = list(numpy.where(numpy.diff(sp) > mingap)) sthr = [sp[x] for x in sthra[0]] # bump indices by 1 for k in sthr: x = xt[k-1:k+1] y = v[k-1:k+1] if interpolate: dx = 0 m = (y[1]-y[0])/dt # local slope b = y[0]-(x[0]*m) s0 = (thresh-b)/m else: s0 = x[1] st = numpy.append(st, x[1]) elif mode is 'peak': pkwidth = 1.0e-3 # in same units as dt - usually msec kpkw = int(pkwidth/dt) z = (numpy.array(numpy.where(numpy.diff(spv) > 1)[0])+1).tolist() z.insert(0, 0) # first element in spv is needed to get starting AP spk = [] for k in z: zk = spv[k] spkp = numpy.argmax(v[zk:zk+kpkw])+zk # find the peak position x = xt[spkp-1:spkp+2] y = v[spkp-1:spkp+2] if interpolate: try: # mimic Igor FindPeak routine with B = 1 m1 = (y[1]-y[0])/dt # local slope to left of peak b1 = y[0]-(x[0]*m1) m2 = (y[2]-y[1])/dt # local slope to right of peak b2 = y[1]-(x[1]*m2) mprime = (m2-m1)/dt # find where slope goes to 0 by getting the line bprime = m2-((dt/2.0)*mprime) st = numpy.append(st, -bprime/mprime+x[1]) spk.append(spkp) except: continue else: st = numpy.append(st, x[1]) # always save the first one spk.append(spkp) return(st, spk)
def makeChacoGraphFile(filename): f = file(filename + '.ele') nodeToEleMap = {} elemVerts = [] # read header while 1: line = f.readline() if line[0] == '#': continue header = line.split() nElems = int(header[0]) d = int(header[1]) - 1 break # read lines, building elements and the element-to-node map while 1: line = f.readline() if not line: break if line[0] == '#': continue toks = line.split() ele = int(toks[0]) verts = Set() for i in range(d + 1): node = int(toks[i + 1]) verts.add(node) if nodeToEleMap.has_key(node): nodeToEleMap[node].add(ele) else: nodeToEleMap[node] = Set() nodeToEleMap[node].add(ele) elemVerts.append(verts) # For each node, assign one of the adjoining elements as its "owner." # The node will later be assigned to the same processer as the owner. # The choice of owner is arbitrary; here, we simply choose the # adjoining element having the largest index. # # We write the ownership information to a file, with the format: # line 1: <num nodes> # line 2: <node 1 number> <node 1 owner> # etc. nodeOwnerFile = file(filename + '.owner', 'w') nodeOwnerFile.write('%d\n' % len(nodeToEleMap.keys())) for node in nodeToEleMap.keys(): owner = max(nodeToEleMap[node]) nodeOwnerFile.write('%d %d\n' % (node, owner)) # determine lists of neighbors for each element neighbors = [] nEdges = 0 for i in range(nElems): allNeighbors = Set() for v in elemVerts[i]: allNeighbors = Set.union(allNeighbors, nodeToEleMap[v]) # get rid of self-references allNeighbors.discard(i) fullNeighbors = [] for j in allNeighbors: numCommonNodes = Set.intersection(elemVerts[i], elemVerts[j]) if len(numCommonNodes) == d: fullNeighbors.append(j) nEdges = nEdges + len(fullNeighbors) neighbors.append(fullNeighbors) nEdges = nEdges / 2 graphFile = file(filename + '.graph', 'w') graphFile.write('%d %d\n' % (nElems, nEdges)) for i in range(nElems): line = '' for j in neighbors[i]: line = line + '%d ' % (j + 1) graphFile.write(line + '\n') graphFile.flush() return (elemVerts, nodeToEleMap)