Lecture
Almost every file in bioinformatics is plain text: FASTA, FASTQ, VCF, GFF, sample sheets. That is deliberate. Text can be opened by any program, read by a person and processed line by line. This week is about looking at text files and doing simple things to them, and about the idea that makes the command line powerful.
Looking at a file
cat genes.fasta # print the whole file
less reads.fastq # page through it: space, b, /search, q to quit
head genes.fasta # the first 10 lines
head -n 4 reads.fastq # the first 4 lines: one FASTQ read
tail -n 3 samples.tsv # the last 3 lines
tail -n +2 samples.tsv # everything from line 2 on: skips a header
Never cat a file you have not looked at with head first. A 20 GB FASTQ will fill your terminal
for a very long time. less is safe on files of any size, because it does not read the whole file
at once.
Compressed files have their own versions: zcat, zless and zgrep work on .gz files directly.
Counting
wc genes.fasta # lines, words, characters
wc -l genes.fasta # lines only
Counting lines is one of the most common operations you will do, because so many formats have a fixed number of lines per record. A FASTQ file has exactly four lines per read, so the number of reads is the number of lines divided by four.
Cutting
head and tail cut rows. cut cuts columns out of tab-separated files:
cut -f 2 samples.tsv # the second column
cut -f 1,3 samples.tsv # columns 1 and 3
cut -c 1-10 genes.fasta # the first 10 characters of every line
cut splits on tabs by default. For other separators use -d, for example cut -d , -f 2 for CSV.
Redirecting input and output
Every program has three standard streams: standard input (where it reads from), standard output (where results go) and standard error (where error messages go). By default they are your keyboard and your screen. You can redirect them:
head -n 40 reads.fastq > first10.fastq # write output to a file (overwrites!)
echo "S25" >> sample_ids.txt # append to the end of a file
wc -l < genes.fasta # read input from a file
ls missing_file 2> errors.txt # send error messages to a file
ls missing_file 2> /dev/null # throw error messages away
Pipelines
The | character (a pipe) sends the output of one program straight into the input of the next.
No temporary files, no waiting:
tail -n +2 samples.tsv | wc -l
Read it left to right: take everything except the header, and count the lines. Pipelines can be as long as you need:
tail -n +2 samples.tsv | cut -f 2 | sort | uniq -c
Skip the header, take the tissue column, sort it, and count how many times each value occurs. You
will meet sort and uniq properly next week.
Why the pipeline is the central idea of UNIX
UNIX was designed around a few rules: write programs that do one thing well, make them work
together, and make them handle text streams, because text is a universal interface. The pipe is
what makes these rules work in practice. wc knows nothing about FASTQ and cut knows nothing
about tissues, but put together they tell you how many samples of each tissue you have.
The same idea runs through the rest of this course. In Python you will write small functions and join them; workflow managers such as Nextflow and Snakemake are pipelines of whole programs.
Practice
Download this week's files:
mkdir -p ~/course/week02 && cd ~/course/week02
base=https://bioinformatics.science/courses/linux-and-programming-for-bioinformatics/data
curl -O $base/genes.fasta -O $base/reads.fastq -O $base/samples.tsv -O $base/variants.vcf
genes.fasta8 gene fragments, FASTA
reads.fastq200 reads of 50 bases, FASTQ
samples.tsv24 samples with tissue and condition, tab-separated
variants.vcf24 variants on three chromosomes, VCF
Open each file with head and with less. Before running any command, say out loud what you expect
it to print, then check.
Exercises
Exercise 1
How many lines are in genes.fasta?
▸Show solution
wc -l genes.fasta
32 lines: 8 header lines and 24 lines of sequence.
Exercise 2
Show the header line of samples.tsv together with the first three samples.
▸Show solution
head -n 4 samples.tsv
Exercise 3
How many samples are in samples.tsv? Do not count the header.
▸Show solution
tail -n +2 samples.tsv | wc -l
Exercise 4
How many reads are in reads.fastq?
▸Show solution
echo $(( $(wc -l < reads.fastq) / 4 ))
wc -l < fileprints only the number (without the file name), and$(( ))does the division.
Exercise 5
Save the first 10 reads into first10.fastq, and check that the new file has the right number
of lines.
▸Show solution
head -n 40 reads.fastq > first10.fastq
wc -l first10.fastq
40 lines, because each read is 4 lines.
Exercise 6
Write only the sample IDs (the first column, without the header) into ids.txt. Then add the
line S25 to the end of it without opening an editor.
▸Show solution
tail -n +2 samples.tsv | cut -f 1 > ids.txt
echo "S25" >> ids.txt
tail -n 3 ids.txt
Exercise 7
The header of variants.vcf is 9 lines long. With a pipeline, count the variants.
▸Show solution
tail -n +10 variants.vcf | wc -l
- Next week you will do this without knowing the header length:
grep -v '^#' variants.vcf | wc -l.
Exercise 8 (harder)
How many variants passed the filter (PASS in the seventh column) and how many
did not?
▸Show solution
tail -n +10 variants.vcf | cut -f 7 | sort | uniq -c
15 PASS and 9 LowQual.