function [geneDist,compDist,uComp]=findCmptmtGeneDistribution(model,pseudoFlag,genes,geneCompartments)
%[geneDist,compDist,uComp]=findCmptmtGeneDistribution(model,genes,geneCompartments)
% This function generates the compartment distribution (compDist)of genes and gene
% distribution (geneDist) of compartments
% INPUT:
% model:    model in question
% pseudoFlag (optional): true, if pseudoDeletion; false otherwise (default)
% genes (optional): genes in questions, model.genes(default)
% geneCompartments (optional):  compartmetns to which genes belong to,
%                               compute it now (default)
% OUTPUT:
% geneDist: distribution of genes among the compartments
% compDist: distribution of compartments among the genes
% uComp:     unique compartments that belonging to this set of genes
%
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % Written By Chintan Joshi on 7/4/2014 % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

if (nargin<2)
    pseudoFlag=false;
end
if (nargin<3)
    genes=model.genes;
end
if (nargin<4)
    [~,geneCompartments,~,~]=findCompartmentOfGenes(model,genes,pseudoFlag);
end

uComp=unique(linearizeCellString(geneCompartments));
geneDist=zeros(length(uComp),1);
compDist=zeros(length(genes),1);
lgeneComps=linearizeCellString(geneCompartments);
for i=1:length(uComp)
    geneDist(i,1)=length(find(strcmp(uComp(i,1),lgeneComps)==1));
    for j=1:length(geneCompartments)
        compDist(j,1)=length(geneCompartments{j,1});
    end
end
if sum(compDist)~=length(linearizeCellString(geneCompartments))
    error('compartments not accounted properly, check again!!!');
end
if sum(geneDist)<length(genes)
    error('genes not accounted properly, check again!!!');
end