# Notebook from HansWesterhoff/Systems_Biology_FBA_tutorial
Path: FBA_tutorials/old_tutorials/extra_glutathione_paracetamol_metabolomics_recon2.ipynb

# Predicting biomarkers for depletion of glutathione upon use of paracetamol in RECON 2
Thierry D.G.A. Mondeel & Hans V. Westerhoff

University of Amsterdam 2017

---

This is for advanced students or student projects.

**Assignment:** Read the (Geenen, 2012) publication.

**Assignment:** The teaching assistants can supply you with the kinetic model that works in Copasi. Try reproducing some of the results shown in the publication. Especially, the relationship between paracetamol increase and ophthalmic acid and oxoproline.

**Assignment:** The main goal here is to see what we can say about metabolic changes upon paracetamol influx (or modeled as a drain on glutathione for simplicity). Specifically, given the Shlomi et al method. _____no_output_____Set up Python, load RECON 2, add opthalmic acid reactions (check if needed for RECON 2.2!), add demand reaction for glutathione._____no_output_____

import cobra
from cobra.solvers import get_solver_name
from cobra import Model, Reaction, Metabolite
from cobra.flux_analysis import parsimonious
from utils import show_map, findBiomarkers

# Load Recon2
M = cobra.io.load_json_model("models/recon_2_2_simple_medium.json")

# Adding the opthalmic acid pathway to RECON 2
opa_e = Metabolite('opa_e')
opa_e.name = 'Ophthalmic acid'
opa_c = Metabolite('opa_c')
opa_c.name = 'Ophthalmic acid'

# exchange reaction
EX_opa_e = Reaction('EX_opa_e')
EX_opa_e.add_metabolites({opa_e: -1.0})
EX_opa_e.lower_bound = 0; EX_opa_e.upper_bound = 1000

# transport reaction going outward
OPAtrs = Reaction('OPAtrs')
OPAtrs.add_metabolites({opa_e: 1.0,opa_c: -1.0})
OPAtrs.lower_bound = -1000; OPAtrs.upper_bound = 1000

# glutathione synthase: OPA <-> gluAB (CE1661)
gluABs = Reaction('gluABs')
gluABs.add_metabolites({opa_c: -1.0,M.metabolites.gly_c:-1.0,M.metabolites.CE1661_c: 1.0})
gluABs.lower_bound = -1000; gluABs.upper_bound = 1000

M.add_reactions([EX_opa_e,OPAtrs,gluABs])

# Find all exchange and transport reactions
exchanges = [rxn for rxn in M.reactions if rxn.products == [] ]
exchangesIds = [rxn.id for rxn in exchanges]
transport = [rxn for rxn in M.reactions if len(rxn.get_compartments()) > 1]

# make all exchanges in/out have a bound of 0/1000
for rxn in exchanges:
    if rxn.lower_bound < 0:
        rxn.lower_bound = 0

    rxn.upper_bound = 1000

# Add reactions to the model
# demand reaction for gthrd[c]
DM_gthrd_c = Reaction('DM_gthrd_c')
DM_gthrd_c.add_metabolites({M.metabolites.gthrd_c: -1.0})
# ATP reaction
atp_synthetase_c = Reaction('atp_synthetase_c')
atp_synthetase_c.add_metabolites({M.metabolites.atp_c: -1.0, M.metabolites.adp_c:1.0,
                                  M.metabolites.pi_c:1.0})
atp_synthetase_c.lower_bound = -1000; atp_synthetase_c.upper_bound = 1000;

M.add_reactions([atp_synthetase_c,DM_gthrd_c])

# set objective
M.reactions.DM_gthrd_c.upper_bound = 1
M.objective = M.reactions.DM_gthrd_c_____no_output_____model = M.copy()
model.objective = model.reactions.biomass_reaction

df = findBiomarkers(model,['EX_5oxpro_e','EX_opa_e','EX_glu_L_e',
                          'EX_cys_L_e','EX_gly_e'],mode='drug',mods=['DM_gthrd_c'],cutoff=0); df_____no_output_____


