Exemplo n.º 1
0
    def go(self, dict):
        """
        Calculate rmsd values for trajectory and plot them.

        @param dict: dictionary with path to pdb files as keys
        @type  dict: dict

        @return: dictionary mapping input:output names
        @rtype: dict        
        """
        d = {}

        print "working on :",
        for pdbIn, out in dict.items():

            print T.stripFilename( pdbIn )

            ## set file name for pickled Structure object
            dir = os.path.dirname( pdbIn )
            if self.params['out'] <> None:
                dir = self.params['out']

            out = dir + '/' + T.stripFilename( pdbIn) + '.model'

            try:
                m = PDBModel( pdbIn, skipRes=self.params['skipRes'] )

                if self.params['amber']:
                    self.renameAmberRes( m )
                    self.renameToAmberAtoms( m )

                if self.params['sort']:
                    m = m.sort()

                m.saveAs( out )

            except:
                if self.params.has_key('report') and self.params['report']:
                    f = open( out[:-6] + '.error', 'w' )
                    f.write( T.lastError() )
                    f.close()
                else:
                    T.errWriteln("Error" + T.lastError()  )


                out = ''

            d[pdbIn] = out

        print "Done."
        return d
Exemplo n.º 2
0
    def go(self, dict):
        """
        Calculate rmsd values for trajectory and plot them.

        @param dict: dictionary with path to pdb files as keys
        @type  dict: dict

        @return: dictionary mapping input:output names
        @rtype: dict        
        """
        d = {}

        print "working on :",
        for pdbIn, out in dict.items():

            print T.stripFilename(pdbIn)

            ## set file name for pickled Structure object
            dir = os.path.dirname(pdbIn)
            if self.params['out'] <> None:
                dir = self.params['out']

            out = dir + '/' + T.stripFilename(pdbIn) + '.model'

            try:
                m = PDBModel(pdbIn, skipRes=self.params['skipRes'])

                if self.params['amber']:
                    self.renameAmberRes(m)
                    self.renameToAmberAtoms(m)

                if self.params['sort']:
                    m = m.sort()

                m.saveAs(out)

            except:
                if self.params.has_key('report') and self.params['report']:
                    f = open(out[:-6] + '.error', 'w')
                    f.write(T.lastError())
                    f.close()
                else:
                    T.errWriteln("Error" + T.lastError())

                out = ''

            d[pdbIn] = out

        print "Done."
        return d
Exemplo n.º 3
0
    def setResValues( self, model, values, key='temperature_factor',
                      lastOnly=0 ):
        """
        Add numeric value per residue to all atoms of all Structures
        or the last Structure in a model. The values will be written to
        either the B- (temperature_factor) or Q-factor 'occupancy' column
        in the temporary pdb-file.
        These values can then be used to display properties in PyMol
        via commands like 'color_b' and 'color_q'. See also
        L{setAtomValues}.
        
        @param model: model name
        @type  model: str
        @param values: list of numbers, len( values ) == number of residues
        @type  values: [float]      
        @param key: key for Atom.properties dictionary
                    (default: temperature_factor)
        @type  key: occupancy|temperature_factor
        @param lastOnly: 0 - add to all in model OR
                         1 - add only to last Structure (default: 0)
        @type  lastOnly: 1|0
        """
        if lastOnly:
            self.dic[ model ][-1].addResProperty( values, key )

        else:
            for m in self.dic[ model ]:
                try:
                    m.addResProperty( values, key )
                except:
                    T.errWriteln( "Warning: error while adding properties.")
                    T.errWriteln( "Key: "+str( key )+" values: "+str( values ) )
                    T.errWriteln( T.lastError() )
