Skip to content

02. Toy dataset format

Download the notebook (executed for this page) · source

What this tutorial shows. The six files a PhosKinTime run reads, written by phoskintime.networkmodel.example.write_inputs: the kinase network (input2), the TF network (input4), the MS table with protein and phosphosite rows (input1), the RNA table (input3), and the KinOpt and TFOpt result workbooks. For each file: its columns, how the time columns map to time points, and which part PhosKinTime uses. Third tutorial of the series (after 01. Model creation).

All numbers are invented smooth curves and invented weights; they are not measurements and not real KinOpt or TFOpt output.

Counterparts: KinOpt 01 shows input1 and input2 as KinOpt reads them, TFOpt 01 shows input1, input3 and input4 as TFOpt reads them. PhosKinTime reads the same four inputs, plus the two tools' results.

import os
import sys
from pathlib import Path

# Find the repository root (the folder with pixi.toml and phoskintime/networkmodel/), put it first on sys.path and work from
# there. A Jupyter kernel started outside pixi does not get pixi's PYTHONPATH.
REPO_ROOT = next(p for p in [Path.cwd(), *Path.cwd().parents]
                 if (p / "pixi.toml").is_file() and (p / "phoskintime" / "networkmodel").is_dir())
sys.path.insert(0, str(REPO_ROOT))
os.chdir(REPO_ROOT)

import tempfile

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from phoskintime.networkmodel import api, example

WORK = Path(tempfile.mkdtemp(prefix="phoskintime-tutorial-01-"))   # never inside the repository
paths = example.write_inputs(WORK / "data")
{k: p.name for k, p in paths.items()}
{'kinase_net': 'kinase_net.csv',
 'tf_net': 'tf_net.csv',
 'ms': 'ms.csv',
 'rna': 'rna.csv',
 'kinopt': 'kinopt_results.xlsx',
 'tfopt': 'tfopt_results.xlsx'}

Kinase network (input2)

Columns GeneID, Psite, Kinase. One row per phosphosite; the Kinase cell lists the site's kinases as {K1, K2}, and each listed kinase becomes one kinase → site edge. This is the file KinOpt reads as input2 (KinOpt 01): KinOpt fits one α per edge.

kin = pd.read_csv(paths["kinase_net"])
display(kin)

# One edge per listed kinase (phoskintime.networkmodel.io.read_kinase_network does the same, upper-casing the names).
edges = kin.assign(Kinase=kin["Kinase"].str.strip("{}").str.split(",")).explode("Kinase")
edges["Kinase"] = edges["Kinase"].str.strip()
print(len(kin), "rows ->", len(edges), "kinase -> site edges")
GeneID Psite Kinase
0 A S_1 {K1}
1 A S_2 {K1, K2}
2 B Y_4 {K1, K2}
3 K1 T_3 {K2}
4 rows -> 6 kinase -> site edges

TF network (input4)

