function res = compareToGO(clustering,classes)

%
% A bad implementation of Fisher's exact test to find enrichments
% of known categories in a clustering.
%
% Input:
% clustering = list of cluster indices for N samples
% classes = a binary matrix with N rows and any number of columns;
%   if sample i belongs to category j then classes(i,j)=1,
%   otherwise 0
%
% Output:
% res = a matrix that gives p-values for all cluster/category
%   pairs. If res(i,j) is close to zero, then the category j
%   was positively enriched in cluster i
%

K = max(clustering);

res = zeros(K,size(classes,2));

for i=1:K;
  for c=1:size(classes,2);
    incluster = find(clustering==i);
    ocluster = find(clustering~=i);
    inclass = find(classes(:,c)==1);
    oclass = find(classes(:,c)==0);
    v1 = length(intersect(incluster,inclass));
    v2 = length(intersect(ocluster,inclass));
    v3 = length(intersect(incluster,oclass));
    v4 = length(intersect(ocluster,oclass));
    [ppos,pneg,pboth]=fisherextest(v1,v2,v3,v4);
    res(i,c) = pneg;
  end;
end;
