Week 6 Exercises - Data Frames, Wrangling and Figures
Short, ungraded practice tasks - about an hour, spread over the week. Companion reading: Ch. 11, Ch. 12, Ch. 13, and Ch. 14. HW1 is out this week and these tasks are direct warm-ups for it; your project proposal is due Monday - task 17 is its skeleton.
TipTwo folder conventions
The L06 examples (and HW1) write into results/, while your Week 2 skeleton has output/figures and output/tables - the tasks below use both. Either home is fine: just be consistent within a script, and make sure the folder exists before you write to it.
- Read
Stickle_RNAseq.tsvinto R withread_tsv()and convertGenotypeandMicrobiotato factors. - Is this table tidy? In one or two sentences, say why or why not for the question “how does expression vary across genes?”
- Reshape it long with
pivot_longer(cols = starts_with("Gene"), names_to = "gene", values_to = "count"). How many rows do you expect (80 fish x 600 genes), and how many do you get? - From the long table,
filter()to justGene1throughGene5(hint:gene %in% paste0("Gene", 1:5)). - Add a log-transformed column with
mutate(log_count = log10(count + 1)). Why the+ 1? - For those five genes, compute mean and standard deviation of
log_countper gene and genotype withgroup_by()andsummarize(); write the table tooutput/tables/gene_summary.csvwithwrite_csv(). - Plot the five genes as boxplots of
log_countby gene, filled byGenotype- oneggplot()call from the long table. - Give the plot informative axis labels and a title with
labs(), and change the look with a theme (theme_minimal()ortheme_classic()). - Upgrade it the lecture’s way: swap
geom_boxplot()forgeom_violin()plus jittered points, and addscale_fill_viridis_d(). - Save your figure to
output/figures/gene_panel.pngwithggsave()and open the file to check it. - Do task 3 in Python with
stickle.melt(...)and confirm the row count matches. - Put tasks 1-10 in a script
scripts/week06.R, restart R, and run it top to bottom from a clean session. - Script the summaries (from the lecture’s Your turn): write
scripts/summarize_stickleback.Ror.pythat readsdata/Stickle_RNAseq.tsv, writes a per-group median table for a gene of your choice toresults/, and saves the distribution gallery asresults/distributions.svgand a 300-dpi PNG. - Rebuild the four-gene panel (Example 1 from the lecture) for four different genes - in the language you know less well - and
ggsave()/savefig()it intoresults/. Working versions of the scripts for tasks 13-14 are on the Examples page - but only look after your own runs. - Stretch: adapt the lecture’s gallery heatmap to the real data - take the 30 most variable stickleback genes and cluster the 80 fish (
scale = "row"matters!). - Self-study checkpoint (this week’s at-home half): work through the companion notebook on the Examples page in R or Python (ideally both) - 31 sections from files through the figure gallery, with control flow and the gotcha/error references at the end. Stretch: write
cv <- function(x) sd(x) / mean(x), test it on a vector where you know the answer, then use it insidesummarize(). - For your term project (proposal due Monday!): write down, in three sentences, what your dataset is, the one question you will ask, and which steps will happen at the shell vs. in R/Python vs. on Talapas. That is the skeleton of your proposal.