%Read in the gel.
[imagey_combine, gelbounds] = readingel('rd91g-rescan.gel');
% Flip the image to get it to a "standard" orientation, with the
% diagonal going from the top left to the bottom right, with
% strong uncleaved spot on the top left, and the "main band"
% going across the top. If the gel has been flipped horizontally
% or vertically, you can use the routines reverse_horizontal and
% reverse_vertical to flip them.
imagey_combine = imagey_combine';
% The alignment.
[imagex_horizalign,anchorlines_vert]  = horizalign(imagey_combine,[]);
[imagex_align,     anchorlines_horiz] = horizalign(imagex_horizalign',[]);

%Read in the sequence. Set the offset to 21 (to use standard
%tetrahymena ribozyme numbering), and the nucleotide should be G.
[POS_ArrayBOMB,sequence,Offset,cleavage_sites,threeprime,SeqSelectionWindow]=Fasta2Cleave('TtLSU.fas','');

%This text file has numbers corresponding to the distances 
% the subfragments of the P4-P6 RNA travels in an 8% PAGE gel.
% Its based on running T1's for different running times 
% on a regular 1D gel. It will be used below to interpolate
% band assignments between the short-RNA fragments (where its easy
% to distinguish the ladder) to the uncleaved RNA positions.
load xsel_ladder_calibrate.txt
% OK, now its time to annotate the gel with x and y coordinates.
% In this case we'll work our way up the gel, marking each
%streak which corresponds to a given bomb with the bomb number.
% coordinates in the orthogonal direction are estimaed by
% reflection off the diagonal.
%  When we run the definegrid2 routine, 
% (1) Hit "d", and delineate the diagonal.
% (2) Hit "m", and delineate the "main band". This is the vertical
%   line of spots that correspond to RNAs that have been cut by 
%   iodine cleavage but not radicals. 
% (3) Click up the gel marking 108, 110, 111, etc. all the G's, as
%    far as we can be comfortable (a little past the 180's,
%    sometimes farther).
% (4) To assign coordinate to longer-length cleavage products, hit
%    "l". The program will use the file xsel_ladder_calibrate,
%    to interpolate between your coordinate assignments and the
%    position of the main band.
%
diagonalline=[];
xsel=[];xsel_ladder=[];diagonalline=[];mainband=0;maxres=260;
[xsel,xsel_ladder,diagonalline,mainband] = definegrid2(imagex_align,POS_ArrayBOMB,110,xsel,xsel_ladder,sequence,Offset,diagonalline,xsel_ladder_calibrate,mainband,maxres);

% Read in the T1 grid gel -- the "ur-gel" that everything will get
% aligned to.
load standard.mat *S
% This automatically uses our coordinate annotations on the new
% gel, and the annotation of the old gel to transform the new gel's
% image so that its coordinate system overlays with the standard.
[imagex_align_new] = plot_two_gels_2dalign(imagex_alignS, ...
xsel_ladderS,  xselS, diagonallineS,imagex_align, xsel_ladder, xsel,diagonalline);

% Of course, the alignment won't be perfect. Luckily there are
% several hits (in the T1 grid) on both gels that should be in the
% same place. This routine lets you pick these corresponding
% points (with mouse clicks), and then it applies a 
% transformation to put the hit in the new gel so that its in the
% same place as on the standard gel (hit "q"). The transformation
% preserves the main band, and the diagonal. Hit "z" when done.
% 
coordpick = [];
[imagex_align_out,coordpick] = ...
    correspondpoints_alignimages(imagex_alignS,imagex_align_new,xselS,xsel_ladderS,mainbandS,POS_ArrayBOMB, diagonallineS,maxres,coordpick);

%Sweet save the aligned image, and the coordinate annotation..
diagonallineS_A5prime = diagonallineS;
imagex_alignS_A5prime = imagex_align_out;
mainbandS_A5prime = mainbandS;
xselS_A5prime = xselS;
xsel_ladderS_A5prime = xsel_ladderS;
save standard_A5prime.mat *S_A5prime
