function plot_idclasses3dso(X,min_ids,idx)

% plot_idclasses3.m
% This functions displays a grahical summary of the classes of annotated
% metabolites in a data set object.
% 
% INPUTS:
% X: Data set object
%    X.axisscale{2,1} = m/z
%    X.axisscale{2,2} = Retention time
% min_ids: min. number of features of a given class to be included
% idx: class index of the data set object i.e. X.classid{2,idx}
% 
% OUTPUTS: plots
% 
% I/O: plot_idclasses3dso(X,0,2)
%
% Warranty:
% No guarantees, whatsoever, are given for the quality of this function
% or for the consequences of its use.
% We would appreciate your feedback about any bugs you find while
% using the script (guira@uv.es)
% 2020-05-03

% Summary annotated
t1 = X.classid{2,idx}';
t1(cellfun('isempty',t1)) = []; % remove empty cells
for i = 1:size(t1,1)
    t2(i,1) = string(t1(i,1));
end
unique_class = unique(t2,'rows');
for i = 1:size(unique_class,1)
    n_unique_class(i,1) = sum(strcmp(t2,unique_class(i)));
end
classes = table(unique_class,n_unique_class);
[classes sidx] = sortrows(classes,2,'descend');
unique_class = unique_class(sidx);

disp('******************************')
disp(['Summary of the distribution of features for class: ',num2str(idx)])
disp(['Only classes/subclasses/direct parents with more than ',num2str(min_ids),' annotated metabolites'])
classes

% prepare plots
x = classes{:,2};
x1 = x(x>min_ids);
if numel(x1)<numel(x)
    labels = [unique_class(x>min_ids,1); 'others'];
    x = [x1; sum(x(x<=min_ids))]; %[x1 & the rest=those classes with n<min_ids elements]
else
    x = x(x>min_ids);
    labels = unique_class(x>min_ids,1);
end

c = jet(numel(x)); % jet is ok, but i cannot differentiate among 'blues'?? so...
C(1,:) = [0 1 0];
C(2,:) = [1 0 1];
C(3,:) = [0 0 1];
C(4,:) = [0 1 1];%[0 0.45 0.74];
C(5,:) = [0.85 0.33 0.1];
C(6,:) = [0.93 0.69 0.13];%[0.8 0.8 0.8];
C(7,:) = [0.47 0.67 0.19];
C(8,:) = [0.3 0.75 0.93];
if numel(x)<=8
    c(1:numel(x),:) = C(1:numel(x),:);
else
    c(1:8,:) = C(1:8,:);
end
c(end,:) = 0.8;

figure('name','Figure: pie chart of %features~class','position',[50 100 400 350])
pie(x)
colormap(c)
hL = legend(labels);
newPosition = [0.13 0.2 0.2 0.3];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits,'FontSize',9);
set(hL,'Location', 'NorthEastOutside','FontSize',9)

figure('name','IDs@LCMS','position',[50 100 400 300])
t1 = X.classid{2,idx}';
for i = 1:size(t1,1)
    t2(i,1) = string(t1(i,1));
end
for i = 1:numel(x)
hold on
[temp,~] = find(strcmp(labels(i),t2)>0);
plot (X.axisscale{2,2}(temp),X.axisscale{2,1}(temp),'o','MarkerEdgeColor','k','MarkerFaceColor',c(i,:))
hold on
end
xlabel('RT (min)','FontSize',10)
ylabel('m/z','FontSize',10)
set(gca,'FontSize',12)
xlim([min(X.axisscale{2,2})*0.95 max(X.axisscale{2,2})*1.05])
ylim([min(X.axisscale{2,1})*0.95 max(X.axisscale{2,1})*1.05])
grid on
box on

end