Example #1
0
 def test_sequenceRepeats(self):
     """match2seq sequence repeat test"""
     seq1 = 'ABCDEFG~~~~~~~~~~~~~~~'
     seq2 = '~~~~~'
     mask1, mask2 = compareSequences(seq1, seq2)
     self.assert_(N.all(mask1 == N0.zeros(len(seq1))))
     self.assert_(N.all(mask2 == N0.zeros(len(seq2))))
Example #2
0
    def randomPatches(self,
                      size,
                      n=None,
                      exclude=None,
                      max_overlap=0,
                      exclude_all=None):
        """
        size - int, number of atoms per patch
        n    - int, number of patches (None -> as many as possible, max 100)
        exclude     - [ 1|0 ], don't touch more than |max_overlap| of these
                      atoms (atom mask)
        max_overlap - int
        exclude_all - [ 1|0 ], don't touch ANY of these atoms
        -> [ [ 1|0 ] ], list of atom masks
        """
        if exclude is None:
            exclude = N0.zeros(self.model.lenAtoms(), 'i')

        if exclude_all is None:
            exclude_all = N0.zeros(self.model.lenAtoms(), 'i')

        n = n or 500

        centers = self.random_translations(n=n, center=self.center)

        ## start from excluded patch (if given) working outwards
        origin = centers[0]

        tabu = exclude_all
        if not N0.any(tabu):
            tabu = exclude
        else:
            origin = self.model.center(mask=tabu)

        centers = self.orderCenters(centers, origin)

        r = []

        for i in range(n):

            m = self.patchAround(centers[i], size)

            if N0.sum( m * exclude ) <= max_overlap \
               and N0.sum( m * exclude_all ) == 0:

                exclude = exclude + m
                r += [m]

        return r
Example #3
0
    def __atom2residueMatrix(self, m):
        """
        Reduce binary matrix of n x k atoms to binary matrix of i x j residues.
        
        @param m: atom contact matrix,
                  array n x k with 1(contact) or 0(no contact)
        @type  m: array
        
        @return: residue contact matrix,
                 2-D numpy array(residues_receptor x residues_ligand)
        @rtype: array
        """
        recInd = N0.concatenate(
            (self.rec().resIndex(), [self.rec().lenAtoms()]))
        ligInd = N0.concatenate(
            (self.lig_model.resIndex(), [self.lig_model.lenAtoms()]))

        residueMatrix = N0.zeros((len(recInd) - 1, len(ligInd) - 1), N0.Int)

        for r in range(len(recInd) - 1):

            for l in range(len(ligInd) - 1):

                res2res = m[int(recInd[r]):int(recInd[r + 1]),
                            int(ligInd[l]):int(ligInd[l + 1])]

                if N0.any(res2res):
                    residueMatrix[r, l] = 1

        return residueMatrix
Example #4
0
    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))
Example #5
0
    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
Example #6
0
    def pairwiseRmsd( self, aMask=None, noFit=0 ):
        """
        Calculate rmsd between each 2 coordinate frames.

        @param aMask: atom mask
        @type  aMask: [1|0]
        @return: frames x frames array of float
        @rtype: array
        """
        frames = self.frames

        if aMask is not None:
            frames = N0.compress( aMask, frames, 1 )

        result = N0.zeros( (len( frames ), len( frames )), N0.Float32 )

        for i in range(0, len( frames ) ):

            for j in range( i+1, len( frames ) ):
                if noFit:
                    d = N0.sqrt(N0.sum(N0.power(frames[i]-frames[j], 2), 1))
                    result[i,j] = result[j,i] = N0.sqrt( N0.average(d**2) )

                else:
                    rt, rmsdLst = rmsFit.match( frames[i], frames[j], 1 )
                    result[i,j] = result[j,i] = rmsdLst[0][1]

        return result
