Atharva Bhagwat

Stage 6: Checking real data

0Plan
Stage 6Core
★★★

Before you plot anything, find out what is in a table, what is missing, and whether the labels mean what they say.

Real data arrives with quirks. The habit that separates careful analysts from unlucky ones is a data audit: a short, systematic look at the table before you draw any conclusions.

What this dataset is about

PTEN is a human protein that acts as a brake on cell growth signalling. When it is damaged the brake weakens, and changes in the PTEN gene are linked to several cancers and inherited conditions. Geneticists keep finding single-letter changes in the gene. Many swap one amino acid in the protein for another (a missense variant), and for most of them nobody knows whether the change matters.

A multiplexed assay of variant effect attacks this by making thousands of variants at once and measuring each one. This table comes from an experiment that measured abundance: how much of each PTEN variant ends up in the cell. Each variant is tagged with a fluorescent marker and put into cells. The cells are sorted into four bins from dim to bright and sequenced, so every variant gets a count of reads per bin. That is the same shape as the toy table in Stage 3, only with about four thousand variants. A change that makes the protein unstable is cleared away by the cell, so its cells look dim.

Two scores, one idea

Both the toy table and this table have a column called score, but they are not the same measurement.

Toy table (Stage 4)PTEN table (this stage)
How it was madeYou calculated it: a weighted average of the four bins.Published by the researchers, from their own analysis.
Typical range1 to 4. Higher means brighter cells.About 0 to 1. It has been rescaled.
Reference pointsNone. 4 means every read was in the brightest bin.The unchanged protein is set to 1. Broken, cut-short proteins sit near 0.

Both turn "where along the dim-to-bright scale do this variant's cells land?" into one number. But the methods and scales differ, so never compare raw numbers across the two tables. Compare patterns inside one table, against that table's own reference points.

Load the table

run this
import pandas as pd

df = pd.read_csv("https://atharvabhagwat.dev/assets/data/pten-variant-abundance.csv")
df.head()

The columns, in plain terms (score matters most):

ColumnMeaning
hgvs_proThe variant's name, such as p.Asn12Ile: old amino acid, position, new amino acid.
scoreThe measured abundance. About 1 means "like the unchanged protein", about 0 means "like a broken one".
sd, se, lower_ci, upper_ciHow uncertain that measurement is.
exptsHow many experiments contributed to the score.
record_typemissense (one amino acid swapped), synonymous (the DNA changed but the amino acid did not), nonsense (a stop signal, so the protein is cut short), wild_type (the unchanged protein itself).
abundance_class, abundance_class_labelA grouped version of the score, as a number and as a word.
do the reference points look right?
df.groupby("record_type")["score"].median()

The audit checklist

run this
print(df.shape)                                  # rows, columns
print(df.dtypes)                                 # is anything that should be a number stored as text?
print(df.isna().sum())                           # missing values in each column
print(df["hgvs_pro"].duplicated().sum())         # how many variant names repeat?
print(df["record_type"].value_counts())          # how many of each kind

If a collaborator tells you how many rows and kinds to expect, check that your table agrees. If a number differs, that is not a nuisance, it is a finding. Write down why.

Turn names into columns, one small step at a time

A variant name packs three facts into one piece of text. To summarise by position later, you need them in separate columns. You already know the first tool: slicing.

run this
name = "p.Asn12Ile"
print(name[2:5])     # old amino acid
print(name[-3:])     # new amino acid

The same slicing works on a whole column when you add .str:

run this
df["ref"] = df["hgvs_pro"].str[2:5]
df["alt"] = df["hgvs_pro"].str[-3:]
df[["hgvs_pro", "ref", "alt"]].head()

Fix those rows with a filter you already know. Then get the position, which is the digits in the middle. In (\d+) below, \d means "a digit" and + means "one or more": it is a tiny pattern for finding numbers in text.

run this
df.loc[df["hgvs_pro"].str.endswith("="), "alt"] = "="

df["position"] = pd.to_numeric(df["hgvs_pro"].str.extract(r"(\d+)", expand=False))

df[df["position"].isna()]      # which rows have no position, and is that expected?

The rows with no position (such as the unchanged-protein row) are your cue to look at them, not to ignore them. Finally, check the labels agree with the names. Each line should print True:

run this
print((df.loc[df["record_type"] == "nonsense", "alt"] == "Ter").all())
print((df.loc[df["record_type"] == "synonymous", "alt"] == "=").all())

Now practise in Colab

You have read the ideas. Next, try them. The notebook repeats the code above with a few blanks for you to fill in, then ends with small exercises. It covers Stages 6 and 7, so scroll to the section with this stage's number. Run the cells in order.

Open the practice notebookYou need a free Google account. When it opens, choose File, then Save a copy in Drive, so your work is yours.

Exercises

These are also in the notebook. Try each one yourself before you open the solution.

Count and check

Use the table df from the notebook. Print how many rows there are of each record_type, and how many values are missing in the score column.
Show a solution (try it first)

Your numbers will depend on the real table, so there is no fixed output to compare with.

one solution
print(df["record_type"].value_counts())
print(df["score"].isna().sum())

Do the reference points look right?

Print the average score of synonymous variants and of nonsense variants. In a text cell, write one sentence: do the two numbers look the way you expect, and why?
Show a solution (try it first)

Your numbers will depend on the real table, so there is no fixed output to compare with.

one solution
print(df.groupby("record_type")["score"].mean())

Check your understanding

First star

How many rows does the table have (not counting the header)?

Stuck? Nudge and solution

df.shape gives (rows, columns). len(df) gives the rows directly.

one way to get it
print(len(df))

Special thanks to Nilay!