function [deleteGenes,ECNums]=mapGenesOnModelB(geneList,modelA,modelB) % if using reaction E.C. numbers
% function [gLists]=mapGenesOnModelB(geneList,modelA,modelB)

% This function maps the genes of modelA on modelB.
% geneList=list of genes that need to be mapped.
% modelB=the target model.
% modelA=the source model.
% deleteGenes=genes corresponding to the genes in modelA
% grRules=grRules of the reactions found in modelB
% rxnName=name of teh reactions of interest
% rxnAbbs=reaction IDs in modelB.

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % Written By Chintan Joshi on 7/4/2014 % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %


constrRxnNames=cell(length(geneList),1);
for i=1:length(geneList)
    [~,~,constrRxnNames{i,1},~]=deleteModelGenes(modelA,geneList{i});
end
constrRxns=constrRxnNames;
constrRxns=constrRxns(cellfun(@isempty,constrRxns)==0);
constrRxns=linearizeCellString(constrRxns);

for i=1:length(constrRxns)
%%% if mapping using reaction abbreviation
%     indB=find(strcmp(constrRxns{i,1},modelB.rxns));
%     if ~isempty(indB)
%         rxnGeneMat{i,1}=modelB.genes(find(modelB.rxnGeneMat(indB,:)~=0));
%         gLists{i,1}=findGenesFromRxns(modelB,modelB.rxns(indB));
%     else
%         rxnGeneMat{i,1}=[];
%         gLists{i,1}=[];
%     end
%%% if mapping using reaction E.C. number
    indA=find(strcmp(constrRxns{i,1},modelA.rxns)==1); 
    rxnECNum{i,1}=modelA.rxnECNumbers{indA,1}; 
end
ECNums=rxnECNum;
%%% if using reaction abbreviation
% gLists1=unique(linearizeCellString(gLists));

%%% if mapping using reaction E.C. number
rxnECNum=regexpi(rxnECNum,'/','split');
rxnECNum=linearizeCellString(rxnECNum,1);
rxnECNum=rxnECNum(cellfun(@isempty,rxnECNum)==0);
rxnECNum=strtrim(rxnECNum);
genes=modelB.genes;

b=0;
for i=1:size(rxnECNum,1)
    indE{i,1}=find(strcmp(rxnECNum{i,1},modelB.rxnECNumbers)==1);
    if (~isempty(indE{i,1}))
        for j=1:size(indE{i,1},1)
            b=b+1;
            indxR(b,1)=indE{i,1}(j,1);
        end
    end
end

if exist('indxR')
    mat=full(modelB.rxnGeneMat(indxR,:));
    geneInd=find(sum(mat,1)~=0);
    deleteGenes=modelB.genes(geneInd,1);
    deleteGenes=unique(deleteGenes);
else
    deleteGenes=[];
    disp('no match found!');
end
