Exemplo n.º 1
0
    def test_delphiCharges2( self ):
        """
        PDB2DelphiCharges test
        """
        if self.local:
            T.errWrite( 'loading PDB...' )

        self.m1 = self.MODEL or PDBModel( T.testRoot( 'lig/1A19_dry.model' ) )
        Test.MODEL = self.m1
        if self.local:
            T.errWriteln( 'Done.' )
        
        if self.local:
            T.errWrite( 'Adding hydrogens to model (reduce)...' )

        self.rmodel = Reduce( self.m1, verbose=self.local ).run()
        self.rmodel.xplor2amber()
        if self.local:
            T.errWriteln( 'Done.' )

        ac = AtomCharger()
        ac.charge(self.rmodel)
        self.rmodel.addChainFromSegid()
        
        self.dc = PDB2DelphiCharges( self.rmodel )
        self.dc.prepare()
        
        self.assertEqual( len(self.dc.resmap['LYS']), 2 )  # normal and N'
        self.assertEqual( len(self.dc.resmap['SER']), 2 )  # normal and C'
        
        if self.local:
            T.errWriteln( 'writing delphi charge file to %s' % self.fcrg )
        self.dc.tofile( self.fcrg )
        
        self.assertTrue( os.path.exists( self.fcrg ) )
Exemplo n.º 2
0
    def test_delphiCharges2(self):
        """
        PDB2DelphiCharges test
        """
        if self.local:
            T.errWrite('loading PDB...')

        self.m1 = self.MODEL or PDBModel(T.testRoot('lig/1A19_dry.model'))
        Test.MODEL = self.m1
        if self.local:
            T.errWriteln('Done.')

        if self.local:
            T.errWrite('Adding hydrogens to model (reduce)...')

        self.rmodel = Reduce(self.m1, verbose=self.local).run()
        self.rmodel.xplor2amber()
        if self.local:
            T.errWriteln('Done.')

        ac = AtomCharger()
        ac.charge(self.rmodel)
        self.rmodel.addChainFromSegid()

        self.dc = PDB2DelphiCharges(self.rmodel)
        self.dc.prepare()

        self.assertEqual(len(self.dc.resmap['LYS']), 2)  # normal and N'
        self.assertEqual(len(self.dc.resmap['SER']), 2)  # normal and C'

        if self.local:
            T.errWriteln('writing delphi charge file to %s' % self.fcrg)
        self.dc.tofile(self.fcrg)

        self.assertTrue(os.path.exists(self.fcrg))
Exemplo n.º 3
0
    def isFailed( self ):
        """
        Overrides Executor method
        """
        if not os.path.exists(self.f_out_name):
            T.errWrite( '\nSurfaceRacer result file %s does not exist. You have probably encountered a very rare SurfaceRacer round off error that have caused the program to terminate. Will now try to recalculate the surface with a slightly increased surface probe radii: increasing radii from %.3f to %.3f.\n'%(self.f_out_name, self.probe,self.probe+0.001))
            return 1

        return self.error is None 
Exemplo n.º 4
0
    def isFailed(self):
        """
        Overrides Executor method
        """
        if not os.path.exists(self.f_out_name):
            T.errWrite(
                '\nSurfaceRacer result file %s does not exist. You have probably encountered a very rare SurfaceRacer round off error that have caused the program to terminate. Will now try to recalculate the surface with a slightly increased surface probe radii: increasing radii from %.3f to %.3f.\n'
                % (self.f_out_name, self.probe, self.probe + 0.001))
            return 1

        return self.error is None
