<jupyter_start><jupyter_text>Load general templates, which only include reaction core<jupyter_code>with open('../data/templates_general.json', 'rb') as fid:
    templates = json.load(fid)
reactions = []
good_templates = []
counts = []
for template in templates:
    try:
        rxn = AllChem.ReactionFromSmarts(str(template))
        rxn.Initialize()
        [rct.UpdatePropertyCache() for rct in rxn.GetReactants()]
        [Chem.AssignStereochemistry(rct) for rct in rxn.GetReactants()]
        if rxn.Validate()[1] == 0:
            reactions.append(rxn)
            good_templates.append(template)
            counts.append(templates[template])
    except ValueError as e:
        pass
print('Loaded {} templates'.format(len(reactions)))
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>What are the most common patterns of the sites of retro disconnections?<jupyter_code>prod_templates = {}
for template in templates:
    prod = template.split('>>')[0]
    if prod == '([c;H0:1]-[n;H0:2](:[c;H0:3]):[cH:4]:[c;H0:5]:[c;H0:6])':
        print(template)
    if prod in prod_templates:
        prod_templates[prod] += templates[template]
    else:
        prod_templates[prod] = templates[template]
sorted_templates = sorted(prod_templates.iteritems(), key = lambda x: x[1], reverse=True)
print('\n{} ........................ {}'.format(sorted_templates[:10], sorted_templates[-10:]))
print('num unique: {}'.format(len(prod_templates)))

with open('../data/templates_general_prods.json', 'wb') as fid:
    json.dump(prod_templates, fid, indent=4)
df = pd.DataFrame({'pop': [np.log10(x) for x in templates.values()]})
plot = df['pop'].hist(bins=25, weights=np.power(10.0, df['pop']))
plot.set_title("Template popularity ({} total examples, {} templates)".format(sum(templates.values()), len(templates)))
plot.set_xlabel("Number of precedents in template (log10)")
plot.set_ylabel("Number of precedents")<jupyter_output><empty_output><jupyter_text>How many unique precursors are there for a given disconnection?<jupyter_code>plot = df['pop'].hist(bins=25)
plot.set_title("Template popularity ({} total examples)".format(sum(templates.values())))
plot.set_xlabel("Number of precedents (log10)")
plot.set_ylabel("Number of templates")
prod_templates_unique = {}
ex_templates = {}
for template in templates:
    prod = template.split('>>')[0]
    if prod == '([c;H0:1]-[c;H0:2])':
        ex_templates[template] = templates[template]
    if prod in prod_templates_unique:
        prod_templates_unique[prod] += 1
    else:
        prod_templates_unique[prod] = 1
sorted_templates_unique = sorted(prod_templates_unique.iteritems(), key = lambda x: x[1], reverse=True)
print('\n{} ........................ {}'.format(sorted_templates_unique[:10], sorted_templates_unique[-10:]))
print('num unique: {}'.format(len(prod_templates)))
with open('../data/templates_general_prods_unique_precursor_options.json', 'wb') as fid:
    json.dump(prod_templates_unique, fid, indent=4)
with open('../data/templates_general_prods_unique_precursor_options_ex.json', 'wb') as fid:
    json.dump(ex_templates, fid, indent=4)
print('Example for c-c disconnection:')
for (key, val) in sorted(ex_templates.iteritems(), key = lambda x: x[1], reverse=True)[:10]:
    print('{} {}'.format(val, key))

df = pd.DataFrame({'num': [np.log10(x) for x in prod_templates_unique.values()]})
plot = df['num'].hist(bins=25)
plot.set_title("Unique precursors ({} total templates)".format(len(prod_templates_unique)))
plot.set_xlabel("Number of unique precursors for a given disconnection (log10)")
plot.set_ylabel("Number of unique disconnections")
df = pd.DataFrame({'num': [np.log10(x) for x in prod_templates_unique.values()]})
plot = df['num'].hist(bins=25, weights=np.power(10.0, df['num']))
plot.set_title("Unique precursors ({} total templates)".format(len(prod_templates_unique)))
plot.set_xlabel("Number of unique precursors for a given disconnection (log10)")
plot.set_ylabel("Number of unique templates")<jupyter_output><empty_output><jupyter_text>What is the largest fragment that determines a retro disconnection?<jupyter_code>num_atoms = {}
for template in templates:
    num = template.split('>>')[0].count('[')
    if num in num_atoms:
        num_atoms[num] += 1
    else:
        num_atoms[num] = 1
sorted_num_atoms = sorted(num_atoms.iteritems(), key = lambda x: x[0])
print('List of (# atoms, # templates)')
print(sorted_num_atoms)

num_atoms = {}
for template in templates:
    num = template.split('>>')[0].count('[')
    if num in num_atoms:
        num_atoms[num] += templates[template]
    else:
        num_atoms[num] = templates[template]
sorted_num_atoms = sorted(num_atoms.iteritems(), key = lambda x: x[0])
print('List of (# atoms, # training examples)')
print(sorted_num_atoms)
plot = plt.scatter(num_atoms.keys(), num_atoms.values())
plt.title("Size of reaction center ({} total examples)".format(sum(num_atoms.values())))
plt.xlabel("Number of involved product atoms")
plt.ylabel("Number of training examples")
plt.yscale('log')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16542386185243327, 'fraction_numerical': 0.013932496075353218, 'mean_word_length': 5.200729927007299, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, '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': '36215', 'license': 'MIT', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1684, 'n_words': 356, 'path': 'retrosim/scripts/analyze_templates.ipynb', 'repo_name': 'medicine-wave/retrosim', 'size': '5096'}