%==============================================================================
%    sens_spec.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/>.
%===============================================================================
function [sens, spec, err] = ...
    sens_spec(phenotypes, M, pathway, pheno, C1,C2, G)
%------------------------------------------------------------------------------
%
% [sens, spec, err] = sens_spec(phenotypes,M,pathway,pheno,C1,C2,G)
% calculates the sensitivity and specificity of each gene to each
% phenotype
%
% Input: 
%   phenotypes, phenotype vector, entry is 1 if species has
%               the phenotype, 0 otherwise
%   M, phylogenetic profile data matrix (rows are genes, columns
%      are species)
%   pathway, is either 'che', 'spo', or 'com'
%   pheno, is either 'mot', 'spo', or 'comp'
%   C1, grouping vector on genes
%   C2, grouping vector on gene function
%   G, strings describing genes
% Output:
%   sens, sensitivity
%   spec, specificity
%   err, linear and quadratic discriminant errors of classifying genes
%        according to both grouping vectors
%
%------------------------------------------------------------------------------

% -- calculate true positives, true negatives, etc
tp = phenotypes * M'; 
tn = (1-phenotypes)*(1-M)';
fp = (1-phenotypes)*(M');
fn = (phenotypes)*(1-M)';


sens = tp./(tp+fn); 
spec = tn./(fp+tn);
s1 = 'Sensitivity'; s2 = 'Specificity';
axislabels = {s1;s2};
str = [];,for i=1:max(C1), str = [str; ['Module ',num2str(i)]];,end
leglabels1 = cellstr(str);
leglabels2 = {'Xtalk';'Regulators';'Actuators';'Sensors';'Enz Regulators';'Unknown'};
%leglabels2 = {'Spo';'Che';'Com';'Xtalk'};

% define fine grid for plotting
incr = 0.001; 
[X Y] = meshgrid(0:incr:1,0:incr:1);
x = X(:); y = Y(:);

% and a coarser grid to estimate error
incr = 0.1; 
[Xc Yc] = meshgrid(0:incr:1,0:incr:1);
xc = Xc(:); yc = Yc(:);

n = 1 + 1/incr;
s = sqrt(length(x));

% ------------------------------------------------------------------------------
% if pathway is chemotaxis, take out bogus modules (cluster numbers 3,4,6)
ind = find(C1==3 | C1==4 | C1==6)
sens1 = sens;
spec1 = spec;
sens1(ind) = [];
spec1(ind) = [];
C11 = C1; C11(ind) = []; 
C22 = C2; C22(ind) = [];

% ------------------------------------------------------------------------------
% -- visualize
figure
mygscatter(sens1',spec1',C11,axislabels,leglabels1,true);
title([pathway,' genes / ',pheno,' phenotype']);
axis([0 1 0 1]), axis square, box off
filename = [pathway,'_',pheno,'_','sens_spec_modules.eps'];
%print('-depsc','-cmyk',filename);

figure
mygscatter(sens1',spec1',C22,axislabels,leglabels2,true);
title([pathway,' genes / ',pheno,' phenotype']);
axis([0 1 0 1]), axis square, box off
filename = [pathway,'_',pheno,'_','sens_spec_ontol.eps'];
print('-depsc','-cmyk',filename);

% -- linear and quadratic discriminant analysis, to classify data
% into pathways

[foo, linerr] = classify([xc yc], [sens1' spec1'], C11, 'linear');
[foo, quaderr] = classify([xc yc], [sens1' spec1'], C11, 'quadratic');
err = [linerr, quaderr];