Example #7
0
    def randomPatches( self, size, n=None, exclude=None,
                       max_overlap=0, exclude_all=None ):
        """
        size - int, number of atoms per patch
        n    - int, number of patches (None -> as many as possible, max 100)
        exclude     - [ 1|0 ], don't touch more than |max_overlap| of these
                      atoms (atom mask)
        max_overlap - int
        exclude_all - [ 1|0 ], don't touch ANY of these atoms
        -> [ [ 1|0 ] ], list of atom masks
        """
        if exclude is None:
            exclude = N0.zeros( self.model.lenAtoms(), 'i' )

        if exclude_all is None:
            exclude_all = N0.zeros( self.model.lenAtoms(), 'i' )

        n = n or 500

        centers = self.random_translations( n=n, center=self.center )

        ## start from excluded patch (if given) working outwards
        origin = centers[0]

        tabu = exclude_all
        if not N0.any( tabu ):
            tabu = exclude
        else:
            origin = self.model.center( mask=tabu )

        centers = self.orderCenters( centers, origin )

        r = []

        for i in range(n):

            m = self.patchAround( centers[i], size )

            if N0.sum( m * exclude ) <= max_overlap \
               and N0.sum( m * exclude_all ) == 0:

                exclude = exclude + m
                r += [ m ]

        return r
Example #8
0
    def patchAround( self, center, nAtoms ):
        """
        patchAround( float_center, int_nAtoms ) -> mask for self.model
        Create single patch of nAtoms atoms that are closest to center.
        """
        dist = self.__distances( center )
        order = N0.argsort( dist )

        r = N0.zeros( len( self.model ), 'i' )
        N0.put( r, order[:nAtoms], 1 )

        return self.centerPatch( r )
Example #9
0
    def patchAround(self, center, nAtoms):
        """
        patchAround( float_center, int_nAtoms ) -> mask for self.model
        Create single patch of nAtoms atoms that are closest to center.
        """
        dist = self.__distances(center)
        order = N0.argsort(dist)

        r = N0.zeros(len(self.model), 'i')
        N0.put(r, order[:nAtoms], 1)

        return self.centerPatch(r)
Example #10
0
    def getResult( self, mirror=0 ):
        """
        Get result matrix ordered such as input trajectory.

        @param mirror: mirror the matrix at diagonal (default: 1)
                       (only for intra-traj)
        @type  mirror: 1|0

        @return: array( (n_frames, n_frames), 'f'), matrix of pairwise rms
        @rtype: array
        """
        if self.verbose:   self.log.write('assembling result matrix...')

        intra_traj = self.traj_2 is None

        n1 = n2 = len( self.traj_1 )
        if self.traj_2 is not None:
            n2 = len( self.traj_2 )

        a  = N0.zeros( (n1,n2), N0.Float32 )

        if self.verbose: self.log.write('#')

        for key, value in self.result.items():
            i_start, i_stop = key[0]
            j_start, j_stop = key[1]

            window = N0.reshape( value, (i_stop-i_start, j_stop-j_start) )
            window = window.astype(N0.Float32)

            a[i_start:i_stop, j_start:j_stop] = window

        if self.verbose: self.log.write('#')

        if intra_traj:
            for i in range( N0.shape(a)[0] ):
                for j in range( i, N0.shape(a)[1] ):
                    if a[j,i] == 0:
                        a[j,i] = a[i,j]
                    else:
                        a[i,j] = a[j,i]

        if self.verbose: self.log.write('#')

        if intra_traj and not mirror:
            for i in range( N0.shape(a)[0] ):
                for j in range( i, N0.shape(a)[1] ):
                    a[j,i] = 0.

        if self.verbose:   self.log.add('done')

        return a
