Exemplo n.º 1
0
class PDBFileList(object):

    def __init__(self, path, structureList=[]):
        self.path = path
        self.pathMap = {}
        self.structure_paths = []

        if os.path.isdir(path):
            if structureList == []:
                for filename in os.listdir(path):
                    if filename[0] == ".": continue # avoid dotfiles (eg .svn dirs)
                    fullpath = os.path.join(path, filename)
                    self.mapFilename(filename, fullpath)
                    self.structure_paths.append(fullpath)
            else:
                print(structureList)
                filenamesInDir = os.listdir(path)
                if filenamesInDir == []:
                    raise IOError, "WARNING : No files in directory %s" % path    
                for filename in structureList:
                    if filename in filenamesInDir:
                        fullpath = os.path.join(path, filename)
                        self.mapFilename(filename, fullpath)
                        self.structure_paths.append(fullpath)
                    else:
                        raise IOError, "WARNING : File %s is not in directory %s" % (filename, path)
        elif os.path.isfile(path):
            self.structure_paths = [path]
        else:
            raise IOError, "WARNING : The path %s is not a directory or a file - it may not exist!" % path

        self.parser = PDBParser(StructureBuilder())

    def mapFilename(self, filename, fullpath):
        # XXX take the first four letters of a filename
        id = filename[0:4]
        self.pathMap[id] = fullpath 

    def __iter__(self):
        try:
            for i, path in enumerate(self.structure_paths):
                yield self.parser.getStructure(path)
        except IOError, ioe:
            raise IOError, "I/O problem with path [%s] %s" % (path, ioe)
Exemplo n.º 2
0
def structureFromFile(filepath):
    parser = PDBParser(StructureBuilder())
    return parser.getStructure(filepath)