Week 5 Exercises - First Steps in R and Python

Short, ungraded practice tasks - about 30 minutes. Prefer running cells to typing in a console? The two L05 companion notebooks on the Examples page hold every lecture example; these exercises go a step further. Companion reading: Ch. 11 and Ch. 12; references: App. E and App. F.

TipAdjust as you go

Commands copied from the lecture slides may need small tweaks to run in your project - most often a file path: the slides often use data/, while your Week 2 skeleton keeps the raw file in data/raw/. Spotting and fixing these little mismatches is not busywork; it is a core skill, and the error messages are your friend.

  1. In Positron, start an R console from the interpreter picker. Run 1 + 1, then R.version.string, then sessionInfo().
  2. Create a vector of the numbers 1 through 20 and compute its mean() and sd().
  3. Build a vector without typing it in: x <- seq(0, 10, by = 0.5), square every element with x^2, and check length(x). What happens if you run the sequence backwards (by = -0.5)?
  4. The L05 warm-up script: write plot_stickleback_boxplots.R or .py into scripts/ (type it from the slides; working versions are on the Examples page for afterwards), run it from the project root, and open the SVG it writes in your browser. Then modify it to plot Gene1 instead of the totals.
  5. Get help two ways: ?mean in the R console, and help(np.mean) (or np.mean?) in a Python console.
  6. Now start a Python console. Run 2 ** 10, then import sys; sys.version. Careful: what does 2 ^ 10 give you, and why? (Check the L05 “same ideas, different syntax” table.)
  7. The list-vs-array distinction from lecture: make x = [1, 2, 3, 4] and try x**2 - read the error. Then import numpy as np; np.array(x)**2. Finally, square the plain list with a list comprehension.
  8. The L05 finale script: write fasta_stats.R or fasta_stats.py (or both) into scripts/ and run it on the chromosome you downloaded in Week 4: Rscript scripts/fasta_stats.R data/raw/chr21.fa or python3 scripts/fasta_stats.py data/raw/chr21.fa. Do R and Python report the same GC content - and does it match your Week 4 shell pipeline and fasta_stats.sh?
  9. Types: in R run class(TRUE), class("A"), class(2), class(2L); in Python run type(True), type("A"), type(2), type(2.0), then 7 / 2 and 7 // 2. Map each R answer onto its Python counterpart using the table on the last L05 slide.
  10. Indexing practice: read the stickleback table (stickle <- read.delim("data/raw/Stickle_RNAseq.tsv", check.names = FALSE) in R, or pd.read_csv(..., sep="\t") in Python) and, using indexing only: count the genotype-B fish; pull out rows 1-5 of the Individual and Geno&Micro columns; and extract the single cell at row 7 of the Microbiota column - once by name and once by position.
  11. Save your R commands into a script scripts/week05.R, restart R, and run the whole script top to bottom - does it work from a clean session?
  12. Deliberately cause an error in each language (e.g., call a function that does not exist), and read the message carefully: what part tells you what went wrong, and what part tells you where?