function effectiveness (genbank_number)
% genbank_number: number of organism in GenBank database
%   HG738867.1 - Escherichia coli str. K-12 substr. MC4100
%   CP014314.1 - Escherichia coli strain JEONG-1266
%   CP014197.1 - Escherichia coli strain MRE600
%   AP010960.1 - Escherichia coli O111:H- str. 11128 DNA

data_gb = get_genbank_data(genbank_number);
[sequence, number_of_genes] = informations_about_genome (data_gb);
[positions_gb, positions_gb_reverse] = get_gene_positions (data_gb, number_of_genes);

final = xlsread('results_of_gene_finder.xls');

score_3 = get_score (final, positions_gb, positions_gb_reverse, 3);
score_30 = get_score (final, positions_gb, positions_gb_reverse, 30);
score_60 = get_score (final, positions_gb, positions_gb_reverse, 60);
score_90 = get_score (final, positions_gb, positions_gb_reverse, 90);

score_starts_3 = get_score_starts (final, positions_gb, positions_gb_reverse, 3);
score_starts_30 = get_score_starts (final, positions_gb, positions_gb_reverse, 30);
score_starts_60 = get_score_starts (final, positions_gb, positions_gb_reverse, 60);
score_starts_90 = get_score_starts (final, positions_gb, positions_gb_reverse, 90);

score_ends_3 = get_score_ends (final, positions_gb, positions_gb_reverse, 3);
score_ends_30 = get_score_ends (final, positions_gb, positions_gb_reverse, 30);
score_ends_60 = get_score_ends (final, positions_gb, positions_gb_reverse, 60);
score_ends_90 = get_score_ends (final, positions_gb, positions_gb_reverse, 90);

[sn_3, sp_3, accuracy_3] = success_rate (final, number_of_genes, score_3);
[sn_30, sp_30, accuracy_30] = success_rate (final, number_of_genes, score_30);
[sn_60, sp_60, accuracy_60] = success_rate (final, number_of_genes, score_60);
[sn_90, sp_90, accuracy_90] = success_rate (final, number_of_genes, score_90);

disp(['U genomu ' data_gb.Definition(1:(length(data_gb.Definition)-1)) ' při toleranci +- 3 báze je správně nalezeno ',num2str(score_3) ' genů, ', num2str(score_starts_3),' začátků, ' num2str(score_ends_3), ' konců. Senzitivita predikce vyšla ' num2str(sn_3),' %, specificita ' num2str(sp_3), ' % a přesnost ' num2str(accuracy_3), ' %.'])
disp(['U genomu ' data_gb.Definition(1:(length(data_gb.Definition)-1)) ' při toleranci +- 30 báze je správně nalezeno ',num2str(score_30) ' genů, ', num2str(score_starts_30),' začátků, ' num2str(score_ends_30), ' konců. Senzitivita predikce vyšla ' num2str(sn_30),' %, specificita ' num2str(sp_30), ' % a přesnost ' num2str(accuracy_30), ' %.'])
disp(['U genomu ' data_gb.Definition(1:(length(data_gb.Definition)-1)) ' při toleranci +- 60 báze je správně nalezeno ',num2str(score_60) ' genů, ', num2str(score_starts_60),' začátků, ' num2str(score_ends_60), ' konců. Senzitivita predikce vyšla ' num2str(sn_60),' %, specificita ' num2str(sp_60), ' % a přesnost ' num2str(accuracy_60), ' %.'])
disp(['U genomu ' data_gb.Definition(1:(length(data_gb.Definition)-1)) ' při toleranci +- 90 báze je správně nalezeno ',num2str(score_90) ' genů, ', num2str(score_starts_90),' začátků, ' num2str(score_ends_90), ' konců. Senzitivita predikce vyšla ' num2str(sn_90),' %, specificita ' num2str(sp_90), ' % a přesnost ' num2str(accuracy_90), ' %.'])

end


function data_gb = get_genbank_data(genbank_number)
data_gb = getgenbank(genbank_number);
end

function [sequence, number_of_genes] = informations_about_genome (data_gb)

