Exemplo n.º 1
0
def test_reset_tree():
    finder = AiZynthFinder()
    finder.target_smiles = "CCCO"
    finder.prepare_tree()

    assert finder.tree is not None

    finder.target_smiles = "CCO"

    assert finder.tree is None
Exemplo n.º 2
0
def _process_multi_smiles(filename: str, finder: AiZynthFinder,
                          output_name: str, do_clustering: bool) -> None:
    output_name = output_name or "output.hdf5"
    with open(filename, "r") as fileobj:
        smiles = [line.strip() for line in fileobj.readlines()]

    results = defaultdict(list)
    for smi in smiles:
        finder.target_smiles = smi
        finder.prepare_tree()
        search_time = finder.tree_search()
        finder.build_routes()
        stats = finder.extract_statistics()

        logger().info(f"Done with {smi} in {search_time:.3} s")
        if do_clustering:
            _do_clustering(finder, stats, detailed_results=True)
        for key, value in stats.items():
            results[key].append(value)
        results["top_scores"].append(", ".join(
            "%.4f" % score for score in finder.routes.scores))
        results["trees"].append(finder.routes.dicts)

    data = pd.DataFrame.from_dict(results)
    with warnings.catch_warnings():  # This wil suppress a PerformanceWarning
        warnings.simplefilter("ignore")
        data.to_hdf(output_name, key="table", mode="w")
    logger().info(f"Output saved to {output_name}")
Exemplo n.º 3
0
 def wrapper(expansions, stock):
     finder = AiZynthFinder()
     root_smi = list(expansions.keys())[0]
     setup_policies(expansions, config=finder.config)
     setup_stock(finder.config, *stock)
     finder.target_smiles = root_smi
     return finder
Exemplo n.º 4
0
def _process_single_smiles(
    smiles: str,
    finder: AiZynthFinder,
    output_name: str,
    do_clustering: bool,
    route_distance_model: str = None,
) -> None:
    output_name = output_name or "trees.json"
    finder.target_smiles = smiles
    finder.prepare_tree()
    finder.tree_search(show_progress=True)
    finder.build_routes()

    with open(output_name, "w") as fileobj:
        json.dump(finder.routes.dicts, fileobj, indent=2)
    logger().info(f"Trees saved to {output_name}")

    scores = ", ".join("%.4f" % score for score in finder.routes.scores)
    logger().info(f"Scores for best routes: {scores}")

    stats = finder.extract_statistics()
    if do_clustering:
        _do_clustering(finder,
                       stats,
                       detailed_results=False,
                       model_path=route_distance_model)
    stats_str = "\n".join(f"{key.replace('_', ' ')}: {value}"
                          for key, value in stats.items())
    logger().info(stats_str)
Exemplo n.º 5
0
def process_smiles_list(finder: AiZynthFinder,
                        target_smiles: List[str]) -> List[Dict[str, str]]:
    stats_list: List[Dict[str, str]] = []
    for smiles in target_smiles:
        finder.target_smiles = smiles
        finder.tree_search()
        finder.build_routes()
        stats_list.append(finder.extract_statistics())
    return stats_list
Exemplo n.º 6
0
def generate_images(smiles, out_dir, config):
    finder = AiZynthFinder(configfile=config)
    finder.stock.select("zinc")
    finder.expansion_policy.select("uspto")
    finder.filter_policy.select("uspto")

    if not os.path.exists(out_dir):
        os.mkdir(out_dir)
    finder.target_smiles = smiles
    finder.tree_search()
    finder.build_routes()
    if finder.routes.images:
        for n, image in enumerate(finder.routes.images):
            image.save(f"{out_dir}/route{n:03d}.png")
        # Combine 4 images into a single image by ImageMagick
        images = glob.glob(f'{out_dir}/route*.png')
        for i, imgs in enumerate(chunked(sorted(images), 3)):
            concat_images(imgs, f"{out_dir}/result{i}.png")