def loadResContacts( self ): """ Uncompress residue contact matrix if necessary. @return: dict with contact matrix and parameters OR None @rtype: dict OR None """ ## Backwards compatibility if self.contacts is not None and type( self.contacts ) == str: self.contacts = t.load( self.contacts ) EHandler.warning("loading old-style pickled contacts.") return self.contacts ## New, uncompression from list of indices into raveled array if self.contacts is not None and \ len( N0.shape( self.contacts['result'])) == 1: try: lenRec, lenLig = self.contacts['shape'] except: EHandler.warning("uncompressing contacts without shape") lenRec = self.rec().lenResidues() lenLig = self.lig().lenResidues() m = N0.zeros( lenRec * lenLig ) N0.put( m, self.contacts['result'], 1 ) self.contacts['result'] = N0.reshape( m, (lenRec, lenLig) ) return self.contacts
def loadResContacts(self): """ Uncompress residue contact matrix if necessary. @return: dict with contact matrix and parameters OR None @rtype: dict OR None """ ## Backwards compatibility if self.contacts is not None and type(self.contacts) == str: self.contacts = t.load(self.contacts) EHandler.warning("loading old-style pickled contacts.") return self.contacts ## New, uncompression from list of indices into raveled array if self.contacts is not None and \ len( N0.shape( self.contacts['result'])) == 1: try: lenRec, lenLig = self.contacts['shape'] except: EHandler.warning("uncompressing contacts without shape") lenRec = self.rec().lenResidues() lenLig = self.lig().lenResidues() m = N0.zeros(lenRec * lenLig) N0.put(m, self.contacts['result'], 1) self.contacts['result'] = N0.reshape(m, (lenRec, lenLig)) return self.contacts
def __atomContacts(self, cutoff, rec_mask, lig_mask, cache): """ Intermolecular distances below cutoff after applying the two masks. @param cutoff: cutoff for B{atom-atom} contact in \AA @type cutoff: float @param rec_mask: atom mask @type rec_mask: [1|0] @param lig_mask: atom mask @type lig_mask: [1|0] @param cache: cache pairwise atom distance matrix @type cache: 1|0 @return: atom contact matrix, array sum_rec_mask x sum_lig_mask @rtype: array """ ## get atom coordinats as array 3 x all_atoms rec_xyz = self.rec().getXyz() lig_xyz = self.lig().getXyz() ## get pair-wise distances -> atoms_rec x atoms_lig dist = getattr( self, 'pw_dist', None ) if dist is None or \ N0.shape( dist ) != ( N0.sum(rec_mask), N0.sum(lig_mask) ): dist = self.__pairwiseDistances(N0.compress( rec_mask, rec_xyz, 0), N0.compress( lig_mask, lig_xyz, 0) ) if cache: self.pw_dist = dist ## reduce to 1 (distance < cutoff) or 0 -> n_atoms_rec x n_atoms_lig return N0.less( dist, cutoff )
def __atomContacts(self, cutoff, rec_mask, lig_mask, cache): """ Intermolecular distances below cutoff after applying the two masks. @param cutoff: cutoff for B{atom-atom} contact in \AA @type cutoff: float @param rec_mask: atom mask @type rec_mask: [1|0] @param lig_mask: atom mask @type lig_mask: [1|0] @param cache: cache pairwise atom distance matrix @type cache: 1|0 @return: atom contact matrix, array sum_rec_mask x sum_lig_mask @rtype: array """ ## get atom coordinats as array 3 x all_atoms rec_xyz = self.rec().getXyz() lig_xyz = self.lig().getXyz() ## get pair-wise distances -> atoms_rec x atoms_lig dist = getattr(self, 'pw_dist', None) if dist is None or \ N0.shape( dist ) != ( N0.sum(rec_mask), N0.sum(lig_mask) ): dist = self.__pairwiseDistances(N0.compress(rec_mask, rec_xyz, 0), N0.compress(lig_mask, lig_xyz, 0)) if cache: self.pw_dist = dist ## reduce to 1 (distance < cutoff) or 0 -> n_atoms_rec x n_atoms_lig return N0.less(dist, cutoff)
def test_MatrixPlot( self ): """MatrixPlot test""" n = 30 z = N0.zeros((n,n), N0.Float) for i in range(N0.shape(z)[0]): for j in range(N0.shape(z)[1]): z[i,j] = N0.exp(-0.01*((i-n/2)**2+(j-n/2)**2)) self.p = MatrixPlot(z, palette='sausage', legend=1) if self.local or self.VERBOSITY > 2: self.p.show() self.assertTrue( self.p is not None )
def area(curve, start=0.0, stop=1.0): """ Numerically add up the area under the given curve. The curve is a 2-D array or list of tupples. The x-axis is the first column of this array (curve[:,0]). (originally taken from biskit.Statistics.ROCalyzer) :param curve: a list of x,y coordinates :type curve: [ (y,x), ] or N0.array :param start: lower boundary (in x) (default: 0.0) :type start: float :param stop: upper boundary (in x) (default: 1.0) :type stop: float :return: the area underneath the curve between start and stop. :rtype: float """ ## convert and swap axes curve = N0.array(curve) c = N0.zeros(N0.shape(curve), curve.dtype) c[:, 0] = curve[:, 1] c[:, 1] = curve[:, 0] assert len(N0.shape(c)) == 2 ## apply boundaries ## here we have a problem with flat curves mask = N0.greater_equal(c[:, 1], start) mask *= N0.less_equal(c[:, 1], stop) c = N0.compress(mask, c, axis=0) ## fill to boundaries -- not absolutely accurate: we actually should ## interpolate to the neighboring points instead c = N0.concatenate((N0.array([ [c[0, 0], start], ]), c, N0.array([ [c[-1, 0], stop], ]))) x = c[:, 1] y = c[:, 0] dx = x[1:] - x[:-1] # distance on x between points dy = y[1:] - y[:-1] # distance on y between points areas1 = y[:-1] * dx # the rectangles between all points areas2 = dx * dy / 2.0 # the triangles between all points return N0.sum(areas1) + N0.sum(areas2)
def centers( self ): """ Get 'center structure' for each cluster. @return: N0.array( n_clusters x n_atoms_masked x 3 ) @rtype: array """ lenAtoms = N0.shape( self.fcCenters )[1] / 3 return N0.reshape( self.fcCenters, ( self.n_clusters, lenAtoms, 3))
def area(curve, start=0.0, stop=1.0 ): """ Numerically add up the area under the given curve. The curve is a 2-D array or list of tupples. The x-axis is the first column of this array (curve[:,0]). (originally taken from biskit.Statistics.ROCalyzer) :param curve: a list of x,y coordinates :type curve: [ (y,x), ] or N0.array :param start: lower boundary (in x) (default: 0.0) :type start: float :param stop: upper boundary (in x) (default: 1.0) :type stop: float :return: the area underneath the curve between start and stop. :rtype: float """ ## convert and swap axes curve = N0.array( curve ) c = N0.zeros( N0.shape(curve), curve.dtype ) c[:,0] = curve[:,1] c[:,1] = curve[:,0] assert len( N0.shape( c ) ) == 2 ## apply boundaries ## here we have a problem with flat curves mask = N0.greater_equal( c[:,1], start ) mask *= N0.less_equal( c[:,1], stop ) c = N0.compress( mask, c, axis=0 ) ## fill to boundaries -- not absolutely accurate: we actually should ## interpolate to the neighboring points instead c = N0.concatenate((N0.array([[c[0,0], start],]), c, N0.array([[c[-1,0],stop ],])) ) x = c[:,1] y = c[:,0] dx = x[1:] - x[:-1] # distance on x between points dy = y[1:] - y[:-1] # distance on y between points areas1 = y[:-1] * dx # the rectangles between all points areas2 = dx * dy / 2.0 # the triangles between all points return N0.sum(areas1) + N0.sum(areas2)
def slim(self): """ Remove coordinates and atoms of ligand and receptor from memory, if they can be restored from file, compress contact matrix. @note: CALLED BEFORE PICKLING """ self.lig_transformed = None self.pw_dist = None ## self.ligandMatrix = self.ligandMatrix.tolist() if 'matrix' in self.info: del self.info['matrix'] ## compress contact matrix array if self.contacts is not None and \ len(N0.shape( self.contacts['result'] ) )==2: m = self.contacts['result'] self.contacts['shape'] = N0.shape( m ) self.contacts['result'] = N0.nonzero( N0.ravel( m ) ).astype(N0.Int32)
def slim(self): """ Remove coordinates and atoms of ligand and receptor from memory, if they can be restored from file, compress contact matrix. @note: CALLED BEFORE PICKLING """ self.lig_transformed = None self.pw_dist = None ## self.ligandMatrix = self.ligandMatrix.tolist() if 'matrix' in self.info: del self.info['matrix'] ## compress contact matrix array if self.contacts is not None and \ len(N0.shape( self.contacts['result'] ) )==2: m = self.contacts['result'] self.contacts['shape'] = N0.shape(m) self.contacts['result'] = N0.nonzero(N0.ravel(m)).astype(N0.Int32)
def random2DArray( matrix, ranNr=1, mask=None): """ Create randomized 2D array containing ones and zeros. :param matrix: matrix to randomize :type matrix: 2D array :param mask: mask OR None (default: None) :type mask: list(1|0) :param ranNr: number of matricies to add up (default: 1) :type ranNr: integer :return: 2D array or |ranNr| added contact matricies :rtype:2D array :raise MathUtilError: if mask does not fit matrix """ ## get shape of matrix a,b = N0.shape( matrix ) ## get array from matrix that is to be randomized if mask is not None: if len(mask) == len( N0.ravel(matrix) ): array = N0.compress( mask, N0.ravel(matrix) ) if len(mask) != len( N0.ravel(matrix) ): raise MathUtilError( 'MatUtils.random2DArray - mask of incorrect length' + '\tMatrix length: %i Mask length: %i'\ %(len( N0.ravel(matrix) ), len(mask))) if not mask: array = N0.ravel(matrix) ## number of ones and length of array nOnes = int( N0.sum( array ) ) lenArray = len( array ) ranArray = N0.zeros( lenArray ) ## create random array for n in range(ranNr): ranArray += randomMask( nOnes, lenArray ) ## blow up to size of original matix if mask is not None: r = N0.zeros(a*b) N0.put( r, N0.nonzero(mask), ranArray) return N0.reshape( r, (a,b) ) if not mask: return N0.reshape( ranArray, (a,b) )
def random2DArray(matrix, ranNr=1, mask=None): """ Create randomized 2D array containing ones and zeros. :param matrix: matrix to randomize :type matrix: 2D array :param mask: mask OR None (default: None) :type mask: list(1|0) :param ranNr: number of matricies to add up (default: 1) :type ranNr: integer :return: 2D array or |ranNr| added contact matricies :rtype:2D array :raise MathUtilError: if mask does not fit matrix """ ## get shape of matrix a, b = N0.shape(matrix) ## get array from matrix that is to be randomized if mask is not None: if len(mask) == len(N0.ravel(matrix)): array = N0.compress(mask, N0.ravel(matrix)) if len(mask) != len(N0.ravel(matrix)): raise MathUtilError( 'MatUtils.random2DArray - mask of incorrect length' + '\tMatrix length: %i Mask length: %i'\ %(len( N0.ravel(matrix) ), len(mask))) if not mask: array = N0.ravel(matrix) ## number of ones and length of array nOnes = int(N0.sum(array)) lenArray = len(array) ranArray = N0.zeros(lenArray) ## create random array for n in range(ranNr): ranArray += randomMask(nOnes, lenArray) ## blow up to size of original matix if mask is not None: r = N0.zeros(a * b) N0.put(r, N0.nonzero(mask), ranArray) return N0.reshape(r, (a, b)) if not mask: return N0.reshape(ranArray, (a, b))
def ramachandran_background(self): """ Creates a background (favoured regions) for a ramachandran plot. @return: list of biggles.Point objects @rtype: [ biggles.Point ] """ bg = [] mat = N0.loadtxt(T.dataRoot() + '/biggles/ramachandran_bg.dat') x, y = N0.shape(mat) for i in range(x): for j in range(y): if mat[i, j] < 200: a = (360. / y) * j - 180 b = (360. / x) * (x - i) - 180 bg += [biggles.Point(a, b, type="dot")] return bg
def ramachandran_background( self ): """ Creates a background (favoured regions) for a ramachandran plot. @return: list of biggles.Point objects @rtype: [ biggles.Point ] """ bg = [] mat = N0.loadtxt( T.dataRoot() + '/biggles/ramachandran_bg.dat') x, y = N0.shape(mat) for i in range(x): for j in range(y): if mat[i,j] < 200: a = (360./y)*j - 180 b = (360./x)*(x-i)- 180 bg += [ biggles.Point( a, b, type="dot" )] return bg
def packBinaryMatrix( cm ): """ Compress sparse array of 0 and ones to list of one-positions (space saving function, upack with :class:`unpackBinaryMatrix`). :param cm: X by Y array of int :type cm: 2D array :return: {'shape':(X,Y), 'nonzero':[int] } :rtype: dict """ if cm is None or type( cm ) == dict: return cm result = {} result['shape'] = N0.shape( cm ) result['nonzero'] = N0.nonzero( N0.ravel( cm ) ) result['nonzero'] = result['nonzero'].tolist() return result
def color_array( self, a, resetLimits=1 ): """ :param a: array of float :type a: array of float :param resetLimits: re-define color range on max and min of values (default: 1) :type resetLimits: 1|0 :return: matrix of color codes with same dimensions as a :rtype: array of float """ s = N0.shape( a ) v = N0.ravel( a ) r = self.colors( v, resetLimits=resetLimits ) r = N0.reshape( r, s ) return r
def packBinaryMatrix(cm): """ Compress sparse array of 0 and ones to list of one-positions (space saving function, upack with :class:`unpackBinaryMatrix`). :param cm: X by Y array of int :type cm: 2D array :return: {'shape':(X,Y), 'nonzero':[int] } :rtype: dict """ if cm is None or type(cm) == dict: return cm result = {} result['shape'] = N0.shape(cm) result['nonzero'] = N0.nonzero(N0.ravel(cm)) result['nonzero'] = result['nonzero'].tolist() return result
def __init__(self, data, n_cluster, weight, seedx=0, seedy=0): """ @param data: cluster this @type data: [float] OR array @param n_cluster: number of clusters @type n_cluster: int @param weight: fuzziness weigth @type weight: float @param seedx: random seed value for RandomArray.seed (default: 0) @type seedx: int OR 0 @param seedy: random seed value for RandomArray.seed (default: 0, set seed from clock) @type seedy: int OR 0 """ self.data = N0.array(data, N0.Float) self.w = weight self.n_cluster = n_cluster self.npoints, self.dimension = N0.shape(data) self.seedx = seedx self.seedy = seedy
def test_FuzzyCluster( self): """FuzzyCluster test""" import biskit.gnuplot as G x1 = R.random_sample((500,2)) x2 = R.random_sample((500,2)) + 1 x3 = R.random_sample((500,2)) + 2 self.x = N0.concatenate((x1, x2, x3)) self.fuzzy = FuzzyCluster(self.x, n_cluster=5, weight=1.5) self.centers = self.fuzzy.go(1.e-30, n_iterations=50, nstep=10, verbose=self.local) if self.local: print("cluster centers are displayed in green") G.scatter( self.x, self.centers ) self.assertEqual( N0.shape(self.centers), (5, 2) )
def __init__(self, data, n_cluster, weight, seedx = 0, seedy = 0): """ @param data: cluster this @type data: [float] OR array @param n_cluster: number of clusters @type n_cluster: int @param weight: fuzziness weigth @type weight: float @param seedx: random seed value for RandomArray.seed (default: 0) @type seedx: int OR 0 @param seedy: random seed value for RandomArray.seed (default: 0, set seed from clock) @type seedy: int OR 0 """ self.data = N0.array(data, N0.Float) self.w = weight self.n_cluster = n_cluster self.npoints, self.dimension = N0.shape(data) self.seedx = seedx self.seedy = seedy
def matrixToList(cm): """ Convert matrix into standard python list remembering the dimensions. Unpack with :class:`listToMatrix`. Note: Not used right now. :param cm: array of int :type cm: 2D array :return: {'shape':(int,..), 'lst':[..] } :rtype: dict """ if cm is None or type(cm) == dict: return cm result = {} result['shape'] = N0.shape(cm) result['lst'] = N0.ravel(cm).tolist() return result
def matrixToList( cm ): """ Convert matrix into standard python list remembering the dimensions. Unpack with :class:`listToMatrix`. Note: Not used right now. :param cm: array of int :type cm: 2D array :return: {'shape':(int,..), 'lst':[..] } :rtype: dict """ if cm is None or type( cm ) == dict: return cm result = {} result['shape'] = N0.shape( cm ) result['lst'] = N0.ravel( cm ).tolist() return result
def test_FuzzyCluster(self): """FuzzyCluster test""" import biskit.gnuplot as G x1 = R.random_sample((500, 2)) x2 = R.random_sample((500, 2)) + 1 x3 = R.random_sample((500, 2)) + 2 self.x = N0.concatenate((x1, x2, x3)) self.fuzzy = FuzzyCluster(self.x, n_cluster=5, weight=1.5) self.centers = self.fuzzy.go(1.e-30, n_iterations=50, nstep=10, verbose=self.local) if self.local: print("cluster centers are displayed in green") G.scatter(self.x, self.centers) self.assertEqual(N0.shape(self.centers), (5, 2))
def thin( self, step=1 ): """ Keep only each step'th frame from trajectory with 10 ensemble members. :param step: 1..keep all frames, 2..skip first and every second, .. (default: 1) :type step: int :return: reduced EnsembleTraj :rtype: EnsembleTraj """ T.ensure( step, int, forbidden=[0] ) ## 10 x lenFrames/10, frame indices of each member mI = [ self.memberIndices( i ) for i in range(self.n_members) ] mI = N0.array( mI ) mI = N0.take( mI, range( -1, N0.shape( mI )[1], step )[1:], 1 ) mI = N0.transpose( mI ) return self.takeFrames( N0.ravel( mI ))
def test_getXyz( self ): """AmberRstParser.getXyz test""" self.xyz = self.p.getXyz() self.assertEqual( N0.shape(self.xyz), (11200,3) )
def __init__(self, matrix, mesh=0, palette="plasma", legend=0, step=1, vmin=None, vmax=None): """ @param matrix: the 2-D array to plot @type matrix: array @param mesh: create a plot with a dotted mesh @type mesh: 1|0 @param palette: color palette name see L{Biskit.ColorSpectrum} @type palette: str @param legend: create a legend (scale) showing the walues of the different colors in the plot. @type legend: 1|0 @param step: reduce matrix -- take only each step position in x and y @type step: int @param vmin: override minimal value, all values below will revert to default color @return: biggles plot object, view with biggles.FramedPlot.show() or save with biggles.FramedPlot.write_eps(file_name). @rtype: biggles.FramedPlot """ if not biggles: raise ImportError('biggles module could not be imported.') FramedPlot.__init__(self) if step != 1: matrix = self.__thinarray( matrix, step ) if vmin is None: vmin = N0.amin( matrix ) if vmax is None: vmax = N0.amax( matrix ) self.palette = ColorSpectrum( palette, vmin=vmin, vmax=vmax ) self.matrix = self.palette.color_array( matrix, resetLimits=0 ) s = N0.shape( self.matrix ) for i in range(s[0]): for j in range(s[1]): col = self.matrix[i,j] x1 = (j, j + 1) y1 = (i, i) y2 = (i + 1, i + 1) cell = biggles.FillBetween(x1, y1, x1, y2, color = col) self.add(cell) if mesh: for i in range(s[0] + 1): self.add(biggles.LineY(i, linetype='dotted')) for i in range(s[1] + 1): self.add(biggles.LineX(i, linetype='dotted')) if legend: legend = self.__make_legend() self.add(legend) self.add(biggles.PlotBox((-0.17, -0.1), (1.25, 1.1))) self.aspect_ratio = 1.0
def test_getXyz(self): """AmberRstParser.getXyz test""" self.xyz = self.p.getXyz() self.assertEqual(N0.shape(self.xyz), (11200, 3))
def lenAtoms( self ): """ :return: number of atoms in frames :rtype: int """ return N0.shape( self.frames )[1]