Example #11
0
    def getResult(self, mirror=0):
        """
        Get result matrix ordered such as input trajectory.

        @param mirror: mirror the matrix at diagonal (default: 1)
                       (only for intra-traj)
        @type  mirror: 1|0

        @return: array( (n_frames, n_frames), 'f'), matrix of pairwise rms
        @rtype: array
        """
        if self.verbose: self.log.write('assembling result matrix...')

        intra_traj = self.traj_2 is None

        n1 = n2 = len(self.traj_1)
        if self.traj_2 is not None:
            n2 = len(self.traj_2)

        a = N0.zeros((n1, n2), N0.Float32)

        if self.verbose: self.log.write('#')

        for key, value in self.result.items():
            i_start, i_stop = key[0]
            j_start, j_stop = key[1]

            window = N0.reshape(value, (i_stop - i_start, j_stop - j_start))
            window = window.astype(N0.Float32)

            a[i_start:i_stop, j_start:j_stop] = window

        if self.verbose: self.log.write('#')

        if intra_traj:
            for i in range(N0.shape(a)[0]):
                for j in range(i, N0.shape(a)[1]):
                    if a[j, i] == 0:
                        a[j, i] = a[i, j]
                    else:
                        a[i, j] = a[j, i]

        if self.verbose: self.log.write('#')

        if intra_traj and not mirror:
            for i in range(N0.shape(a)[0]):
                for j in range(i, N0.shape(a)[1]):
                    a[j, i] = 0.

        if self.verbose: self.log.add('done')

        return a
Example #12
0
def compareSequences(seqAA_1, seqAA_2):
    """
    """
    seqAA_1 = list(seqAA_1)
    seqAA_2 = list(seqAA_2)
    seqNr_1 = range(len(seqAA_1))
    seqNr_2 = range(len(seqAA_2))

    # get mask
    mask_1 = N0.zeros(len(seqNr_1))
    mask_2 = N0.zeros(len(seqNr_2))

    # compare sequences
    seqDiff = getOpCodes(seqAA_1, seqAA_2)

    # get delete lists
    del_1, del_2 = getSkipLists(seqDiff)

    del_1 = [expandRepeats(seqAA_1, *pos) for pos in del_1]
    del_2 = [expandRepeats(seqAA_2, *pos) for pos in del_2]

    mask1 = del2mask(seqAA_1, *del_1)
    mask2 = del2mask(seqAA_2, *del_2)

    seqAA_1 = N0.compress(mask1, seqAA_1).tolist()
    seqNr_1 = N0.compress(mask1, seqNr_1).tolist()
    seqAA_2 = N0.compress(mask2, seqAA_2).tolist()
    seqNr_2 = N0.compress(mask2, seqNr_2).tolist()

    # get equal parts
    seqDiff = getOpCodes(seqAA_1, seqAA_2)
    equal_1, equal_2 = getEqualLists(seqDiff)
    seqAA_1, seqNr_1 = getEqual(seqAA_1, seqNr_1, equal_1)
    seqAA_2, seqNr_2 = getEqual(seqAA_2, seqNr_2, equal_2)

    N0.put(mask_1, seqNr_1, 1)
    N0.put(mask_2, seqNr_2, 1)

    return mask_1, mask_2
Example #13
0
 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))
Example #14
0
    def test_SparseArray(self):
        """SparseArray test"""

        a = N0.zeros( (6,), N0.Float32 )

        self.sa = SparseArray( a.shape )
        self.sa[3] = 1.
        self.sa[5] = 2.

        b = N0.zeros( (5, 6), N0.Float32 )
        b[0,1] = 3.
        b[0,2] = 4
        b[4,2] = 5
        b[3,0] = 6

        self.sb = SparseArray( b )

        self.sb.append( self.sa )

        if self.local:
            print self.sa.toarray()

        self.assert_( N.all( self.sb.toarray() == self.EXPECTED) )
Example #15
0
    def test_SparseArray(self):
        """SparseArray test"""

        a = N0.zeros((6, ), N0.Float32)

        self.sa = SparseArray(a.shape)
        self.sa[3] = 1.
        self.sa[5] = 2.

        b = N0.zeros((5, 6), N0.Float32)
        b[0, 1] = 3.
        b[0, 2] = 4
        b[4, 2] = 5
        b[3, 0] = 6

        self.sb = SparseArray(b)

        self.sb.append(self.sa)

        if self.local:
            print self.sa.toarray()

        self.assert_(N.all(self.sb.toarray() == self.EXPECTED))