Exemplo n.º 4
0
    def array_or_list(self, prof, asarray):
        """
        Convert to array or list depending on asarray option
        
        Beware: empty lists will be upgraded to empty Float arrays.

        @param prof: profile
        @type  prof: list OR array
        @param asarray: 1.. autodetect type, 0.. force list, 2.. force array
        @type  asarray: 2|1|0
        
        @return: profile
        @rtype: list OR array
        
        @raise ProfileError:
        """
        try:

            ## autodetect type
            if asarray == 1:

                if isinstance(prof, N.ndarray):
                    return self.__picklesave_array(prof)

                if type(prof) is str:  # tolerate strings as profiles
                    return list(prof)

                if len(prof) == 0:  # don't create arrays from empty lists
                    return list(prof)

                p = self.__picklesave_array(N.array(prof))
                if p.dtype.char not in ['O', 'c',
                                        'S']:  ## no char or object arrays!
                    return p

                return list(prof)

            ## force list
            if asarray == 0:

                if isinstance(prof, N.ndarray):
                    return prof.tolist()

                return list(prof)

            ## force array
            if asarray == 2:
                if isinstance(prof, N.ndarray):
                    return self.__picklesave_array(prof)

                return self.__picklesave_array(N.array(prof))

        except TypeError, why:
            ## Numeric bug: N.array(['','','']) raises TypeError
            if asarray == 1 or asarray == 0:
                return list(prof)

            raise ProfileError, "Cannot create array from given list. %r"\
                  % T.lastError()
Exemplo n.º 5
0
    def array_or_list( self, prof, asarray ):
        """
        Convert to array or list depending on asarray option

        @param prof: profile
        @type  prof: list OR array
        @param asarray: 1.. autodetect type, 0.. force list, 2.. force array
        @type  asarray: 2|1|0
        
        @return: profile
        @rtype: list OR array
        
        @raise ProfileError:
        """
        try:

            ## autodetect type
            if asarray == 1:

                if isinstance( prof, N.ndarray ):
                    return self.__picklesave_array( prof )

                if type( prof ) is str:  # tolerate strings as profiles
                    return list( prof )
    
                p = self.__picklesave_array( N.array( prof ) )
                if p.dtype.char not in ['O','c','S']: ## no char or object arrays!
                    return p

                return list( prof )

            ## force list
            if asarray == 0:

                if isinstance( prof, N.ndarray ):
                    return prof.tolist()

                return list( prof )

            ## force array
            if asarray == 2:
                if isinstance( prof, N.ndarray ):
                    return self.__picklesave_array( prof )
                
                return self.__picklesave_array( N.array( prof ) )

        except TypeError, why:
            ## Numeric bug: N.array(['','','']) raises TypeError
            if asarray == 1 or asarray == 0:
                return list( prof )

            raise ProfileError, "Cannot create array from given list. %r"\
                  % T.lastError()
Exemplo n.º 6
0
    def add(self, str):
        """
        Add String str and line break to file.

        @param str: string to add to pml file
        @type  str: str        
        """
        try:
            self.fgenerate.write(str + '\n')
        except (IOError):
            T.errWriteln(
                "PymolInput.add(): Error adding string to pymol script file.")
            T.errWriteln( T.lastError() )
Exemplo n.º 7
0
    def addResProperty( self, values, key='temperature_factor'):
        """
        Does the same thing as L{addProperty} but on the residue level,
        i.e adds extra value to each residue in Structure.
        (The same value is added to all atoms of a residue.)
        These values can then be used to display properties in PyMol
        via commands like 'color_b' and 'color_q'.
        
        @param values: list of numbers, len( values ) == number of residues
        @type  values: [float]      
        @param key: key for Atom.properties dictionary
                    ('occupancy' OR 'temperature_factor')
        @type  key: occupancy|temperature_factor
        """
        try:
            if self.struct == None:
                self.struct = PDBModel( self.fname )
                self.temporary = 1

            self.struct[ key ] = self.struct.res2atomProfile( values )
        except:
            print T.lastError()
Exemplo n.º 8
0
    def add(self, str):
        """
        Add String str and line break to file.

        @param str: string to add to pml file
        @type  str: str        
        """
        try:
            self.fgenerate.write(str + '\n')
        except (IOError):
            T.errWriteln(
                "PymolInput.add(): Error adding string to pymol script file.")
            T.errWriteln(T.lastError())
Exemplo n.º 9
0
    def addResProperty(self, values, key='temperature_factor'):
        """
        Does the same thing as L{addProperty} but on the residue level,
        i.e adds extra value to each residue in Structure.
        (The same value is added to all atoms of a residue.)
        These values can then be used to display properties in PyMol
        via commands like 'color_b' and 'color_q'.
        
        @param values: list of numbers, len( values ) == number of residues
        @type  values: [float]      
        @param key: key for Atom.properties dictionary
                    ('occupancy' OR 'temperature_factor')
        @type  key: occupancy|temperature_factor
        """
        try:
            if self.struct is None:
                self.struct = PDBModel(self.fname)
                self.temporary = 1

            self.struct[key] = self.struct.res2atomProfile(values)
        except:
            print T.lastError()
