1 #!/bin/env python 2 # 3 # File: PyMOLConvertPMLToPSE.py 4 # Author: Manish Sud <msud@san.rr.com> 5 # 6 # Copyright (C) 2026 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 import os 33 import sys 34 import time 35 import re 36 37 # PyMOL imports... 38 try: 39 import pymol 40 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 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), "..", "lib", "Python")) 52 try: 53 from docopt import docopt 54 import MiscUtil 55 import PyMOLUtil 56 except ImportError as ErrMsg: 57 sys.stderr.write("\nFailed to import MayaChemTools module/package: %s\n" % ErrMsg) 58 sys.stderr.write("Check/update your MayaChemTools environment and try again.\n\n") 59 sys.exit(1) 60 61 ScriptName = os.path.basename(sys.argv[0]) 62 Options = {} 63 OptionsInfo = {} 64 65 66 def main(): 67 """Start execution of the script.""" 68 69 MiscUtil.PrintInfo( 70 "\n%s (PyMOL v%s; MayaChemTools v%s; %s): Starting...\n" 71 % (ScriptName, pymol.cmd.get_version()[0], MiscUtil.GetMayaChemToolsVersion(), time.asctime()) 72 ) 73 74 (WallClockTime, ProcessorTime) = MiscUtil.GetWallClockAndProcessorTime() 75 76 # Retrieve command line arguments and options... 77 RetrieveOptions() 78 79 # Process and validate command line arguments and options... 80 ProcessOptions() 81 82 # Perform actions required by the script... 83 GeneratePSEFile() 84 85 MiscUtil.PrintInfo("\n%s: Done...\n" % ScriptName) 86 MiscUtil.PrintInfo("Total time: %s" % MiscUtil.GetFormattedElapsedTime(WallClockTime, ProcessorTime)) 87 88 89 def GeneratePSEFile(): 90 """Comvert PML file to PSE file.""" 91 92 PMLFile = OptionsInfo["Infile"] 93 PSEFile = OptionsInfo["Outfile"] 94 95 MiscUtil.PrintInfo("\nGenerating file %s..." % PSEFile) 96 97 PyMOLUtil.ConvertPMLFileToPSEFile(PMLFile, PSEFile, OutputFeedback=OptionsInfo["Feedback"]) 98 99 if not os.path.exists(PSEFile): 100 MiscUtil.PrintWarning("Failed to generate PSE file, %s..." % (PSEFile)) 101 102 103 def ProcessOptions(): 104 """Process and validate command line arguments and options.""" 105 106 MiscUtil.PrintInfo("Processing options...") 107 108 # Validate options... 109 ValidateOptions() 110 111 OptionsInfo["Feedback"] = True if re.match("^Yes$", Options["--feedback"], re.I) else False 112 113 OptionsInfo["Infile"] = Options["--infile"] 114 OptionsInfo["Outfile"] = Options["--outfile"] 115 116 117 def RetrieveOptions(): 118 """Retrieve command line arguments and options.""" 119 120 # Get options... 121 global Options 122 Options = docopt(_docoptUsage_) 123 124 # Set current working directory to the specified directory... 125 WorkingDir = Options["--workingdir"] 126 if WorkingDir: 127 os.chdir(WorkingDir) 128 129 # Handle examples option... 130 if "--examples" in Options and Options["--examples"]: 131 MiscUtil.PrintInfo(MiscUtil.GetExamplesTextFromDocOptText(_docoptUsage_)) 132 sys.exit(0) 133 134 135 def ValidateOptions(): 136 """Validate option values.""" 137 138 MiscUtil.ValidateOptionTextValue("-f, --feedback", Options["--feedback"], "yes no") 139 140 MiscUtil.ValidateOptionFilePath("-i, --infile", Options["--infile"]) 141 MiscUtil.ValidateOptionFileExt("-i, --infile", Options["--infile"], "pml") 142 143 MiscUtil.ValidateOptionFileExt("-o, --outfile", Options["--outfile"], "pse") 144 MiscUtil.ValidateOptionsOutputFileOverwrite( 145 "-o, --outfile", Options["--outfile"], "--overwrite", Options["--overwrite"] 146 ) 147 MiscUtil.ValidateOptionsDistinctFileNames( 148 "-i, --infile", Options["--infile"], "-o, --outfile", Options["--outfile"] 149 ) 150 151 152 # Setup a usage string for docopt... 153 _docoptUsage_ = """ 154 PyMOLConvertPMLToPSE.py - Convert PML to PSE 155 156 Usage: 157 PyMOLConvertPMLToPSE.py [--feedback <yes or no>] [--overwrite] 158 [-w <dir>] -i <infile> -o <outfile> 159 PyMOLConvertPMLToPSE.py -h | --help | -e | --examples 160 161 Description: 162 Convert PyMOL script language (PML) file to PyMOL session (PSE) file. 163 164 The supported input and output file formats are PML (.pml) and PSE (.pse). 165 166 Options: 167 -f, --feedback <yes or no> [default: yes] 168 PyMOL output feedback during loading of PML file. This option may not 169 work in all versions of PyMOL across various platforms. 170 -e, --examples 171 Print examples. 172 -h, --help 173 Print this help message. 174 -i, --infile <infile> 175 Input file name. 176 -o, --outfile <outfile> 177 Output file name. 178 --overwrite 179 Overwrite existing files. 180 -w, --workingdir <dir> 181 Location of working directory which defaults to the current directory. 182 183 Examples: 184 To convert a PML file to a PSE file, type: 185 186 % PyMOLConvertPMLToPSE.py -i Sample.pml -o Sample.pse 187 188 To convert a PML file to a PSE file along with turning off PyMOL feedback 189 during loading of PML file, type: 190 191 % PyMOLConvertPMLToPSE.py -f no -i Sample.pml -o Sample.pse 192 193 Author: 194 Manish Sud(msud@san.rr.com) 195 196 See also: 197 PyMOLConvertLigandFileFormat.py, PyMOLSplitChainsAndLigands.py, 198 PyMOLVisualizeMacromolecules.py 199 200 Copyright: 201 Copyright (C) 2026 Manish Sud. All rights reserved. 202 203 The functionality available in this script is implemented using PyMOL, a 204 molecular visualization system on an open source foundation originally 205 developed by Warren DeLano. 206 207 This file is part of MayaChemTools. 208 209 MayaChemTools is free software; you can redistribute it and/or modify it under 210 the terms of the GNU Lesser General Public License as published by the Free 211 Software Foundation; either version 3 of the License, or (at your option) any 212 later version. 213 214 """ 215 216 if __name__ == "__main__": 217 main()