number_of_genes = length(data_gb.CDS);
sequence = upper(data_gb.Sequence);

end

function [positions_gb, positions_gb_reverse] = get_gene_positions (data_gb, number_of_genes)

CellCDS(:,:) = struct2cell(data_gb.CDS);
positions_gb = [0 0];
positions_gb_reverse  = [0 0];
a = 1;
b = 1;

for i = 1:number_of_genes
    if CellCDS{5,i}(1)<CellCDS{5,i}(2)
        positions_gb (a,1) =  CellCDS{5,i}(1);
        positions_gb (a,2) =  CellCDS{5,i}(2);
        a = a+1;
    else
        positions_gb_reverse (b,1) =  CellCDS{5,i}(2);
        positions_gb_reverse (b,2) =  CellCDS{5,i}(1);
        b = b+1;
    end
end
end

function score = get_score (final, positions_gb, positions_gb_reverse, tolerance)

f_leading = [0 0 0 0];
f_reverse = [0 0 0 0];
a = 1;
b = 1;

for i = 1:length(final)
   if  final(i,3)>0
       f_leading (a,:) = final(i,:);
       a = a+1;
   else
       f_reverse (b,:) = final(i,:);
       b = b+1;
   end
end
  
score_leading = 0;
for i = 1:length(positions_gb)
    for j = 1:length(f_leading)
        if abs(positions_gb(i,1)-f_leading(j,1))<=tolerance
            if abs(positions_gb(i,2)-f_leading(j,2))<=tolerance
                score_leading = score_leading + 1;
            end
        end
    end
end

score_reverse = 0;
for i = 1:length(positions_gb_reverse)
    for j = 1:length(f_reverse)
       if abs(positions_gb_reverse(i,1) - f_reverse(j,1))<=tolerance
           if abs(positions_gb_reverse(i,2) - f_reverse(j,2))<=tolerance
                score_reverse = score_reverse+1;
           end
        end
    end
end

score = score_reverse + score_leading;
end

function score_starts = get_score_starts (final, positions_gb, positions_gb_reverse, tolerance)

f_leading = [0 0 0 0];
f_reverse = [0 0 0 0];
a = 1;
b = 1;

for i = 1:length(final)
    if  final(i,3)>0
        f_leading (a,:) = final(i,:);
        a = a+1;
    else
        f_reverse (b,:) = final(i,:);
        b = b+1;
    end
end

score_leading = 0;
for i = 1:length(positions_gb)
    for j = 1:length(f_leading)
        if abs(positions_gb(i,1)-f_leading(j,1))<=tolerance
            score_leading = score_leading + 1;
        end
    end
end

score_reverse = 0;
for i = 1:length(positions_gb_reverse)
    for j = 1:length(f_reverse)
        if abs(positions_gb_reverse(i,1) - f_reverse(j,1))<=tolerance
            score_reverse = score_reverse+1;
        end
    end
end

score_starts = score_reverse + score_leading;
end

function score_ends = get_score_ends (final, positions_gb, positions_gb_reverse, tolerance)

f_leading = [0 0 0 0];
f_reverse = [0 0 0 0];
a = 1;
b = 1;

for i = 1:length(final)
    if  final(i,3)>0
        f_leading (a,:) = final(i,:);
        a = a+1;
    else
        f_reverse (b,:) = final(i,:);
        b = b+1;
    end
end

score_leading = 0;
for i = 1:length(positions_gb)
    for j = 1:length(f_leading)
        if abs(positions_gb(i,2)-f_leading(j,2))<=tolerance
            score_leading = score_leading + 1;
        end
    end
end

score_reverse = 0;
for i = 1:length(positions_gb_reverse)
    for j = 1:length(f_reverse)
        if abs(positions_gb_reverse(i,2) - f_reverse(j,2))<=tolerance
            score_reverse = score_reverse+1;
        end
    end
end

score_ends = score_reverse + score_leading;
end

function [sn, sp, accuracy] = success_rate (final, number_of_gene, score)

number_of_gene_final = length(final);
sn = (score/number_of_gene)*100;
sp = (score/number_of_gene_final)*100;
accuracy = (sn+sp)/2;
end