Exemplo n.º 10
0
    def _completeResidues(self, chain):
        """
        Look for missing or unknown atom names, add missing atoms,
        report unknown atoms.
        
        @param chain: Scientific.IO.PDB.PeptideChain object
        @type  chain: object
        
        @return: Scientific.IO.PDB.PeptideChain object
        @rtype: chain object
        """
        chain.deleteHydrogens()  ## delete all hydrogens
        i = 0
        self.log.add("Checking atoms of chain " + chain.segment_id)

        for res in chain:
            try:
                if i < len(chain) - 1:  # normal residue
                    alowed = self.aminoAcidDict[res.name]
                else:  # c-terminal residue
                    alowed = self._res2Terminal(self.aminoAcidDict[res.name])

                name_list = []

                for atom in res.atom_list:  # check for unknown atom names
                    # store for missing atom check
                    name_list = name_list + [atom.name]
                    if not (atom.name in alowed):
                        self.log.add('\tunknown atom: ' + atom.name + ' : '+\
                                     res.name+ str(res.number))

                for name in alowed:  # check for missing atoms
                    if not (name in name_list):
                        # add missing atom with 0 xyz
                        self._addMissing(res, name)
                        self.log.add('\tadded missing atom -> '+ name+ ' : '+\
                                     res.name+ str(res.number))

            except:
                s = "\ncompleteResidues(): Error while checking atoms.\n"
                s = s + "residue " + str(i) + " :" + str(res) + "\n"
                s = s + T.lastError()
                T.errWriteln(
                    "Error while completing residues, check log for details.")
                self.log.add(s)

            i = i + 1

        return chain
Exemplo n.º 11
0
    def _completeResidues(self, chain):
        """
        Look for missing or unknown atom names, add missing atoms,
        report unknown atoms.
        
        @param chain: Scientific.IO.PDB.PeptideChain object
        @type  chain: object
        
        @return: Scientific.IO.PDB.PeptideChain object
        @rtype: chain object
        """
        chain.deleteHydrogens() ## delete all hydrogens
        i = 0
        self.log.add("Checking atoms of chain "+chain.segment_id)

        for res in chain:
            try:
                if i < len(chain)-1:            # normal residue
                    alowed = self.aminoAcidDict[res.name]
                else:                           # c-terminal residue
                    alowed = self._res2Terminal(self.aminoAcidDict[res.name])

                name_list = []

                for atom in res.atom_list:      # check for unknown atom names
                    # store for missing atom check
                    name_list = name_list + [atom.name]
                    if not (atom.name in alowed):
                        self.log.add('\tunknown atom: ' + atom.name + ' : '+\
                                     res.name+ str(res.number))

                for name in alowed:              # check for missing atoms
                    if not (name in name_list):
                        # add missing atom with 0 xyz
                        self._addMissing(res, name)  
                        self.log.add('\tadded missing atom -> '+ name+ ' : '+\
                                     res.name+ str(res.number))

            except:
               s = "\ncompleteResidues(): Error while checking atoms.\n"
               s = s + "residue " + str(i)+ " :"+ str(res) + "\n"
               s = s + T.lastError()
               T.errWriteln(
                   "Error while completing residues, check log for details.")
               self.log.add(s)

            i = i+1

        return chain
Exemplo n.º 12
0
    def removeTER(self, fname):
        """
        Remove TER record from PDB.

        @param fname: name of existing file.
        @type  fname: string
        """
        try:
            #command = 'egrep -v "^TER " ' + fname + '> temp.pdb'
            path = os.path.dirname(fname)
            command = 'egrep -v "^TER " %s > %s/temp.pdb' % (fname, path)
            commands.getstatusoutput(command)
            os.rename('%s/temp.pdb' % path, fname)
        except (OSError):
            T.errWriteln("Error removing 'TER' statement from %s: ")
            T.errWriteln(T.lastError())
Exemplo n.º 13
0
    def removeTER(self, fname):
        """
        Remove TER record from PDB.

        @param fname: name of existing file.
        @type  fname: string
        """
        try:
            #command = 'egrep -v "^TER " ' + fname + '> temp.pdb'
            path = os.path.dirname( fname )
            command = 'egrep -v "^TER " %s > %s/temp.pdb'%( fname, path )
            commands.getstatusoutput(command)
            os.rename('%s/temp.pdb'%path, fname)
        except (OSError):
            T.errWriteln("Error removing 'TER' statement from %s: ")
            T.errWriteln( T.lastError() )
