Skip to content

Commit 099cb31

Browse files
committed
added function that finds all possible partial reatt rxns and adds to seed
1 parent 1c03b8a commit 099cb31

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

rmgpy/molecule/fragment_utils.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,88 @@ def process_new_fragment(species_smiles, starting_fragment_smiles,species_cuttin
546546
def make_new_reaction_string(species_smiles, starting_fragment_smiles, frag_list):
547547
frag_list_str = " + ".join(frag_list)
548548
return f"{species_smiles} + {starting_fragment_smiles} => {frag_list_str}"
549+
550+
551+
def generate_add_partial_reattachment_reactions(seed_dir, starting_fragments)
552+
seed_reactions_filename = os.path.join(seed_dir, "reactions.py")
553+
seed_dictionary_filename = os.path.join(seed_dir, "dictionary.txt")
554+
555+
with open(seed_dictionary_filename,'r') as f:
556+
dictionary_lines = f.readlines()
557+
558+
fragment_to_name_dictionary = {}
559+
560+
dlines_iter = iter(dictionary_lines)
561+
name = next(dlines_iter,"end").strip().strip('\n')
562+
line = next(dlines_iter,"end")
563+
while line != "end" and name !="end":
564+
species_adjlist =""
565+
line = next(dlines_iter,"end")
566+
while line.strip().strip('\n') != "":
567+
species_adjlist +=line
568+
line = next(dlines_iter, "end")
569+
570+
f = Fragment().from_adjacency_list(species_adjlist)
571+
fragment_to_name_dictionary[f] = name
572+
name = next(dlines_iter,"end").strip().strip('\n')
573+
574+
rxn_strs = []
575+
for f in fragment_to_name_dictionary.keys():
576+
smiles = f.smiles
577+
try:
578+
for starting_fragment in starting_fragments:
579+
options = process_new_fragment(smiles, starting_fragment,species_cutting_threshold=12)
580+
if type(options) == list:
581+
for option in options:
582+
option_frags = [Fragment().from_smiles_like_string(x) for x in option]
583+
if all([option in fragment_to_name_dictionary.keys() for option in option_frags]):
584+
smiles_name = fragment_to_name_dictionary[Fragment().from_smiles_like_string(smiles)]
585+
starting_fragment_name = fragment_to_name_dictionary[Fragment().from_smiles_like_string(starting_fragment)]
586+
option_name = [fragment_to_name_dictionary[x] for x in option_frags]
587+
rxn_str = make_new_reaction_string(smiles_name, starting_fragment_name, option_name)
588+
if rxn_str not in rxn_strs:
589+
rxn_strs.append(rxn_str)
590+
591+
except:
592+
continue
593+
594+
seed_reaction_template = """
595+
entry(
596+
index = {},
597+
label = \"{}\",
598+
degeneracy = 1.0,
599+
reversible = False,
600+
kinetics = Arrhenius(A=(3.18795e+13,'m^3/(mol*s)'), n=-1.177, Ea=(0,'kJ/mol'), T0=(1,'K'), Tmin=(300,'K'), Tmax=(1500,'K')),
601+
longDesc =
602+
\"\"\"
603+
\"\"\",
604+
)"""
605+
606+
with open(seed_reactions_filename,"r") as f:
607+
lines = f.readlines()
608+
for line in lines[::-1]:
609+
if "index = " in line:
610+
largest_idx = int(line.split()[-1].strip('\n').strip(','))
611+
break
612+
613+
rxn_strs_in_seed_core = []
614+
for line in lines:
615+
if "label = " in line:
616+
r = line.split("\"")[1]
617+
if r not in rxn_strs_in_seed_core:
618+
rxn_strs_in_seed_core.append(r)
619+
620+
j = 0
621+
seed_reaction_entries = []
622+
for i, rxn_str in enumerate(rxn_strs):
623+
if rxn_str not in rxn_strs_in_seed_core:
624+
j+=1
625+
entry = seed_reaction_template.format(largest_idx +j,rxn_str)
626+
seed_reaction_entries.append(entry+"\n")
627+
628+
if seed_reaction_entries != []:
629+
with open(seed_reactions_filename,'a') as f:
630+
f.writelines(seed_reaction_entries)
631+
return f"Added {len(seed_reaction_entries)} partial reattachment reactions to {seed_reactions_filename}."
632+
else:
633+
return "No partial reattachment reactions to add"

0 commit comments

Comments
 (0)