<jupyter_start><jupyter_text>Data reconstruction by DRL- Convert molecular information into binary by molecular fingerprint, and restore it by DRL    - Molecular fingerprint: Avalon fingerprint    - DRL: REINVENT 2.0<jupyter_code>import glob
import os
import sys
import joblib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from rdkit import Chem

sys.path.append("../lib")
from fp_func import fp_func, fp_similarity

sys.path.append("../REINVENT/lib/")
from train_agent import train_agent
#load random 150 molecules from Bradley's dataset
file_path="../database/random_smiles.csv"
df=pd.read_csv(file_path)
smiles_list=list(df["SMILES"].values)
smiles_list[:3]
# DRL
def reconstruct(smiles):
    
    #calc fingerprint (fp)
    
    mol = Chem.MolFromSmiles(smiles)

    if os.getcwd().split("/")[-1] != "REINVENT":
        os.chdir('../REINVENT/')

    fp = fp_func(mol)
    fp = [str(i) for i in fp]
    fp = "".join(fp)

    #settings for DRL
    arg_dict = {
        "scoring_function": "fp_similarity",
        "scoring_function_kwargs": {
            "query_bit": fp,
        },
        "n_steps": 300,
        "verbose": False
    }

    # run
    train_agent(**arg_dict)

    os.chdir('../1_reconstruction_test')

    # analyze
    result_path = sorted(glob.glob("../REINVENT/data/results/*"))[-1]+"/memory"
    dqn_df = pd.read_csv(result_path, delimiter=" ")
    best_smiles = list(dqn_df.sort_values(
        ["Score", "PriorLogP"], ascending=False)[0:1]["SMILES"])[0]

    return best_smiles
from tqdm.notebook import tqdm
reconst_sm_list=[]

#loop
for sm1 in tqdm(smiles_list):
    try:
        sm2=reconstruct(sm1)
    except:
        sm2="C"
    reconst_sm_list.append(sm2)
    
    #joblib.dump(reconst_sm_list,"dqn_smiles_temp.bin")
    break
#show result
print(f"original: {sm1}")
print(f"reconstructed: {sm2}")<jupyter_output>original: c1ccc2c(c1)nc(s2)N
reconstructed: Nc1nc2ccccc2s1
{'fraction_non_alphanumeric': 0.13278008298755187, 'fraction_numerical': 0.015560165975103735, 'mean_word_length': 3.571090047393365, 'pattern_counts': {'":': 6, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '542822', 'license': 'MIT', 'n_tokens_mistral': 741, 'n_tokens_neox': 701, 'n_words': 146, 'path': '1_reconstruction_test/1_DRL_conversion.ipynb', 'repo_name': 'KanHatakeyama/anneal_project2', 'size': '1928'}