Author Archives: Andrea Telatin

Try to close a gap

Here you are a simple homework to practice with Primer3 and our genome finishing goal.

Using the information stored in mate-paired reads, we see that the 5′-end of “contig05189” is linked to 3′-end of “contig01316”. Please note the 3′ and 5′ ends are indicated in gray boxes for each connection.

You can see that the path to the 3′ is a little bit more complex.

Continue reading

Tagged ,

Hello_everyone.pl

Now that we know where Perl stores command line parameters (hint: @ARGV), we can revise the original Hello world! script.

1) Write a say_hello.pl script that takes a single argument from command line, that is the name of a person, and then prints “Hello name!”.

2) Write a hello_everyone.pl script that receive as input a list of names, and then print for each name a “Hello name” line.
This means that we want to type a command like this:

perl hello_everyone.pl Lineweaver Burk

and to have, as output:

Hello Lineweaver!
Hello Burk!

3) Write a program hello_count.pl that counts the people to greet and prints the number. This means that we want to type a command like this:

perl hello_everyone.pl Lineweaver Burk 

and to have, as output:

Hello you 2 guys!

First computer lab

Welcome to the Computer Lab #1

From text editor to program execution

Today we start playing with our computer: we want to

  1. start using the shell terminal
  2. start using a programming editor
  3. preparing and executing simple Perl scripts
  4. Make Primer3 design a couple of primers

Continue reading

Tagged

Auto-completing filenames

The shell if filled with shortcuts and tricks, but there a vital feature you can’t miss at all: the auto-completion (see what says Wikipedia about it). In a few words if you start typing a filename or command an you press [Tab] once it will be automatically completed. If more than a file exist that starts with the characters you typed, double [Tab] prints a list of possibilities.

This is much more than a trick: it’s a safe way to check that you are giving the right parameter to your scripts/programs.

If you mis-type a filename (or directory name) you’ll see the error only executing the command.

cd ~
cd Documanti
-bash: cd: Documanti: No such file or directory
 
cd ~
cd Docu[Tab]
cd Documenti (automatically completed)

 

 

Tagged ,

UNIX shell, from simple to complex

We used some very simple commands during our first lab. Let see a new command. This post shows how to use two handy features of the shell: output redirection and program piping.

grep look for a patter into a line, and print it only if the pattern is present in the line. If we have a multifasta file, each header has a “>” sign. If we want to view all headers we can type:

grep ">" multifasta.fa

Output redirection

Most UNIX commands prints their output to screen. If we want to save that output we can use the “>” character:

ls -l > filelist.txt

we won’t see as usual the file listing with this command, because the “>” redirects ls’s output to the filelist.txt file. If we print the file:

cat filelist.txt

we will have a proof.

Program “pipes”

A very useful feature of the UNIX shell is the possibility to send the output of a program to another program. This is done with the pipe character: |.

ls -la | head -n 3

With this command we will see the first three lines of the ls output. Nice?

cat multifasta.fa | grep ">" | wc -l
  • can you guess what happens with this command???
  • can you write a command that prints only files created in march? (answer with comments)
Tagged , , , ,

Second lesson: slides

Today we had an extremely fast introduction to Perl (crash course). In particular we

  • Reviewed variables and learned what hashes are and how to use them
  • Reviewed the for and foreach cycles
  • Introduced the while loop
  • Introduced mathematical and string operators and functions
  • Introduced the if test
  • Saw how to open a file and get parameters (@ARGV).

 Download slides

Tagged

IF statement and Perl operators

See this tutorial for a comprehensive list.

Remember that “=” has to be used only to assign a value to a scalar. To compare two numbers we’ll use the ‘==’ operator, while to compare two strings the ‘eq’ operator.

This script is a shor example:

#!/usr/bin/perl
 
$n1 = 10;
$n2 = $n1;
$n1++;
 
if ($n1 == $n2) {
print "Equal numbers: $n1 = $n2.\n";
} elsif ($n1>$n2) {
print "$n1 is greater than $n2.\n";
} else {
print "$n2 is greater than $n1.\n";
}
Tagged

Reading a (text) file line by line with Perl

#!/usr/bin/perl
 
# Get the filename from the user via command line
$filename = $ARGV[0];
 
# Here we use the open command that reads a file (second parameter)
# and link it to a "handle" (first parameters, conventionally uppercase).
# "||" means "or", that is if open returns false... exit with an error message
open(FILE, "$filename") || die " FATAL ERROR:\n Unable to open \"$file\".\n";
 
# Now we retrieve line by line calling the file handle with < and >.
# Line content is stored in the special scalar "$_"
while (<FILE>) {
  $linecounter++;
  print "$linecounter: $_";
}

to use this script we need to add a parameter to the command line: the filename! eg:

perl script.pl filename.txt

You can find a detailed tutorial on the open command here.

Tagged , ,

Perl variables

In any programming language we need to store information, an to retrieve it when needed. Perl has three types of variables:

  • scalar – they can store numbers or text (strings).
  • array – they are ordered lists of scalar.
  • hashes – they are unordered lists of values with a “label” called name.

Scalar are identified by the ‘$’ character. You can assign a value to a scalar with the ‘=’:

$age = 15;
$name = 'Larry';
$age = $age+1;        # an more compact alternative is $age++, if you have to add 1
$combine1 = "$name is $age";
$combine2 = '$name is $age';
print "Double quotes:  $combine1\n";
print "Sinngle quotes: $combine2\n;"

This small script has two assignation, a number and a string. Then we increment the $age variable by 1.
Note that for numbers we don’t use any quote, while for string we use single quotes or double quotes. The difference – as mentioned – is that double quotes are interpreted and substitute $name with the content of the variable itself.

Arrays are list of strings. To create a new array the syntax is a list of comma separated scalars enclosed by round brackets:

@array = ('first', 'second', 15, 16);

each element of the array has an index starting from 0, thus the last element of the array in the example has index 3. As each element of the array is a scalar, we refer to it with the “$” sign, specifying the index:

$firstElement = $array[0];

A special variable is created by Perl and contains the index of the last element:

$lastindex = $#array;

Thus:

$lastElement = $array[$#array];

Finally hashes are unordered lists of couples of scalar: one is the key and has an associated value.  We’ll see hashes in detail in lesson2.

(in Italian) c’è un buon tutorial su array ed hash in questo sito, valido anche per altri argomenti ovviamente… vi consiglio il corso completo come lettura serale ;).

Tagged , , , ,

Perl: Hello World!

Here you are the code of our very first script we made in lesson1:

#!/usr/bin/perl
 
$yourname = 'George';
print "Hello world!\n";
print "and hello $yourname too!\n";

Remember that if you want to add comments they should be preceded by the # character. The very first line is an instruction for the shell rather than for Perl, but at the moment just remember to add it.

Each instruction has to end with a “;”.

\n is the special way to add a new-line.

Do you remember the difference between double quotes and single quotes?

 

Tagged ,