LoadDir

From PyMOLWiki
Revision as of 08:51, 20 October 2008 by Inchoate (talk | contribs) (→‎Example)
Jump to navigation Jump to search

Overview

Load all files of the suffix suff from the directory dirName, where suff and dirName are function parameters.

Example

# load the script
run ~/loadDir.pml

# load all SD files from /tmp
loadDir /tmp, sdf
loadDir /tmp, .sdf
loadDir /tmp, *.sdf
# even stupid stuff works; hopefully as one would want.
# load all PDBs from /tmp
loadDir /tmp, foo.woo.pdb

The Code

from glob import glob
from os.path import sep
from string import split

def loadDir(dirName=".", suff="pdb"):
        """
        Loads all files with the suffix suff (the input parameter) from the directory dirName).

        dirName:        directory path
        suff:           file suffix.  Should be simply "pdb" or "sdf" or similar.  Will accept the
                        wildcard and dot in case the user doesn't read this.  So, "*.pdb", ".pdb",
                        and "pdb" should work.  The suffix can be anything valid that PyMOL knows
                        how to natively load.

        example:
                # load all the PDBs in the current directory
                loadAll

                # load all SD files from /tmp
                loadAll /tmp, "sdf"

        notes:
                make sure you call this script w/o quotes around your parameters:
                        loadDir ., .pdb
                as opposed to
                        loadDir ".", "*.pdb"
                Use the former.
        """

        if "." in suff:
                idx = len(split(suff, "."))-1
        else:
                idx = 0

        g = dirName + sep + "*." + split(suff, ".")[idx]

        for c in glob( g ):
                cmd.load(c)

cmd.extend("loadDir", loadDir)