Replace optparse with argparse.

Optparse is deprecated.  Argparse will allow tighter integration with
PDB2PQR/APBS.
This commit is contained in:
Nathan Baker
2020-05-12 13:13:28 -07:00
parent aeb1663d7b
commit ae7e8b6430
3 changed files with 107 additions and 64 deletions

View File

@@ -4,6 +4,8 @@ from __future__ import print_function
import sys import sys
import pkg_resources import pkg_resources
import logging import logging
import argparse
logger = logging.getLogger("propka") logger = logging.getLogger("propka")
stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler = logging.StreamHandler(sys.stdout)
@@ -136,9 +138,95 @@ def parse_res_string(res_str):
return chain, resnum, inscode return chain, resnum, inscode
def build_parser(parser_=None):
"""Build an argument parser for PROPKA.
Args:
parser_: existing parser. If this is not None, then the PROPKA parser will
be created as a subparser to this existing parser. Otherwise, a
new parser will be created.
Returns:
ArgumentParser object.
"""
if parser_ is not None:
subparsers = parser_.add_subparsers(title="PROPKA sub-command group",
dest="propka_group", action="store_true")
parser = subparsers.add_parser("PROPKA", help="PROPKA invocation options",
dest="propka", action="store_true")
else:
parser = argparse.ArgumentParser(description=("PROPKA predicts the pKa values of ionizable "
"groups in proteins and protein-ligand "
"complexes based in the 3D structure"),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-f", "--file", action="append", dest="filenames",
help="read data from <filename>, i.e. <filename> is added to arguments")
parser.add_argument("-r", "--reference", dest="reference", default="neutral",
help=("setting which reference to use for stability calculations "
"[neutral/low-pH]"))
parser.add_argument("-c", "--chain", action="append", dest="chains",
help=('creating the protein with only a specified chain. Specify '
'" " for chains without ID [all]'))
parser.add_argument("-i", "--titrate_only", dest="titrate_only",
help=('Treat only the specified residues as titratable. Value should '
'be a comma-separated list of "chain:resnum" values; for example: '
'-i "A:10,A:11"'))
parser.add_argument("-t", "--thermophile", action="append", dest="thermophiles",
help=("defining a thermophile filename; usually used in "
"'alignment-mutations'"))
parser.add_argument("-a", "--alignment", action="append", dest="alignment",
help=("alignment file connecting <filename> and <thermophile> "
"[<thermophile>.pir]"))
parser.add_argument("-m", "--mutation", action="append", dest="mutations",
help=("specifying mutation labels which is used to modify "
"<filename> according to, e.g. N25R/N181D"))
parser.add_argument("-v", "--version", dest="version_label", default="Jan15",
help="specifying the sub-version of propka [Jan15/Dec19]")
parser.add_argument("-p", "--parameters", dest="parameters",
default=pkg_resources.resource_filename(__name__, "propka.cfg"),
help="set the parameter file [%default]")
parser.add_argument("-z", "--verbose", dest="verbosity", action="store_const",
const=2, help="output debugging information")
parser.add_argument("-q", "--quiet", dest="verbosity", action="store_const",
const=0, default=1, help="inhibit printing to stdout")
parser.add_argument("-o", "--pH", dest="pH", type=float, default=7.0,
help="setting pH-value used in e.g. stability calculations [7.0]")
parser.add_argument("-w", "--window", dest="window", nargs=3, type=float,
default=(0.0, 14.0, 1.0),
help=("setting the pH-window to show e.g. stability profiles "
"[0.0, 14.0, 1.0]"))
parser.add_argument("-g", "--grid", dest="grid", nargs=3, type=float,
default=(0.0, 14.0, 0.1),
help=("setting the pH-grid to calculate e.g. stability "
"related properties [0.0, 14.0, 0.1]"))
parser.add_argument("--mutator", dest="mutator",
help="setting approach for mutating <filename> [alignment/scwrl/jackal]")
parser.add_argument("--mutator-option", dest="mutator_options", action="append",
help="setting property for mutator [e.g. type=\"side-chain\"]")
parser.add_argument("-d", "--display-coupled-residues", dest="display_coupled_residues",
action="store_true", help=("Displays alternative pKa values due "
"to coupling of titratable groups"))
parser.add_argument("-l", "--reuse-ligand-mol2-files", dest="reuse_ligand_mol2_file",
action="store_true", default=False,
help=("Reuses the ligand mol2 files allowing the user to alter "
"ligand bond orders"))
parser.add_argument("-k", "--keep-protons", dest="keep_protons", action="store_true",
help="Keep protons in input file", default=False)
parser.add_argument("--protonate-all", dest="protonate_all", action="store_true",
help="Protonate all atoms (will not influence pKa calculation)",
default=False)
parser.add_argument("input_pdb",
help="read data from <filename>")
return parser
def loadOptions(*args): def loadOptions(*args):
""" """
Load the arguments parser with options. Note that verbosity is set as soon as this function is invoked. Load the arguments parser with options. Note that verbosity is set as soon
as this function is invoked.
Returns:
argparse namespace
""" """
from optparse import OptionParser from optparse import OptionParser
@@ -149,68 +237,17 @@ def loadOptions(*args):
parser = OptionParser(usage) parser = OptionParser(usage)
# loading the parser # loading the parser
parser.add_option("-f", "--file", action="append", dest="filenames", parser = build_parser()
help="read data from <filename>, i.e. <filename> is added to arguments")
parser.add_option("-r", "--reference", dest="reference", default="neutral",
help="setting which reference to use for stability calculations [neutral/low-pH]")
parser.add_option("-c", "--chain", action="append", dest="chains",
help='creating the protein with only a specified chain. Specify " " for chains without ID [all]')
parser.add_option("-i", "--titrate_only", dest="titrate_only",
help='Treat only the specified residues as titratable. Value should '
'be a comma-separated list of "chain:resnum" values; for example: '
'-i "A:10,A:11"')
parser.add_option("-t", "--thermophile", action="append", dest="thermophiles",
help="defining a thermophile filename; usually used in 'alignment-mutations'")
parser.add_option("-a", "--alignment", action="append", dest="alignment",
help="alignment file connecting <filename> and <thermophile> [<thermophile>.pir]")
parser.add_option("-m", "--mutation", action="append", dest="mutations",
help="specifying mutation labels which is used to modify <filename> according to, e.g. N25R/N181D")
parser.add_option("-v", "--version", dest="version_label", default="Jan15",
help="specifying the sub-version of propka [Jan15/Dec19]")
parser.add_option("-p", "--parameters",dest="parameters", default=pkg_resources.resource_filename(__name__, "propka.cfg"),
help="set the parameter file [%default]")
parser.add_option("-z", "--verbose", dest="verbosity", action="store_const", const=2,
help="output debugging information")
parser.add_option("-q", "--quiet", dest="verbosity", action="store_const", const=0, default=1,
help="inhibit printing to stdout")
parser.add_option("-o", "--pH", dest="pH", type="float", default=7.0,
help="setting pH-value used in e.g. stability calculations [7.0]")
parser.add_option("-w", "--window", dest="window", nargs=3, type="float", default=(0.0, 14.0, 1.0),
help="setting the pH-window to show e.g. stability profiles [0.0, 14.0, 1.0]")
parser.add_option("-g", "--grid", dest="grid", nargs=3, type="float", default=(0.0, 14.0, 0.1),
help="setting the pH-grid to calculate e.g. stability related properties [0.0, 14.0, 0.1]")
parser.add_option("--mutator", dest="mutator",
help="setting approach for mutating <filename> [alignment/scwrl/jackal]")
parser.add_option("--mutator-option", dest="mutator_options", action="append",
help="setting property for mutator [e.g. type=\"side-chain\"]")
parser.add_option("-d","--display-coupled-residues", dest="display_coupled_residues", action="store_true",
help="Displays alternative pKa values due to coupling of titratable groups")
parser.add_option("-l","--reuse-ligand-mol2-files", dest="reuse_ligand_mol2_file", action="store_true",
help="Reuses the ligand mol2 files allowing the user to alter ligand bond orders", default=False)
parser.add_option("-k","--keep-protons", dest="keep_protons", action="store_true",
help="Keep protons in input file", default=False)
parser.add_option("--protonate-all", dest="protonate_all", action="store_true",
help="Protonate all atoms (will not influence pKa calculation)", default=False)
# parsing and returning options and arguments # parsing and returning options and arguments
if len(args) == 0: if len(args) == 0:
# command line # command line
options, args = parser.parse_args() options = parser.parse_args()
else: else:
options, args = parser.parse_args(list(args)) options = parser.parse_args(list(args))
# adding specified filenames to arguments # adding specified filenames to arguments
if options.filenames: options.filenames.append(options.input_pdb)
for filename in options.filenames:
args.append(filename)
# checking at early stage that there is at least one pdbfile to work with. The error message is misleading
# if one is using the python interface via Molecular_container.
if len(args) == 0:
info("No pdbfile provided")
#sys.exit(9)
# Convert titrate_only string to a list of (chain, resnum) items: # Convert titrate_only string to a list of (chain, resnum) items:
if options.titrate_only is not None: if options.titrate_only is not None:
@@ -236,10 +273,7 @@ def loadOptions(*args):
logger.warning("Invalid verbosity level, using default") logger.warning("Invalid verbosity level, using default")
# done! # done!
return options, args return options
def makeTidyAtomLabel(name,element): def makeTidyAtomLabel(name,element):

