Difference between revisions of "LoadDir"

From PyMOLWiki
Jump to navigation Jump to search
(loadall)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Overview ==
 
 
Load all files of the suffix '''suff''' from the directory '''dirName''', where '''suff''' and '''dirName''' are function parameters.
 
Load all files of the suffix '''suff''' from the directory '''dirName''', where '''suff''' and '''dirName''' are function parameters.
 +
 +
''This script is superseded by the [[loadall]] command, which was added in PyMOL 1.7.2.''
  
 
== Install ==
 
== Install ==
Line 7: Line 8:
 
# run loadDir.  See examples below.
 
# run loadDir.  See examples below.
  
== Example ==
+
== Examples ==
 
<source lang="python">
 
<source lang="python">
 
# load the script
 
# load the script
Line 19: Line 20:
 
# load all PDBs from /tmp
 
# load all PDBs from /tmp
 
loadDir /tmp, foo.woo.pdb
 
loadDir /tmp, foo.woo.pdb
 +
 +
# load all the PDBs in all the directories under ./binders/ERK
 +
loadDir ./binders/ERK/*, .pdb
 +
 +
# load the PDBs into groups: now we can load all the files in the tree under
 +
# ./ERK into the group "ERK" and the files from ./SYK into the group "SYK"
 +
loadDir ./binders/ERK/*, .pdb, group=ERKb
 +
loadDir ./binders/SYK/*, .pdb, group=SYKb
 
</source>
 
</source>
  
Line 24: Line 33:
 
<source lang="python">
 
<source lang="python">
 
from glob import glob
 
from glob import glob
from os.path import sep
+
from os.path import sep, basename
from string import split
 
  
def loadDir(dirName=".", suff="pdb"):
+
def loadDir(dirName=".", suff="pdb", group=None):
 
         """
 
         """
 
         Loads all files with the suffix suff (the input parameter) from the directory dirName).
 
         Loads all files with the suffix suff (the input parameter) from the directory dirName).
Line 36: Line 44:
 
                         and "pdb" should work.  The suffix can be anything valid that PyMOL knows
 
                         and "pdb" should work.  The suffix can be anything valid that PyMOL knows
 
                         how to natively load.
 
                         how to natively load.
 +
        group:          groupName to add the files to.
  
 
         example:
 
         example:
 
                 # load all the PDBs in the current directory
 
                 # load all the PDBs in the current directory
                 loadAll
+
                 loadDir
  
 
                 # load all SD files from /tmp
 
                 # load all SD files from /tmp
                 loadAll /tmp, "sdf"
+
                 loadDir /tmp, "sdf"
  
 
         notes:
 
         notes:
Line 52: Line 61:
 
         """
 
         """
  
        if "." in suff:
+
         g = dirName + sep + "*." + suff.split(".")[-1]
                idx = len(split(suff, "."))-1
 
        else:
 
                idx = 0
 
 
 
         g = dirName + sep + "*." + split(suff, ".")[idx]
 
  
 
         for c in glob( g ):
 
         for c in glob( g ):
 
                 cmd.load(c)
 
                 cmd.load(c)
 +
 +
                if ( group != None ):
 +
                        cmd.group( group, basename(c).split(".")[0], "add" )
  
 
cmd.extend("loadDir", loadDir)
 
cmd.extend("loadDir", loadDir)
 
</source>
 
</source>
  
[[Category:Script_Library|loadDir]]
+
[[Category:Script_Library|LoadDir]]
 +
[[Category:System_Scripts]]

Latest revision as of 11:15, 2 March 2017

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

This script is superseded by the loadall command, which was added in PyMOL 1.7.2.

Install

  1. copy the source below to the a file called "loadDir.pml" somewhere on your computer
  2. load the file with "run /your/path/toLoadDir/loadDir.pml"
  3. run loadDir. See examples below.

Examples

# 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

# load all the PDBs in all the directories under ./binders/ERK
loadDir ./binders/ERK/*, .pdb

# load the PDBs into groups: now we can load all the files in the tree under
# ./ERK into the group "ERK" and the files from ./SYK into the group "SYK"
loadDir ./binders/ERK/*, .pdb, group=ERKb
loadDir ./binders/SYK/*, .pdb, group=SYKb

The Code

from glob import glob
from os.path import sep, basename

def loadDir(dirName=".", suff="pdb", group=None):
        """
        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.
        group:          groupName to add the files to.

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

                # load all SD files from /tmp
                loadDir /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.
        """

        g = dirName + sep + "*." + suff.split(".")[-1]

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

                if ( group != None ):
                        cmd.group( group, basename(c).split(".")[0], "add" )

cmd.extend("loadDir", loadDir)