1 #!/bin/env python 2 # 3 # File: PyMOLConvertLigandFileFormat.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 36 # PyMOL imports... 37 try: 38 import pymol 39 40 # Finish launching PyMOL in a command line mode for batch processing (-c) 41 # along with the following options: disable loading of pymolrc and plugins (-k); 42 # suppress start up messages (-q) 43 pymol.finish_launching(["pymol", "-ckq"]) 44 except ImportError as ErrMsg: 45 sys.stderr.write("\nFailed to import PyMOL module/package: %s\n" % ErrMsg) 46 sys.stderr.write("Check/update your PyMOL environment and try again.\n\n") 47 sys.exit(1) 48 49 # MayaChemTools imports... 50 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), "..", "lib", "Python")) 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 65 def main(): 66 """Start execution of the script.""" 67 68 MiscUtil.PrintInfo( 69 "\n%s (PyMOL v%s; MayaChemTools v%s; %s): Starting...\n" 70 % (ScriptName, pymol.cmd.get_version()[0], MiscUtil.GetMayaChemToolsVersion(), time.asctime()) 71 ) 72 73 (WallClockTime, ProcessorTime) = MiscUtil.GetWallClockAndProcessorTime() 74 75 # Retrieve command line arguments and options... 76 RetrieveOptions() 77 78 # Process and validate command line arguments and options... 79 ProcessOptions() 80 81 # Perform actions required by the script... 82 ConvertLigandFileFormat() 83 84 MiscUtil.PrintInfo("\n%s: Done...\n" % ScriptName) 85 MiscUtil.PrintInfo("Total time: %s" % MiscUtil.GetFormattedElapsedTime(WallClockTime, ProcessorTime)) 86 87 88 def ConvertLigandFileFormat(): 89 """Comvert ligand file format.""" 90 91 Infile = OptionsInfo["Infile"] 92 Outfile = OptionsInfo["Outfile"] 93 94 MiscUtil.PrintInfo("\nGenerating file %s..." % Outfile) 95 96 PyMOLUtil.ConvertFileFormat(Infile, Outfile) 97 98 if not os.path.exists(Outfile): 99 MiscUtil.PrintWarning("Failed to generate Outfile file, %s..." % (Outfile)) 100 101 102 def ProcessOptions(): 103 """Process and validate command line arguments and options.""" 104 105 MiscUtil.PrintInfo("Processing options...") 106 107 # Validate options... 108 ValidateOptions() 109 110 OptionsInfo["Infile"] = Options["--infile"] 111 OptionsInfo["Outfile"] = Options["--outfile"] 112 113 114 def RetrieveOptions(): 115 """Retrieve command line arguments and options.""" 116 117 # Get options... 118 global Options 119 Options = docopt(_docoptUsage_) 120 121 # Set current working directory to the specified directory... 122 WorkingDir = Options["--workingdir"] 123 if WorkingDir: 124 os.chdir(WorkingDir) 125 126 # Handle examples option... 127 if "--examples" in Options and Options["--examples"]: 128 MiscUtil.PrintInfo(MiscUtil.GetExamplesTextFromDocOptText(_docoptUsage_)) 129 sys.exit(0) 130 131 132 def ValidateOptions(): 133 """Validate option values.""" 134 135 MiscUtil.ValidateOptionFilePath("-i, --infile", Options["--infile"]) 136 MiscUtil.ValidateOptionFileExt("-i, --infile", Options["--infile"], "mol mol2 pdb") 137 138 MiscUtil.ValidateOptionFileExt("-o, --outfile", Options["--outfile"], "mol mol2 pdb") 139 MiscUtil.ValidateOptionsOutputFileOverwrite( 140 "-o, --outfile", Options["--outfile"], "--overwrite", Options["--overwrite"] 141 ) 142 MiscUtil.ValidateOptionsDistinctFileNames( 143 "-i, --infile", Options["--infile"], "-o, --outfile", Options["--outfile"] 144 ) 145 146 147 # Setup a usage string for docopt... 148 _docoptUsage_ = """ 149 PyMOLConvertLigandFileFormat.py.py - Convert between ligand file formats 150 151 Usage: 152 PyMOLConvertLigandFileFormat.py.py [--overwrite] 153 [-w <dir>] -i <infile> -o <outfile> 154 PyMOLConvertLigandFileFormat.py.py -h | --help | -e | --examples 155 156 Description: 157 Convert between ligand file formats. 158 159 The supported input and output file formats are: MDLMOL (.mol), MOL2 (.mol2), 160 and PDB (.pdb). 161 162 Options: 163 -e, --examples 164 Print examples. 165 -h, --help 166 Print this help message. 167 -i, --infile <infile> 168 Input file name. 169 -o, --outfile <outfile> 170 Output file name. 171 --overwrite 172 Overwrite existing files. 173 -w, --workingdir <dir> 174 Location of working directory which defaults to the current directory. 175 176 Examples: 177 To convert MDLMOL file format to MOL2 file format, type: 178 179 % PyMOLConvertLigandFileFormat.py -i caffeine.mol -o caffeine.mol2 180 181 To convert MDLMOL file format to PDB file format, type: 182 183 % PyMOLConvertLigandFileFormat.py -i caffeine.mol -o caffeine.pdb 184 185 Author: 186 Manish Sud(msud@san.rr.com) 187 188 See also: 189 PyMOLConvertPMLToPSE.py, PyMOLSplitChainsAndLigands.py, 190 PyMOLVisualizeMacromolecules.py 191 192 Copyright: 193 Copyright (C) 2026 Manish Sud. All rights reserved. 194 195 The functionality available in this script is implemented using PyMOL, a 196 molecular visualization system on an open source foundation originally 197 developed by Warren DeLano. 198 199 This file is part of MayaChemTools. 200 201 MayaChemTools is free software; you can redistribute it and/or modify it under 202 the terms of the GNU Lesser General Public License as published by the Free 203 Software Foundation; either version 3 of the License, or (at your option) any 204 later version. 205 206 """ 207 208 if __name__ == "__main__": 209 main()