# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # CS_Signalling_ERBB_RAS_AKT

import h5py
import numpy as np
import pandas as pd
import re

import sys
sys.path.insert(0, 'CS_Signalling_ERBB_RAS_AKT_petab')
import CS_Signalling_ERBB_RAS_AKT_petab as modelModule
model = modelModule.getModel()

res_file = '/home/dweindl/Downloads/ipopt-ERBB_RAS_AKT_Drugs_r389936-noHierarchical-noProtein-CV1-srv24ib.886758.0_rank00000.h5'

# ## Fixed parameters

model_k = model.getFixedParameterIds()

with h5py.File(res_file, 'r') as f:
    res_k = f['/inputData/fixedParameters/parameterNames'][:]

map_k = pd.DataFrame(data={'model_id': model_k, 'results_id': None})
map_k.set_index(['model_id'], inplace=True)
map_k.head()

# map species:
for model_id in map_k.index:
    if model_id in res_k:
        map_k.loc[model_id] = model_id

# show unmapped
map_k.loc[map_k.results_id.isnull()]


def update_parameter_name(old_name):
    # p_r_23811_k_RPKM2protein_reaction_23811
    # -> reaction_23811_r23811_k_RPKM2protein
    # if no match, return as is
    # new_name = re.sub(r'p_r_(\d+)_(.*)_(reaction_\d+)',
    # r'\3_r\1_\2', old_name)
    new_name = re.sub(r'p_r_(\d+_.*)_(reaction_\d+)', r'r\1', old_name)
    return new_name


# old names to new names:
for results_id in res_k:
    model_id = update_parameter_name(results_id)
    if model_id in map_k.index:
        map_k.loc[model_id, 'results_id'] = results_id

# show unmapped
map_k.loc[map_k.results_id.isnull()]

# +
# TODO: estimate all Genespecific scaling
# -

map_k.to_csv("fixed_parameter_map.tsv", sep='\t')

# ## Dynamic parameters

model_p = model.getParameterIds()

with h5py.File(res_file, 'r') as f:
    res_p = f['/inputData/parameters/modelParameterNames'][:]

map_p = pd.DataFrame(data={'model_id': model_p, 'results_id': None})
map_p.set_index(['model_id'], inplace=True)
map_p

# +
# old names to new names:
for results_id in res_p:
    model_id = update_parameter_name(results_id)
    if model_id in map_p.index:
        map_p.loc[model_id, 'results_id'] = results_id
        
# show unmapped
map_p.loc[map_p.results_id.isnull()]
# -

# these parameters won't be used
map_p.loc['observableParameter1_proliferation', 'results_id'] = 1.0
map_p.loc['noiseParameter1_proliferation', 'results_id'] = 1.0

map_p.to_csv("dynamic_parameter_map.tsv", sep='\t')



StarcoderdataJupyterScriptsDedupFiltered