File size: 6,298 Bytes
5631464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Functionalities related to CNS modules."""

import os
import shutil
import tarfile
from pathlib import Path

from haddock import log
from haddock import toppar_path as global_toppar
from haddock.core.defaults import cns_exec as global_cns_exec
from haddock.core.typing import Any, FilePath, Optional, Union
from haddock.gear.expandable_parameters import populate_mol_parameters_in_module
from haddock.libs.libio import working_directory
from haddock.modules import BaseHaddockModule


class BaseCNSModule(BaseHaddockModule):
    """
    Operation module for CNS.

    Contains additional functionalities excusive for CNS modules.
    """

    def __init__(
        self, order: int, path: Path, initial_params: FilePath, cns_script: FilePath
    ) -> None:
        """
        Instantitate CNSModule.

        Parameters
        ----------
        cns_script : str or pathlib.Path
            Path to the main module's cns script.
        """
        super().__init__(order, path, initial_params)

        self.cns_folder_path = Path(Path(cns_script).resolve().parent)
        self.cns_protocol_path = Path(cns_script)
        self.toppar_path = global_toppar
        self.recipe_str = self.cns_protocol_path.read_text()

    def run(self, **params: Any) -> None:
        """Execute the module."""
        log.info(f"Running [{self.name}] module")

        self.update_params(**params)

        # the `mol_*` parameters exist only for CNS jobs.
        if self._num_of_input_molecules:
            populate_mol_parameters_in_module(
                self._params,
                self._num_of_input_molecules,
                self._original_params,
            )

        self.add_parent_to_paths()
        self.envvars = self.default_envvars()

        if self.params["self_contained"]:
            self.make_self_contained()

        with working_directory(self.path):
            self._run()

        log.info(f"Module [{self.name}] finished.")

    def default_envvars(self) -> dict[str, str]:
        """Return default env vars updated to `envvars` (if given)."""
        default_envvars = {
            "MODULE": str(self.cns_folder_path),
            "MODDIR": ".",
            "TOPPAR": str(self.toppar_path),
        }

        return default_envvars

    def save_envvars(self, filename: FilePath = "envvars") -> None:
        """Save envvars needed for CNS to a file in the module's folder."""
        # there are so few variables, best to handle them by hand
        lines = (
            "#!/bin/bash",
            "# for debugging purposes source this file from within the ",
            "# module folder for example, from within '00_topoaa'",
            "export MODULE=cns",
            "export MODDIR=.",
            "export TOPPAR=../toppar",
        )

        fstr = os.linesep.join(lines)
        Path(self.path, filename).write_text(fstr)
        return

    def make_self_contained(self) -> None:
        """Create folders to make run self-contained."""
        _ = Path(self.path, "cns")
        shutil.copytree(self.cns_folder_path, _)
        self.cns_folder_path = Path(".", "cns")

        self.cns_protocol_path = Path(
            self.cns_folder_path,
            self.cns_protocol_path.name,
        )

        if not Path(self.toppar_path.name).exists():
            shutil.copytree(self.toppar_path, self.toppar_path.name)
        self.toppar_path = Path("..", self.toppar_path.name)

        self.envvars = self.default_envvars()
        self.save_envvars()

        _cns_exec = self.params["cns_exec"] or global_cns_exec
        new_cns = Path(".", Path(_cns_exec).name)
        if not new_cns.exists():
            shutil.copyfile(_cns_exec, new_cns)
            shutil.copystat(_cns_exec, new_cns)
            self.params["cns_exec"] = Path("..", Path(_cns_exec).name)

    def get_ambig_fnames(
        self, prev_ambig_fnames: list[Union[None, FilePath]]
    ) -> Union[list[FilePath], None]:
        """Get the correct ambiguous restraint names.

        Parameters
        ----------
        prev_ambig_fnames : list
            list of ambig_fname files encoded in previous models

        Returns
        -------
        ambig_fnames : list or None
            list of ambig_fname files to be used by the CNS module
        """
        ambig_fname: Optional[Path] = self.params["ambig_fname"]
        ambig_fnames: Union[list[FilePath], None] = None
        if ambig_fname:
            if ambig_fname.name.endswith("tgz"):
                self.log(f"Searching for tbl files in {ambig_fname}")
                basepath = ambig_fname.parent
                # Open archive to retrieve members
                with tarfile.open(ambig_fname, mode="r:gz") as targz:
                    # Get filenames
                    tbl_files = targz.getmembers()
                # At that stage, they should be already extracted
                # and we simply need to generate the full paths
                ambig_fnames = []
                for tbl in tbl_files:
                    fname = tbl.name
                    # Filter non-".tbl" and hidden files
                    if fname.startswith("."):
                        continue
                    # Build path
                    tbl_fpath = Path(basepath, fname)
                    # Make sure it exists
                    if tbl_fpath.exists() and tbl_fpath.suffix == ".tbl":
                        ambig_fnames.append(tbl_fpath)
                # abort execution if no files are found
                if len(ambig_fnames) == 0:
                    raise Exception(f"No tbl files found in {ambig_fname} !")
                self.log(f"Found {len(ambig_fnames)} tbl files")
                # Sort them to make sure we get the same order every time
                ambig_fnames.sort()
        else:
            if self.params["previous_ambig"]:
                # check if there is restraint information in all models
                if None in prev_ambig_fnames:
                    raise Exception(
                        "'previous_ambig' option selected but no available "
                        "restraint information in models"
                    )
                self.log("Using previously defined restraints")
                ambig_fnames = prev_ambig_fnames.copy()
        return ambig_fnames