% Read the DNA and "sps_count_MH#" data files, arrange the data in a structure.

% SEND:
% --- 1) the DNA TXT file name
% --- 2) the sps_count TXT file name.

% GET:
% data structure that contains the DNA Short-Read line where an SP was found, the SP that was found, 
% the EC number associated with this SP, the number of hits, the frame.


function data = read_data(dna,sps_count)

% read SPs bank (Seq,Length, EC)
[NUMERIC,TXT,SPs] = xlsread('SP_S61.xls');
% read the SP counts data (num of hits, SRs, frames)
Hits = textread(sps_count);
clear NUMERIC TXT

% take only SPs with L >=9
SP_length = cell2mat(SPs(:,2));
big = find(SP_length >=9);

% take the relevant sub ensemble
SPs = SPs(big,:);
Hits = Hits(big,:);

% intialize data structure
data = struct([]);
% get the length of SPs bank you searched 
Lh = size(SPs); Lh = Lh(1);

% arrange the vector of hits by ascending order (Colum1 is the index of hit in the SP bank, second is the num of hits)
COL = Hits(:,1);
COL = [(1:Lh)' COL];
COL = sortrows(COL,-2);

% get the number of SPs that were found on this Species 
tmp = find(COL(:,2) ==0); 
N = tmp(1) - 1; 
clear tmp
% OPTION: decide about the N leading ones
%N = 100;

% go over the "bank" if there were hits on SPs that passed some creteria
% gather
for i=1:N
    % the SP
    data(i).SP = SPs{COL(i,1),1};
    % its EC
    data(i).EC = SPs{COL(i,1),3};
    % num of hits
    data(i).num_hits = Hits(COL(i,1),1);
    % the SR indexes where the hits were found
    h = data(i).num_hits;
    data(i).SR_id = Hits(COL(i,1),2:1+h);
    data(i).Frame_id = Hits(COL(i,1),2+h:2+2*h-1);
end

% get the PPs of the relevant SR and their relevant Frames
data = sr2pp(dna,data);

