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.
- In Positron, start an R console from the interpreter picker. Run
1 + 1, thenR.version.string, thensessionInfo(). - Create a vector of the numbers 1 through 20 and compute its
mean()andsd(). - Build a vector without typing it in:
x <- seq(0, 10, by = 0.5), square every element withx^2, and checklength(x). What happens if you run the sequence backwards (by = -0.5)? - The L05 warm-up script: write
plot_stickleback_boxplots.Ror.pyintoscripts/(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 plotGene1instead of the totals. - Get help two ways:
?meanin the R console, andhelp(np.mean)(ornp.mean?) in a Python console. - Now start a Python console. Run
2 ** 10, thenimport sys; sys.version. Careful: what does2 ^ 10give you, and why? (Check the L05 “same ideas, different syntax” table.) - The list-vs-array distinction from lecture: make
x = [1, 2, 3, 4]and tryx**2- read the error. Thenimport numpy as np; np.array(x)**2. Finally, square the plain list with a list comprehension. - The L05 finale script: write
fasta_stats.Rorfasta_stats.py(or both) intoscripts/and run it on the chromosome you downloaded in Week 4:Rscript scripts/fasta_stats.R data/raw/chr21.faorpython3 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 andfasta_stats.sh? - Types: in R run
class(TRUE),class("A"),class(2),class(2L); in Python runtype(True),type("A"),type(2),type(2.0), then7 / 2and7 // 2. Map each R answer onto its Python counterpart using the table on the last L05 slide. - Indexing practice: read the stickleback table (
stickle <- read.delim("data/raw/Stickle_RNAseq.tsv", check.names = FALSE)in R, orpd.read_csv(..., sep="\t")in Python) and, using indexing only: count the genotype-B fish; pull out rows 1-5 of theIndividualandGeno&Microcolumns; and extract the single cell at row 7 of theMicrobiotacolumn - once by name and once by position. - 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? - 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?