Ejemplo n.º 1
0
def test_node_expandability(basic_tree: Tree, use_normalized_rmsd):
    basic_tree.use_normalized_rmsd = use_normalized_rmsd
    basic_tree.create_root_node()
    parent_node = basic_tree.nodes[0]
    parent_metric = basic_tree.get_metric(parent_node)
    # deactivate relative improvement check
    basic_tree.min_relative_improvement = 1

    node = basic_tree.create_node(0)
    node_metric = parent_metric - 0.5 * basic_tree.min_absolute_improvement
    node.nrmsd = node.rmsd = node_metric
    assert basic_tree.is_expandable(node) is False

    node_metric = parent_metric - 1.5 * basic_tree.min_absolute_improvement
    node.nrmsd = node.rmsd = node_metric
    assert basic_tree.is_expandable(node) is True

    # deactivate absolute improvement check and reactivate relative
    basic_tree.min_absolute_improvement = parent_metric
    basic_tree.min_relative_improvement = 0.1

    node_metric = parent_metric * (1 -
                                   0.5 * basic_tree.min_relative_improvement)
    node.nrmsd = node.rmsd = node_metric
    assert basic_tree.is_expandable(node) is False

    node_metric = parent_metric * (1 -
                                   1.5 * basic_tree.min_relative_improvement)
    node.nrmsd = node.rmsd = node_metric
    assert basic_tree.is_expandable(node) is True
Ejemplo n.º 2
0
class LigBinder:
    def __init__(self,
                 path: str = ".",
                 config_file: Optional[str] = None) -> None:
        self.path = path
        if config_file is not None:
            SETTINGS.update_settings_with_file(
                self.get_config_file(config_file))
        self.tree = Tree(self.path, **SETTINGS["tree"])

    def get_config_file(self,
                        config_file: Optional[str] = None) -> Optional[str]:
        local_default_config_file = os.path.join(self.path, "config.yml")
        if config_file is not None and os.path.exists(config_file):
            return config_file
        elif os.path.exists(local_default_config_file):
            return local_default_config_file
        return None

    def run(self):
        self.log_initial_info()
        metric_name = "nrmsd" if self.tree.use_normalized_rmsd else "rmsd"
        self.setup_hmr()
        if len(self.tree.nodes) == 0:
            logger.info("No root node found. Instantiating...")
            self.tree.create_root_node(**SETTINGS["data_files"])
        while not self.tree.has_converged() and self.tree.can_grow():
            node: Node = self.tree.create_node_from_candidate()
            logger.info("New node chosen for expansion.")
            logger.info(f"\tdepth: {node.depth}/{self.tree.max_depth}")
            parent_node = self.tree.nodes[node.parent_id]
            parent_metric = self.tree.get_metric(parent_node)
            logger.info(f"\t{metric_name}: {parent_metric:.3f}")
            engine = AmberMDEngine(node.path, **SETTINGS["md"])
            engine.run()
            node.calc_node_rmsd()
            node_metric = self.tree.get_metric(node)
            if node_metric < parent_metric:
                logger.info(
                    f"Node {node.node_id} improved {metric_name} by {parent_metric - node_metric:.3f}!"
                    f" current {metric_name}: {node_metric:.3f}")
        logger.info("Exploration finished.")
        Reporter(self.tree).compile_results()

    def setup_hmr(self):
        if not SETTINGS["md"]["use_hmr"]:
            return
        top_file = SETTINGS["data_files"]["top_file"]
        logger.info(f"Applying HMR on topology file {top_file}")
        parm = AmberParm(top_file)
        HMassRepartition(parm).execute()
        parm.write_parm(top_file)
        logger.info("HMR applied")

    def log_initial_info(self):
        logger.info(f"Running ligbinder v{VERSION}")
        logger.info(f"Current directory: {os.path.abspath(os.getcwd())}")
        logger.info("Full settings for this run:")
        logger.info(SETTINGS.data)
Ejemplo n.º 3
0
def test_tree_create_node(basic_tree: Tree):
    basic_tree.create_root_node()
    node = basic_tree.create_node(0)
    assert len(basic_tree.nodes) == 2
    assert node.parent_id == 0
    assert node.rmsd is None
    assert node.depth == 1
    assert len(basic_tree.nodes[0].children) == 1
    assert os.path.exists(os.path.join(node.path, "top.prmtop"))
    assert os.path.exists(os.path.join(node.path, "initial.rst7"))
    assert os.path.exists(os.path.join(node.path, "ref.crd"))
Ejemplo n.º 4
0
def test_tree_expandability(use_normalized_rmsd, basic_tree: Tree):
    basic_tree.use_normalized_rmsd = use_normalized_rmsd
    basic_tree.max_children = 2
    basic_tree.max_depth = 1
    basic_tree.create_root_node()
    node = basic_tree.create_node(0)
    node.rmsd = basic_tree.nodes[0].rmsd + 1
    node.nrmsd = basic_tree.nodes[0].nrmsd + 1
    basic_tree.max_nodes = 1
    assert basic_tree.can_grow() is False
    basic_tree.max_nodes = 500
    assert basic_tree.can_grow()
    node = basic_tree.create_node_from_candidate()
    node.rmsd = basic_tree.nodes[0].rmsd + 1
    node.nrmsd = basic_tree.nodes[0].nrmsd + 1
    assert basic_tree.can_grow() is False
Ejemplo n.º 5
0
def test_tree_create_root_node(basic_tree: Tree):
    node = basic_tree.create_root_node()
    assert node.rmsd == pytest.approx(17.29049097)
    assert node.parent_id is None
    assert node.depth == 0
    assert os.path.exists(os.path.join(node.path, ".info.yml"))
    assert len(basic_tree.nodes) == 1
Ejemplo n.º 6
0
def test_node_weight(basic_tree: Tree):
    root_node = basic_tree.create_root_node()
    assert basic_tree.get_node_weight(root_node) == 1

    node = basic_tree.create_node(0)
    assert basic_tree.get_node_weight(root_node) == 1
    assert basic_tree.get_node_weight(node) == 1

    basic_tree.create_node(0)
    last_node = basic_tree.create_node(0)
    assert basic_tree.get_node_weight(root_node) == 3
    assert basic_tree.get_node_weight(node) == 1

    basic_tree.create_node(last_node.node_id)
    basic_tree.create_node(last_node.node_id)
    assert basic_tree.get_node_weight(root_node) == 4
    assert basic_tree.get_node_weight(node) == 1
    assert basic_tree.get_node_weight(last_node) == 2
Ejemplo n.º 7
0
def test_tree_has_converged(use_normalized_rmsd, basic_tree: Tree):
    basic_tree.use_normalized_rmsd = use_normalized_rmsd
    basic_tree.create_root_node()
    assert basic_tree.has_converged() is False
    basic_tree.nodes[0].nrmsd = basic_tree.nodes[0].rmsd = 0.2
    assert basic_tree.has_converged() is True