% This program locates the genes z{1}, z{2}, ... in the mRNA sequence. Some
% genes are found to have identical replicas (i.e. 5'-3' strand) in the
% mRNA sequence, others are found to have not only complimented but also
% flipped replicas ((i.e. 3'-5' strand)) in the mRNA sequence. So, to save
% time of aligning these genes to the original mRNA sequence we have
% complimented the original mRNA sequence and flipped it and named the new
% sequence as "mRNAfc" where "fc" stands for flipped and complimented. The
% idea of alignment is simply to take two sliding windows at the same time,
% each of length equal to the length of the gene under study (i.e. the gene
% to be aligned) and then sliding the first one from left to right on the
% mRNA sequence and shifting it one unit at a time, and at the same time
% sliding the other window from right to left onthe mRNAfc sequence and
% shifting it one unit at a time. By doing so, we get two portions: the
% first one belongs to the mRNA sequence and the second one belongs to the
% mRNAfc sequence, then we check if these two portions are equal to the gene
% under study. If YES then we save the location of the gene. If NOT then we
% proceed by sliding the two windows one more unit until the gene is
% located. The "+" sign indicates the the gene was found to have an
% identical replica in the mRNA sequence. The "-" sign indicates that the
% genewas found to have a complimented flipped replica in the mRNAfc
% sequence. The program saves the gene number, the start position, the end
% position, and the starnd (+ or -) in a cell array "genelocation".
clc, close all
clear genelocation;
identical = 0; complimented = 0; k = 1;
for i = 1:length(genes)
    if i == 12, k = k - 150; end
    if i >= 257, k = k - 3000; end
    tic
    for j = k:length(DNA{1})- length(char(genes{i})) + 1
        portion1 = {DNA{1}(j:j+length(char(genes{i}))-1)};
        portion2 = reverse(portion1);
        if toc > 15, break; end
        if  isequal(portion1,genes{i}),
            DNAncr{1}(j:j+length(genes{i})-1) = 'X';
            genelocation{i,1} = i;
            genelocation{i,2} = j;
            genelocation{i,3} = j+length(char(genes{i}))-1;
            genelocation{i,4} = '+';
            identical = identical + 1;
%             gene = i,start = j,stop = j+length(char(genes{i}))-1
%             genelocation{i,4}
            {i,j,j+length(char(genes{i}))-1,genelocation{i,4}}
            k = j;
            break; 
        elseif  isequal(portion2,genes{i}),
            DNAncr{1}(j:j+length(char(genes{i}))-1) = 'X';
            genelocation{i,1} = i;
            genelocation{i,2} = j;
            genelocation{i,3} = j+length(char(genes{i}))-1;
            genelocation{i,4} = '-';
            complimented = complimented + 1;
%             gene = i,start = j,stop = j+length(char(genes{i}))-1
%             genelocation{i,4} 
            {i,j,j+length(char(genes{i}))-1,genelocation{i,4}}
            k = j;
            break;
            if toc > 5, break; end
        end
    end
end




           


