Introduction

Welcome. We are pleased to have you take this Introduction to bioinformatics analysis of Plasmodium falciparum genome data training with us. Before you begin, it’s important to have some foundational skills in Linux, R and few bioinformatics tools. Below you will find self-assessments for Linux and R. If you are unable to complete these, see our list of recommended resources (below) to help bring you up to speed in these areas.

Background

Humans and computers commonly interact in many different ways, such as through a keyboard and mouse, touch screen interfaces, or using speech recognition systems. The most widely used way to interact with personal computers is called a graphical user interface (GUI). With a GUI, we give instructions by clicking a mouse and using menu-driven interactions.

While the visual aid of a GUI makes it intuitive to learn, this way of delivering instructions to a computer scales very poorly. Imagine the following task: for a literature search, you have to copy the third line of one thousand text files in one thousand different directories and paste it into a single file. Using a GUI, you would not only be clicking at your desk for several hours, but you could potentially also commit an error in the process of completing this repetitive task. This is where we take advantage of the Unix shell. The Unix shell is both a command-line interface (CLI) and a scripting language, allowing such repetitive tasks to be done automatically and fast. With the proper commands, the shell can repeat tasks with or without some modification as many times as we want. Using the shell, the task in the literature example can be accomplished in seconds.

Practical Goals

  • navigate to a file/directory
  • create a file/directory
  • check the length of a file
  • chain commands together
  • retrieve a set of files
  • iterate over files
  • run a shell script containing her pipeline

The Shell

The shell is a program where users can type commands. With the shell, it’s possible to invoke complicated programs like climate modeling software or simple commands that create an empty directory with only one line of code. The most popular Unix shell is Bash (the Bourne Again SHell, so-called because it’s derived from a shell written by Stephen Bourne). Bash is the default shell on most modern implementations of Unix and in most packages that provide Unix-like tools for Windows.

Using the shell will take some effort and some time to learn. While a GUI presents you with choices to select, CLI choices are not automatically presented to you, so you must learn a few commands like new vocabulary in a language you’re studying. However, unlike a spoken language, a small number of “words” (i.e. commands) gets you a long way, and we’ll cover those essential few today.

The grammar of a shell allows you to combine existing tools into powerful pipelines and handle large volumes of data automatically. Sequences of commands can be written into a script, improving the reproducibility of workflows.

In addition, the command line is often the easiest way to interact with remote machines and supercomputers. Familiarity with the shell is near essential to run a variety of specialized tools and resources including high-performance computing systems. As clusters and cloud computing systems become more popular for scientific data crunching, being able to interact with the shell is becoming a necessary skill. We can build on the command-line skills covered here to tackle a wide range of scientific questions and computational challenges.

Let’s get started.

First, let’s open our windows terminal by clicking on the terminal icon or from the search bar type Terminal. When the shell is first opened, you are presented with a prompt, indicating that the shell is waiting for input.

:~$

The shell typically uses $ as the prompt, but may use a different symbol. In the examples for this lesson, we’ll show the prompt as $. Most importantly, when typing commands, either from these lessons or from other sources, do not type the prompt, only the commands that follow it. Also note that after you type a command, you have to press the Enter key to execute it.

The prompt is followed by a text cursor, a character that indicates the position where your typing will appear. The cursor is usually a flashing or solid block, but it can also be an underscore or a pipe. You may have seen it in a text editor program, for example.

So let’s try our first command by typing ls which is short for listing. This command will list the contents of the current directory:

ls

Output

Documents   Downloads     Library     Movies      Music
Pictures      Public     Malaria_training_2022   

Your results may be slightly different depending on your operating system and how you have arranged your filesystem.

If the shell can’t find a program whose name is the command you typed, it will print an error message such as:

ks

Output

ks: command not found

This might happen if the command was mis-typed or if the program corresponding to the command is not installed.

Working With Files and Directories

Creating directories

Now, we know how to explore files and directories, but how do we create them in the first place?

We will learn about creating and moving files and directories, using the Malaria_training_2022 directory as an example.

Step one: see where we are and what we already have

We should still be in the Malaria_training_2022 directory, which we can check using pwd. If you’re not there, move back to that folder.

Create a directory

Let’s create a new directory called Linux using the command mkdir Linux:

mkdir Linux