Example #16
0
    def centerPatch(self, patch_mask):
        """
        patch_mask - [ 1|0 ], mask of non-centered patch
        -> [ 1|0 ], mask of patch around geometric center of first patch
        """
        c = self.model.center(patch_mask)
        dist = self.__distances(c)

        n_atoms = len(N0.nonzero(patch_mask))
        i_dist = N0.argsort(dist)[:n_atoms]

        result = N0.zeros(len(patch_mask))
        N0.put(result, i_dist, 1)

        return result
Example #17
0
    def centerPatch( self, patch_mask ):
        """
        patch_mask - [ 1|0 ], mask of non-centered patch
        -> [ 1|0 ], mask of patch around geometric center of first patch
        """
        c    = self.model.center( patch_mask )
        dist = self.__distances( c )

        n_atoms= len( N0.nonzero( patch_mask ) )
        i_dist = N0.argsort( dist )[:n_atoms]

        result = N0.zeros( len( patch_mask ) )
        N0.put( result, i_dist, 1 )

        return result
Example #18
0
    def toarray(self):
        """
        Reconstruct dense array::
          L.toarray() -> numpy.array, normal dense array

        @return: normal dense array
        @rtype: array
        """
        if self.default() is 0:
            a = N0.zeros((self.shape), self.typecode())
        else:
            a = N0.ones((self.shape), self.typecode()) * self.default()

        N0.put(a, self.nondefault(), self.nondefault_values())

        return a
Example #19
0
    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.assert_(self.p is not None)
Example #20
0
    def toarray( self ):
        """
        Reconstruct dense array::
          L.toarray() -> numpy.array, normal dense array

        @return: normal dense array
        @rtype: array
        """
        if self.default() is 0:
            a = N0.zeros( ( self.shape ), self.typecode() )
        else:
            a = N0.ones( (self.shape ), self.typecode() ) * self.default()

        N0.put( a, self.nondefault(), self.nondefault_values() )

        return a
Example #21
0
    def random_contacts( self, contMat, n, maskRec=None, maskLig=None ):
        """
        Create randomized surface contact matrix with same number of
        contacts and same shape as given contact matrix.
        
        @param contMat: template contact matrix
        @type  contMat: matrix
        @param n: number of matrices to generate
        @type  n: int
        @param maskRec: surface masks (or something similar)
        @type  maskRec: [1|0]
        @param maskLig: surface masks (or something similar)
        @type  maskLig: [1|0]
        
        @return: list of [n] random contact matricies
        @rtype: [matrix]
        """
        a,b = N0.shape( contMat )
        nContacts = N0.sum( N0.sum( contMat ))

        if not maskLig:
            r_size, l_size = N0.shape( contMat )
            maskLig = N0.ones( l_size )
            maskRec = N0.ones( r_size )

        c_mask = N0.ravel( N0.outerproduct( maskRec, maskLig ) )
        c_pos = N0.nonzero( c_mask )

        # get array with surface positions from complex
        cont = N0.take( N0.ravel(contMat), c_pos )
        length = len( cont )

        result = []

        for i in range( n ):
            # create random array
            ranCont = mathUtils.randomMask( nContacts,length )

            # blow up to size of original matrix
            r = N0.zeros(a*b)
            N0.put( r, c_pos, ranCont)

            result += [ N0.reshape( r, (a,b) ) ]

        return result
Example #22
0
    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, types.IntType):
            N0.put(result, self.memberIndices(member), 1)

        if type(member) == types.ListType:
            for m in member:
                N0.put(result, self.memberIndices(m), 1)

        return result
