%==============================================================================
%    probe_module_sig.m
%    Copyright (C) 2007  Amoolya H. Singh, singh@embl.de
%
%    This program is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.
%===============================================================================
%------------------------------------------------------------------------------
%
% probe_module_sig.m identifies genes with the same phylogenetic
% profile as module genes.  Need to have all variables from
% evmodules.m loaded in memory.
%
%------------------------------------------------------------------------------

for i=1:ngeneclusts
  ind = find(T==i);
  n = length(ind);
  rem = mod(n,10);
  cutoff = n - rem;
  spec_ind = find(mod(sum(M(ind,:)),cutoff));
  profile = zeros(1,nspecies);
  profile(spec_ind) = 1;
  modd = squareform(pdist([profile;SUB],'euclidean'));
  [a,b] = sort(modd(1,:));
  % spo- module 1: 6.5, module 2: 3, module 3: 4
  % che- module 1: 4.5, 2: 6, 5:6, 7:6, 8:6
  maxindex = length(a(a<6));	
  foo = Gsub(b(1:maxindex));
  F = fopen(['module',num2str(i),'probe.txt'],'w');
  for j=1:maxindex
    fprintf(F,'%s\n',char(foo(j)));
  end
  fclose(F);
end


