%plot_analysis_results(index)
% index should be column vector with either files numbers or their names 
% Ex:
% index = [2;5;9]; OR 
% index = ['NDB_00010.ct';'PDB_01311.ct';'PDB_00916.ct'];
% 
function plot_analysis_results(index)

    matfiles = dir(fullfile(cd,'*.ct'));
    [tmp ind] = sort([matfiles.bytes]);
    matfiles= matfiles(ind);
    len = size(index,1);
    
    if ischar(index)
        name = index;
    else
        sub = matfiles(index);
        for j = 1:len
            name(j,:) = sub(j).name;
        end
    end
    
    for lm = 1:len

        str = [cd,'/',name(lm,:)];
        fid = fopen(str);
        inFile = textscan(fid,'%s');
        idx = find(strcmp(inFile{1,1}(1:min(100,length(inFile{1,1}))), '1'));
        fclose(fid);
        pc = 1;
        while (~(strcmp(inFile{1,1}(idx(pc)+6), '2')) || ~(strcmp(inFile{1,1}(idx(pc)+5), '1')));
            pc = pc + 1;
        end
        idx1 = idx(pc);
        in = reshape(inFile{1,1}(idx1:length(inFile{1,1})),6,(length(inFile{1,1})-idx1+1)/6);
        
        connectNum = in(3:5,:);
        connectType = in(2,:);
        ind_CG = find(strcmpi(connectType,'C'));
        CG_pairs = 0;
        for i = ind_CG
            toBeCompared = str2double(connectNum(:,i));
            cond = find(toBeCompared ~= 0 & toBeCompared ~= i-1 & toBeCompared ~=i+1);
            if ~isempty(cond)
                if strcmpi(connectType(toBeCompared(cond)),'G')
                    CG_pairs = CG_pairs + 1;  
                end
            end
        end
    
        ind_AT = find(strcmpi(connectType,'A'));
        AT_pairs = 0;
        for i = ind_AT
            toBeCompared = str2double(connectNum(:,i));
            cond = find(toBeCompared ~= 0 & toBeCompared ~= i-1 & toBeCompared ~=i+1);
            if ~isempty(cond)
                if (strcmpi(connectType(toBeCompared(cond)),'T')) || (strcmpi(connectType(toBeCompared(cond)),'U'))
                    AT_pairs = AT_pairs + 1;  
                end
            end
        end
    
        ind_GU = find(strcmpi(connectType,'G'));
        GU_pairs = 0;
        for i = ind_GU
            toBeCompared = str2double(connectNum(:,i));
            cond = find(toBeCompared ~= 0 & toBeCompared ~= i-1 & toBeCompared ~=i+1);
            if ~isempty(cond)
                if strcmpi(connectType(toBeCompared(cond)),'U')
                    GU_pairs = GU_pairs + 1;  
                end
            end
        end 
            
    %reading the results for analysis
    str2 = [cd,'/resultGU/',name(lm,:),'.txt'];
    fid2 = fopen(str2,'r');
    inFile2 = textscan(fid2,'%d %d %d');
    fclose(fid2);
    xv = double(inFile2{1,1});
    yv = double(inFile2{1,2});
    zv = double(inFile2{1,3});
    poly3D = [xv, yv, zv];
    poly_sum = sum(poly3D,1);
    dist(lm) = 0;
    dim = 0;
    if size(poly3D,1)>2 
        switch (num2str([poly_sum(1) ~= 0, poly_sum(2) ~=0, poly_sum(3) ~=0]))
            case '1  1  1'
                k = convhulln(poly3D);
                poly = poly3D;
                point = [AT_pairs, CG_pairs, GU_pairs]
                dist(lm) = point2_3Dpoly_dist(k, poly3D, point);
                dim = 3;
                message = 'contains AT, CG and AT pairs';
            case '1  1  0'
                which2 = 1;
                dim = 2;
                dist(lm) = p_poly_dist(AT_pairs, CG_pairs, xv, yv);
                poly = poly3D(:,1:2);
                point = [AT_pairs, CG_pairs];
                message = 'only contains CG and AT pairs';
            case '1  0  1';
                which2 = 2;
                dim = 2;
                dist(lm) = p_poly_dist(AT_pairs, GU_pairs, xv, zv);
                poly = poly3D(:,[1,3]);
                point = [AT_pairs, GU_pairs];
                message = 'only contains AT and GU pairs';
            case '0  1  1';
                which2 = 3;
                dim = 2;
                dist(lm) = p_poly_dist(CG_pairs, GU_pairs, yv, zv);
                poly = poly3D(:,2:3);
                point = [CG_pairs, GU_pairs];
                message = 'only contains CG and GU pairs';
            case '1  0  0';
                dim = 1;
                message = 'only contains AT pairs';
            case '0  1  0';
                dim = 1;
                message = 'only contains CG pairs';
            case '0  0  1';
                dim = 1;
                message = 'only contains GU pairs';
            case '0  0  0';
                dim = 0;
                message = 'does not contain any pairs';
        end
        
        %keeping the data required for plotting the polygon for the
        %desired RNA strand file chosen above
    end
       
        
    
        if dim == 3
            sprintf('The distance from point to Volume is %0.5g',dist(lm))
            figure;
            trisurf(k,poly(:,1),poly(:,2),poly(:,3));
            %fill3(poly(:,1),poly(:,2),poly(:,3),'r')
            hold on
            plot3(point(1),point(2),point(3),'r*')
            if dist(lm) ~=0
                title('The point is NOT on the surface')
            else
                title('The point is on the surface')
            end
            xlabel('AT pairs');
            ylabel('CG pairs');
            zlabel('GU pairs');
        end

        if dim == 2
            sprintf('The distance from point to surface is %0.5g',dist(lm))
            figure;
            fill(poly(:,1), poly(:,2),'b')
            hold on
            plot(point(1),point(2),'r*')
            if dist(lm) ~=0
                title('The point is NOT on the surface')
            else
                title('The point is on the surface')
            end
            if which2 == 1
                xlabel('AT pairs');
                ylabel('CG pairs');
            end
            if which2 == 2
                xlabel('AT pairs');
                ylabel('GU pairs');
            end
            if which2 == 3
                xlabel('CG pairs');
                ylabel('GU pairs');
            end
        end
        
        if dim == 1
            message
        end

        if dim == 0
            'Has less than 3 points'
        end
    
    end    

end
