Last week you saw it

Five figures, one answer: melanoma lines die without SOX10. The skin box sat far below every other lineage.

What the eye says

Skin median −1.40, everything else −0.19. That looks like a difference.

What a reviewer asks

Any two groups of cell lines have different medians. Is this one bigger than chance alone would give you?

Today you put a number on it.

Today: Is the SOX10 dependency difference real?

The data

DepMap, again

The same 1,165 cell lines, the same SOX10 column.

The question

Real, or chance?

Is the skin difference bigger than a random split of cell lines would give?

The technique

seaborn + scipy.stats

Better figures in fewer lines, then the tests your project needs.

Two parts, the final sprint of the course!

Part 1

Better figures, fewer lines

First a map of the plotting libraries you will meet. Then two shortcuts built on matplotlib: pandas, for a quick look, and seaborn, for statistical figures.

The plotting landscape

Python has many plotting libraries. They split by where the picture ends up: a file for a paper, or a page in a browser.

Static: a picture in a file

PNG, PDF, SVG. Python draws it.

matplotlib●the foundation: every panel, line and label under your control
seaborn●statistical plots on top of matplotlib; give it a table, name the columns
pandas .plot()a quick look straight from a DataFrame; matplotlib underneath
plotnineR’s ggplot2 grammar in Python, if you come from R
Interactive: a page in a browser

Python writes a description; JavaScript draws it.

Plotlyhover, zoom and click in the browser; plotly.express is one line per plot
Bokehinteractive plots and dashboards served from Python
Altairdescribe the plot, not the drawing; rendered by Vega-Lite in the browser

● this course. For a report, a thesis or your project, matplotlib and seaborn are the standard. Reach for the browser libraries when someone needs to explore the data themselves.

pandas: the table plots itself

Every Series and DataFrame has a .plot: .plot.hist(), .plot.barh(), .plot.scatter(). The labels come from the index and the column names.

⏳Python editor will load when you reach this slide.

The fastest way to look at a result while you explore: one line after a groupby. It is matplotlib underneath, so ax= puts it in your panel.

seaborn: name the columns

matplotlib wants the numbers, already split into groups. seaborn wants the whole table and the names of the columns: x= the groups, y= the values. It does the splitting for you.

# Lab 7: box plot plus dots, by hand
names = ["Skin", "Lung", "Bowel"]
groups = [df[df["oncotree_lineage"] == n]["SOX10"]
          for n in names]
fig, ax = plt.subplots()
ax.boxplot(groups, tick_labels=names)
for i, g in enumerate(groups):
    x = np.random.normal(i + 1, 0.08, len(g))
    ax.scatter(x, g, s=4, color="black")
# seaborn: name the columns
fig, ax = plt.subplots()
sns.boxplot(data=df, x="oncotree_lineage",
            y="SOX10", order=names, ax=ax)
sns.stripplot(data=df, x="oncotree_lineage",
              y="SOX10", order=names, ax=ax)

It is still matplotlib underneath: pass ax=ax and seaborn draws on your panel, so ax.set_ylabel() and everything from last week still works.

Three groups, every cell line shown

Make a column that says which group each row belongs to, then let seaborn split by it. order= fixes the left-to-right order, so the figure reads as a story.

⏳Python editor will load when you reach this slide.

showfliers=False hides the box's own outlier dots, because the strip plot already shows every point. Your project figure has the same shape: three groups, in an order that tells the story, every sample visible.

Quiz 1

Which library? Three histograms

The same SOX10 histogram, written three ways. All three import everything, so read the line that draws. Run it, then vote: which library draws the bars?

# every question starts the same way
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv("depmap_lecture.csv")
/poll

Question 1 — which library?

⏳Python editor will load when you reach this slide.
Amatplotlib
Bseaborn
Cpandas
/poll

Question 2 — which library?

⏳Python editor will load when you reach this slide.
Amatplotlib
Bseaborn
Cpandas
/poll