Example #23
0
    def calcRmsd(self, window, f1, f2):
        """
        Calulate the rmsd between two frame chunks.
        
        @param window: start and end of two frame chunks within the
                       whole trajectory
        @type  window: ((int, int),(int,int))
        @param f1: frame chunk
        @type  f1: array
        @param f2: frame chunk
        @type  f2: array
        
        @return: the rms between the frames
        @rtype: [float]
        """
        try:
            i_start, i_stop = window[0]
            j_start, j_stop = window[1]

            a = N0.zeros((i_stop - i_start, j_stop - j_start), N0.Float)

            i = j = -1

            ## block on the diagonal, only calculate one half of it
            S = (self.only_off_diagonal and window[0] == window[1])

            for i in range(i_start, i_stop):
                for j in range(S * i - S * j_start + j_start, j_stop):

                    if self.requested(i, j):

                        rt, rmsdLst = rmsFit.match(f1[i - i_start],
                                                   f2[j - j_start], 1)
                        a[i - i_start, j - j_start] = rmsdLst[0][1]

            return N0.ravel(a).tolist()

        except Exception, why:
            self.reportError('ERROR ' + str(why), (i, j))
            return
Example #24
0
    def calcRmsd( self, window, f1, f2 ):
        """
        Calulate the rmsd between two frame chunks.
        
        @param window: start and end of two frame chunks within the
                       whole trajectory
        @type  window: ((int, int),(int,int))
        @param f1: frame chunk
        @type  f1: array
        @param f2: frame chunk
        @type  f2: array
        
        @return: the rms between the frames
        @rtype: [float]
        """
        try:
            i_start, i_stop = window[0]
            j_start, j_stop = window[1]

            a = N0.zeros( (i_stop-i_start, j_stop-j_start), N0.Float )

            i = j = -1

            ## block on the diagonal, only calculate one half of it
            S = (self.only_off_diagonal and window[0] == window[1])

            for i in range( i_start, i_stop ):
                for j in range( S * i - S * j_start + j_start, j_stop ):

                    if self.requested( i, j ):

                        rt, rmsdLst = rmsFit.match( f1[i-i_start],
                                                    f2[j-j_start], 1 )
                        a[i-i_start,j-j_start] = rmsdLst[0][1]

            return N0.ravel(a).tolist()

        except Exception, why:
            self.reportError( 'ERROR '+str(why), (i,j) )
            return
Example #25
0
    def memberMap(self, traj):
        """
        Tell which traj frame belongs to which member trajectory.

        @param traj: Trajectory
        @type  traj: Trajectory

        @return: member index of each frame OR None if traj is
                 not a EnsembleTraj
        @rtype: [ int ] OR None
        """
        if not isinstance( traj, EnsembleTraj ):
            return None

        r = N0.zeros( len(traj), N0.Int )

        for i in range( traj.n_members ):

            mi = traj.memberIndices( i )
            N0.put( r, mi, i )

        return r.tolist()
Example #26
0
    def test_FlexMaster(self):
        """TrajFlexMaster test"""
        from Biskit.MatrixPlot import MatrixPlot
        from numpy.random.mtrand import random_sample as random

        assert len(hosts.cpus_all) > 0,\
               'Master requires at least 1 PVM node for initialisation.'

        traj_1 = T.load(T.testRoot() + '/lig_pcr_00/traj.dat')
        traj_1 = traj2ensemble(traj_1)

        ## create fake second trajectory by adding
        ## increasing noise to first
        frames = []
        for i in range(len(traj_1)):
            f = traj_1.frames[i]
            d = N0.zeros(N0.shape(f), N0.Float32)
            if i > 0:
                d = random(N0.shape(f)) * ((i / 10) + 1)
            frames += [f + d]

        traj_2 = traj_1.clone()
        traj_2.frames = frames

        master = TrajFlexMaster(traj_1,
                                traj_2,
                                hosts=hosts.cpus_all,
                                show_output=self.local,
                                add_hosts=1,
                                log=None,
                                slaveLog=None,
                                verbose=self.local,
                                only_cross_member=0)

        r = master.calculateResult(mirror=0)

        if self.local:
            p = MatrixPlot(r, palette='plasma2', legend=1)
            p.show()
