diff --git a/ties/__init__.py b/ties/__init__.py index 2bd6a8b..bff8871 100644 --- a/ties/__init__.py +++ b/ties/__init__.py @@ -1,4 +1,6 @@ import logging +logging.basicConfig(encoding='utf-8', level=logging.INFO) + from pathlib import Path from . import cli @@ -12,5 +14,3 @@ from ._version import __version__ __all__ = [Ligand, Protein, Pair, Config, LigandMap, Protein, __version__] - -logging.basicConfig(encoding='utf-8', level=logging.INFO) \ No newline at end of file diff --git a/ties/cli.py b/ties/cli.py index ec093bd..02c0baa 100755 --- a/ties/cli.py +++ b/ties/cli.py @@ -62,7 +62,7 @@ def command_line_script(): help='Align the coordinates in the "END" ligand to the "INITIAL" ligand using ' 'the generated maximum common substructure (MCS).') parser.add_argument("-v", "--logging-level", metavar="str or bool", dest="logging_level", - type=ArgparseChecker.logging_lvl, required=False, default="WARNING", + type=ArgparseChecker.logging_lvl, required=False, default="INFO", help="Logging level. Can be a boolean value " "(False disables logging by setting it to ERROR). " "A string should specify a logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL). ") @@ -152,6 +152,8 @@ def command_line_script(): # initialise the config class args = parser.parse_args() + # set the root logger + logging.getLogger().setLevel(args.logging_level) logger.setLevel(args.logging_level) for handler in logger.handlers: handler.setLevel(args.logging_level) diff --git a/ties/config.py b/ties/config.py index e86757b..e66b3a0 100644 --- a/ties/config.py +++ b/ties/config.py @@ -92,6 +92,11 @@ def __init__(self, **kwargs): # assign all the initial configuration values self.set_configs(**kwargs) + # logging + self.logging_breakdown = True + self.logging_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + self.logging_level = logging.INFO + @property def workdir(self): """ @@ -900,6 +905,9 @@ def get_serializable(self): if k in host_specific: continue + if type(v) is logging.Formatter: + continue + if type(v) is pathlib.PosixPath: v = str(v) diff --git a/ties/pair.py b/ties/pair.py index 61078b6..eda022b 100644 --- a/ties/pair.py +++ b/ties/pair.py @@ -158,6 +158,8 @@ def superimpose(self, **kwargs): if starting_node_pairs: logger.debug(f'Starting nodes will be used: {starting_node_pairs}') + logging_key = str(self) + # fixme - simplify to only take the ParmEd as input suptop = superimpose_topologies(ligand1_nodes.values(), ligand2_nodes.values(), disjoint_components=self.config.allow_disjoint_components, @@ -178,6 +180,7 @@ def superimpose(self, **kwargs): starting_node_pairs=starting_node_pairs, parmed_ligA=parmed_ligA, parmed_ligZ=parmed_ligZ, starting_pair_seed=self.config.superimposition_starting_pair, + logging_key=logging_key, config=self.config) self.set_suptop(suptop, parmed_ligA, parmed_ligZ) diff --git a/ties/topology_superimposer.py b/ties/topology_superimposer.py index 08489b8..b9b942e 100644 --- a/ties/topology_superimposer.py +++ b/ties/topology_superimposer.py @@ -3166,6 +3166,7 @@ def superimpose_topologies(top1_nodes, check_atom_names_unique=True, starting_pairs_heuristics=True, starting_pair_seed=None, + logging_key=None, config=None): """ The main function that manages the entire process. @@ -3174,6 +3175,12 @@ def superimpose_topologies(top1_nodes, - check if each molecule topology is connected """ + if config.logging_breakdown: + file_log_handler = logging.FileHandler(config.workdir / f'{logging_key}.log') + file_log_handler.setLevel(config.logging_level) + file_log_handler.setFormatter(config.logging_formatter) + logger.addHandler(file_log_handler) + if not ignore_charges_completely: whole_charge = SuperimposedTopology.validate_charges(top1_nodes, top2_nodes) @@ -3382,6 +3389,9 @@ def take_largest(x, y): logger.info(f'Disappearing atoms: { (len(top1_nodes) - len(suptop.matched_pairs)) / len(top1_nodes) * 100:.1f}%') logger.info(f'Appearing atoms: { (len(top2_nodes) - len(suptop.matched_pairs)) / len(top2_nodes) * 100:.1f}%') + if config.logging_breakdown: + logger.removeHandler(file_log_handler) + return suptop