Exemplo n.º 14
0
    def writeChain(self, chain):
        """
        Write single chain as PDB. File name will be segid + '_seg.pdb'.

        @param chain: Scientific.IO.PDB.PeptideChain object
        @type  chain: chain object
        """
        if (chain <> None):
            try:

                fname = self.path + '/' + chain.segment_id + "_seg.PDB"
                file = self._startPDB(chain, fname)     # include comments
                chain.writeToFile(file)     # create PDB
                file.close()                # make sure file is complete!
                self.removeTER(file.file.file.name) #remove TER record from PDB
                return 1        # write a chain

            except (IOError):
                T.errWriteln("Error writing chain to file %s:" % fname)
                T.errWriteln( T.lastError() )

        return 0            # false, no more chains to write
Exemplo n.º 15
0
    def setResValues(self,
                     model,
                     values,
                     key='temperature_factor',
                     lastOnly=0):
        """
        Add numeric value per residue to all atoms of all Structures
        or the last Structure in a model. The values will be written to
        either the B- (temperature_factor) or Q-factor 'occupancy' column
        in the temporary pdb-file.
        These values can then be used to display properties in PyMol
        via commands like 'color_b' and 'color_q'. See also
        L{setAtomValues}.
        
        @param model: model name
        @type  model: str
        @param values: list of numbers, len( values ) == number of residues
        @type  values: [float]      
        @param key: key for Atom.properties dictionary
                    (default: temperature_factor)
        @type  key: occupancy|temperature_factor
        @param lastOnly: 0 - add to all in model OR
                         1 - add only to last Structure (default: 0)
        @type  lastOnly: 1|0
        """
        if lastOnly:
            self.dic[model][-1].addResProperty(values, key)

        else:
            for m in self.dic[model]:
                try:
                    m.addResProperty(values, key)
                except:
                    T.errWriteln("Warning: error while adding properties.")
                    T.errWriteln("Key: " + str(key) + " values: " +
                                 str(values))
                    T.errWriteln(T.lastError())
Exemplo n.º 16
0
    def update( self, model, source, skipRes=None, updateMissing=0, force=0,
                headPatterns=[]):
        """
        Update empty or missing fields of model from the source. The
        model will be connected to the source via model.source.
        Profiles that are derived from the source are labeled 'changed'=0.
        The same holds for coordinates (xyzChanged=0).
        However, existing profiles or coordinates or fields remain untouched.

        @param model: existing model
        @type  model: PDBModel
        @param source: source PDB file
        @type  source: str
        @param skipRes: list residue names that should not be parsed
        @type  skipRes: [ str ]
        @param updateMissing: ignored
        @type  updateMissing: 1|0
        @param headPatterns: [(putIntoKey, regex)] extract given REMARKS
        @type  headPatterns: [(str, str)]

        @raise PDBParserError - if something is wrong with the source file
        """

        try:
            # atoms and/or coordinates need to be updated from PDB
            if force or self.needsUpdate( model ):

                atoms, xyz, info = self.__collectAll( source, skipRes,
                                                        headPatterns )
#                print atoms["serial_number"]
#                for atom in atoms:
#                    print atom
                keys = MU.union( atoms.keys(),  self.DEFAULTS.keys() )

                for k in keys:

                    if model.atoms.get( k, default=0, update=False ) in \
                            (0,None):

                        dflt = self.DEFAULTS.get( k, None )
                        model.atoms.set(k, atoms.get(k, dflt), changed=0 )

                if model.xyz is None:
                    model.xyz = xyz
                    model.xyzChanged = 0

                model._resIndex  =None
                model._chainIndex=None

                model.fileName = model.fileName or source

                model.pdbCode = model.pdbCode or info.get('pdb_code', None) or \
                                self.idFromName( model.fileName)

                model.info.update( info )

        except:
            msg = self._PDBParseFile__xplorAtomIndicesTest( source ) or ' '
            raise PDBParserError('Cannot read ' + str(source) + ' as PQR\n'\
                           '\ERROR: ' + T.lastError() + msg)

        model.setSource( source )