As you might guess from its name, mkdir means ‘make directory’. Since Linux is a relative path (i.e., does not have a leading slash), the new directory is created in the current working directory:

ls -F

Since we’ve just created the Linux directory, there’s nothing in it yet:

ls -F Linux

Note that mkdir is not limited to creating single directories one at a time. The -p option allows mkdir to create a directory with nested sub-directories in a single operation:

mkdir -p ../training/data ../training/results

The -R option to the ls command will list all nested sub-directories within a directory. Let’s use ls -FR to recursively list the new directory hierarchy we just created in the training directory:

ls -FR ../training

Naming files and directories

Complicated names of files and directories can make your life painful when working on the command line. Here we provide a few useful tips for the names of your files and directories.

  1. Don’t use spaces.

Spaces can make a name more meaningful, but since spaces are used to separate arguments on the command line it is better to avoid them in names of files and directories. You can use - or _ instead (as you can see in the workshop naming style). To test this out, try typing mkdir malaria training civ and see what directory (or directories!) are made when you check with ls -F.

# Create new directory using space in the naming
mkdir malaria training civ

# List directory
ls -F
## mkdir: cannot create directory ‘malaria’: File exists
## mkdir: cannot create directory ‘training’: File exists
## mkdir: cannot create directory ‘civ’: File exists
## civ/
## iris_data.txt
## malaria/
## session2_Lecture_command_lines.pptx
## session2_part1_unix_command_lines.html
## session2_part1_unix_command_lines.Rmd
## session2_part2_advanced_unix_command.Rmd
## session2_part3_writing_scripts_working_with_data.Rmd
## training/

What do you notice?

  1. Don’t begin names with - (dash).

Commands treat names starting with - as options.

  1. Stick with letters, numbers, . (period or ‘full stop’), - (dash) and _ (underscore).

Many other characters have special meanings on the command line. We will learn about some of these during this lesson. There are special characters that can cause your command to not work as expected and can even result in data loss.

If you need to refer to names of files or directories that have spaces or other special characters, you should surround the name in quotes (” “).

Create a text file

Let’s change our working directory to Linux folder we created using cd, then run a text editor called Nano to create a file called example.txt:

cd Linux
nano example.txt

Text Editor

Nano is a text editor that can only work with plain character data, not tables, images, or any other human-friendly media. We use it in examples because it is one of the least complex text editors. However, because of this trait, it may not be powerful enough or flexible enough for the work you need to do after this workshop. On Unix systems (such as Linux and macOS), many programmers use Emacs or Vim (both of which require more time to learn), or a graphical editor such as Gedit. On Windows, you may wish to use Notepad++. Windows also has a built-in editor called notepad that can be run from the command line in the same way as nano for the purposes of this session.

No matter what editor you use, you will need to know where it searches for and saves files. If you start it from the shell, it will (probably) use your current working directory as its default location. If you use your computer’s start menu, it may want to save files in your desktop or documents directory instead. You can change this by navigating to another directory the first time you ‘Save As…’

Let’s type in a few lines of text. Once you’re happy with our text, you can press Ctrl+O (press the Ctrl or Control key and, while holding it down, press the O key) to write our data to disk (you’ll be asked what file you want to save this to: press Return to accept the suggested default of example.txt).
Once our file is saved, you can use Ctrl+X to quit the editor and return to the shell.

In nano, along the bottom of the screen you’ll see ^G Get Help ^O WriteOut. This means that you can use Ctrl-G to get help and Ctrl-O to save your file.

nano doesn’t leave any output on the screen after it exits, but ls now shows that you have created a file called example.txt.

A Different Way for Creating Files

We have seen how to create text files using the nano editor. Now, try the following command:

touch my_file.txt

Question 10: What did the touch command do? When you look at your current directory using the GUI file explorer, does the file show up?

Question 11: Use ls -l to inspect the files. How large is my_file.txt?

Question 12: When might you want to create a file this way?

Removing Files / Directories

You might create a lot of files and directories in a process of analyzing data. What if you want to get rid of them? This is actually quite an important skill and one that is often overlooked. When you work with lots of data, getting rid of files you don’t need is very, very important. Cluttered directories are an absolute nightmare to deal with.

