%Read in the gel.
[imagey_combine, gelbounds]  = readingel('rd149a_p4p6gtpalphaS_LeakToRecirculate_MKtop_RDbottom.gel');

% Now align the best we can in each direction by making the T1 grid
% spot line up vertically and horizontally.
[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','');
diagonalline=[];

%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.
%
xsel=[];xsel_ladder=[];diagonalline=[];mainband=0;maxres=260;
[xsel,xsel_ladder,diagonalline,mainband] = definegrid2(imagex_align,POS_ArrayBOMB,108,xsel,xsel_ladder,sequence,Offset,diagonalline,xsel_ladder_calibrate,mainband,maxres);

%Save the aligned image and coordinate annotations to a file. 
diagonallineS = diagonalline;
imagex_alignS = imagex_align;
mainbandS = mainband;
xselS = xsel;
xsel_ladderS = xsel_ladder;
save standard.mat *S