Exemplo n.º 17
0
    def __collectAll( self, fname, skipRes=None, headPatterns=[] ):
        """
        Parse ATOM/HETATM lines from PDB. Collect coordinates plus
        dictionaries with the other pdb records of each atom.
        REMARK, HEADER, etc. lines are ignored.

        Some changes are made to the dictionary from PDBFile.readline()::
            - the 'position' entry (with the coordinates) is removed
            - leading and trailing spaces are removed from 'name' ..
            - .. but a 'name_original' entry keeps the old name with spaces
            - a 'type' entry is added. Its value is 'ATOM' or 'HETATM'
            - a 'after_ter' entry is added. Its value is 1, if atom is
              preceeded by a 'TER' line, otherwise 0
            - empty 'element' entries are filled with the first non-number
              letter from the atom 'name'

        @param fname: name of pdb file
        @type  fname: str
        @param skipRes: list with residue names that should be skipped
        @type  skipRes: list of str

        @return: tuple of (1) dictionary of profiles
                 and (2) xyz array N x 3
        @rtype: ( list, array )
        """
        xyz   = []

        aProfs = {}

        info = {}

        in_header = True
        headPatterns = headPatterns or self.RE_REMARKS
        patterns = [ (key, re.compile(ex)) for key,ex in headPatterns ]

        for k in bi.PDBModel.PDB_KEYS:
            aProfs[k] = list()

        f = open( fname ,'r' )

        try:
            line, i = ('',''), 0

            while line[0] <> 'END' and line[0] <> 'ENDMDL':

                i += 1
                try:
                    line = f.readline().split()
                except ValueError, what:
                    self.log.add('Warning: Error parsing line %i of %s' %
                                 (i, T.stripFilename( fname )) )
                    self.log.add('\tError: '+str(what) )
                    continue

                if not line: break
                
                ## header handling
                if in_header and line[0] == 'HEADER':
                    info.update( self._PDBParseFile__parseHeader( line ) )

                if in_header and line[0] == 'REMARK':
                    info.update( self._PDBParseFile__parseRemark( line, patterns ) )


                ## preserve position of TER records
#                print line
                newChain = line[0] == 'TER'
                if newChain:
                    line = f.readline().split()

                if (line[0] in ['ATOM','HETATM'] ):

                    if in_header: in_header = False  ## switch off HEADER parsing
                    if len(line) == 11: # contains chain name
                        a = {'serial_number': int(line[1]),
                                 'name': line[2],
                                 'alternate': '',
                                 'residue_name': string.strip(line[3]),
                                 'chain_id': string.strip(line[4]),
                                 'residue_number': int(line[5]),
                                 'insertion_code': '',
                                 'position': map(float,line[6:9]),
                                 'occupancy': 1.0,
                                 'temperature_factor': 0.0,
                                 'segment_id': '',
                                 'element': '',
                                 'charge': line[9]}
                    else:   # without chain name
                        a = {'serial_number': int(line[1]),
                                 'name': line[2],
                                 'alternate': '',
                                 'residue_name': string.strip(line[3]),
                                 'chain_id': '',
                                 'residue_number': int(line[4]),
                                 'insertion_code': '',
                                 'position': map(float,line[5:8]),
                                 'occupancy': 1.0,
                                 'temperature_factor': 0.0,
                                 'segment_id': '',
                                 'element': '',
                                 'charge': line[8]}
#                    print line
#                    print a
                    if skipRes and a['residue_name'] in skipRes:
                        continue

                    a['name_original'] = a['name']
                    a['name'] = a['name'].strip()

                    a['type'] = line[0]

                    if newChain:
                        a['after_ter'] = 1
                    else:
                        a['after_ter'] = 0

                    if a['element'] == '':
                        a['element'] = self._PDBParseFile__firstLetter( a['name'] )

#                    print a
                    xyz.append( a['position'] )
                    del( a['position'])
                    
                    for k, v in a.items():
                        aProfs[k].append( v )

        except:
            raise PDBParserError("Error parsing file "+fname+": " \
                                 + T.lastError())
        try:
            f.close()
        except:
            pass

        if len( xyz ) == 0:
            raise PDBParserError("Error parsing file "+fname+": "+
                            "Couldn't find any atoms.")

        return aProfs, npy.array( xyz, npy.float64 ), info