Question 3 — which library?

⏳Python editor will load when you reach this slide.
Amatplotlib
Bseaborn
Cpandas
/poll
Part 2

Real, or chance?

Chance makes differences too. A test tells you whether yours is bigger than chance would make.

Chance makes differences too

Pick 73 non-skin lines at random and call them a group: the same size as skin, with no biology behind it. Plot skin, the random group and everything else side by side.

⏳Python editor will load when you reach this slide.

The random box is what “no real difference” looks like (the null hypothesis): each run, its median moves a little either side of 0. A test asks how often a random group lands as far away as skin does. That fraction is the p-value.
▶ Watch: Statistics for Hackers, Jake VanderPlas (PyCon 2016) →

What a p-value is

Start by assuming the dull explanation: skin and the rest come from the same population, and the gap is chance. That is the null hypothesis.

The p-value

If the null were true, how often would chance give a gap at least this big? A small p means chance is a poor explanation.

p < 0.05

0.05 is a cut-off you choose before testing, by convention. If p falls below it, we call the difference significant.

What p is not

Not the chance that the null is true. Not how big the difference is. Not how important it is.

Which test? Ask the data

Same idea as choosing a plot: the kind of data decides. All three live in scipy.stats.

two groups, roughly bell-shaped valuest-testttest_ind(a, b)
two groups, skewed, long tails, or unsureMann–Whitney Umannwhitneyu(a, b)
two columns: do they move together?Spearman correlationspearmanr(x, y)

SOX10 has a long tail out to −3, so we use Mann–Whitney. It ranks the values and compares the ranks, so one extreme cell line cannot drag the result. Report medians with it, not means.

SciPy: the scientist's toolbox

numpy gives you arrays; SciPy gives you the maths scientists do with them. Started in 2001, written and maintained by volunteers, free, and behind a huge share of published analyses (SciPy 1.0 paper: Virtanen et al., Nature Methods 2020).

One package, many toolboxes
scipy.statstests, correlations, distributions: today
scipy.optimizefit a curve to your data, e.g. a dose–response
scipy.ndimagefilter and measure images, e.g. count nuclei
scipy.signalpeaks and noise in a trace, e.g. a neuron recording
scipy.integratesolve differential equations, e.g. a growth model
Every test works the same way
from scipy.stats import mannwhitneyu

result = mannwhitneyu(a, b)
result.statistic   # the test's U value
result.pvalue      # the p-value

Import the test, give it your groups, read .pvalue. The documentation page of each test says what it assumes. Read it before you trust it.

Skin vs the rest: mannwhitneyu

Two groups of numbers in, one result out. result.pvalue is the p-value.

⏳Python editor will load when you reach this slide.

1.3e-27 means 1.3 × 10−27: chance would almost never make a gap this big. Always report the difference and the p-value together.

The 0.05 cut-off: 1 false alarm in 20

0.05 is not a p-value. It is a cut-off you choose before testing. Back to the random splits, which have no biology in them: how often does p land below the cut-off anyway?

⏳Python editor will load when you reach this slide.

About 10 of 200, every time you run it. When there is truly no difference, p falls below 0.05 one time in 20: that is the false positive rate you accept for each test. Run 20 tests on nothing and expect one false alarm.

Significant is not the same as big

The same test, skin vs the rest, on three genes. Two pass p < 0.05. Only one of them matters.

genedifferencepreading
SOX10−1.221e-27big and real: melanoma lines die without it
KRAS+0.070.0009real, but tiny: no biologist would care
PAX8−0.020.5no evidence of any difference

With 1,165 cell lines a test can detect a shift too small to mean anything. So read the difference first: is it big enough to care about? Then the p-value: could it be chance?

Write it once, use it everywhere

The table on the last slide came from this: one function runs the test and returns everything you report, in a dictionary.

⏳Python editor will load when you reach this slide.

One function, three genes, no copy-paste: the pattern your project asks for.

