function [aa_P] = aaAnova2Pvalues(aaTotal, group, threshP);
% aaAnova2Pvalues is used to find significant differences between counts of amino acids 
% by univariate two-way unbalanced ANOVA.
% Program by Steinar Thorvaldsen, steinart@math.uit.no, Nov. 2006. 
% Ref.: http://www.math.uit.no/bi/deltaprot/
% Last changes 7. Nov 2006.

% Use:     [aa_P]=aaAnova2Pvalues(aaTotal, group, 0.05);

% Input
% aaTotal:  M x 20 x levels with counts of aa, where M is the number of seq.
%           Counts of amino acids from 2 or more different groups of organisms
% group:    a cell array with vectors containin factor levels (groups) 
%           of the observations in aaTotal 
% threshP:  Threshold p-value for significance level. For later use in ploting.

% Output
% aa_P:     20 x levels x groups. Array with statistical P-values for the observed differences.

% The function is made flexible conserning input parameters:
if nargin < 3
    threshP=0.05; % default
end

[M N D] = size(aaTotal);
Garray=cell2mat(group);
[Mg,G] = size(Garray); %Mg must be same for all groups
if M ~= Mg
    error('Anova test: Matrix with observations must match the group vectors');
end
%N=1; % to test Alanin
%aaTotal(:,1,5)
%Garray
% Perform statistical testing:
for d=1:D % Each layer is analysed separately
    for aaNo=1:N 
        % perform ANOVA2 test. 'model' may be 'linear', ('interaction' or 'full' when replications exists):
        aa_P(aaNo,d,:)= anovan(aaTotal(:,aaNo,d),group,'model','linear','display','off');
    end
end %layer loop
