MayaChemTools

   1 #!/bin/env python
   2 #
   3 # File: PyMOLConvertPMLToPSE.py
   4 # Author: Manish Sud <msud@san.rr.com>
   5 #
   6 # Copyright (C) 2024 Manish Sud. All rights reserved.
   7 #
   8 # The functionality available in this script is implemented using PyMOL, a
   9 # molecular visualization system on an open source foundation originally
  10 # developed by Warren DeLano.
  11 #
  12 # This file is part of MayaChemTools.
  13 #
  14 # MayaChemTools is free software; you can redistribute it and/or modify it under
  15 # the terms of the GNU Lesser General Public License as published by the Free
  16 # Software Foundation; either version 3 of the License, or (at your option) any
  17 # later version.
  18 #
  19 # MayaChemTools is distributed in the hope that it will be useful, but without
  20 # any warranty; without even the implied warranty of merchantability of fitness
  21 # for a particular purpose.  See the GNU Lesser General Public License for more
  22 # details.
  23 #
  24 # You should have received a copy of the GNU Lesser General Public License
  25 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
  26 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
  27 # Boston, MA, 02111-1307, USA.
  28 #
  29 
  30 from __future__ import print_function
  31 
  32 # Add local python path to the global path and import standard library modules...
  33 import os
  34 import sys;  sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), "..", "lib", "Python"))
  35 import time
  36 import re
  37 
  38 # PyMOL imports...
  39 try:
  40     import pymol
  41     # Finish launching PyMOL in  a command line mode for batch processing (-c)
  42     # along with the following options:  disable loading of pymolrc and plugins (-k);
  43     # suppress start up messages (-q)
  44     pymol.finish_launching(['pymol', '-ckq'])
  45 except ImportError as ErrMsg:
  46     sys.stderr.write("\nFailed to import PyMOL module/package: %s\n" % ErrMsg)
  47     sys.stderr.write("Check/update your PyMOL environment and try again.\n\n")
  48     sys.exit(1)
  49 
  50 # MayaChemTools imports...
  51 try:
  52     from docopt import docopt
  53     import MiscUtil
  54     import PyMOLUtil
  55 except ImportError as ErrMsg:
  56     sys.stderr.write("\nFailed to import MayaChemTools module/package: %s\n" % ErrMsg)
  57     sys.stderr.write("Check/update your MayaChemTools environment and try again.\n\n")
  58     sys.exit(1)
  59 
  60 ScriptName = os.path.basename(sys.argv[0])
  61 Options = {}
  62 OptionsInfo = {}
  63 
  64 def main():
  65     """Start execution of the script."""
  66     
  67     MiscUtil.PrintInfo("\n%s (PyMOL v%s; MayaChemTools v%s; %s): Starting...\n" % (ScriptName, pymol.cmd.get_version()[0], MiscUtil.GetMayaChemToolsVersion(), time.asctime()))
  68     
  69     (WallClockTime, ProcessorTime) = MiscUtil.GetWallClockAndProcessorTime()
  70     
  71     # Retrieve command line arguments and options...
  72     RetrieveOptions()
  73     
  74     # Process and validate command line arguments and options...
  75     ProcessOptions()
  76 
  77     # Perform actions required by the script...
  78     GeneratePSEFile()
  79     
  80     MiscUtil.PrintInfo("\n%s: Done...\n" % ScriptName)
  81     MiscUtil.PrintInfo("Total time: %s" % MiscUtil.GetFormattedElapsedTime(WallClockTime, ProcessorTime))
  82 
  83 def GeneratePSEFile():
  84     """Comvert PML file to PSE file."""
  85 
  86     PMLFile = OptionsInfo["Infile"]
  87     PSEFile = OptionsInfo["Outfile"]
  88     
  89     MiscUtil.PrintInfo("\nGenerating file %s..." % PSEFile)
  90 
  91     PyMOLUtil.ConvertPMLFileToPSEFile(PMLFile, PSEFile, OutputFeedback = OptionsInfo["Feedback"])
  92     
  93     if not os.path.exists(PSEFile):
  94         MiscUtil.PrintWarning("Failed to generate PSE file, %s..." % (PSEFile))
  95 
  96 def ProcessOptions():
  97     """Process and validate command line arguments and options."""
  98 
  99     MiscUtil.PrintInfo("Processing options...")
 100     
 101     # Validate options...
 102     ValidateOptions()
 103     
 104     OptionsInfo["Feedback"] = True if re.match("^Yes$", Options["--feedback"], re.I) else False
 105     
 106     OptionsInfo["Infile"] = Options["--infile"]
 107     OptionsInfo["Outfile"] = Options["--outfile"]
 108 
 109 def RetrieveOptions(): 
 110     """Retrieve command line arguments and options."""
 111     
 112     # Get options...
 113     global Options
 114     Options = docopt(_docoptUsage_)
 115 
 116     # Set current working directory to the specified directory...
 117     WorkingDir = Options["--workingdir"]
 118     if WorkingDir:
 119         os.chdir(WorkingDir)
 120     
 121     # Handle examples option...
 122     if "--examples" in Options and Options["--examples"]:
 123         MiscUtil.PrintInfo(MiscUtil.GetExamplesTextFromDocOptText(_docoptUsage_))
 124         sys.exit(0)
 125 
 126 def ValidateOptions():
 127     """Validate option values."""
 128 
 129     MiscUtil.ValidateOptionTextValue("-f, --feedback", Options["--feedback"], "yes no")
 130     
 131     MiscUtil.ValidateOptionFilePath("-i, --infile", Options["--infile"])
 132     MiscUtil.ValidateOptionFileExt("-i, --infile", Options["--infile"], "pml")
 133     
 134     MiscUtil.ValidateOptionFileExt("-o, --outfile", Options["--outfile"], "pse")
 135     MiscUtil.ValidateOptionsOutputFileOverwrite("-o, --outfile", Options["--outfile"], "--overwrite", Options["--overwrite"])
 136     MiscUtil.ValidateOptionsDistinctFileNames("-i, --infile", Options["--infile"], "-o, --outfile", Options["--outfile"])
 137 
 138 # Setup a usage string for docopt...
 139 _docoptUsage_ = """
 140 PyMOLConvertPMLToPSE.py - Convert PML to PSE
 141 
 142 Usage:
 143     PyMOLConvertPMLToPSE.py [--feedback <yes or no>] [--overwrite]
 144                             [-w <dir>] -i <infile> -o <outfile>
 145     PyMOLConvertPMLToPSE.py -h | --help | -e | --examples
 146 
 147 Description:
 148     Convert PyMOL script language (PML) file to PyMOL session (PSE) file.
 149 
 150     The supported input and output file formats are PML (.pml) and PSE (.pse).
 151 
 152 Options:
 153     -f, --feedback <yes or no>  [default: yes]
 154         PyMOL output feedback during loading of PML file. This option may not
 155         work in all versions of PyMOL across various platforms.
 156     -e, --examples
 157         Print examples.
 158     -h, --help
 159         Print this help message.
 160     -i, --infile <infile>
 161         Input file name.
 162     -o, --outfile <outfile>
 163         Output file name.
 164     --overwrite
 165         Overwrite existing files.
 166     -w, --workingdir <dir>
 167         Location of working directory which defaults to the current directory.
 168 
 169 Examples:
 170     To convert a PML file to a PSE file, type:
 171 
 172         % PyMOLConvertPMLToPSE.py -i Sample.pml -o Sample.pse
 173 
 174     To convert a PML file to a PSE file along with turning off PyMOL feedback
 175     during loading of PML file, type:
 176 
 177         % PyMOLConvertPMLToPSE.py -f no -i Sample.pml -o Sample.pse
 178 
 179 Author:
 180     Manish Sud(msud@san.rr.com)
 181 
 182 See also:
 183     PyMOLConvertLigandFileFormat.py, PyMOLSplitChainsAndLigands.py,
 184     PyMOLVisualizeMacromolecules.py
 185 
 186 Copyright:
 187     Copyright (C) 2024 Manish Sud. All rights reserved.
 188 
 189     The functionality available in this script is implemented using PyMOL, a
 190     molecular visualization system on an open source foundation originally
 191     developed by Warren DeLano.
 192 
 193     This file is part of MayaChemTools.
 194 
 195     MayaChemTools is free software; you can redistribute it and/or modify it under
 196     the terms of the GNU Lesser General Public License as published by the Free
 197     Software Foundation; either version 3 of the License, or (at your option) any
 198     later version.
 199 
 200 """
 201 
 202 if __name__ == "__main__":
 203     main()