View File

@@ -1,13 +1,18 @@
# entry point for propka script # entry point for propka script
import logging
import propka.lib, propka.molecular_container import propka.lib, propka.molecular_container
_LOGGER = logging.getLogger("PROPKA")
def main(): def main():
""" """
Reads in structure files, calculates pKa values, and prints pKa files Reads in structure files, calculates pKa values, and prints pKa files
""" """
# loading options, flaggs and arguments # loading options, flaggs and arguments
options, pdbfiles = propka.lib.loadOptions() options = propka.lib.loadOptions()
pdbfiles = options.filenames
for pdbfile in pdbfiles: for pdbfile in pdbfiles:
my_molecule = propka.molecular_container.Molecular_container(pdbfile, options) my_molecule = propka.molecular_container.Molecular_container(pdbfile, options)
@@ -25,7 +30,10 @@ def single(pdbfile, optargs=None):
single("protein.pdb", optargs=["--mutation=N25R/N181D", "-v", "--pH=7.2"]) single("protein.pdb", optargs=["--mutation=N25R/N181D", "-v", "--pH=7.2"])
""" """
optargs = optargs if optargs is not None else [] optargs = optargs if optargs is not None else []
options, ignored_pdbfiles = propka.lib.loadOptions(*optargs) options = propka.lib.loadOptions(*optargs)
pdbfile = options.filenames.pop(0)
if len(options.filenames) > 0:
_LOGGER.warning("Ignoring filenames: %s", options.filenames)
my_molecule = propka.molecular_container.Molecular_container(pdbfile, options) my_molecule = propka.molecular_container.Molecular_container(pdbfile, options)
my_molecule.calculate_pka() my_molecule.calculate_pka()

View File

@@ -16,7 +16,8 @@ def main():
Reads in structure files, calculates pKa values, and prints pKa files Reads in structure files, calculates pKa values, and prints pKa files
""" """
# loading options, flaggs and arguments # loading options, flaggs and arguments
options, pdbfiles = propka.lib.loadOptions() options = propka.lib.loadOptions()
pdbfiles = options.filenames
for pdbfile in pdbfiles: for pdbfile in pdbfiles:
my_molecule = propka.molecular_container.Molecular_container(pdbfile, options) my_molecule = propka.molecular_container.Molecular_container(pdbfile, options)