Exemplo n.º 5
0
    def __collectFrames( self, pdbs, castAll=0 ):
        """
        Read coordinates from list of pdb files.

        :param pdbs: list of file names
        :type  pdbs: [str]
        :param castAll: analyze atom content of each frame for casting
                        (default: 0)
        :type  castAll: 0|1

        :return: frames x (N x 3) Numpy array (of float)
        :rtype: array
        """
        frameList = []
        i = 0
        atomCast = None

        if self.verbose: T.errWrite('reading %i pdbs...' % len(pdbs) )

        refNames = self.ref.atomNames()  ## cache for atom checking

        for f in pdbs:

            ## Load
            m = PDBModel(f)

            ## compare atom order & content of first frame to reference pdb
            if castAll or i==0:
                atomCast, castRef = m.compareAtoms( self.ref )

                if castRef != list(range( len( self.ref ))):
                    ## we can take away atoms from each frame but not from ref
                    raise TrajError("Reference PDB doesn't match %s."
                                    %m.fileName)

                if N0.all( atomCast == list(range( len( m ))) ):
                    atomCast = None   ## no casting necessary
                else:
                    if self.verbose: T.errWrite(' casting ')

            ## assert that frame fits reference
            if atomCast:
                m = m.take( atomCast )

            ## additional check on each 100st frame
            if i%100 == 0 and m.atomNames() != refNames:
                raise TrajError("%s doesn't match reference pdb."%m.fileName )

            frameList.append( m.xyz )

            i += 1
            if i%10 == 0 and self.verbose:
                T.errWrite('#')

        if self.verbose: T.errWrite( 'done\n' )

        ## convert to 3-D Numpy Array
        return N0.array(frameList).astype(N0.Float32)
Exemplo n.º 6
0
    def go(self, errorthreshold, n_iterations=1e10, nstep=10, verbose=1):
        """
        Start the cluestering. Run until the error is below the error
        treshold or the max number of iterations have been run.

        @param errorthreshold: treshold value for error 
        @type  errorthreshold: float
        @param n_iterations: treshold value for number of iterations
                             (default: 1e10)
        @type  n_iterations: int
        @param nstep: print information for every n'th step in the iteration
        @type  nstep: int

        @return: array with cluster centers
        @rtype: array('f')
        """
        iteration = 0
        rel_err = 1e10
        error = 1.e10

        msm = self.create_membership_matrix()
        centers = self.calc_cluster_center(msm)

        while rel_err > errorthreshold and iteration < n_iterations:
            d2, msm, centers = self.iterate(centers)
            old_error = error
            error = self.error(msm, d2)
            rel_err = abs(1. - error / old_error)
            iteration = iteration + 1
            if not iteration % nstep and verbose:
                tools.errWrite("%i %f\n" % (iteration, error))

        self.centers = centers
        self.msm = msm
        self.d2 = d2

        return centers
Exemplo n.º 7
0
    def go(self, errorthreshold, n_iterations=1e10, nstep=10, verbose=1):
        """
        Start the cluestering. Run until the error is below the error
        treshold or the max number of iterations have been run.

        @param errorthreshold: treshold value for error 
        @type  errorthreshold: float
        @param n_iterations: treshold value for number of iterations
                             (default: 1e10)
        @type  n_iterations: int
        @param nstep: print information for every n'th step in the iteration
        @type  nstep: int

        @return: array with cluster centers
        @rtype: array('f')
        """
        iteration = 0
        rel_err = 1e10
        error = 1.e10

        msm = self.create_membership_matrix()
        centers = self.calc_cluster_center(msm)

        while rel_err > errorthreshold and iteration < n_iterations:
            d2, msm, centers = self.iterate(centers)
            old_error = error
            error = self.error(msm, d2)
            rel_err = abs(1. - error/old_error)
            iteration = iteration+1
            if not iteration % nstep and verbose:
                tools.errWrite( "%i %f\n" % (iteration, error) )

        self.centers = centers
        self.msm = msm
        self.d2 = d2

        return centers