Example #27
0
    def memberMap(self, traj):
        """
        Tell which traj frame belongs to which member trajectory.

        @param traj: Trajectory
        @type  traj: Trajectory

        @return: member index of each frame OR None if traj is
                 not a EnsembleTraj
        @rtype: [ int ] OR None
        """
        if not isinstance(traj, EnsembleTraj):
            return None

        r = N0.zeros(len(traj), N0.Int)

        for i in range(traj.n_members):

            mi = traj.memberIndices(i)
            N0.put(r, mi, i)

        return r.tolist()
Example #28
0
    def test_FlexMaster(self):
        """TrajFlexMaster test"""
        from Biskit.MatrixPlot import MatrixPlot
        from numpy.random.mtrand import random_sample as random

        assert len(hosts.cpus_all) > 0,\
               'Master requires at least 1 PVM node for initialisation.'

        traj_1 = T.load( T.testRoot() + '/lig_pcr_00/traj.dat' )
        traj_1 = traj2ensemble( traj_1 )

        ## create fake second trajectory by adding
        ## increasing noise to first
        frames = []
        for i in range( len( traj_1 ) ):
            f = traj_1.frames[i]
            d = N0.zeros( N0.shape( f ), N0.Float32)
            if i > 0:
                d = random( N0.shape( f ) ) * ((i / 10) + 1) 
            frames += [f + d]

        traj_2 = traj_1.clone()
        traj_2.frames = frames

        master = TrajFlexMaster( traj_1, traj_2,
                                 hosts=hosts.cpus_all,
                                 show_output= self.local,
                                 add_hosts=1,
                                 log=None,
                                 slaveLog=None,
                                 verbose= self.local,
                                 only_cross_member=0 )

        r = master.calculateResult( mirror=0 )

        if self.local:
            p = MatrixPlot( r, palette='plasma2', legend=1 )
            p.show()            
Example #29
0
    def getFluct_local( self, mask=None, border_res=1,
                        left_atoms=['C'], right_atoms=['N'], verbose=1 ):
        """
        Get mean displacement of each atom from it's average position after
        fitting of each residue to the reference backbone coordinates of itself
        and selected atoms of neighboring residues to the right and left.

        @param mask: N_atoms x 1 array of 0||1, atoms for which fluctuation
                     should be calculated
        @type  mask: array
        @param border_res: number of neighboring residues to use for fitting
        @type  border_res: int
        @param left_atoms: atoms (names) to use from these neighbore residues
        @type  left_atoms: [str]
        @param right_atoms: atoms (names) to use from these neighbore residues
        @type  right_atoms: [str]

        @return: Numpy array ( N_unmasked x 1 ) of float
        @rtype: array
        """
        if mask is None:
            mask = N0.ones( len( self.frames[0] ), N0.Int32 )

        if verbose: T.errWrite( "rmsd fitting per residue..." )

        residues = N0.nonzero( self.ref.atom2resMask( mask ) )

        ## backbone atoms used for fit
        fit_atoms_right = N0.nonzero( self.ref.mask( right_atoms ) )
        fit_atoms_left  = N0.nonzero( self.ref.mask( left_atoms ) )
        ## chain index of each residue
        rchainMap = N0.take( self.ref.chainMap(), self.ref.resIndex() )

        result = []

        for res in residues:

            i_res, i_border = self.__resWindow(res, border_res, rchainMap,
                                               fit_atoms_left, fit_atoms_right)

            try:
                if not len( i_res ): raise PDBError, 'empty residue'

                t_res = self.takeAtoms( i_res + i_border )

                i_center = range( len( i_res ) )

                mask_BB = t_res.ref.maskBB() * t_res.ref.maskHeavy()

                ## fit with border atoms ..
                t_res.fit( ref=t_res.ref, mask=mask_BB, verbose=0 )
                ## .. but calculate only with center residue atoms
                frames = N0.take( t_res.frames, i_center, 1 )

                avg = N0.average( frames )

                rmsd = N0.average(N0.sqrt(N0.sum(N0.power(frames - avg, 2), 2) ))

                result.extend( rmsd )

                if verbose: T.errWrite('#')

            except ZeroDivisionError:
                result.extend( N0.zeros( len(i_res), N0.Float32 ) )
                T.errWrite('?' + str( res ))

        if verbose: T.errWriteln( "done" )

        return result