# Notebook from mfredriksz/adaptnlp
Path: nbs/06a_tutorial.easy_sequence_classifier.ipynb

# Tutorial - Sequence Classification
> Performing Sequence Classification with AdaptNLP_____no_output_____Sequence Classification (or Text Classification) is the NLP task of predicting a label for a sequence of words.

For example, a string of `That movie was terrible because the acting was bad` could be tagged with a label of `negative`. A string of `That movie was great because the acting was good` could be tagged with a label of `positive`.

A model that can predict sentiment from text is called a sentiment classifier, which is an example of a sequence classification model.

 Below, we'll walk through how we can use AdaptNLP's EasySequenceClassification module to easily do the following:
1. Load pre-trained models and tag data using mini-batched inference
2. Train and fine-tune a pre-trained model on your own dataset
3. Evaluate your model_____no_output_____## Loading Pretrained Models and Tag Data using Mini-Batched Inference_____no_output_____We'll first get started by importing the EasySequenceClassifier class from AdaptNLP and instantiating the
`EasySequenceClassifier` class object._____no_output_____

from adaptnlp import EasySequenceClassifier
from pprint import pprint

classifier = EasySequenceClassifier()_____no_output_____

With this class we can dynamically load models to run on inference.

Let's use the `HFModelHub` to search for some pre-trained sequence classification models to use:_____no_output_____

from adaptnlp import HFModelHub
hub = HFModelHub()_____no_output_____

We can either seach by task or by model name. Below is an example of the associated models HuggingFace has come out with:_____no_output_____

hub.search_model_by_task('text-classification')_____no_output_____

