From a22bb488792f35794becf2b8123eec065eb23a7b Mon Sep 17 00:00:00 2001 From: FAGES Timothee <timothee.fages@univ-lorraine.fr> Date: Fri, 3 May 2024 17:23:14 +0000 Subject: [PATCH] Upload New File --- TIRAMISU/Main_exe_2.py | 194 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 TIRAMISU/Main_exe_2.py diff --git a/TIRAMISU/Main_exe_2.py b/TIRAMISU/Main_exe_2.py new file mode 100644 index 0000000..e21e353 --- /dev/null +++ b/TIRAMISU/Main_exe_2.py @@ -0,0 +1,194 @@ +import time +import datetime +import pickle +import sys +import os +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +from TIRAMISU import read_model_new as rm +from TIRAMISU import make_translation as mt +from TIRAMISU import add_and_write_mechanism as awm + + +# ============================================================================= +# ############################# INITIALISATION ############################### +# ============================================================================= + +with open(sys.argv[1], 'r') as file_input : + file = file_input.readlines() +# with open("0.txt", 'r') as file_input : +# file = file_input.readlines() +file=[x for x in file if x.strip()[0] != '!'] +n=10000 +core_path=file[0].strip() +translate_path=file[1].strip() +name_core=os.path.basename(core_path) +name_translate=os.path.basename(translate_path) +output=file[2].strip() +intro='!' +task='full' +task_requested=False +order_list=['kt'] +order_requested=False +string_to_del=[] +list_to_del=[] + +inp=f'{output}tmp_species_traduction.inp' +with open(f'{output}{name_translate}.pick', 'rb') as m1 : + model_to_translate = pickle.load(m1) +with open(f'{output}{name_core}.pick', 'rb') as m2 : + model_core = pickle.load(m2) +translation, warning=mt.parse_file(inp) +translation={key : value for key,value in translation.items() if key != value} +count=0 +for element in translation : + if translation[element] not in model_core.reactions[1] : + print(f'{element} {translation[element]} not in species !') + else : + count+=1 + +print(f'\nnb of species translated vs nb of translation : {count} {len(translation)}') +print(f'nb of species/reactions in model_to_translate : {len(model_to_translate.reactions[1])} {len(model_to_translate.reactions[2])}') +print(f'nb of species/reactions in model_core : {len(model_core.reactions[1])} {len(model_core.reactions[2])}\n') + +out= f'{output}new_model_TIRAMISU' +md_translate=f'{name_translate}' +md_core=f'{name_core}' +to_add=[] + +depth='' + +# ============================================================================= +# ############################### PARAMETERS ################################# +# ============================================================================= + +for line in file : + if "introduction=" in line.strip()[:13] : + intro='!'+str(line.strip()[13:]) + elif "n=" in line[:2].strip() : + n=int(line.strip()[2:]) + elif "task=" in line.strip()[:5] : + task_request=True + if 'full' in line.strip()[5:].casefold() : + task='full' + elif 'extraction' in line.strip()[5:].casefold() : + task='extraction' + else : + raise Exception('Error: unknown merging task requested. Only "extraction" or "full" are accepted.') + elif "order=" in line.strip()[:6] : + order_requested=True + order_list=line.strip()[6:].split(',') + order_list=[x.strip().lower() for x in order_list] + elif "string_to_del=" in line.strip()[:14] : + string_to_del=line.strip()[14:].split(',') + string_to_del=[x.strip().lower() for x in string_to_del] + elif "list_to_del=" in line.strip()[:12] : + list_to_del=line.strip()[12:].split(',') + list_to_del=[x.strip().lower() for x in list_to_del] + +# ============================================================================= +# ############################### VALIDATION ################################# +# ============================================================================= + +if order_requested is False : + print('No mechanism designed as main ("kt", "k", "t", "n").\nk = model_core main for kinetic data and model_to_translate main for thermo data.\n'+\ + 't = model_to_translate main for kinetic data and core_mechanism main for thermo data.\n'+\ + 'kt = model_core main for kinetic and themo data.\nn = modle_to_translate main for kinetic and thermo data.\n'+\ + 'Default order task is order=kt. You can also specify a list of order to generate multiple mechanism in the form "order=order1, order2, order3, ...".\n'+\ + '\nORDER FOR EXTRACTION_MERGING IS ALWAYS SET TO "kt" !!!') +if task_requested is False : + print('No merging task requested ("extraction" or "full"). Default merging task is task=full.\n') +if string_to_del==[] : + print('No string to remove. You can specify to delete species containing a specific string with "string_to_del=chr1, chr2, chr3, ...".\n') +if list_to_del==[] : + print('No species to remove. You can specify to delete specific species "list_to_del=species1, species2, species3, ...".\n') + +# ============================================================================= +# ############################### EXTRACTION ################################# +# ============================================================================= + +if task == 'extraction' : + depth=1e10 + depth_requested=False + list_to_add=[] + order_list=['kt'] + for line in file : + if "depth=" in line.strip()[:6] : + depth_requested=True + depth=int(line.strip()[6:]) + elif "mech_to_extract=" in line.strip()[:16] : + list_to_add=line.strip()[16:].split(',') + list_to_add=[x.strip().lower() for x in list_to_add] + + + + if depth_requested is False : + print('No maximum depth requested ("depth="). Default behavior is that the extracting loop will continue until no new species are found.') + if list_to_add==[] : + raise Exception('No species sub-mechanism to extract provided ("mech_to_extract=species1, species2, species3, ...").') + + print(f'EXTRACTION MERGING\n\nSub-mechanism to extract : {list_to_add}') + + model_to_translate_translated = mt.translate(model_to_translate, translation) + # model_to_translate_translated.spe_model={x : name_translate for x in model_to_translate_translated.spe} + time_start = time.time() + + to_add = awm.recursive_addition(list_to_add, model_to_translate_translated, model_core, warning, depth) + + print(f'time for recursive_addition : {time.time()-time_start}') + + if to_add == '' : + raise Exception('No species extracted ?!?.') + + print(f'\n\nLength of to_add : (reactions/species)\n{len(to_add[0])} {len(to_add[1])}') + + print('\nCheck Lumping START') + + for reaction in model_to_translate_translated.rea : + if reaction.species[0] == reaction.species[1] : + print(f'UN-LUMPING NEEDED : {reaction.species[0]} = {reaction.species[1]}') + print('Check Lumping END\n') + +# ============================================================================= +# ################################### FULL #################################### +# ============================================================================= + +elif task == 'full' : + print('FULL MERGING\n') + + # prefix_to_del=['bb', 'pb', 'c6h5c3', 'c6h5c4', 'cyc'] + # list_to_del=['a1c2hac', 'a1c2h3ac', 'a2-x', 'a2o'] + + model_to_translate_translated = mt.translate(model_to_translate, translation) + # model_to_translate_translated.spe_model={x : name_translate for x in model_to_translate_translated.spe} + +# ============================================================================= +# ################################# MERGING ################################### +# ============================================================================= + +""" New order are 'k', 't', 'kt', 'n' depending on whether the core mechanism retains its kinetics, its thermodynamics, both or neither""" + +max_len_name=max(list(set([len(x.from_model) if x.from_model != '' else len(x.model) for x in model_core.rea]+ + [len(x.from_model) if x.from_model != '' else len(x.model) for x in model_to_translate_translated.rea]))) + +for order in order_list : + + if order == 'kt' : + retained = f'kinetics and thermodynamics retained from {md_core}' + elif order == 'k' : + retained = f'kinetics retained from {md_core}, thermodynamics retained from {md_translate}' + elif order == 't' : + retained = f'kinetics retained from {md_translate}, thermodynamics retained from {md_core}' + elif order == 'n' : + retained = f'kinetics and thermodynamics retained from {md_translate}' + else : + raise Exception("Wrong order. New order are 'k', 't', 'kt', 'n' depending on whether the core mechanism retains its kinetics, its thermodynamics, both or neither") + + introduction=f'{intro}\n! n={n} '+(f'depth={int(depth)} ' if depth != '' else '') + f'{retained}' + f'\n! {datetime.date.today().strftime("%d/%m/%Y")}\n' + flag_extraction=False if to_add==[] else True + + awm.merging(out+f'_{order}', md_core, md_translate, max_len_name, model_core, model_to_translate_translated, order, custom=introduction, flag_extraction=flag_extraction, to_add=to_add) + + + + + \ No newline at end of file -- GitLab