<jupyter_start><jupyter_text>Results - Reaction search example> An example of a reaction search in the Schneider 50k data set<jupyter_code># hide
import json
import numpy as np
import pandas as pd
import tmap as tm
from tqdm import tqdm

from rdkit.Chem import AllChem
from rdkit.Chem import Draw

# use IPythonConsole for pretty drawings
from rdkit.Chem.Draw import IPythonConsole
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code># data
lf = tm.LSHForest(256, 128)
mh_encoder = tm.Minhash()

with open('../data/rxnclass2name.json', 'r') as f:
    rxnclass2name = json.load(f)

schneider_df = pd.read_csv('../data/schneider50k.tsv', sep='\t', index_col=0)
ft_10k_fps = np.load('../data/fps_ft.npz')['fps']
schneider_df['mhfp'] =  [mh_encoder.from_weight_array(fp.tolist(), method="I2CWS") for fp in tqdm(ft_10k_fps)]<jupyter_output>100%|██████████| 50000/50000 [00:15<00:00, 3194.60it/s]<jupyter_text>Initiate LSHForest with training set fingerprints<jupyter_code># data
train_df = schneider_df[schneider_df.split=='train']
train_df.reset_index(inplace=True)
lf.batch_add(train_df.mhfp.values.tolist())
lf.index()<jupyter_output><empty_output><jupyter_text>Query for similar reactions<jupyter_code># data
for i, row in schneider_df[schneider_df.split=='test'].sample(n=10, random_state=42).iterrows():
    print('------------------------------------------------------------------------------------------------')
    print('Query: Reaction class - {} {}'.format(row['rxn_class'], rxnclass2name[row['rxn_class']]))
    display(AllChem.ReactionFromSmarts(row['rxn'], useSmiles=True))
    print(row['rxn'])
    print('------------------------------------------------------------------------------------------------')
    print()
    
    nns = lf.query_linear_scan(row['mhfp'], 3, kc=200)
    for n, (_, j) in enumerate(nns):
        rxn = train_df.iloc[j]['rxn']
        display(AllChem.ReactionFromSmarts(rxn, useSmiles=True))
        print(rxn)
              
        print('NN-{} - {} {}'.format(n+1, train_df.iloc[j]['rxn_class'], rxnclass2name[train_df.iloc[j]['rxn_class']]))
        
    print()
    print()
    print()<jupyter_output>------------------------------------------------------------------------------------------------
Query: Reaction class - 5.1.1 N-Boc protection
{'fraction_non_alphanumeric': 0.25781584582441114, 'fraction_numerical': 0.025267665952890792, 'mean_word_length': 2.761674718196457, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '41462', 'license': 'MIT', 'n_tokens_mistral': 856, 'n_tokens_neox': 785, 'n_words': 145, 'path': 'nbs/06_results_reaction_search.ipynb', 'repo_name': 'jidushanbojue/rxnfp', 'size': '2335'}