For this example though we will tag some text with a model that [NLP Town](https://www.nlp.town/) has trained called `nlptown/bert-base-multilingual-uncased-sentiment`. Let's find it in the model hub:_____no_output_____

model = hub.search_model_by_name('nlptown/bert-base', user_uploaded=True)[0]; model_____no_output_____

This is a multi-lingual model that predicts how many stars (1-5) a text review has given a product. More information can be found via. the Transformers model card [here](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment)_____no_output_____Next we can perform some inference. First let's write some example text:_____no_output_____

example_text = "This didn't work at all"_____no_output_____

Then we can tell our classifier to tag some text with `tag_text`:_____no_output_____

sentences = classifier.tag_text(
    text=example_text,
    model_name_or_path=model,
    mini_batch_size=1
)2021-09-30 14:38:54,487 loading file nlptown/bert-base-multilingual-uncased-sentiment

Now let's look at our outputs:_____no_output_____

#hide_input
print(f"Tag Score Outputs:\n")
print(f'Sentences: {sentences["sentences"]}\n')
print(f'Predictions: {sentences["predictions"]}\n')
print(f"Probabilities: \n\t{sentences['probs']}")Tag Score Outputs:

Sentences: ["This didn't work at all"]

Predictions: ['1 star']

Probabilities:
	tensor([[8.4212e-01, 1.3793e-01, 1.8024e-02, 1.2419e-03, 6.8153e-04]])

It's easy to pass in multiple sentences at once as well (in an array). Let's try that now:_____no_output_____

multiple_text = ["This didn't work well at all.",
                 "I really liked it.",
                 "It was really useful.",
                 "It broke after I bought it."]_____no_output_____

We'll pass it into the `classifier` just like before:_____no_output_____

sentences = classifier.tag_text(
    text=multiple_text,
    model_name_or_path=model,
    mini_batch_size=2
)_____no_output_____

And we can check the outputs again:_____no_output_____

#hide_input
print(f"Tag Score Outputs:\n")
print(f'Sentences: {sentences["sentences"]}\n')
print(f'Predictions: {sentences["predictions"]}\n')
print(f"Probabilities: \n\t{sentences['probs']}")Tag Score Outputs:

Sentences: ["This didn't work well at all.", 'I really liked it.', 'It was really useful.', 'It broke after I bought it.']

Predictions: ['1 star', '4 stars', '5 stars', '1 star']

Probabilities:
	tensor([[6.2198e-01, 3.3563e-01, 4.0320e-02, 1.5827e-03, 4.8790e-04],
        [3.2305e-03, 4.7872e-03, 5.4017e-02, 4.8129e-01, 4.5668e-01],
        [5.9679e-03, 9.2630e-03, 7.0121e-02, 4.1363e-01, 5.0102e-01],
        [4.4894e-01, 3.9348e-01, 1.4158e-01, 1.2110e-02, 3.8937e-03]])

> Note: The output is going to be a probility distribution of what the text should be tagged. If you're running this on a GPU, you can specify the `mini_batch_size` parameter to run mini-batch inference against your data for faster run time._____no_output_____You can set `model_name_or_path` to any of Transformer's or Flair's pre-trained sequence classification models.

Let's tag some text with another model, specifically Oliver Guhr's German sentiment model called `oliverguhr/german-sentiment-bert`._____no_output_____First we'll write some german text:_____no_output_____

german_text = ["Das hat überhaupt nicht gut funktioniert.",
               "Ich mochte es wirklich.",
               "Es war wirklich nützlich.",
               "Es ist kaputt gegangen, nachdem ich es gekauft habe."]_____no_output_____

And then tag it:_____no_output_____

sentences = classifier.tag_text(
    german_text,
    model_name_or_path="oliverguhr/german-sentiment-bert",
    mini_batch_size=1
)_____no_output_____

> Note: As seen here, you can either search for a model through the various `ModelHub` classes, or you can directly pass in the string to the model you want_____no_output_____Let's look at the output:_____no_output_____

#hide_input
print(f"Tag Score Outputs:\n")
print(f'Sentences: {sentences["sentences"]}\n')
print(f'Predictions: {sentences["predictions"]}\n')
print(f"Probabilities: \n\t{sentences['probs']}")Tag Score Outputs:

Sentences: ['Das hat überhaupt nicht gut funktioniert.', 'Ich mochte es wirklich.', 'Es war wirklich nützlich.', 'Es ist kaputt gegangen, nachdem ich es gekauft habe.']

Predictions: ['negative', 'positive', 'positive', 'negative']

Probabilities:
	tensor([[8.2706e-04, 9.9915e-01, 2.7373e-05],
        [7.0231e-01, 2.0294e-01, 9.4746e-02],
        [9.8132e-01, 1.8442e-02, 2.3914e-04],
        [4.2462e-03, 9.9566e-01, 9.4817e-05]])

Don't forget you can still quickly run inference with the multi-lingual review sentiment model you loaded in earlier (memory permitting)! Just change the `model_name_or_path` param to the model you used before._____no_output_____


# Notebook from Nosa1323/ICH_table_analysis
Path: old_notebooks/3_recordings calculate.ipynb

Here, we need to analyse data from recordings. In particular, we should to measure amplitude ratio, decay time of 1 and 5 transporter_____no_output_____

from IPython import get_ipython
get_ipython().magic('reset -sf') _____no_output_____import pandas as pd
import glob
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
import seaborn as sns
import matplotlib.ticker as ticker
from statannot import add_stat_annotation_____no_output_____

Step 1. Import data from excel to pandas df_____no_output_____

def file_extraction(search_string, set):
    filepaths = glob.glob(search_string)
    df = pd.DataFrame()
    col_name_suffix = 1
    for find_files in filepaths:
        if set == 'ctrl':
            df = df.join (pd.read_excel(find_files, sheet_name='CTRL'), how='outer',lsuffix=col_name_suffix)
            col_name_suffix +=1
        else:
            df = df.join (pd.read_excel(find_files, sheet_name='SE'), how='outer',lsuffix=col_name_suffix)
            col_name_suffix +=1
    return(df)_____no_output_____rec_count_CTRL= file_extraction('**/rec*.xlsx', set = 'ctrl')
rec_count_SE = file_extraction('**/rec*.xlsx', set = 'SE')
all_rec_count = pd.concat([rec_count_CTRL, rec_count_SE], ignore_index= True)
all_rec_count_____no_output_____

Step 2. Calculate amplitude ratio and decay time_____no_output_____Calculate amplitude and decay time ratio as ratio IGluT(5) to IGluT(1)_____no_output_____

all_rec_count ['IGluT(5)/IGluT(1)'] = all_rec_count['IGluT(5)']/all_rec_count['IGluT(1)']
all_rec_count ['τdecay IGluT(5)/τdecay IGluT(1)'] = all_rec_count['τdecay IGluT(5)']/all_rec_count['τdecay IGluT(1)']_____no_output_____all_rec_count_____no_output_____def adjust_box_widths(g, fac): # from https://stackoverflow.com/questions/56838187/how-to-create-spacing-between-same-subgroup-in-seaborn-boxplot
    """
    Adjust the withs of a seaborn-generated boxplot.
    """

    # iterating through Axes instances
    for ax in g.axes:

        # iterating through axes artists:
        for c in ax.get_children():

            # searching for PathPatches
            if isinstance(c, PathPatch):
                # getting current width of box:
                p = c.get_path()
                verts = p.vertices
                verts_sub = verts[:-1]
                xmin = np.min(verts_sub[:, 0])
                xmax = np.max(verts_sub[:, 0])
                xmid = 0.5*(xmin+xmax)
                xhalf = 0.5*(xmax - xmin)

                # setting new width of box
                xmin_new = xmid-fac*xhalf
                xmax_new = xmid+fac*xhalf
                verts_sub[verts_sub[:, 0] == xmin, 0] = xmin_new
                verts_sub[verts_sub[:, 0] == xmax, 0] = xmax_new

                # setting new width of median line
                for l in ax.lines:
                    if np.all(l.get_xdata() == [xmin, xmax]):
                        l.set_xdata([xmin_new, xmax_new])_____no_output_____def stat_to_list(stat):
    l = []
    for i in stat:
        l.append(str(i).split(','))
    for i in range(len(l)):
            l[i][1] = l[i].extend(l[i][1].split())
            l[i][1]= l[i].extend(l[i][2].split('='))
            l[i][1]= l[i].extend(l[i][3].split('='))
            del l[i][1]
            del l[i][1]
            del l[i][1]
    return l_____no_output_____

Transform df to create hue-groupped boxplot._____no_output_____

all_rec_count.drop(all_rec_count[all_rec_count['τdecay IGluT(5)']< 8].index, inplace=True) #delete outliers

all_rec_count_ampl = pd.melt (all_rec_count, id_vars= ['Group'],
                            value_vars=['IGluT(1)','IGluT(5)'],
                            var_name= '', value_name= 'Амплитуда, pA')
all_rec_count_decay = pd.melt (all_rec_count, id_vars= ['Group'],
                            value_vars=['τdecay IGluT(1)','τdecay IGluT(5)'],
                            var_name= '', value_name= 'τdecay, мс')
all_rec_count_ampl_____no_output_____def boxplot_graph_hue(data, dots, fig_name, big_tick, ylim, xyhue, box_pairs):
    fig_name = 'figs/recordings calculate/' + fig_name + '.tif'
    fig = plt.figure(figsize=(12,7)) # меняет размер графика
    custom_params = {"axes.spines.right": False, "axes.spines.top": False}
    sns.set_theme(font_scale=1.4, style="ticks",context="poster", rc= custom_params)
    plot = sns.boxplot(data = data, x = xyhue[0], y = xyhue[1], hue = xyhue[2],
                                            palette="vlag", linewidth = 7, dodge=True)

    plot.spines['left'].set_linewidth(4)
    plot.spines['bottom'].set_linewidth(4)
    plot.yaxis.set_major_locator(ticker.MultipleLocator(big_tick))
    plt.ylim(ylim)
    stat = add_stat_annotation(plot, data=data,x = xyhue[0], y = xyhue[1], hue = xyhue[2],
                    box_pairs=box_pairs,loc = 'outside',
                    test='Mann-Whitney', text_format='star',  verbose=1, comparisons_correction=None, linewidth=4) #loc = 'outside'
    if dots == True:
        sns.swarmplot(x = xyhue[0], y = xyhue[1], hue = xyhue[2], data=data,
                                            size = 15, palette = "Set2", linewidth=4 ,dodge = True)
    handles, labels = plot.get_legend_handles_labels()
    plt.legend(handles[0:2], labels[0:2], loc=2, bbox_to_anchor=(1.03, 1), borderaxespad=0)
    adjust_box_widths(fig, 0.9)
    plot = plot.get_figure()
    plt.tight_layout()
    plot.savefig(fig_name, dpi=800)
    return stat_____no_output_____ax, stat  = boxplot_graph_hue(all_rec_count_ampl, dots = True, fig_name = 'амлитуды',big_tick = 25, ylim = [-100,0],
                xyhue =['','Амплитуда, pA','Group'], box_pairs = [(('IGluT(1)', 'Контроль'),( 'IGluT(1)', 'ЭС')),
                                                                 (('IGluT(5)', 'Контроль'),('IGluT(5)', 'ЭС'))])
stat_list = []
stat_list.extend(stat_to_list(stat))   p-value annotation legend:
ns: 5.00e-02 < p <= 1.00e+00
*: 1.00e-02 < p <= 5.00e-02
**: 1.00e-03 < p <= 1.00e-02
***: 1.00e-04 < p <= 1.00e-03
****: p <= 1.00e-04

IGluT(5)_Контроль v.s. IGluT(5)_ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=5.830e-01 U_stat=1.150e+01
IGluT(1)_Контроль v.s. IGluT(1)_ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=1.000e+00 U_stat=1.550e+01

ax, stat = boxplot_graph_hue(all_rec_count_decay, dots = True, fig_name = 'время затухания',big_tick = 2, ylim = [6,18],
                xyhue =['','τdecay, мс','Group'], box_pairs = [(('τdecay IGluT(1)', 'Контроль'),( 'τdecay IGluT(1)', 'ЭС')),
                                                                 (('τdecay IGluT(5)', 'Контроль'),('τdecay IGluT(5)', 'ЭС'))])
stat_list.extend(stat_to_list(stat))  p-value annotation legend:
ns: 5.00e-02 < p <= 1.00e+00
*: 1.00e-02 < p <= 5.00e-02
**: 1.00e-03 < p <= 1.00e-02
***: 1.00e-04 < p <= 1.00e-03
****: p <= 1.00e-04

τdecay IGluT(5)_Контроль v.s. τdecay IGluT(5)_ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=3.030e-02 U_stat=3.000e+00
τdecay IGluT(1)_Контроль v.s. τdecay IGluT(1)_ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=2.468e-01 U_stat=8.000e+00
all_rec_count.drop(all_rec_count[all_rec_count['IGluT(5)/IGluT(1)']< 0.55].index, inplace=True) #удалим выбросы

fig = plt.figure(figsize=(6,8)) # меняет размер графика
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(font_scale=1.5, style="ticks",context="poster", rc= custom_params)
plot = sns.boxplot (data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['IGluT(5)/IGluT(1)'],  palette="vlag", linewidth = 7, dodge=True)
plot.spines['left'].set_linewidth(4)
plot.spines['bottom'].set_linewidth(4)
plot.yaxis.set_major_locator(ticker.MultipleLocator(0.25))
plt.ylim([0.75,2])
ax, stat  = add_stat_annotation(plot, data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['IGluT(5)/IGluT(1)'],
                    box_pairs=[("Контроль", "ЭС")],loc = 'outside',
                    test='Mann-Whitney', text_format='star',  verbose=1, comparisons_correction=None, linewidth=4) #loc = 'outside'
sns.stripplot(data=all_rec_count, x=all_rec_count['Group'], y = all_rec_count['IGluT(5)/IGluT(1)'],
              size=17, linewidth=4,palette = "Set2", jitter = True)
plot = plot.get_figure()
plt.tight_layout()
plot.savefig('figs/recordings calculate/отношения амплитуд', dpi=800)

stat_list.extend(stat_to_list(stat))  p-value annotation legend:
ns: 5.00e-02 < p <= 1.00e+00
*: 1.00e-02 < p <= 5.00e-02
**: 1.00e-03 < p <= 1.00e-02
***: 1.00e-04 < p <= 1.00e-03
****: p <= 1.00e-04

Контроль v.s. ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=4.413e-02 U_stat=2.650e+01
all_rec_count.drop(all_rec_count[all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)']> 1.7].index, inplace=True) #удалим выбросы

fig = plt.figure(figsize=(6,8)) # меняет размер графика
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(font_scale=1.5, style="ticks",context="poster", rc= custom_params)
plot = sns.boxplot (data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)'],  palette="vlag", linewidth = 7, dodge=True)
plot.spines['left'].set_linewidth(4)
plot.spines['bottom'].set_linewidth(4)
plot.yaxis.set_major_locator(ticker.MultipleLocator(0.25))
plt.ylim([0.5,1.75])
ax, stat = add_stat_annotation(plot, data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)'],
                    box_pairs=[("Контроль", "ЭС")],loc = 'outside',
                    test='Mann-Whitney', text_format='star',  verbose=1, comparisons_correction=None, linewidth=4) #loc = 'outside'
sns.stripplot(data=all_rec_count, x=all_rec_count['Group'], y = all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)'],
              size=17, linewidth=4,palette = "Set2", jitter = True)
