Day 4 — Unzip and inspect laboratory data
Lecture slides · Quiz · Weekly revision
Which seedlings received the treatment?
A lab group has sent you lab-data.zip. Its README says it holds four seedling pots, two control and two nutrient-treated, each measured once after seven days, with lengths in millimetres. A table called a manifest (samples.tsv) lists each sample’s ID, its treatment and the file holding its measurement. Today you use the terminal to find out which samples were treated and whether they are longer than the controls.
This is a small, separate practice dataset with four samples; the 24-plant dataset comes back on day 5 in R. The data are simulated.
Save today: treated-samples.txt, working/manifest-practice.tsv and working/observations.txt, all inside the extracted lab-data folder.
Getting the archive into your workspace
Download lab-data.zip with your browser. The download lands in your Downloads folder, which is not where your terminal works. In the terminal, run cd ~/introcomputing-shell and then open that folder graphically with explorer.exe . (Ubuntu on Windows) or open . (Mac). Copy the ZIP into it with the file manager, then run ls in the terminal: lab-data.zip should be listed.
What is in the archive?
Use unzip -l to find out what the lab group sent before unpacking anything:
unzip -l lab-data.zipThe listing shows eight files: README.txt, samples.tsv, four measurement files measurements/S01.csv to S04.csv, and two short field notes. -l only lists; nothing has been extracted yet. Now extract into a folder of your choice and read the README:
unzip lab-data.zip -d lab-data
cd lab-data
less README.txt-d lab-data names the destination folder. Extraction makes ordinary files you can open and leaves the ZIP as it was. In less, Space moves down and q returns to the prompt.
unzip asks whether to replace a file
You have extracted into lab-data before. Answer n, look at the existing folder in the file manager, and extract into a fresh name such as lab-data-2 instead. Then use that name in cd.
How many samples are there?
Use head and wc -l to find out how many samples the manifest describes:
head -n 3 samples.tsv
wc -l samples.tsvhead -n 3 shows the first three lines: a header naming the columns (sample_id, treatment, file), then S01 and S02. wc -l counts lines and prints 5: one header plus four samples. The columns are separated by tabs, which is what TSV (tab-separated values) means; a CSV uses commas.
Which samples were treated?
Use grep to find out which samples received the treatment:
grep -F treated samples.tsvgrep prints every line that contains the search text; -F means plain text, not a pattern. You should see two lines, S03 and S04. The header and the control lines do not contain the word, so they are not printed.
grep matches text, not meaning: a line saying untreated would also match, because it contains treated. Always look at the lines it selected, not only at how many there are.
Saving and counting the treated samples
Save the treated lines to a file, and count them with a pipe:
grep -F treated samples.tsv > treated-samples.txt
less treated-samples.txt
grep -F treated samples.tsv | wc -l> sends the output into treated-samples.txt instead of the screen. The manifest is read and stays unchanged. The vertical bar | is a pipe: it passes grep’s lines straight to wc -l, which prints 2.
> replaces whatever the destination file contained before, so never put the input file after it: grep … samples.tsv > samples.tsv can empty the manifest before grep reads it.
Are the treated seedlings longer?
The manifest’s file column says where each measurement is. Use head on those files to find out how long the treated and control seedlings were:
head measurements/S03.csv
head measurements/S04.csv
head measurements/S01.csv
head measurements/S02.csvEach file has one header line and one value. The treated seedlings are 42.4 mm (S03) and 39.8 mm (S04); the controls are 32.1 mm (S01) and 35.2 mm (S02). Both treated seedlings are longer than both controls. With two pots per group that is a hint, not a result; the 24-plant dataset in week 2 is where we start answering the question properly.
A practice copy of the manifest
You will want a copy of the manifest you can change without risking the original. Make one in a working folder and give it a clearer name:
mkdir working
cp samples.tsv working/samples-copy.tsv
mv working/samples-copy.tsv working/manifest-practice.tsv
ls working
lscp makes a second file; mv renames (or moves) it. ls working shows only manifest-practice.tsv, and ls shows that the original samples.tsv is still there. Both cp and mv silently replace an existing file with the destination name, so check names first.
Then use the file manager to create working/observations.txt with your answers from today: how many samples there are and why wc -l says 5, which two were treated, their lengths, and which file holds S04’s measurement.
When untreated also matches
A colleague’s version of the manifest labels S01 as untreated instead of control. Use your practice copy to find out what grep -F treated selects then.
- Copy
working/manifest-practice.tsvtoworking/search-trap.tsv. Open only this copy in a text editor and change S01’scontroltountreated. - Predict, then run
grep -F treated working/search-trap.tsv | wc -l.
The count is 3: S01, S03 and S04. untreated contains the text treated, so grep cannot tell the two labels apart. Running grep -F treated working/search-trap.tsv without the count shows you the S01 line and makes the problem visible. The original samples.tsv still gives 2.
A measurement file that seems to be missing
When you receive data, every file the manifest names should exist. Use a copy of the folder to find out what you see when one has been renamed.
- In the file manager, copy the whole
lab-datafolder toreceived-check. Inreceived-check/measurements, renameS04.csvtoS04-renamed.csv. - In the terminal,
cdintoreceived-checkand tryhead measurements/S04.csv. It fails with No such file or directory, although the manifest still lists that path. - Use
ls measurementsandhead measurements/S04-renamed.csvto find out which sample that file holds. It containsS04,39.8, the S04 measurement. - Because you know it is S04, restore the name with
mv measurements/S04-renamed.csv measurements/S04.csvand check thathead measurements/S04.csvworks again.
In real data, a file whose identity you cannot confirm from its contents is a question for the lab group, not something to rename by guessing. Add the problem, the evidence and the repair to working/observations.txt.
Which samples are esters? (an organic-laboratory inventory)
This optional task uses the same commands on a different archive: a simulated inventory of four organic-chemistry samples, each with a supplied class label. No chemistry knowledge is needed.
Download organic-lab.zip, copy it into ~/introcomputing-shell with the file manager as above, and use the day’s commands to find out which samples are labelled ester and how many there are:
cd ~/introcomputing-shell
unzip -l organic-lab.zip
unzip organic-lab.zip -d organic-lab
cd organic-lab
less README.txt
grep -F ester inventory.tsv > ester-samples.txt
grep -F ester inventory.tsv | wc -lThe inventory has a header and four samples. Two match: O02 and O03. grep searched the supplied labels; it does not recognise chemical structures. The archive also contains organic-recovery.csv, which you will use for calculations later in the course.
Check your understanding
grep change the manifest? Why does wc -l count 5 for four samples?
grep only reads the manifest; > wrote its output to a separate file. wc -l counts lines, and the manifest has one header line plus four sample lines. The saved treated-samples.txt has no header, because the header did not match.
unzip -l before extracting it?
You see what you received, and the folder structure it will create, before any files are written. If something is missing, you can ask the sender straight away.
Save today: treated-samples.txt, working/manifest-practice.tsv and working/observations.txt in the extracted lab-data folder, and keep lab-data.zip unchanged.
Next: Day 5 — meet R and RStudio.