Example #30
0
    def _removeDuplicateChains(self, chainMask=None):
        """
        Get rid of identical chains by comparing all chains with Blast2seq.

        @param chainMask: chain mask for overriding the
                          chain identity checking (default: None)
        @type  chainMask: [int]
        
        @return: number of chains removed
        @rtype: int
        """
        chainCount = len(self.chains)
        matrix = 1.0 * N0.zeros((chainCount, chainCount))
        chain_ids = []

        ## create identity matrix for all chains against all chains
        for i in range(0, chainCount):
            chain_ids = chain_ids + [self.chains[i].chain_id
                                     ]  # collect for log file
            for j in range(i, len(self.chains)):

                # convert 3-letter-code res list into 1-letter-code String
                seq1 = singleAA(self.chains[i].sequence())
                seq2 = singleAA(self.chains[j].sequence())

                ##                 if len(seq1) > len(seq2):           # take shorter sequence
                ##                 # aln len at least half the len of the shortest sequence
                ##                     alnCutoff = len(seq2) * 0.5
                ##                 else:
                ##                     alnCutoff = len(seq1) * 0.5
                ##                 if id['aln_len'] > alnCutoff:
                ##                     matrix[i,j] = id['aln_id']
                ##                 else:                           # aln length too short, ignore
                ##                     matrix[i,j] = 0

                matrix[i, j] = self._compareSequences(seq1, seq2)

        ## report activity
        self.log.add("\n  Chain ID's of compared chains: " + str(chain_ids))
        self.log.add("  Cross-Identity between chains:\n" + str(matrix))
        self.log.add("  Identity threshold used: " + str(self.threshold))

        ## override the automatic chain deletion by supplying a
        ## chain mask to this function
        if chainMask:
            if len(chainMask) == chainCount:
                self.chains = N0.compress(chainMask, self.chains)
                self.log.add(
                    "NOTE: chain mask %s used for removing chains.\n" %
                    chainMask)

            else:
                self.log.add("########## ERROR ###############")
                self.log.add("# Chain mask is only %i chains long" %
                             len(chainMask))
                self.log.add("# when a mask of length %i is needed" %
                             chainCount)
                self.log.add("# No cleaning will be performed.\n")

        if not chainMask:
            ## look at diagonals in "identity matrix"
            ## (each chain against each)
            duplicate = len(self.chains)
            for offset in range(1, chainCount):
                diag = N0.diagonal(matrix, offset, 0, 1)
                # diagonal of 1's mark begin of duplicate
                avg = 1.0 * N0.sum(diag) / len(diag)
                if (avg >= self.threshold):
                    duplicate = offset
                    break
            self.chains = self.chains[:duplicate]
            self.log.add(
                "NOTE: Identity matrix will be used for removing identical chains."
            )

        ## report activit
        self.log.add(str(chainCount - len(self.chains))+\
                     " chains have been removed.\n")

        # how many chains have been removed?
        return (chainCount - len(self.chains))
