def histogram(data, nbins, range=None): """ Create a histogram. Comes from Konrad Hinsen: Scientific Python @param data: data list or array @type data: [any] @param nbins: number of bins @type nbins: int @param range: data range to create histogram from (min val, max val) @type range: (float, float) OR None @return: array (2 x len(data) ) with start of bin and witdh of bin. @rtype: array """ data = N0.array(data, N0.Float) if range is None: min = N0.minimum.reduce(data) max = N0.maximum.reduce(data) else: min, max = range data = N0.repeat( data, N0.logical_and(N0.less_equal(data, max), N0.greater_equal(data, min))) bin_width = (max - min) / nbins data = N0.floor((data - min) / bin_width).astype(N0.Int) histo = N0.add.reduce(N0.equal(N0.arange(nbins)[:, N0.NewAxis], data), -1) histo[-1] = histo[-1] + N0.add.reduce(N0.equal(nbins, data)) bins = min + bin_width * (N0.arange(nbins) + 0.5) return N0.transpose(N0.array([bins, histo]))
def residusMaximus( self, atomValues, mask=None ): """ Take list of value per atom, return list where all atoms of any residue are set to the highest value of any atom in that residue. (after applying mask) @param atomValues: list 1 x N, values per atom @type atomValues: [ float ] @param mask: list 1 x N, 0|1, 'master' atoms of each residue @type mask: [1|0] @return: Numpy array 1 x N of float @rtype: array """ if mask is None: mask = N0.ones( len( self.frames[0] ), N0.Int32 ) ## eliminate all values that do not belong to the selected atoms masked = atomValues * mask result = [] ## set all atoms of each residue to uniform value for res in range( 0, self.resMap()[-1]+1 ): ## get atom entries for this residue resAtoms = N0.compress( N0.equal( self.resMap(), res ), masked ) ## get maximum value masterValue = max( resAtoms ) result += resAtoms * 0.0 + masterValue return N0.array( result )
def __setAll_1D(self, a): """ Replace content of this sparseArray with values from Numeric array or list of numbers -- only for 1-dimensional arrays. @param a: array OR list @type a: array OR [ number ] """ if type(a) is list: a = N0.array(a, self.__typecode) if self.shape != a.shape: raise SparseArrayError, 'dimensions not aligned' self.indices = N0.nonzero(N0.logical_not(N0.equal(a, self.__default))) self.indices = self.indices.tolist() self.values = N0.take(a, self.indices) self.values = self.values.tolist()
def __setAll_1D( self, a ): """ Replace content of this sparseArray with values from Numeric array or list of numbers -- only for 1-dimensional arrays. @param a: array OR list @type a: array OR [ number ] """ if type( a ) is list: a = N0.array( a, self.__typecode ) if self.shape != a.shape: raise SparseArrayError, 'dimensions not aligned' self.indices = N0.nonzero( N0.logical_not( N0.equal(a, self.__default) ) ) self.indices = self.indices.tolist() self.values = N0.take( a, self.indices ) self.values = self.values.tolist()
def memberFrames(self, threshold=0.): """ Get indices of all frames belonging to each cluster. Each frame is guaranteed to belong, at least, to the cluster for which it has its maximum membership. If threshold > 0, it can additionally pop up in other clusters. @param threshold: minimal cluster membership or 0 to consider only max membership (default: 0) @type threshold: float @return: n_cluster, lst of lst of int, frame indices @rtype: [[int]] """ ## best cluster for each frame msm = self.memberships() maxMemb = N0.argmax(msm, 0) r = [ N0.nonzero(N0.equal(maxMemb, i)) for i in range(0, self.n_clusters) ] r = [x.tolist() for x in r] ## same thing but now taking all above threshold ## -> same frame can end up in several clusters if threshold > 0.: r2 = [N0.nonzero(N0.greater(l, threshold)) for l in msm] ## add only additional frames for i in range(0, len(r)): try: frames = r[i].tolist() except: frames = r[i] r[i] = frames + [fr for fr in r2[i] if fr not in r[i]] ## sort frames within each cluster by their membership r = [self.membershipSort(r[i], i) for i in range(0, len(r))] return r
def test_Ramachandran(self): """Ramachandran test""" self.traj = T.load(T.testRoot() + '/lig_pcr_00/traj.dat') self.traj.ref.atoms.set('mass', self.traj.ref.masses()) self.mdl = [self.traj[0], self.traj[11]] self.mdl = [md.compress(md.maskProtein()) for md in self.mdl] self.rama = Ramachandran(self.mdl, name='test', profileName='mass', verbose=self.local) self.psi = N0.array(self.rama.psi) if self.local: self.rama.show() r = N0.sum( N0.compress(N0.logical_not(N0.equal(self.psi, None)), self.psi)) self.assertAlmostEqual(r, -11717.909796797909, 2)
def conservationScore(self, cons_type='cons_ent', ranNr=150, log=StdLog(), verbose=1): """ Score of conserved residue pairs in the interaction surface. Optionally, normalized by radom surface contacts. @param cons_type: precalculated conservation profile name, see L{Biskit.PDBDope}. @type cons_type: str @param ranNr: number of random matricies to use (default: 150) @type ranNr: int @param log: log file [STDOUT] @type log: Biskit.LogFile @param verbose: give progress report [1] @type verbose: bool | int @return: conservation score @rtype: float """ try: recCons = self.rec().profile(cons_type, updateMissing=1) except: if verbose: log.add('\n'+'*'*30+'\nNO HHM PROFILE FOR RECEPTOR\n'+\ '*'*30+'\n') recCons = N0.ones(self.rec().lenResidues()) try: ligCons = self.lig().profile(cons_type, updateMissing=1) except: if verbose: log.add(\ '\n'+'*'*30+'\nNO HHM PROFILE FOR LIGAND\n'+'*'*30+'\n') ligCons = N0.ones(self.lig().lenResidues()) if self.rec().profile('surfMask'): recSurf = self.rec().profile('surfMask') else: d = PDBDope(self.rec()) d.addSurfaceMask() if self.lig().profile('surfMask'): ligSurf = self.lig().profile('surfMask') else: d = PDBDope(self.lig()) d.addSurfaceMask() surfMask = N0.ravel(N0.outerproduct(recSurf, ligSurf)) missing = N0.outerproduct(N0.equal(recCons, 0), N0.equal(ligCons, 0)) cont = self.resContacts() * N0.logical_not(missing) consMat = N0.outerproduct(recCons, ligCons) score = cont * consMat # get a random score if ranNr != 0: if self.verbose: self.log.write('.') ranMat = mathUtils.random2DArray(cont, ranNr, mask=surfMask) random_score = N0.sum(N0.sum(ranMat * consMat)) / (ranNr * 1.0) return N0.sum(N0.sum(score)) / random_score else: return N0.sum(N0.sum(score)) / N0.sum(N0.sum(cont))