function [mua,ba,valgamma,gamma]=D2DsumII(VRMA,day)
%function [mua,ba,valgamma,gamma]=D2DsumII(VRMA,day)
% Input:
% * VRMA - G x A matrix whose elements are the gene expression levels computed by D2Dsum (or standard RMA) in its first step.
% * day - A x 1 vector whose elements are d=h(a), i.e. the day d at which the array a was hybridized.
% Output:
% * V - G x A matrix whose elements are the log2 gene expression levels as computed by D2Dsum.
%
% By Paolo Magni - Lab. Informatica Medica - Dip. Informatica e 
% Sistemistica - Univ. Pavia - 2006
%
% Algorithm presented in Magni et al. IEEE/ACM Transactions on Computational Biology and Bioinformatics 2011
% doi:10.1109.TCBB.2010.82
%

loggene=log2(VRMA);
nc=length(unique(day));
narray=length(day);

for i=1:nc
    valgamma(:,i)=median(loggene(:,day==i)')';
end

gamma=zeros(size(loggene));
for i=1:nc
    gamma(:,day==i)=repmat(valgamma(:,i),1,length(find(day==i)));
end

valgamma=valgamma-repmat(median(gamma')',1,nc);
gamma=gamma-repmat(median(gamma')',1,narray);
mua=loggene-gamma;

mu=median(mua')';
ba=mua-repmat(mu,1,narray);

%mu=mu+median(gamma')';
%valgamma=valgamma-repmat(median(gamma')',1,nc);
%gamma=gamma-repmat(median(gamma')',1,narray);
%mua=ba+repmat(mu,1,narray);
