def __unmaskedMatrix(self, contacts, rec_mask, lig_mask): """ Map contacts between selected rec and lig atoms back to all atoms matrix. @param contacts: contact matrix, array sum_rec_mask x sum_lig_mask @type contacts: array @param rec_mask: atom mask @type rec_mask: [1|0] @param lig_mask: atom mask @type lig_mask: [1|0] @return: atom contact matrix, array N_atoms_rec x N_atoms_lig @rtype: array """ l_rec = len(self.rec_model) l_lig = len(self.lig_model) ## map contacts back to all atoms matrix r = N0.zeros(l_rec * l_lig) rMask = N0.ravel(N0.outerproduct(rec_mask, lig_mask)) ## (Optimization: nonzero is time consuming step) N0.put(r, N0.nonzero(rMask), N0.ravel(contacts)) return N0.resize(r, (l_rec, l_lig))
def unpackBinaryMatrix( pcm, raveled=0 ): """ Uncompress array of 0 and 1 that was compressed with :class:`packBinaryMatrix`. :param pcm: {'shape':(X,Y,..), 'nonzero':[int]} :type pcm: dict :param raveled: return raveled (default: 0) :type raveled: 1|0 :return: N0.array(X by Y by ..) of int :rtype: 2D array """ if type( pcm ) != dict: return pcm s = pcm['shape'] m = N0.zeros( N0.cumproduct( s )[-1], N0.Int) pass ## m.savespace( 1 ) N0.put( m, pcm['nonzero'], 1 ) if raveled: return m return N0.reshape( m, s )
def randomMask( nOnes, length ): """ Create random array of given lenght and number of ones. :param nOnes: number of ones :type nOnes: int :param length: lenght of array :type length: int :return: array with ones and zeros :rtype: array( 1|0 ) """ r = N0.zeros( length ) pos = [] ## add random ones for i in range( nOnes ): pos += [ int( random.random() * length ) ] N0.put( r, pos, 1 ) ## if two ones ended up on the same position while nOnes != N0.sum(r): pos = int( random.random() * length ) N0.put( r, pos, 1 ) return r
def unpackBinaryMatrix(pcm, raveled=0): """ Uncompress array of 0 and 1 that was compressed with :class:`packBinaryMatrix`. :param pcm: {'shape':(X,Y,..), 'nonzero':[int]} :type pcm: dict :param raveled: return raveled (default: 0) :type raveled: 1|0 :return: N0.array(X by Y by ..) of int :rtype: 2D array """ if type(pcm) != dict: return pcm s = pcm['shape'] m = N0.zeros(N0.cumproduct(s)[-1], N0.Int) pass ## m.savespace( 1 ) N0.put(m, pcm['nonzero'], 1) if raveled: return m return N0.reshape(m, s)
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 randomMask(nOnes, length): """ Create random array of given lenght and number of ones. :param nOnes: number of ones :type nOnes: int :param length: lenght of array :type length: int :return: array with ones and zeros :rtype: array( 1|0 ) """ r = N0.zeros(length) pos = [] ## add random ones for i in range(nOnes): pos += [int(random.random() * length)] N0.put(r, pos, 1) ## if two ones ended up on the same position while nOnes != N0.sum(r): pos = int(random.random() * length) N0.put(r, pos, 1) return r
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 __unmaskedMatrix( self, contacts, rec_mask, lig_mask ): """ Map contacts between selected rec and lig atoms back to all atoms matrix. @param contacts: contact matrix, array sum_rec_mask x sum_lig_mask @type contacts: array @param rec_mask: atom mask @type rec_mask: [1|0] @param lig_mask: atom mask @type lig_mask: [1|0] @return: atom contact matrix, array N_atoms_rec x N_atoms_lig @rtype: array """ l_rec = len( self.rec_model ) l_lig = len( self.lig_model ) ## map contacts back to all atoms matrix r = N0.zeros( l_rec * l_lig ) rMask = N0.ravel( N0.outerproduct( rec_mask, lig_mask ) ) ## (Optimization: nonzero is time consuming step) N0.put( r, N0.nonzero( rMask ), N0.ravel( contacts ) ) return N0.resize( r, (l_rec, l_lig))
def blockFit( self, ref=None, mask=None ): """ RMSD-fit the average of each member trajectory (i.e. the trajectory en block) onto the overall average (default) or a given structure. :param ref: reference structure (default: average structure) :type ref: PDBModel :param mask: atoms to consider (default: None, all heavy) :type mask: [1|0] OR None """ ref = ref or self.avgModel() for m in range( self.n_members ): indices = self.memberIndices( m ) ## get a copy of this member's Trajectory traj = self.takeFrames( indices ) m_avg = traj.avgModel() r, t = m_avg.transformation( ref, mask ) traj.transform( r, t ) ## replace original frames of this member N0.put( self.frames, indices, traj.frames )
def __inverseIndices( self, model, i_atoms ): """ :param model: model :type model: PDBMode :param i_atoms: atom index :type i_atoms: [int] :return: remaining atom indices of m that are NOT in i_atoms :rtype: [int] """ mask = N0.zeros( len( model ),N0.Int ) N0.put( mask, i_atoms, 1 ) return N0.nonzero( N0.logical_not( mask ) )
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 memberMask( self, member ): """ Get mask for all frames belonging to a given ensemble member. :param member: member index starting with 0 :type member: int :return: member mask, N0.array( N_frames x 1) of 1||0 :rtype: [1|0] """ result = N0.zeros( self.lenFrames() ) if isinstance( member, (int, N0.integer)): N0.put( result, self.memberIndices( member ), 1 ) if type( member ) == list: for m in member: N0.put( result, self.memberIndices( m ), 1 ) return result