Five figures, one answer: melanoma lines die without SOX10. The skin box sat far below every other lineage.
Skin median −1.40, everything else −0.19. That looks like a difference.
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.
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!
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.
Python has many plotting libraries. They split by where the picture ends up: a file for a paper, or a page in a browser.
PNG, PDF, SVG. Python draws it.
Python writes a description; JavaScript draws it.
● 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.
Every Series and DataFrame has a .plot: .plot.hist(), .plot.barh(), .plot.scatter(). The labels come from the index and the column names.
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.
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.
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.
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.
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")matplotlibseabornpandasmatplotlibseabornpandasmatplotlibseabornpandasChance makes differences too. A test tells you whether yours is bigger than chance would make.
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.
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) →
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.
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.
0.05 is a cut-off you choose before testing, by convention. If p falls below it, we call the difference significant.
Not the chance that the null is true. Not how big the difference is. Not how important it is.
Same idea as choosing a plot: the kind of data decides. All three live in scipy.stats.
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.
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).
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.
Two groups of numbers in, one result out. result.pvalue is the p-value.
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.
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?
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.
The same test, skin vs the rest, on three genes. Two pass p < 0.05. Only one of them matters.
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?
The table on the last slide came from this: one function runs the test and returns everything you report, in a dictionary.
One function, three genes, no copy-paste: the pattern your project asks for.
The same compare(), two more genes. MDM2 keeps the tumour suppressor p53 switched off. Run it, then swap in any gene from the table.
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?
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
What does the p-value mean?
There is a 3% chance that skin and the rest are the sameIf there were no real difference, a gap this big would turn up about 3% of the timeSkin differs from the rest by 3%There is a 97% chance the result is importantTwo conditions, and you are unsure how the data are distributed.
t-testMann–Whitney USpearman correlationNo test: compare the meansKRAS, skin vs the rest: median difference +0.1, p = 0.009.
Skin lines depend strongly on KRASThe difference is probably real, but too small to matter biologicallyThe test must be wrong: the difference is tinyNot significantThere 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.
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.
That is everything the project needs. The rest is your gene, your cancer type, and what you make of the answer.
Eight weeks ago most of you had never written a line of Python.
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.
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.
You are at the very beginning of the journey. Next steps:
Two bonus sessions after week 11, once your project is in. Not assessed: come for what you want to do next.