plot = plot.get_figure()
plt.tight_layout()
plot.savefig('figs/recordings calculate/отношения времени затухания', dpi=800)

stat_list.extend(stat_to_list(stat))  p-value annotation legend:
ns: 5.00e-02 < p <= 1.00e+00
*: 1.00e-02 < p <= 5.00e-02
**: 1.00e-03 < p <= 1.00e-02
***: 1.00e-04 < p <= 1.00e-03
****: p <= 1.00e-04

Контроль v.s. ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=8.225e-02 U_stat=5.000e+00

Create p-value containing df to export to excel_____no_output_____

def df_pval_tramsform(stat_list):
    df_stat_out = pd.DataFrame(stat_list, columns = ['Test', 'feature', 'P-Val', 'feature2','U-stat'] ,index=['IGluT(5)', 'IGluT(1)', 'τdecay IGluT(5)', 'τdecay IGluT(1)','IGluT(5)/IGluT(1)', 'τd IGluT(5)/τd IGluT(1)'])
    df_stat_out.drop(['feature','feature2'], axis= 'columns', inplace= True)
    return df_stat_out

def df_out_tramsform(all_rec_count):
    mask_ctrl = all_rec_count['Group']=='Контроль'
    perc_cell_count_CTRL = all_rec_count[mask_ctrl].quantile([0.25, 0.50 , 0.75])
    mask_SE = all_rec_count['Group']=='ЭС'

    perc_cell_count_SE = all_rec_count[mask_SE].quantile([0.25, 0.50 , 0.75])
    perc_cell_count_CTRL ['Group'] = 'Контроль'
    perc_cell_count_SE ['Group'] = 'ЭС'
    perc_out = pd.concat([perc_cell_count_CTRL, perc_cell_count_SE])
    return perc_out _____no_output_____df_stat_out = df_pval_tramsform(stat_list)
perc_out = df_out_tramsform(all_rec_count)_____no_output_____df_stat_out_____no_output_____perc_out_____no_output_____with pd.ExcelWriter('output_table/final_out_rec_count.xlsx') as writer:
     perc_out.to_excel(writer, sheet_name='Percentiles', float_format="%.2f")
     df_stat_out.to_excel(writer, sheet_name='P-val, MW(U)', float_format="%.2f")_____no_output_____

Profit!_____no_output_____