One more clue: skin needs MDM2

The same compare(), two more genes. MDM2 keeps the tumour suppressor p53 switched off. Run it, then swap in any gene from the table.

⏳Python editor will load when you reach this slide.

Skin needs MDM2 far more than the rest (difference −0.71), and grows better without TP53 (+0.41). Both only happen when p53 is working: most melanomas keep it intact. Your project asks the reverse question, in patients: when a tumour loses p53, what happens to your gene?

Quiz 2

Real or chance, three questions

What a p-value means, which test, and what to conclude.

r = compare(skin["SOX10"], rest["SOX10"])
r["difference"]   # -1.22
r["p"]            # 1.3e-27
/poll

Question 1 — Reading p

What does the p-value mean?

AThere is a 3% chance that skin and the rest are the same
BIf there were no real difference, a gap this big would turn up about 3% of the time
CSkin differs from the rest by 3%
DThere is a 97% chance the result is important
/poll

Question 2 — Which test?

Two conditions, and you are unsure how the data are distributed.

At-test
BMann–Whitney U
CSpearman correlation
DNo test: compare the means
/poll

Question 3 — What do you conclude?

KRAS, skin vs the rest: median difference +0.1, p = 0.009.

ASkin lines depend strongly on KRAS
BThe difference is probably real, but too small to matter biologically
CThe test must be wrong: the difference is tiny
DNot significant
/poll

Two columns: do they move together?

There is another statistical test that we use a lot in biology: are two distributions following a similar linear trend? This is expressed as a correlation r that runs from −1 (one rises as the other falls) through 0 (no relation) to +1 (they rise together). In DepMap, if two genes show a positive correlation, it means a cancer cell that needs gene A is also dependent on gene B. We measure this with the Pearson or Spearman coefficient.

⏳Python editor will load when you reach this slide.

The line slopes up: skin lines that need BRAF also need SOX10. Pearson (0.64) measures how close the dots sit to that straight line; Spearman (0.67) ranks the values first, like Mann–Whitney. They agree, so a few extreme lines are not driving it. Unsure? Use Spearman. And r says they move together, not that one causes the other.

What you can do now

sns.boxplot · sns.stripplot · sns.histplotgive it the table, name the columns: x=, y=, hue=, order=
sns.boxplot(..., ax=ax)seaborn draws on your panel; fig and ax work as before
mannwhitneyu(a, b).pvaluetwo groups, skewed data: is the gap bigger than chance?
pearsonr · spearmanrtwo columns: do they move together? Unsure? Spearman
difference first, then psignificant is not the same as big

That is everything the project needs. The rest is your gene, your cancer type, and what you make of the answer.

The last lecture

Looking back

Eight weeks ago most of you had never written a line of Python.

Eight weeks, one toolkit

1Types and variablesa number, a string, a name for it
2Lists, strings, dictionariesa whole sequence, and a codon table
3Control flowloops and decisions, so the program runs itself
4Functions and fileswrite it once, read real files, handle errors
5Arraysa neuron’s trials as one numpy array
6DataFramesfilter, group and ask a table a question
7Plottinga figure that finds the answer for you
8Statisticswhether the answer is real

Each week built on the last. Your project uses all eight: read a file, clean a table, write functions, test, plot, and say what it means.

Where next? Two paths

Use it when you need it

Not everyone wants to code every day, and you don't need to. What you learned stays useful: you can load a data table, analyse it, make a figure, write a short script when you need one, and read code that someone else, or an AI, wrote for you.

Go further

You are at the very beginning of the journey. Next steps:

  • ▸Learn the tools: the terminal, git, VS Code
  • ▸Learn agentic coding: working with an AI that writes code
  • ▸Keep going on DataCamp: statistics, then machine learning
  • ▸Try a new area: web development, electronics, image analysis

Two bonus sessions after week 11, once your project is in. Not assessed: come for what you want to do next.

Resources

Where to learn more

List to come
Python for Biologists