You can easily remove files with the rm command. For instance, let’s remove the file you’ve just created before proceeding with the rest of the session, otherwise future outputs may vary from those given in the lesson. To do this, you can use the following command:

rm my_file.txt

We could also use this following command:

rm -i my_file.txt

Here, there is a flag after rm -i which simply tells the command to ask permission before deleting. Indeed, when you run the command above, you will receive a prompt asking you if you really want to delete a file. We will see later on how to remove multiple files and directories.

Moving files and directories

Let’s move back to our Malaria_training_2022/ directory,

cd ~/Malaria_training_2022/

In our training directory, we have a file example.txt which isn’t a particularly informative name, so let’s change the file’s name using mv, which is short for ‘move’:

mv Linux/example.txt Linux/commands.txt

The first argument tells mv what we’re ‘moving’, while the second is where it’s to go. In this case, we’re moving Linux/example.txt to Linux/commands.txt, which has the same effect as renaming the file. Sure enough, ls shows us that Linux now contains one file called commands.txt

ls Linux

One must be careful when specifying the target file name, since mv will silently overwrite any existing file with the same name, which could lead to data loss. An additional option, mv -i (or mv –interactive), can be used to make mv ask you for confirmation before overwriting.

Note that mv also works on directories.

Let’s move commands.txt into the current working directory. We use mv once again, but this time we’ll use just the name of a directory as the second argument to tell mv that we want to keep the filename but put the file somewhere new. (This is why the command is called ‘move’.) In this case, the directory name we use is the special directory name that we mentioned earlier.

mv Linux/commands.txt .

The effect is to move the file from the directory it was in to the current working directory. ls now shows us that Linux is empty:

ls Linux

Alternatively, we can confirm the file commands.txt is no longer present in the Linux directory by explicitly trying to list it:

ls Linux/commands.txt

ls with a filename or directory as an argument only lists the requested file or directory. If the file given as the argument doesn’t exist, the shell returns an error as we saw above. We can use this to see that commands.txt is now present in our current directory:

ls commands.txt

Copying files and directories

The cp command works very much like mv, except it copies a file instead of moving it. We can check that it did the right thing using ls with two paths as arguments — like most Unix commands, ls can be given multiple paths at once:

cp commands.txt Linux/linux_commands.txt
ls commands.txt Linux/linux_commands.txt

We can also copy a directory and all its contents by using the recursive option -r, e.g. to back up a directory:

cp -r Linux Linux_backup

We can check the result by listing the contents of both the Linux and Linux_backup directory:

ls Linux Linux_backup

Renaming Files

Suppose that you created a plain-text file in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it: statstics.txt

Question 13: After creating and saving this file you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?

  1. cp statstics.txt statistics.txt
  2. mv statstics.txt statistics.txt
  3. mv statstics.txt .
  4. cp statstics.txt .

Moving and Copying

Question 14: What is the output of the closing ls command in the sequence shown below?

pwd

Output

/Users/tony/data

ls

Output

proteins.fasta

mkdir recombined
mv proteins.fasta recombined/
cp recombined/proteins.fasta ../proteins-saved.fasta
ls

Answer

  1. proteins-saved.fasta recombined
  2. recombined
  3. proteins.fasta recombined
  4. proteins-saved.fasta

Removing files and directories

Returning to the Malaria_training_2022/ directory, let’s tidy up this directory by removing the commands.txt file we created. The Unix command we’ll use for this is rm (short for ‘remove’):

rm commands.txt

We can confirm the file has gone using ls:

ls commands.txt

The Unix shell doesn’t have a trash bin that we can recover deleted files from (though most graphical interfaces to Unix do). Instead, when we delete files, they are unlinked from the file system so that their storage space on disk can be recycled. Tools for finding and recovering deleted files do exist, but there’s no guarantee they’ll work in any particular situation, since the computer may recycle the file’s disk space right away.

Question 15: What happens when we execute rm -i Linux_backup/linux_commands.txt? Why would we want this protection when using rm?

Now, try to remove the Linux directory using rm Linux and see what will happen:

rm Linux

This happens because rm by default only works on files, not directories.

rm can remove a directory and all its contents if we use the recursive option -r, and it will do so without any confirmation prompts:

rm -r Linux

Given that there is no way to retrieve files deleted using the shell, rm -r should be used with great caution (you might consider adding the interactive option rm -r -i).