This R function plots a matrix with arrows pointing between adjacent cells. I wrote it to help with some assignments in my quantitative bioinformatics course at Loyola. It enabled me to quickly and conveniently draw sequence alignment matrices.
The direction of the arrows depends on the values of the pointer matrix. For example, if the value at a cell is 2, then a diagonal up-left arrow will be drawn starting at that cell. A 3 will draw an up arrow and a 4 will draw a left arrow. It’s easy to add more directions or change them.
Requirements
To use this script, you should have two matrices: values and pointers.
One example of a function that outputs two matrices is Dr. Liping Tong‘s implementation of the Needleman-Wunsch algorithm. You can see Dr. Tong’s code below.
Usage
This script requires two matrices. Here’s an example:
> x = needleman_wunsch("CTT","CCATGT",2,1,2)
> x
$F
C C A T G T
0 -2 -4 -6 -8 -10 -12
C -2 2 0 -2 -4 -6 -8
T -4 0 1 -1 0 -2 -4
T -6 -2 -1 0 1 -1 0
$Ptr
C C A T G T
0 0 0 0 0 0 0
C 0 2 6 4 4 4 4
T 0 3 2 6 2 4 6
T 0 3 5 2 2 6 2
> draw_alignment_matrix(x$F,x$Ptr)
Output

So, my function displays an image of a matrix with numbers from the x$F matrix and arrows with directions defined by the x$Ptr matrix.
Heh
I followed you back! Y’know, I realized I did something similar this week — except, I’m writing out PNG heatmaps corresponding to the values in the score matrix (so unfortunately, no arrows). On the bright side, it does make for a pretty picture
I made mine for a phylogeny project since I have a hard time reading giant blocks of numbers (1.8 kbases by 1.8 kbases).
Thanks for the reply!
Just thinking about your example… if you want to display the path without using arrows, and each residue corresponds to one pixel, perhaps you could simply highlight the path with a totally different color, so it stands out from the heatmap. Just a thought.
Pingback: C# & Science: A 2D Heatmap Visualization Function (PNG) at Ed's Big Plans