Exemplo n.º 8
0
    def fit( self, mask=None, ref=None, n_it=1,
             prof='rms', verbose=1, fit=1, **profInfos ):
        """
        Superimpose all coordinate frames on reference coordinates. Put rms
        values in a profile. If n_it > 1, the fraction of atoms considered
        for the fit is put into a profile called |prof|_considered
        (i.e. by default 'rms_considered').

        :param mask: atom mask, atoms to consider default: [all]
        :type  mask: [1|0]
        :param ref: use as reference, default: None, average Structure
        :type  ref: PDBModel
        :param n_it: number of fit iterations, kicking out outliers on the way
                     1 -> classic single fit, 0 -> until convergence
                     (default: 1)
        :type  n_it: int
        :param prof: save rms per frame in profile of this name, ['rms']
        :type  prof: str
        :param verbose: print progress info to STDERR (default: 1)
        :type  verbose: 1|0
        :param fit: transform frames after match, otherwise just calc rms
                    (default: 1)          
        :type  fit: 1|0
        :param profInfos: additional key=value pairs for rms profile info []
        :type profInfos: key=value
        """
        if ref is None:
            refxyz = N0.average( self.frames, 0 )
        else:
            refxyz = ref.getXyz()

        if mask is None:
            mask = N0.ones( len( refxyz ), N0.Int32 )

        refxyz = N0.compress( mask, refxyz, 0 )

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

        rms = []          ## rms value of each frame
        non_outliers = [] ## fraction of atoms considered for rms and fit
        iterations = []   ## number of iterations performed on each frame

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

            xyz = self.frames[i]

            if n_it != 1:
                (r, t), rmsdList = rmsFit.match( refxyz,
                                                 N0.compress( mask, xyz, 0), n_it)
                iterations.append( len( rmsdList ) )
                non_outliers.append( rmsdList[-1][0] )

                xyz_transformed = N0.dot( xyz, N0.transpose(r)) + t

                rms += [ rmsdList[-1][1] ]

            else:
                r, t = rmsFit.findTransformation( refxyz,
                                                  N0.compress( mask, xyz, 0))

                xyz_transformed = N0.dot( xyz, N0.transpose(r)) + t

                d = N0.sqrt(N0.sum(N0.power( N0.compress(mask, xyz_transformed,0)\
                                             - refxyz, 2), 1))


                rms += [ N0.sqrt( N0.average(d**2) ) ]


            if fit:
                self.frames[i] = xyz_transformed.astype(N0.Float32)

            if verbose and i%100 == 0:
                T.errWrite( '#' )

        self.setProfile( prof, rms, n_iterations=n_it, **profInfos )

        if non_outliers:
            self.setProfile( prof+'_considered', non_outliers,
                             n_iterations=n_it,
                             comment='fraction of atoms considered for iterative fit' )

        if verbose: T.errWrite( 'done\n' )
Exemplo n.º 9
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
Exemplo n.º 10
0
    def parse2new(self, source, ref=None, traj=None):
        """
        Create / Replace Trajectory from the source list of PDBModels or PDBs.
        
        Args:
            source (str): list of file names or PDBModel instances
            ref (str or PDBModel): reference structure instance or file
            traj (Biskit.md.Trajectory): existing instance to be updated

        Returns:
           Biskit.Trajectory: new Trajectory instance
        """
        r = traj
        if traj is None:
            import biskit.md
            r = biskit.md.Trajectory()

        r.setRef(B.PDBModel(ref or source[0]))
        n_frames = len(source)

        if self.rmwat:
            r.ref = r.ref.compress(N.logical_not(r.ref.maskSolvent()))

        r.resIndex = r.ref.resMap()
        refNames = r.ref.atomNames()  ## cache for atom checking

        if self.verbose: T.errWrite('reading %i pdbs...' % n_frames)

        r.frames = N.zeros(
            (n_frames, r.ref.lenAtoms(), 3))  ## target coordinate array
        r.frameNames = ['#%i07' % i for i in range(n_frames)]

        atomCast = None
        reportIntervall = 1 if n_frames < 100 else round(n_frames / 100)

        for i, f in enumerate(source):

            m = B.PDBModel(f)

            ## compare atom order & content of first frame to reference pdb
            if self.analyzeEach or i == 0:
                atomCast, castRef = m.compareAtoms(r.ref)

                if castRef != list(range(r.ref.lenAtoms())):
                    ## we can remove/reorder atoms from each frame but not from ref
                    raise P.TrajParserError("Reference PDB doesn't match %s." %
                                            m.fileName)

                if N.all(atomCast == list(range(len(m)))):
                    atomCast = None  ## no casting necessary
                else:
                    if self.verbose: T.errWrite(' casting ')

            ## assert that frame fits reference
            if atomCast:
                m = m.take(atomCast)

            ## additional check on each 100st frame
            if i % reportIntervall == 0 and m.atomNames() != refNames:
                raise P.TrajParserError("%s doesn't match reference pdb." %
                                        m.fileName)

            r.frames[i] = m.xyz

            if type(f) is str:  ## save original file name
                r.frameNames[i] = T.stripFilename(f)

            if i % reportIntervall == 0 and self.verbose:
                T.errWrite('#')

        if self.verbose: T.errWrite('done\n')
        return r