Columns Source (the TF) and Target (the gene whose mRNA it regulates). The same file TFOpt reads as input4 (TFOpt 01): TFOpt fits one α per edge. The file carries no sign; where the sign of an edge comes from is set by tf_sign_source (default "beta": the sign of TFOpt's TF-level β).

tf = pd.read_csv(paths["tf_net"])
tf
Source Target
0 T1 A
1 T1 B
2 T2 A
3 T3 K1
4 A B

MS table (input1): protein rows and phosphosite rows

Columns GeneID, Psite, x1 … x14. A row with an empty Psite is the protein's total abundance; a row with a site is that phosphosite. Column x_i is the value at the i-th time point of the protein time grid, [networkmodel.timepoints] protein in config.toml (minutes). A file with more time columns than grid points is an error (tutorial 03).

The kinase drive \(K_k(t)\) of the model is a kinase's protein row from this table.

# The time grids of the example are those of the repository's configs/config.toml.
grids = api.load_config()["timepoints"]
assert grids["protein"] == example.PROTEIN_TIMES and grids["rna"] == example.RNA_TIMES
print("protein / phosphosite grid (min):", example.PROTEIN_TIMES)
print("RNA grid (min):                  ", example.RNA_TIMES)

ms = pd.read_csv(paths["ms"])
ms.round(3)
protein / phosphosite grid (min): [0.0, 0.5, 0.75, 1.0, 2.0, 4.0, 8.0, 16.0, 30.0, 60.0, 120.0, 240.0, 480.0, 960.0]
RNA grid (min):                   [4.0, 8.0, 15.0, 30.0, 60.0, 120.0, 240.0, 480.0, 960.0]
GeneID Psite x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14
0 A NaN 2.0 2.027 2.040 2.053 2.102 2.190 2.333 2.510 2.600 2.441 2.119 2.004 2.000 2.000
1 B NaN 2.0 1.991 1.987 1.982 1.965 1.932 1.873 1.778 1.670 1.600 1.706 1.920 1.997 2.000
2 K1 NaN 2.0 2.160 2.232 2.300 2.529 2.824 3.000 2.736 2.240 2.011 2.000 2.000 2.000 2.000
3 K2 NaN 2.0 2.480 2.676 2.847 3.319 3.600 3.177 2.319 2.018 2.000 2.000 2.000 2.000 2.000
4 T3 NaN 2.0 2.009 2.014 2.018 2.036 2.070 2.136 2.254 2.423 2.659 2.800 2.589 2.159 2.006
5 A S_1 2.0 2.720 3.014 3.270 3.978 4.400 3.766 2.478 2.027 2.000 2.000 2.000 2.000 2.000
6 A S_2 2.0 2.115 2.170 2.223 2.420 2.741 3.154 3.400 3.094 2.336 2.016 2.000 2.000 2.000
7 B Y_4 2.0 2.287 2.418 2.540 2.953 3.484 3.800 3.324 2.432 2.020 2.000 2.000 2.000 2.000
8 K1 T_3 2.0 3.588 4.102 4.473 5.000 4.207 2.597 2.022 2.000 2.000 2.000 2.000 2.000 2.000
9 T1 S_9 2.0 2.053 2.080 2.105 2.203 2.381 2.666 3.021 3.200 2.883 2.239 2.009 2.000 2.000
is_protein = ms["Psite"].isna()                     # an empty Psite cell is read as NaN
n_time = sum(c.startswith("x") for c in ms.columns)
print(f"{is_protein.sum()} protein rows, {(~is_protein).sum()} phosphosite rows, {n_time} time columns")
5 protein rows, 5 phosphosite rows, 14 time columns

RNA table (input3)

Columns GeneID, x1 … x9, on the RNA grid [networkmodel.timepoints] rna (first point 4 min). The RNA input is optional for a run; TFOpt fits its α and β to these series.

rna = pd.read_csv(paths["rna"])
rna.round(3)
GeneID x1 x2 x3 x4 x5 x6 x7 x8 x9
0 A 3.305 3.571 3.953 4.484 4.800 4.324 3.358 3.013 3.000
1 B 3.105 3.203 3.360 3.635 3.989 4.200 3.883 3.239 3.009
2 K1 2.715 2.500 2.258 2.100 2.338 2.821 2.993 3.000 3.000
3 T1 3.952 4.666 5.473 6.000 5.207 3.597 3.022 3.000 3.000
xcols = lambda df: [c for c in df.columns if c.startswith("x")]
fig, axes = plt.subplots(1, 3, figsize=(13, 3.6), sharex=True)
panels = [(ms[is_protein], example.PROTEIN_TIMES, "MS: protein rows"),
          (ms[~is_protein], example.PROTEIN_TIMES, "MS: phosphosite rows"),
          (rna, example.RNA_TIMES, "RNA")]
for ax, (df, t, title) in zip(axes, panels):
    for _, row in df.iterrows():
        label = f"{row['GeneID']} {row['Psite']}" if title == "MS: phosphosite rows" else row["GeneID"]
        ax.plot(t, row[xcols(df)].to_numpy(float), marker="o", ms=3, label=label)
    ax.set_xscale("symlog", linthresh=1)
    ax.set_title(title)
    ax.set_xlabel("time (min)")
    ax.legend(fontsize=7)
axes[0].set_ylabel("invented value")
fig.tight_layout()

png

The run does not fit these raw values: with the default scaling_method = "fc_start" every series is divided by its value at the first time point, so the model is fitted to fold changes that start at 1 (tutorial 03 shows the counts, tutorial 06 the observables).

Which name plays which role

The same name can be a substrate protein, a kinase and a TF at once. The table is computed from the four files.

names = sorted(set(edges["GeneID"]) | set(edges["Kinase"]) | set(tf["Source"]) | set(tf["Target"])
               | set(ms["GeneID"]) | set(rna["GeneID"]))
roles = pd.DataFrame([{
    "name": n,
    "sites in kinase network": ", ".join(kin.loc[kin["GeneID"] == n, "Psite"]),
    "kinase": n in set(edges["Kinase"]),
    "TF": n in set(tf["Source"]),
    "TF target": n in set(tf["Target"]),
    "MS protein row": bool((is_protein & (ms["GeneID"] == n)).any()),
    "MS site rows": ", ".join(ms.loc[~is_protein & (ms["GeneID"] == n), "Psite"]),
    "RNA row": n in set(rna["GeneID"]),
} for n in names]).set_index("name")
roles
sites in kinase network kinase TF TF target MS protein row MS site rows RNA row
name
A S_1, S_2 False True True True S_1, S_2 True
B Y_4 False False True True Y_4 True
K1 T_3 True False True True T_3 True
K2 True False False True False
T1 False True False False S_9 True
T2 False True False False False
T3 False True False True False

What this means for the model (tutorial 04 shows how the network is built):

  • A, B, K1 are proteins with sites in the kinase network; they become model proteins (mRNA, protein and phosphorylation states).
  • K1 is also a kinase, K2 only a kinase: their protein rows are the kinase drives.
  • A is a TF and a protein with sites. T1, T2, T3 are TFs that are not proteins with sites in the kinase network: orphan TFs. T1 has mRNA and a site row (S_9) that has no kinase edge, T2 has no data at all, T3 only a protein row. Under tf_orphan_policy = "data_drive" (the only policy since v0.7.0) an orphan's activity is its own measured data: T1 is driven by its mRNA; T2 and T3 have no mRNA and become their own mRNA and protein states (data_drive_fallback = "state"; KNOWN_ISSUES N31, tutorial 04).

KinOpt workbook (kinopt_results.xlsx)

KinOpt writes many sheets; the contract checks only the ones PhosKinTime reads (External tools):

sheet columns PhosKinTime uses it for
Alpha Values Gene, Psite, Kinase, Alpha the kinase → site weight in W (merged on Gene/Psite/Kinase)
Beta Values Kinase, Psite, Beta only the kinase-level rows (empty Psite): c_k's start and prior centre
Estimated Gene, Psite, x1… not a model input; read only by phoskintime analyze similarity

In KinOpt, α sums to 1 over the kinases of a site, and β sums to 1 over a kinase's protein row (empty Psite) and its own sites, with signs allowed. How KinOpt lays these out and constrains them: KinOpt 04. Parameter layout: alpha and beta.

kinopt = pd.read_excel(paths["kinopt"], sheet_name=None)
print("sheets:", list(kinopt))
display(kinopt["Alpha Values"])
display(kinopt["Beta Values"])
sheets: ['Alpha Values', 'Beta Values', 'Estimated']
Gene Psite Kinase Alpha
0 A S_1 K1 1.0
1 A S_2 K1 1.0
2 A S_2 K2 0.0
3 B Y_4 K1 0.7
4 B Y_4 K2 0.3
5 K1 T_3 K2 1.0
Kinase Psite Beta
0 K1 NaN -0.4
1 K1 T_3 1.4
2 K2 NaN 1.0
# KinOpt's constraints, checked on the invented workbook: alpha sums to 1 per site, beta to 1 per kinase.
alpha_k, beta_k = kinopt["Alpha Values"], kinopt["Beta Values"]
display(alpha_k.groupby(["Gene", "Psite"])["Alpha"].sum().rename("sum of alpha").to_frame())
display(beta_k.groupby("Kinase")["Beta"].sum().rename("sum of beta").to_frame())
print("kinase-level beta (empty Psite), the only beta PhosKinTime reads:")
print(beta_k[beta_k["Psite"].isna()].set_index("Kinase")["Beta"].to_dict())
sum of alpha
Gene Psite
A S_1 1.0
S_2 1.0
B Y_4 1.0
K1 T_3 1.0
sum of beta
Kinase
K1 1.0
K2 1.0
kinase-level beta (empty Psite), the only beta PhosKinTime reads:
{'K1': -0.4, 'K2': 1.0}

Two things to note:

  • A S_2 ← K2 has α = 0. The edge stays in the network but carries no weight: a vertex solution, which in real KinOpt output is common and silent (KNOWN_ISSUES N32).
  • K1's kinase-level β is −0.4. It is a share inside KinOpt's activity mixture (−0.4 × protein + 1.4 × T_3), not a scale. As c_k's starting value it becomes max(0.01, β) = 0.01; this misuse of β is why ck_prior_centre = "design" exists (N38).

TFOpt workbook (tfopt_results.xlsx)

sheet columns PhosKinTime uses it for
Alpha Values mRNA, TF, Value the TF → target weight α (merged on mRNA/TF)
Beta Values TF, PSite, Value only the TF-level rows (empty PSite): the weight becomes α × β
Estimated mRNA, x1… not a model input

Note the column name PSite (TFOpt) versus Psite (KinOpt); names are case-sensitive. TFOpt 0.2.0 adds an optional Source column to Beta Values (which series a β row weighs); this example does not have it. TFOpt's layout: TFOpt 04. Parameter layout: alpha and beta.

tfopt = pd.read_excel(paths["tfopt"], sheet_name=None)
print("sheets:", list(tfopt))
display(tfopt["Alpha Values"])
display(tfopt["Beta Values"])
sheets: ['Alpha Values', 'Beta Values', 'Estimated']
mRNA TF Value
0 A T1 0.6
1 B T1 0.5
2 A T2 0.4
3 K1 T3 1.0
4 B A 0.5
TF PSite Value
0 T1 NaN 0.6
1 T1 S_9 0.4
2 T2 NaN 1.0
3 T3 NaN 1.0
4 A NaN -0.7
5 A S_1 1.7
alpha_t, beta_t = tfopt["Alpha Values"], tfopt["Beta Values"]
display(alpha_t.groupby("mRNA")["Value"].sum().rename("sum of alpha").to_frame())
display(beta_t.groupby("TF")["Value"].sum().rename("sum of beta").to_frame())
tf_level = beta_t[beta_t["PSite"].isna()].set_index("TF")["Value"]
print("TF-level beta, the only beta PhosKinTime reads:", tf_level.to_dict())
sum of alpha
mRNA
A 1.0
B 1.0
K1 1.0
sum of beta
TF
A 1.0
T1 1.0
T2 1.0
T3 1.0
TF-level beta, the only beta PhosKinTime reads: {'T1': 0.6, 'T2': 1.0, 'T3': 1.0, 'A': -0.7}

TF A has a negative TF-level β (−0.7). With the default tf_sign_source = "beta" its edge weight α × β is negative, so A represses B in the model. That sign is an artefact of reading a mixture share as a mode of regulation; tf_sign_source = "network" takes the sign from CollecTRI instead (External tools, N31). Every TF edge, orphans included, uses its own TF's β (tutorial 04).

Summary: what PhosKinTime takes from each file

file read by PhosKinTime uses
input2 kinase network phoskintime.networkmodel.io.read_kinase_network kinase → site edges
input4 TF network phoskintime.networkmodel.io.read_tf_network TF → target edges
input1 MS phoskintime.networkmodel.io.read_ms protein and phosphosite observations; kinase drives
input3 RNA phoskintime.networkmodel.io.read_rna mRNA observations
KinOpt workbook phoskintime.networkmodel.io.read_kinopt α → W; kinase-level β → c_k centre
TFOpt workbook phoskintime.networkmodel.io.read_tfopt α and TF-level β → TF weights

None of these weights is refitted by PhosKinTime; they are fixed inputs. PhosKinTime fits rates.

Next

03. Load and validate data: check a whole project without fitting, read the inputs report, and see what a broken workbook looks like.