%==============================================================================
%    phylo_benchmark.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/>.
%===============================================================================
%------------------------------------------------------------------------------
%
% phylo_benchmark.m calculates the fraction of orthologs shared by
% two species as a function of their phylogenetic distance
%
%------------------------------------------------------------------------------

%------------------------------------------------------------------------------
% -- import data

taxids = importdata('../db/string-taxids.txt');
nspecies = length(taxids)
[species_names] = importdata('../db/string-species.csv');
S = char(species_names);

% phylogenetic tree
tree = phytreeread('../db/mrp_species_tree_stringtaxids.txt');

% ortholog counts from each species
% format of this file: orthgroup, taxid, num paralogs
orthdata = importdata('../db/orthcounts.txt');

%------------------------------------------------------------------------------
% -- process ortholog counts, calculate fraction of shared orthologs

% number of orths per species
numorths = [];
% number of orths in common between every pair of species
commonorths = [];

ii = find(orthdata(:,2)==taxids(nspecies));
numorths(nspecies) = length(ii);

for i=1:nspecies-1
  % find all orthgroups of this species
  ii = find(orthdata(:,2)==taxids(i));
  numorths(i) = length(ii);
  o1 = orthdata(ii,1);
  
  for j=i+1:nspecies
    jj = find(orthdata(:,2)==taxids(j));
    o2 = orthdata(jj,1);
    commonorths(i,j) = length(intersect(o1,o2));
  end
end

for i=1:nspecies-1
  i
  for j=i+1:nspecies
    n = numorths(i) + numorths(j) - commonorths(i,j);
    if (n>0)
      F(i,j) = commonorths(i,j) / n;
    else
      F(i,j) = 0;
    end
  end
end

for i=1:nspecies-1
  for j=i+1:nspecies
    commonorths(j,i) = commonorths(i,j);
    F(j,i) = F(i,j);
  end
end

% visualize
H = reshape(triu(F),1,prod(size(F)));
myhist(H(find(H)),50)
xlabel('Fraction of orthologs in common');
ylabel('Number of genome pairs');
print('-depsc','-cmyk','fraction-orthologs.eps')

[i,j] = find(F>0.9);
[S(i,:),S(j,:)]

%------------------------------------------------------------------------------
% -- process phylogenetic tree, calculate all interspecies distances

[dists, branches, ell] = get(tree, 'Distances','BranchNames','LeafNames');
treedist = pdist(tree,'nodes','leaves','squareform',true);
leaves = str2num(char(ell));

% tree has 360 species, ortholog data has 373 species, reconcile them
[c,ai,bi] = intersect(taxids,leaves);
CC = squareform(commonorths(ai,ai));
FF = squareform(F(ai,ai));
%TT = squareform(treedist(ai,ai));
TT = squareform(treedist(bi,bi));
TT = TT./max(TT);

%------------------------------------------------------------------------------
% -- bin phylogenetic distances

edges = 0:0.01:1;
n = length(edges);
[binnedTT, bin_indices] = histc(TT,edges);

%------------------------------------------------------------------------------
% -- put orthologs into phylogenetic bins

mean_probs = [];
median_probs = [];
err_probs = [];
std_probs = [];
for i=1:max(bin_indices)
  f = FF(find(bin_indices==i));
  n = length(f);
  err_probs(i) = std(f) / sqrt(n);
  % f = FFpos(find(bin_indices==i));
  %plot(edges(i),mean(f),'.k');
  %plot(edges(i),mean(f)+std(f),'+r');
  %plot(edges(i),mean(f)-std(f),'+r');
  mean_probs(i) = mean(f);
  std_probs(i) = std(f);
  median_probs(i) = median(f);
end

%------------------------------------------------------------------------------
% -- plot figure

figure,errorbar(edges,mean_probs,err_probs,-err_probs)

% figure,plot(edges,mean_probs,'o-b')
% axis([0 1 0 0.5])
% box off
% xlabel('Phylogenetic distance')
% ylabel('Probability of identical gene content')

figure,plot(bin_indices./max(bin_indices),FF,'+b')
hold on
%bar(edges,mean_probs,'histc')
bar(edges,median_probs,'histc')
hp = findobj(gcf,'type','patch');
set(hp,'FaceColor','b','FaceAlpha',0.2)
axis([0 1 0 1])
axis square
xlabel('Phylogenetic distance')
ylabel('Probability of identical gene content')


hold on
plot(edges,mean_probs,'bo-')
plot(edges,mean_probs+err_probs,'r+-')
plot(edges,mean_probs-err_probs,'r+-')
legend('Mean','Standard error')
legend('Species pair','Mean','Standard error')
legend boxoff

print('-depsc', '-cmyk','phylo-benchmark.eps');
print('-dpng', 'phylo-benchmark.png');

mm = [];

for i=1:n-1
  mm(i) = mean(FF(find( (TT>=edges(i)) & (TT<edges(i+1)) )));
end

bar(0:0.01:0.99,mm,'histc')
hp = findobj(gcf,'type','patch');
set(hp,'FaceColor','b','FaceAlpha',0.2)
axis([0 1 0 1])
axis square
xlabel('Phylogenetic distance')
ylabel('Probability of identical gene content')
mean(mm)
range(mm)
max(mm)
print('-depsc', '-cmyk','phylo-benchmark.eps');
print('-dpng', 'phylo-benchmark.png');
