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.

  1. Read Stickle_RNAseq.tsv into R with read_tsv() and convert Genotype and Microbiota to factors.
  2. Is this table tidy? In one or two sentences, say why or why not for the question “how does expression vary across genes?”
  3. 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?
  4. From the long table, filter() to just Gene1 through Gene5 (hint: gene %in% paste0("Gene", 1:5)).
  5. Add a log-transformed column with mutate(log_count = log10(count + 1)). Why the + 1?
  6. For those five genes, compute mean and standard deviation of log_count per gene and genotype with group_by() and summarize(); write the table to output/tables/gene_summary.csv with write_csv().
  7. Plot the five genes as boxplots of log_count by gene, filled by Genotype - one ggplot() call from the long table.
  8. Give the plot informative axis labels and a title with labs(), and change the look with a theme (theme_minimal() or theme_classic()).
  9. Upgrade it the lecture’s way: swap geom_boxplot() for geom_violin() plus jittered points, and add scale_fill_viridis_d().
  10. Save your figure to output/figures/gene_panel.png with ggsave() and open the file to check it.
  11. Do task 3 in Python with stickle.melt(...) and confirm the row count matches.
  12. Put tasks 1-10 in a script scripts/week06.R, restart R, and run it top to bottom from a clean session.
  13. Script the summaries (from the lecture’s Your turn): write scripts/summarize_stickleback.R or .py that reads data/Stickle_RNAseq.tsv, writes a per-group median table for a gene of your choice to results/, and saves the distribution gallery as results/distributions.svg and a 300-dpi PNG.
  14. 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 into results/. Working versions of the scripts for tasks 13-14 are on the Examples page - but only look after your own runs.
  15. 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!).
  16. 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 inside summarize().
  17. 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.