Hands-on training 1: Using Linux in Bioinformatics¶
1. Introduction¶
We are about to start a journey into real Bioinformatics. This series of four hands-on training activities will show you the basic workflows in bioinformatics: from the data management and processing with Linux, visualization, and posterior functional analyses. The practicals are divided into two big parts: Part I: Basics on Bioinformatics workflows: Transforming data into insight and Part II: Solving real cases in genomics.
These practicals also aim to develop other skills, very valuable in research but rarely experienced during the Degree, such as collaborating and doing reproducible research.
1.1 Part I: Basics on Bioinformatics workflows¶
The first part, Basics on Bioinformatics workflows: Transforming data into insight, consists of two practicals:
In this first practical (P1), Using Linux in Bioinformatics, you will learn the basic commands of Linux. Getting familiar with these commands is key in bioinformatics, as many pipelines rely on the combination of different tools through a terminal. In addition, biological data are stored in large text files that can be easily processed using Linux. In the second practical (P2), Data exploration and visualization, we will transform biological data into knowledge graphs.
P1 Learning outcomes¶
- Get familiar with the Unix directories
- Learn basic text-processing Linux commands and piping
- Use the Linux command-line to perform tasks on the data
P2 Learning outcomes¶
- Learn the grammar of graphics of
ggplot2 - Create the most common bioinformatics graphs (scatterplots, lineplots, barplots, ...)
- Understand the important elements of a ready-to-publish figure
1.2 Practicals organization¶
1.2.1 Jupyter Notebook¶
In these practicals, we are going to use Jupyter Notebook dashboard. A Jupyter Notebook is an interactive work environment that allows you to develop code in Python (and not only Python) dynamically, integrating blocks of code, text, graphics and images in the same document. It emerged in 2014 to offer the scientific community a very powerful set of tools to work with data, visualize it and be able to share the results with the community.
This new work philosophy contrasts a lot with the idea that we usually have of what programming and writing code is. This way of programming, called literate programming, emphasizes the purpose of writing comfortable text to read and understand, separated by blocks of code, combining text, equations and figures, and allowing sharing your research and results very easily.
1.2.2 Writing a report¶
Finally, to write our scientific reports, we recommend using an online $\LaTeX$ editor that's easy to use: Overleaf. We have created a ready-to-use template available here so you can write your results.
1.3. Jupyter Notebook menu¶
All navigation and actions in the Jupyter Notebook are available using the mouse through the toolbar:
save changes and creates a checkpoint
insert a cell below a selected cell
cut selected cell(s)
copy selected cell(s)
paste cell(s) below
move selected cell(s) up
move selected cell(s) down
run a cell
interrupt the kernel
restart the kernel
restart the kernel, then re-run the whole notebook
Shortcuts¶
We recommend learning the command-mode shortcuts:
Basic navigation: enter, shift-enter, up/k, down/j
Saving the notebook: s
Change cell types: c, m
Cell creation: a, b
Cell editing: x, c, v, d, z
2. Tools installation¶
If you're using Windows 10 or 11, a good alternative is to install Ubuntu 22.04 on it. The application is available in the Microsoft Store. Simply click on the Install button, and it will be downloaded and installed automatically. When launched for the first time, Ubuntu will inform you that it's Installing and you'll need to wait a few moments. When complete, you'll be asked for a username and password specific to your Ubuntu installation. With this step complete, you'll find yourself at the Ubuntu bash command line.
If you're using MacOS, you won't need to install Linux, as Apple has been using Unix as the underlying operating system on all of their computers since 2001. However, take into account that the installation of tools is different and may require additional steps that go beyond the scope of this practicals.
2.1 Installing Jupyter Notebook with pip¶
pip is a standard package-management system used to install and manage software packages written in Python.
- Install pip through the terminal window (if not already installed):
sudo apt install python3-pip
- Install Jupyter Lab (which includes Jupyter Notebook):
pip install jupyterlab
2.2 Installing Jupyter Notebook with conda¶
Conda is an open-source, cross-platform, language-agnostic package manager and environment management system. It was originally developed to solve difficult package management challenges faced by Python data scientists, and today is a popular package manager for Python and R.
Download the installer:
- Miniconda installer for Linux (Linux 64 bits)
- Anaconda installer for Linux (Linux 64 bits)
Both Anaconda and Miniconda uses conda as the package manager. The difference is that Miniconda only has the package management system, while Anaconda also comes with a bundle of pre-installed packages (e.g. jupyterlab, which contains Jupyter Notebook).
In your terminal window, run:
- Miniconda:
bash Miniconda3-latest-Linux-x86_64.sh - Anaconda:
bash Anaconda3-2025.06-1-Linux-x86_64.sh
- Miniconda:
Follow the prompts on the installer screens.
If you are unsure about any setting, accept the defaults. You can change them later.
To make the changes take effect, close and then re-open your terminal window.
You'll know that conda is installed because your command-line will be preceded with (base) to denote you are in your base conda environment.
- Install Jupyter Lab (which includes Jupyter Notebook) in the
baseenvironment if it is not installed by default.
conda install -c conda-forge jupyterlab
3. Quick introduction to Linux¶
Linux is an open source operating system, similar to Microsoft Windows or Apple Mac OS. The Unix operating system was created in 1969, when there was no such thing as a graphical user interface. Instructions sent to the computer were typed into the screen instead of using a mouse.
In Linux, folder navigation and program executions are performed in the terminal by typing commands. The terminal allows you to type input to the computer (i.e. run programs, move/view files etc.) and to see output from those programs. You can open a terminal using the Ctrl + Alt + T shortcut, or looking for the Konsole or Terminal application from the installed programs list in your computer.
You can also use Linux commands inside the Jupyter Notebook by specifying %%bash at the top of the cell. If nothing is specified, then the commands are expected to be in the Python programming language. Take into account that each %%bash cell runs in a new terminal: if you use cd in one cell, the next cell starts again in the original folder.
This table exemplifies the usage the most common commands used in Linux that we will use during this practical:
| command | function |
|---|---|
pwd |
print the current working directory |
cd <dir> |
change to directory "dir" |
ls |
list files and directories of current directory |
mkdir <name> |
create a new folder with name "name" |
rmdir <name> |
remove an empty folder |
mv <from> <to> |
move or rename a file |
cp <from> <to> |
copy a file |
rm <file> |
remove a file (cannot be undone!) |
touch <name> |
create empty file |
echo <something> |
print something (to a file if it is specified) |
grep |
searches the given file for lines containing a match to the given strings or words |
cut |
cutting out the sections from each line of files |
cat |
print file content |
head |
print first lines of a file |
tail |
print last lines of a file |
less and more |
print file content (large files) |
sort |
order file content |
uniq |
delete or show repeated lines |
tr |
replace a character with another |
fold |
wrap line to a specific width |
wc |
count lines, words and characters |
paste, join |
combine two files horizontally / by a common field |
man <command> |
show the manual of a command |
wget <url> |
download the contents from a link in the current directory |
cat filetmp \| cut -f1 > file |
redirect the output of a command to a file |
gunzip file.gz |
uncompress a gz file |
unzip file.zip |
uncompress a zip file |
Example of the ls command running from the terminal:
(TAB)1244149@MRB014:~/Documents/Project$ ls
Example of the ls command running from the Jupyter Notebook:
%%bash
ls
P1.ipynb P1_original.ipynb P1_solutions.ipynb PFAM_seqs.zip pfam seqs
Preparing the examples¶
In this practical, all the examples take place in the Desktop of the user 1244149, which contains some folders and files (Data, DND, TAB, PHB.pdf, ...). To reproduce the examples exactly in your own computer, we have prepared a copy of this Desktop. Run the following cell once: it downloads P1_examples.zip, unzips it, and copies its contents into your own Desktop (~/Desktop).
~/Desktop is a folder of your Linux home, not the Windows Desktop. If you run this cell again, the example files are copied again and their original version is restored. If you get the error unzip: command not found (it may happen in Ubuntu on Windows), install it with sudo apt install unzip.
From now on, when an example shows /home/UAB/1244149/, remember that in your computer the path to your home folder is different (you can check it with cd and then pwd). With this Desktop, you can try every example of this practical in the terminal: just follow them in order, because some examples modify the files that the previous ones created.
%%bash
cd ~
wget -O P1_examples.zip https://github.com/marta-coronado/TAB/raw/refs/heads/main/P1/P1_examples.zip
unzip -o P1_examples.zip
mkdir -p ~/Desktop
cp -r P1_examples/Desktop/. ~/Desktop/
ls ~/Desktop
4. The Unix Tree¶
Looking at directories from within a Unix terminal can often seem confusing. But bear in mind that these directories are exactly the same type of folders that you can see if you use Apple’s or Windows' graphical file-management programs. A tree analogy is often used when describing computer filesystems. From the root level (/) there can be one or more top level directories. In the example above, we show just three. When you log in to a computer you are working with your files in your home directory, and this will nearly always be inside a ‘Users’ directory. On many computers there will be multiple users.
Navigating your filesystem¶
Where are you? The first step to navigate the Linux filesystem is to know where you are. The command pwd prints the current directory into the screen.
(TAB)1244149@MRB014:~/Documents/Project$ pwd
/home/UAB/1244149/Documents/Project
When you log in to a Unix computer, you are typically placed into your home directory. In this example, we are not in our home directory (/home/UAB/1244149/) but in another subdirectory. The first forward slash that appears in a list of directory names always refers to the top level directory of the file system (known as the root directory). The remaining forward slashes delimit the various parts of the directory hierarchy. If you ever get ‘lost’ in Unix, remember the pwd command.
As you learn Unix you will frequently type commands that don’t seem to work. Most of the time this will be because you are in the wrong directory, so it’s a really good habit to get used to running the pwd command a lot.
To change directories, you type the command cd (change directory) followed by the path to the directory.
(TAB)1244149@MRB014:~/Documents/Project$ cd /home/UAB/1244149/Desktop
(TAB)1244149@MRB014:~/Desktop$
cd takes you to your home directory.
Note that the above path starts with / (root). This kind of paths are known as absolute paths. There is another type of path, called relative paths, in which you write the path in relation to your current directory. These paths do not start with /.
To see the files contained within your current directory, you use ls. ls shows a lists of subdirectories and files. It is useful if you don't know the content of a directory.
(TAB)1244149@MRB014:~/Desktop$ ls
Data DND PHB.pdf file.fasta.gz TAB
Directories, files and compressed files are shown with different colours. In this example, Data, DND and TAB are directories, PHB.pdf is a file, and file.fasta.gz is a compressed file.
How would you move to the Data subdirectory? Write the command using (a) an absolute path and (b) a relative path. (Consider you are in /home/UAB/1244149/Desktop, as in the previous example.)
%%bash
# Write here the command with the absolute and relative path
What if you wanted to go to an upper directory without having to write the full path? Two dots (..) are used in Unix to refer to the parent directory (the previous one). Every directory, except the root, has a parent directory.
(TAB)1244149@MRB014:~/Desktop/Data$ cd ..
(TAB)1244149@MRB014:~/Desktop$
You can even combine more than one .. (separated by /). Note that in these examples we are using a relative path (it does not start with /).
(TAB)1244149@MRB014:~/Desktop$ cd Data/HP
(TAB)1244149@MRB014:~/Desktop/Data/HP$ cd ../..
(TAB)1244149@MRB014:~/Desktop$
Current directory can be expressed in a path as a . or be omitted.
Let's practice absolute and relative paths! Change the following paths from absolute to relative assuming that you are currently located at /home/apeiron. For all paths, consider that you always start in /home/apeiron. (This one is a paper exercise: these folders do not exist in your computer.)
%%bash
# /home/apeiron/dodos
# /home
# /home/apeiron/dodos/alvilda
# /tmp
# /home/pablo/corona
TAB key.
Create a working directory¶
When starting any bioinformatic pipeline, usually the first step is to create the folder where we'll place all our data and scripts. This is called the working directory.
Directories are created in Linux with the command mkdir.
(TAB)1244149@MRB014:~/Desktop$ mkdir TFG
(TAB)1244149@MRB014:~/Desktop$ ls
Data DND PHB.pdf file.fasta.gz TAB TFG
You can also create a directory and a subdirectory at the same time by typing mkdir -p. If the parent directory already exists (as DND in our Desktop), mkdir -p simply creates the subdirectory inside it.
(TAB)1244149@MRB014:~/Desktop$ mkdir -p DND/DM
(TAB)1244149@MRB014:~/Desktop$ ls
Data DND PHB.pdf file.fasta.gz TAB TFG
(TAB)1244149@MRB014:~/Desktop$ cd DND
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM spells.txt
If you want to remove an (empty) directory, you can use rmdir.
(TAB)1244149@MRB014:~/Desktop/DND$ cd ..
(TAB)1244149@MRB014:~/Desktop$ rmdir TFG
(TAB)1244149@MRB014:~/Desktop$ ls
Data DND PHB.pdf file.fasta.gz TAB
rm -r. But take into account that you cannot undo it. This is considered the MOST DANGEROUS LINUX COMMAND YOU WILL EVER LEARN! Create a directory called P1_TAB in your home folder (~/P1_TAB) to store all the files that we will create in the next sections.
%%bash
# Write here the command to create that directory
5. Working with files in Linux¶
Creating files¶
We can create an empty file using the command touch. Simply type that command followed by the path where you want to create a file.
(TAB)1244149@MRB014:~/Desktop$ cd DND
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM spells.txt
(TAB)1244149@MRB014:~/Desktop/DND$ touch races.txt
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM races.txt spells.txt
You can also create files with content using echo.
(TAB)1244149@MRB014:~/Desktop/DND$ echo "Artificer" > my_classes.txt
> in a file that already exists, its content will be overwritten. That's why we use a new file, my_classes.txt: with echo "Artificer" > classes.txt we would lose the list of classes in classes.txt!
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM my_classes.txt races.txt spells.txt
For adding new lines to a file that already exists, use >> instead of >.
(TAB)1244149@MRB014:~/Desktop/DND$ echo "Bard" >> my_classes.txt
Create an empty file called animals.txt in the P1_TAB directory that you just created. Then, use echo to add at least three animals to that file, one per line. Check the result with cat.
%%bash
# Write here the commands used to create those files
Moving and copying files¶
Sometimes, you may have created a file in the wrong folder. What do you do now? Should you delete it using rm and create it again in the correct path? This may be a solution if your file is empty or if it is easy to create again. But imagine that you have spent 5 hours writing the introduction of your TFG, you save it in the wrong folder and you don't have a graphical interface. The solution is simple: moving the file. The command mv allows you to move a file from one path to another (the filename must be included in the path).
Let's see an example. Imagine you want to move the races.txt file into the DM folder.
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM my_classes.txt races.txt spells.txt
(TAB)1244149@MRB014:~/Desktop/DND$ mv races.txt DM/races.txt
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM my_classes.txt spells.txt
(TAB)1244149@MRB014:~/Desktop/DND$ cd DM
(TAB)1244149@MRB014:~/Desktop/DND/DM$ ls
races.txt
In this case we have used the relative paths, but we could have used the absolute paths to any other directory of our computer.
Let's see another use of mv. For that we will create a new file called weapons.txt.
(TAB)1244149@MRB014:~/Desktop/DND/DM$ cd ..
(TAB)1244149@MRB014:~/Desktop/DND$ touch weapns.txt
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM my_classes.txt spells.txt weapns.txt
Wait a minute... We wrote it wrong! Let's fix it using mv. For that you just have to move your file to the same directory but with a new name.
(TAB)1244149@MRB014:~/Desktop/DND$ mv weapns.txt weapons.txt
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM my_classes.txt spells.txt weapons.txt
Now it's perfect.
Suppose now that you want to copy the file spells.txt into the DM folder. We will use cp for that. It works exactly as mv but the result will be that your original file will still be in place.
(TAB)1244149@MRB014:~/Desktop/DND$ cp spells.txt DM/spells.txt
(TAB)1244149@MRB014:~/Desktop/DND$ ls
classes.txt DM my_classes.txt spells.txt weapons.txt
(TAB)1244149@MRB014:~/Desktop/DND$ cd DM
(TAB)1244149@MRB014:~/Desktop/DND/DM$ ls
races.txt spells.txt
Go to your home folder and create a file called colours.txt with 5 colours (one per line) using echo. Then, copy that file into the P1_TAB directory and remove the original file from your home folder. Finally, rename P1_TAB/colours.txt to colors.txt.
%%bash
# Write the commands you used here
Reading file contents¶
You have already learned how to create files, and how to move them from one folder to another. But how can you read the contents of a file using the command line?
One of the most used is cat. cat prints the whole contents of a file into the terminal. To use it, just type cat followed by the path to your file.
(TAB)1244149@MRB014:~/Desktop/DND/DM$ cd ..
(TAB)1244149@MRB014:~/Desktop/DND$ cat classes.txt
Artificer
Barbarian
Bard
Cleric
Druid
Fighter
Monk
Paladin
Ranger
Rogue
Sorcerer
Warlock
Wizard
Many times, however, you may find yourself with a very long file. Printing all its contents when you only want to see the first lines is not helpful, as you will have to scroll up a lot until you find the beginning (if it is really long you may not even find the beginning).
Luckily, there are other commands that allow us to partially see a file. The first one is head. This command prints, by default, the first 10 lines of a file.
(TAB)1244149@MRB014:~/Desktop/DND$ head classes.txt
Artificer
Barbarian
Bard
Cleric
Druid
Fighter
Monk
Paladin
Ranger
Rogue
You can specify the number of lines to be shown with head -n followed by the number of lines.
(TAB)1244149@MRB014:~/Desktop/DND$ head -n 2 classes.txt
Artificer
Barbarian
You can even use head to show all lines except the last X ones by adding a - before the number.
(TAB)1244149@MRB014:~/Desktop/DND$ head -n -2 classes.txt
Artificer
Barbarian
Bard
Cleric
Druid
Fighter
Monk
Paladin
Ranger
Rogue
Sorcerer
The opposite to head is tail. It shows, by default, the last 10 lines of a file.
(TAB)1244149@MRB014:~/Desktop/DND$ tail classes.txt
Cleric
Druid
Fighter
Monk
Paladin
Ranger
Rogue
Sorcerer
Warlock
Wizard
You can also specify the number of lines using tail -n.
(TAB)1244149@MRB014:~/Desktop/DND$ tail -n 3 classes.txt
Sorcerer
Warlock
Wizard
And you can show all lines except the first X ones by adding a + before the number. In this case, the number refers to the line where you start.
(TAB)1244149@MRB014:~/Desktop/DND$ tail -n +3 classes.txt
Bard
Cleric
Druid
Fighter
Monk
Paladin
Ranger
Rogue
Sorcerer
Warlock
Wizard
There are two more commands (pun not intended) to see the contents of a file: more and less. These two commands allow you to see the contents one "page" at a time from the beginning to the end. The difference between them is that less is faster than more because it does not load the whole file and it allows to go back and forward.
Try them in a terminal (not in the notebook) with any of the files of your Desktop. To exit, press q.
Getting information from a file¶
Sometimes, you need to find a specific line in your file, or count words, or obtain any other information from your file. This can be done automatically using some commands, avoiding the tedious task of opening the file and doing it yourself (which is almost impossible with files like the ones in FASTA format).
The first command you will learn is grep. grep filters lines that contain (or not) a given string of characters. It accepts regular expressions and is case sensitive.
(TAB)1244149@MRB014:~/Desktop/DND$ grep "Fire" spells.txt
Delayed Blast Fireball 7 Evocation 1 action Yes
Faerie Fire 1 Evocation 1 action Yes
Fire Bolt 0 Evocation 1 action No
Fire Shield 4 Evocation 1 action No
Fire Storm 7 Evocation 1 action No
Fireball 3 Evocation 1 action No
Wall of Fire 4 Evocation 1 action Yes
There are additional options to use with grep that will allow you to make more sophisticated filters.
man followed by the command name.
For instance, you can use grep -i to ignore case, grep -v to filter lines that do not contain the pattern, grep -E to filter more than one pattern (different patterns are specified using |), or grep -n to print line numbers.
A couple of useful options are grep -c and grep -cw, which count the number of lines that contain the pattern. Let's see the difference with an example:
(TAB)1244149@MRB014:~/Desktop/DND$ grep -c "Fire" spells.txt
7
(TAB)1244149@MRB014:~/Desktop/DND$ grep -cw "Fire" spells.txt
5
grep -cw counts fewer lines than grep -c because the w option refers to "word". grep -cw counts the lines that contain the word Fire, while grep -c counts also the lines with words that contain the pattern Fire. You can check the count by checking the output of the first grep example.
Speaking of counting words, the command wc is used to count the number of lines, words, and characters (respectively) of a file.
(TAB)1244149@MRB014:~/Desktop/DND$ wc classes.txt
13 13 96 classes.txt
The options wc -l, wc -w and wc -c only print the number of lines, words and characters, respectively.
Sometimes, like in the spells.txt file above, information is stored in fields separated by TABs, dots, spaces or any other character. You may want to obtain the content of a specific field. In this case, you should use the command cut. This command extracts the field given to the -f option. Let's see an example:
(TAB)1244149@MRB014:~/Desktop/DND$ cut -f1 spells.txt
Abi-Dalzim's Horrid Wilting
Absorb Elements
Acid Splash
Aganazzar's Scorcher
Aid
Alarm (Ritual)
[...]
The above code extracts the first field (f1) separated by TAB. By default, cut expects TAB-separated values. If you have fields separated by any other character, you must specify it with the option -d"separator".
(TAB)1244149@MRB014:~/Desktop/DND$ cut -f4 -d" " spells.txt
You can also select more than one field with a comma. For example, here, fields 1 and 3 are selected
(TAB)1244149@MRB014:~/Desktop/DND$ cut -f1,3 spells.txt
Abi-Dalzim's Horrid Wilting Necromancy
Absorb Elements Abjuration
Acid Splash Conjuration
Aganazzar's Scorcher Evocation
Aid Abjuration
Alarm (Ritual) Abjuration
[...]
When selecting a range of non-alternating fields, you use a hyphen.
(TAB)1244149@MRB014:~/Desktop/DND$ cut -f1-3 spells.txt
Abi-Dalzim's Horrid Wilting 8 Necromancy
Absorb Elements 1 Abjuration
Acid Splash 0 Conjuration
Aganazzar's Scorcher 2 Evocation
Aid 2 Abjuration
Alarm (Ritual) 1 Abjuration
[...]
In the above example, fields from 1 to 3 have been selected. If you need all fields from field X to the last one, do not write a second number after the hyphen.
The next command, uniq, is very useful for files with repeated lines. You can remove all the repetitions but one (uniq), obtain ONLY the lines that are not repeated (uniq -u), the lines that are repeated (uniq -d) and even count the number of times each line is repeated (uniq -c).
uniq only compares consecutive lines. If the repeated lines are not one after the other, uniq will not detect them. That's why uniq is almost always used after sort (you will see an example in section 6).
Modifying the contents of a file¶
It is likely that you do not want to obtain certain information but modify something in the file. In the next section you will learn how to save the outputs into files. For now, you'll see some commands that could be used to alter the contents of a file.
In python, the method translate allows you to substitute a character by another one. The command tr does the same in Linux. Imagine that you want to transcribe the following DNA sequence into RNA.
(TAB)1244149@MRB014:~/Desktop/DND$ cd ../TAB
(TAB)1244149@MRB014:~/Desktop/TAB$ cat dna.txt
CACGGAGTTGTTTAGTTGTAATTATTGTACGCATAAGGATTGGTATCGTTGGGGGGATAATAAGCA
You just have to pass the DNA sequence to tr using | and change all Ts into Us.
(TAB)1244149@MRB014:~/Desktop/TAB$ cat dna.txt | tr 'T' 'U'
CACGGAGUUGUUUAGUUGUAAUUAUUGUACGCAUAAGGAUUGGUAUCGUUGGGGGGAUAAUAAGCA
tr does not replace whole patterns. It replaces the first character of the first string to the corresponding character in the second string.
The tr command can also be used to squeeze letters that are repeated in tandem using tr -s or to delete a given character from the string using tr -d (followed in both cases with the character to squeeze/delete).
Another useful command is sort, which, as the name states, orders the lines of a file. By default, lines are ordered lexicographically (including numbers). You can sort in reverse order using sort -r, or sort by field using sort -k (followed by the field number). Numerical sorting can also be specified using sort -n.
fold is a command that may not seem useful at first, but that can be used to obtain information in a very elegant way. This command takes a file and folds the line to fit a specific number of characters (specified using -w). For example:
(TAB)1244149@MRB014:~/Desktop/TAB$ fold -w 3 dna.txt
CAC
GGA
GTT
GTT
TAG
TTG
TAA
TTA
TTG
TAC
GCA
TAA
GGA
TTG
GTA
TCG
TTG
GGG
GGA
TAA
TAA
GCA
Finally, there are 3 commands that can be used to combine information from 2 files: paste, join and cat.
The difference among them is the way they combine files, paste combines files horizontally, join by fields (first field by default) and cat vertically.
6. Pipes and redirections¶
Pipes¶
Pipelines are created by combining multiple instructions in a row, so that the output of one is the input for the next. In Linux, pipes (|) work the same way. There was an example above of a pipe, when you learned tr. In that case, the output of the cat command was sent to tr as an input.
(TAB)1244149@MRB014:~/Desktop/TAB$ cat dna.txt | tr 'T' 'U'
CACGGAGUUGUUUAGUUGUAAUUAUUGUACGCAUAAGGAUUGGUAUCGUUGGGGGGAUAAUAAGCA
You can pipe more than two commands at the same time, creating what are called one-liners. The following one-liner converts a DNA sequence into RNA, separates each codon in a different line, and then filters only those lines that contain the codon "UAA".
(TAB)1244149@MRB014:~/Desktop/TAB$ cat dna.txt | tr 'T' 'U' | fold -w 3 | grep "UAA"
UAA
UAA
UAA
UAA
Pipes are very useful for combining many commands without having to create intermediate files that you will have to remove afterwards.
Remember that uniq only compares consecutive lines? Let's count how many spells of each school (3rd field) there are in spells.txt. Since we are in ~/Desktop/TAB, we use the relative path ../DND/spells.txt. Without sort, the same school appears several times, because its lines are not consecutive:
(TAB)1244149@MRB014:~/Desktop/TAB$ cut -f3 ../DND/spells.txt | uniq -c | head -n 6
1 Necromancy
1 Abjuration
1 Conjuration
1 Evocation
2 Abjuration
1 Enchantment
If we sort the lines first, all the identical lines are together and uniq -c counts them correctly:
(TAB)1244149@MRB014:~/Desktop/TAB$ cut -f3 ../DND/spells.txt | sort | uniq -c
4 Abjuration
4 Conjuration
2 Divination
2 Enchantment
14 Evocation
1 Illusion
1 Necromancy
2 Transmutation
And adding a second sort (numerical and in reverse order) we get a ranking, with the most common school on top:
(TAB)1244149@MRB014:~/Desktop/TAB$ cut -f3 ../DND/spells.txt | sort | uniq -c | sort -nr | head -n 3
14 Evocation
4 Conjuration
4 Abjuration
sort | uniq -c | sort -nr is one of the most used one-liners in bioinformatics!
Combine the commands cat, head and/or tail using pipes (|) to print ONLY the 3rd line of the colors.txt file that you have created in the P1_TAB directory.
%%bash
# Write here the commands used
Redirections¶
Until now, all the outputs shown in the examples are displayed in the terminal but not saved in any file. Remember the > and >> symbols from the above example of the echo command? Probably not, but those symbols are used as output redirectors.
There are 3 input/output redirectors: <, >, and >>.
<(input file): you pass a file as an input to a command.
(TAB)1244149@MRB014:~/Desktop/TAB$ cat < dna.txt
CACGGAGTTGTTTAGTTGTAATTATTGTACGCATAAGGATTGGTATCGTTGGGGGGATAATAAGCA
>(output file): you save the output of a command into a file, instead of showing it in the terminal. If the file already exists, its content is overwritten.
(TAB)1244149@MRB014:~/Desktop/TAB$ cat dna.txt | tr 'T' 'U' > rna.txt
(TAB)1244149@MRB014:~/Desktop/TAB$ cat rna.txt
CACGGAGUUGUUUAGUUGUAAUUAUUGUACGCAUAAGGAUUGGUAUCGUUGGGGGGAUAAUAAGCA
>>(append file): you append the output of a command to a file. If the file does not exist, it is created.
(TAB)1244149@MRB014:~/Desktop/TAB$ echo "CACGCATCCTCCTTACGCTAGTCTTCTGGATCC" >> dna.txt
(TAB)1244149@MRB014:~/Desktop/TAB$ cat < dna.txt
CACGGAGTTGTTTAGTTGTAATTATTGTACGCATAAGGATTGGTATCGTTGGGGGGATAATAAGCA
CACGCATCCTCCTTACGCTAGTCTTCTGGATCC
Practice redirections with the files in P1_TAB: for example, save the sorted list of animals of animals.txt into a new file called animals_sorted.txt, and then append the contents of colors.txt to it. Check the result with cat.
%%bash
# Write here the commands used
The end: Integrative exercise¶
In your research group you're analyzing the proteome (the set of proteins expressed by an organism or cell) of different species. This information is available for downloading here: https://github.com/marta-coronado/TAB/raw/refs/heads/main/P1/PFAM_seqs.zip
Integrating different commands that you have learnt during this practical, answer the following questions:
- Once you have downloaded the data (use
wget!), unzip the file with the proper function according to the type of compression. - Indicate the basic properties of this data: how many folders and files have you unzipped? How many lines do they have? etc.
Create a folder called IntegrativeExerciseP1 inside P1_TAB and move the unzipped folders into it. Then, answer the following biological questions:
- How many proteins does the human proteome have? And the mouse? (The human proteome is stored in the file
Homo_sapiens.GRCh38.pep.all.faand the mouse proteome inMus_musculus.GRCm38.pep.all.faand have the structure of a regular fasta file) - The files with the extension
.lisocontain only the longest protein (isoform) of each gene. How many sequences does each of these files contain? - The files of the
pfamfolder (.dom) contain the protein domains found in the longest isoform of each gene. The7tm_4domain is characteristic of olfactory receptors. How many7tm_4domains are there in human, mouse and chicken? What does this suggest about these species? - Save the identifiers of all the proteins of the human proteome, without the comment, into a file called
human_ids.txt(the comment always starts after the first space in the header line of each sequence of the fasta file). Check withwc -lthat the file has as many lines as proteins in the human proteome. - How many genes are represented in the human proteome file?
- How many genes of the human proteome have a single isoform? And how many have more than one isoform? Check that both numbers add up to the answer of question 5.
- What is the most common amino acid in the human proteome? Use the file with the longest isoform of each gene (
Homo_sapiens.GRCh38.pep.all.fa.liso). (Hint: remove the header lines and usefoldto put each amino acid in a different line) - In the output of the previous question, some letters are not among the 20 standard amino acids. Which ones? Look for what they mean. How many times does the letter
Uappear? - How many domains are there in the human proteome? How many are different? (The domains are in the file
Homo_sapiens.GRCh38.pep.all.fa.liso.domand their identifiers always start with PF. Since the columns of this file are separated by multiple spaces, first replace them by a single space with the commandsed -r 's/ +/ /g') - Which human protein has the most domains? Which gene does it belong to (look for its identifier in the
.lisofile)? Which are its two most frequent domains? - How many times is the 19th most frequent domain found? Which domain is it?
%%bash