Stage 4: Calculations and groups
Make new columns with maths, summarise by group, and line two replicates up side by side.
Raw counts are hard to compare: one row has 20 reads, another has 700. A common fix is a weighted average of the bins. Bin 1 counts as 1, bin 2 as 2, and so on. A row with all its reads in bin 1 scores 1, all in bin 4 scores 4, and anything in between lands in between.
score = (1 x bin1 + 2 x bin2 + 3 x bin3 + 4 x bin4) / total readsNew columns are just maths on columns
pandas does maths on a whole column at once, with no loop needed. First reload the table, then add two new columns.
import pandas as pd
df = pd.read_csv("https://atharvabhagwat.dev/assets/data/toy_vampseq_bin_counts.csv")
df["total_reads"] = df["bin1_count"] + df["bin2_count"] + df["bin3_count"] + df["bin4_count"]
df["score"] = (df["bin1_count"] * 1 + df["bin2_count"] * 2 + df["bin3_count"] * 3 + df["bin4_count"] * 4) / df["total_reads"]
df[["variant", "replicate", "total_reads", "score"]]Summarise by group
groupby splits the table by a column, summarises each piece, and puts the answers together: split, summarise, combine.
print(df.groupby("variant_type")["score"].mean()) # average score per kind
print(df.groupby("variant")["score"].mean()) # average score per variantPut the two replicates side by side
Every variant was measured twice. A pivot turns that into one row per variant, with one column per replicate. Then you can compare them.
wide = df.pivot(index="variant", columns="replicate", values="score")
widewide["difference"] = (wide[1] - wide[2]).abs() # how much the two replicates disagree
wide.sort_values("difference", ascending=False)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 3 to 5, 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.
Make a fraction column
Show a solution (try it first)
The output should look like this:
variant replicate frac_bin4
0 WT 1 0.541787
1 p.Gly12Gly 1 0.520593
2 p.Trp18Ter 1 0.015873
3 p.Gln36Ter 1 0.028871
4 p.Tyr52Ter 1 0.021021import pandas as pd
df = pd.read_csv("https://atharvabhagwat.dev/assets/data/toy_vampseq_bin_counts.csv")
df["total_reads"] = df["bin1_count"] + df["bin2_count"] + df["bin3_count"] + df["bin4_count"]
df["frac_bin4"] = df["bin4_count"] / df["total_reads"]
print(df[["variant", "replicate", "frac_bin4"]].head())Average by group
variant_type.Show a solution (try it first)
The output should look like this:
variant_type
missense 398.214286
nonsense 393.166667
synonymous 572.500000
wild_type 660.500000
Name: total_reads, dtype: float64import pandas as pd
df = pd.read_csv("https://atharvabhagwat.dev/assets/data/toy_vampseq_bin_counts.csv")
df["total_reads"] = df["bin1_count"] + df["bin2_count"] + df["bin3_count"] + df["bin4_count"]
print(df.groupby("variant_type")["total_reads"].mean())Check your understanding
First star
What is the average score of the WT rows (both replicates), to 2 decimal places?
Stuck? Nudge and solution
Keep the rows where variant equals "WT", take the score column, and use .mean().
print(df[df["variant"] == "WT"]["score"].mean())Second star
Which variant has the largest disagreement between replicate 1 and replicate 2? Type its name exactly as it appears.
Stuck? Nudge and solution
Use the wide table and its difference column. Sort it, or use wide["difference"].idxmax(). Then look at that variant's total_reads: should you trust it?
print(wide["difference"].idxmax())Special thanks to Nilay!