Building and scanning with weight matrices

Weight matrices are used to represent motifs and to find these motifs in sequences. The advantages of weight matrices includes that the motifs does not need to be perfectly conserved and that when a sequence is scanned with a weight matrix, then all the motif found in the sequence has score indicating the odds of seeing that motif in that sequence.

Here we are going to first build a weight matrix using sequences from exon-intron donor splice sites and then use the matrix to scan for exon-intron borders. This could be used together with an acceptor splice site scanner as part of a gene predictor.

Given a file with a set of donor splice sites and a sequence to scan the following process is required:

  1. Calculate the nucleotide frequencies at each position, and calculate the log odds ratio with the background distribution
  2. Scan the sequence for potential donor sites

Setting up for today

  1. Open a terminal and change into your Perl course directory:
    cd perlCourse
  2. Make a new subdirectory named pswm:
    mkdir pswm
    and change into it:
    cd pswm
  3. Make a new subdirectory named bin
    mkdir bin
    and one named data:
    mkdir data

Making a weight matrix

The script makeWeightMatrix is supposed to make a position specific weight matrix from sequences like those in the file donorSpliceSites.seq.

  1. Save the makeWeightMatrix script in the perlCourse/pswm/bin directory and make sure you have execute permission on the script
    chmod u+x bin/makeWeightMatrix
    Save the donorSpliceSites.seq file in the data directory. You do not have to change the permissions on this file.

  2. Open the script bin/makeWeightMatrix with kate (or another text editor)
    kate bin/makeWeightMatrix &

  3. Run the script using the data file as input:
    bin/makeWeightMatrix data/donorSpliceSites.seq
    This should give you some error messages, which you now have to fix. Always start with the first one, fix it, and then rerun to see if fixing it also fixed the other error messages

  4. Now that the script seems to be working, take a look at input and output. Looking at the first position of all the sequences in the data file (open it in a text editor or with less):
    less data/donorSpliceSites.seq
    In less use q to quit. Is the output what you would expect? Perl can not find all bugs in your program automatically (bugs are errors in the program). Find the bug and fix it. It is often usefull to print out the values of variables at different places in the program to check if the values are what you would expect

  5. Read through the script. Try to understand what each line of code does.
    1. Which variables are scalar? arrays? hashes?
    2. What is the function of my, foreach, while, chomp, uc, chop, keys, sort, print?
    3. The scalar $nuc is declared (my $nuc) several times in the code. What is the scope of each of these declarations?

  6. Some of the code is very redundant, which makes it harder to read and it is also a breeding ground for bugs. Write a subroutine which given a position number, and a pswm hash (%pswm) will print the part of the output which relates to that position. Remove the code which is no longer necessary and add code which uses the new subroutine

  7. Write a subroutine which calculates the log odds score, and make the necessary changes to the code. Making a subroutine for a calculation like this makes it possible to change/correct the calculation one place rather than having to make the same change in five places.

  8. Run your script to build the weight matrix for the next exercise:
    bin/makeWeightMatrix data/donorSpliceSites.seq > donor.pswm

Scanning a sequence with a weight matrix

The next step is to scan a sequence with the weight matrix. In this case we have picked the CTDSP1 gene which regulates the RNA pol II gene.
  1. Download the script scanSequenceWithWeightMatrix to the bin directory, and run it with a short sequence f.x.
    bin/scanSequenceWithWeightMatrix AGAGT donor.pswm
    It is usually a good idea to test with a short example before using a longer one. On linux machines the keyboard combination ctrl- c stops a program.

  2. To find out where the bug is add lines like print "1\n"; at strategic places throughout the script (using a different number for each line). A good place is usually right after the { starting a block of code. Then rerun the script and see if it prints out what you would expect. Printing out the contents of a variable can also help. Chancing the print statements to print "1: $_\n"; will print out the number and the contents of the $_ variable. Use this to track down the bug

  3. The first while loop in the script reads the position specific weight matrix (pswm) made by the previous script. This part of the code is highly redundant, write a sub routine which will read the weights for one position in the matrix and return a hash with the scores. Using this the first if statement becomes:
    if ($_ eq "Position 1") {
       %pswm1 = readMatrixColumn();
    }
    
    What is the difference between last and return? Would it have been easier to fix the bug in the previous step if you had rewritten the code first?

  4. What is the contents of the @ARGV array (Line 7), and where does it come from? Why is it not necessary to declare @ARGV with my before it is used, and why is the name the array in uppercase letters? What does shift do? Try printing the contents of @ARGV before and after the my $sequence = shift @ARGV; line.

  5. What is the function of the following lines of code:
    $score += $pswm1{substr($sequence, $pos-1, 1)};
    $score += $pswm2{substr($sequence, $pos, 1)};
    $score += $pswm3{substr($sequence, $pos+1, 1)};
    $score += $pswm4{substr($sequence, $pos+2, 1)};
    $score += $pswm5{substr($sequence, $pos+3, 1)};
    
    Why is it necessary to subtract one from $pos in the first line?

  6. What is the variable $minScoreCutoff used for?

  7. Read the script and make sure you understand what every line does and why it is done in that way. Pay special attention to the <> operator. Why is it usefull to read input at more than one place in the script?

  8. Write a new sub routine which takes as input a position and a score. If the score is above the $minScoreCutoff then sub routine should print out the position and the score in this format (BED):
    chr2 position position donor score
    
    Write chr2 and donor literally. Position should be the sequence position plus 219264478 (to make it the genomic position) and score is the weight matrix score.

  9. Set the $minScoreCutoff = 5 and scan the full CTDSP1 gene for donor sites:
     bin/scanSequenceWithWeightMatrix "sequence" donor.pswm > CTDSP1_donorSites.bed
    
    Use less to look at the output:
    less CTDSP1_donorSites.bed
    
    Does the output look like expected?

  10. Go to the UCSC genome browser and select genome browser in the upper left corner. In the My Data drop down menu select Custom tracks. Copy/paste your results into the data box. Press the Submit button and click on go to genome browser. Copy/paste chr2:219264478-219270664 into the position box and press go. Compare the User Track to the exon intron annotation of the CTDSP1 gene. Does the weight matrix give good predictions?