Example #31
0
    def go(self, model_list=None, reference=None):
        """
        Run benchmarking.

        @param model_list: list of models
                           (default: None S{->} outFolder/L{F_PDBModels})
        @type  model_list: ModelList
        @param reference: reference model
                        (default: None S{->} outFolder/L{F_INPUT_REFERENCE})
        @type  reference: PDBModel
        """
        model_list = model_list or self.outFolder + self.F_PDBModels
        reference = reference or self.outFolder + self.F_INPUT_REFERENCE

        pdb_list = T.load('%s' % model_list)
        reference = PDBModel(reference)

        # check with python 2.4
        iref, imodel = reference.compareAtoms(pdb_list[0])

        mask_casting = N0.zeros(len(pdb_list[0]))
        N0.put(mask_casting, imodel, 1)

        reference = reference.take(iref)
        #reference_mask_CA = reference_rmsd.maskCA()

        atom_mask = N0.zeros(len(pdb_list[0]))
        N0.put(atom_mask, imodel, 1)

        rmask = pdb_list[0].profile2mask("n_templates", 1, 1000)
        amask = pdb_list[0].res2atomMask(rmask)

        mask_final_ref = N0.compress(mask_casting, amask)
        mask_final = mask_casting * amask

        reference = reference.compress(mask_final_ref)

        for i in range(len(pdb_list)):

            #self.cad(reference, pdb_list[i])

            pdb_list[i], pdb_wo_if = self.output_fittedStructures(\
                pdb_list[i], reference, i, mask_final)

            fitted_model_if = pdb_list[i].compress(mask_final)
            fitted_model_wo_if = pdb_wo_if.compress(mask_final)

            coord1 = reference.getXyz()
            coord2 = fitted_model_if.getXyz()

            aprofile = self.rmsd_res(coord1, coord2)

            self.calc_rmsd(fitted_model_if, fitted_model_wo_if, reference,
                           pdb_list[i])

            pdb_list[i].atoms.set('rmsd2ref_if',
                                  aprofile,
                                  mask=mask_final,
                                  default=-1,
                                  comment="rmsd to known reference structure")

        self.output_rmsd_aa(pdb_list)
        self.output_rmsd_ca(pdb_list)
        self.output_rmsd_res(pdb_list)

        self.write_PDBModels(pdb_list)
Example #32
0
    def go(self, model_list = None, reference = None):
        """
        Run benchmarking.

        @param model_list: list of models
                           (default: None S{->} outFolder/L{F_PDBModels})
        @type  model_list: ModelList
        @param reference: reference model
                        (default: None S{->} outFolder/L{F_INPUT_REFERENCE})
        @type  reference: PDBModel
        """
        model_list = model_list or self.outFolder + self.F_PDBModels
        reference = reference or self.outFolder + self.F_INPUT_REFERENCE

        pdb_list = T.load('%s'%model_list)
        reference = PDBModel(reference)

        # check with python 2.4
        iref, imodel = reference.compareAtoms(pdb_list[0])

        mask_casting = N0.zeros(len(pdb_list[0]))
        N0.put(mask_casting, imodel, 1)

        reference = reference.take(iref)
        #reference_mask_CA = reference_rmsd.maskCA()

        atom_mask = N0.zeros(len(pdb_list[0]))
        N0.put(atom_mask,imodel,1)

        rmask = pdb_list[0].profile2mask("n_templates", 1,1000)
        amask = pdb_list[0].res2atomMask(rmask)

        mask_final_ref = N0.compress(mask_casting, amask)
        mask_final = mask_casting * amask

        reference = reference.compress(mask_final_ref)

        for i in range(len(pdb_list)):

            #self.cad(reference, pdb_list[i])

            pdb_list[i], pdb_wo_if = self.output_fittedStructures(\
                pdb_list[i], reference, i, mask_final)

            fitted_model_if = pdb_list[i].compress(mask_final)
            fitted_model_wo_if = pdb_wo_if.compress(mask_final)

            coord1 = reference.getXyz()
            coord2 = fitted_model_if.getXyz()

            aprofile = self.rmsd_res(coord1,coord2)

            self.calc_rmsd(fitted_model_if, fitted_model_wo_if,
                           reference, pdb_list[i])

            pdb_list[i].atoms.set('rmsd2ref_if', aprofile,
                                  mask=mask_final, default = -1,
                                  comment="rmsd to known reference structure")

        self.output_rmsd_aa(pdb_list)
        self.output_rmsd_ca(pdb_list)
        self.output_rmsd_res(pdb_list)

        self.write_PDBModels(pdb_list)