Exemplo n.º 1
0
    def getRemotePDBHandle(self, id, rcsb_url=settings.rcsb_url):
        """
        Get the coordinate file remotely from the RCSB.

        @param id: pdb code, 4 characters
        @type  id: str
        @param rcsb_url: template url for pdb download
                         (default: L{settings.rcsb_url})
        @type  rcsb_url: str

        @return: the requested pdb file as a file handle
        @rtype: open file handle

        @raise PDBParserError: if couldn't retrieve PDB file
        """
        try:
            from Bio import File
        except:
            raise PDBParserError('Could not find Biopython - ' + \
                                 'remote fetching of PDBs is not supported.')

        handle = urllib.urlopen(rcsb_url % (id, id))

        uhandle = File.UndoHandle(handle)

        if not uhandle.peekline():
            raise PDBParserError("Couldn't retrieve ", rcsb_url)

        return uhandle
Exemplo n.º 2
0
    def parsePdbFromHandle(self, handle, first_model_only=True):
        """
        Parse PDB from file/socket or string handle into memory.

        @param handle: fresh open file/socket handle to PDB ressource or string
        @type  handle: open file-like object or str
        @param first_model_only: only take first of many NMR models [True]
        @type  first_model_only: bool

        @return: pdb file as list of strings, dictionary with resolution
        @rtype: [str], {'resolution':float }
        @raise PDBParserError: if passed in string is too short
        """
        lines = []
        res_match = None
        infos = {}

        if type(handle) is str:
            if len(handle) < 5000:
                raise PDBParserError("Couldn't extract PDB Info.")
            handle = cStringIO.StringIO(handle)

## if handle.peekline()[:6] != 'TITLE':
##     raise PDBParserError, 'Ressource does not seem to be a PDB:\n%r' %\
##     handle.peekline()

        for l in handle:
            lines += [l]

            res_match = res_match or self.ex_resolution.search(l)

            if first_model_only and l[:6] == 'ENDMDL':
                break

        if len(lines) < 10 and '<div>' in lines[0]:
            raise PDBParserError, 'No PDB found with this ID.'

        if res_match:
            if res_match.groups()[0] == 'NOT APPLICABLE':
                infos['resolution'] = self.NMR_RESOLUTION
            else:
                infos['resolution'] = float(res_match.groups()[0])
        else:
            raise PDBParserError, 'No resolution record found in PDB.'

        return lines, infos
Exemplo n.º 3
0
    def getLocalPDBHandle(self, id, db_path=settings.pdb_path):
        """
        Get the coordinate file from a local pdb database.

        @param id: pdb code, 4 characters
        @type  id: str
        @param db_path: path to local pdb database
                        (default: L{settings.pdb_path})
        @type  db_path: str

        @return: the requested pdb file as a file handle
        @rtype: open file handle

        @raise PDBParserError: if couldn't find PDB file
        """
        id = str.lower(id)
        filenames = [
            os.path.join(db_path, '%s.pdb' % id), db_path + '/pdb%s.ent' % id,
            db_path + '/%s/pdb%s.ent.Z' % (id[1:3], id)
        ]

        for f in filenames:
            if os.path.exists(f):
                ## gzipped pdb file
                if f[-3:] == '.gz':
                    return gzip.open(f)
                ## the gzip module doesn't handle .Z files
                ## doesn't return open file handle
                elif f[-2:] == '.Z':
                    p = subprocess.Popen(['gunzip', '-c', f],
                                         stdout=subprocess.PIPE)
                    return p.communicate()[0]
                ## uncompressed
                else:
                    return open(f)

        raise PDBParserError("Couldn't find PDB file locally.")