- Author:
- WeiweiAi <wai484@aucklanduni.ac.nz>
- Date:
- 2022-08-02 15:14:59+12:00
- Desc:
- Add the BG of a chemical reaction
- Permanent Source URI:
- https://models.cellml.org/workspace/6bc/rawfile/c856b59f39db721d14e9e3e77a231ff986471f95/Scripts/buildModel.m
function model=buildModel(modelname,comp,listComp,idx)
model.name=modelname;
nComp=length(listComp);
cmnames=string(extractfield(comp,'name'));%unique
if length(cmnames)>length(unique(cmnames))
disp('There are duplicate component names')
return
end
for i=1:nComp
% default
model.comps(i).def=listComp(i).def;
model.comps(i).name=listComp(i).name;
model.comps(i).from=listComp(i).from;
model.comps(i).impComp=listComp(i).impComp;
comp_i=comp(cmnames==model.comps(i).def);
model.comps(i).vars=comp_i.vars; % original vars
model.comps(i).vars(:,idx.vctg+1)=comp_i.vars(:,idx.var);%add alias=original name
if ~isempty(listComp(i).label) % Label alias for some vars
[~,idxVar,idxLabel]=intersect(comp_i.vars(:,idx.var),listComp(i).label(:,1),'stable');
comp_i.vars(idxVar,idx.var)=listComp(i).label(idxLabel,2);
end
model.comps(i).vars(:,idx.vctg+1)=comp_i.vars(:,idx.var);% alias
end
%% Mapping based on aliases
model.vmaps=[];
if nComp>1
icmnames=string(extractfield(model.comps,'name'));%unique
n=0;
for i=1:length(icmnames)
comp_i=model.comps(i);
icomp_def=comp(cmnames==model.comps(i).def);
for j=i+1:length(icmnames)
comp_j=model.comps(j);
jcomp_def=comp(cmnames==model.comps(j).def);
if ~isempty(icomp_def.children)&& ~isempty(find(icomp_def.children==comp_j.name,1))
idxes_i=comp_i.vars(:,idx.priv)=="out";
idxes_j=comp_j.vars(:,idx.pub)=='in';
alias_i=comp_i.vars(idxes_i,idx.vctg+1);
alias_j=comp_j.vars(idxes_j,idx.vctg+1);
vars_i=comp_i.vars(idxes_i,idx.var);
vars_j=comp_j.vars(idxes_j,idx.var);
idxes_i2=comp_i.vars(:,idx.priv)=="in";
idxes_j2=comp_j.vars(:,idx.pub)=='out';
alias_i2=comp_i.vars(idxes_i2,idx.vctg+1);
alias_j2=comp_j.vars(idxes_j2,idx.vctg+1);
vars_i2=comp_i.vars(idxes_i2,idx.var);
vars_j2=comp_j.vars(idxes_j2,idx.var);
elseif ~isempty(jcomp_def.children)&& ~isempty(find(jcomp_def.children==comp_i.name,1))
idxes_i=comp_i.vars(:,idx.pub)=="out";
idxes_j=comp_j.vars(:,idx.priv)=='in';
alias_i=comp_i.vars(idxes_i,idx.vctg+1);
alias_j=comp_j.vars(idxes_j,idx.vctg+1);
vars_i=comp_i.vars(idxes_i,idx.var);
vars_j=comp_j.vars(idxes_j,idx.var);
idxes_i2=comp_i.vars(:,idx.pub)=="in";
idxes_j2=comp_j.vars(:,idx.priv)=='out';
alias_i2=comp_i.vars(idxes_i2,idx.vctg+1);
alias_j2=comp_j.vars(idxes_j2,idx.vctg+1);
vars_i2=comp_i.vars(idxes_i2,idx.var);
vars_j2=comp_j.vars(idxes_j2,idx.var);
else
idxes_i=comp_i.vars(:,idx.pub)=="out";
idxes_j=comp_j.vars(:,idx.pub)=='in';
alias_i=comp_i.vars(idxes_i,idx.vctg+1);
alias_j=comp_j.vars(idxes_j,idx.vctg+1);
vars_i=comp_i.vars(idxes_i,idx.var);
vars_j=comp_j.vars(idxes_j,idx.var);
idxes_i2=comp_i.vars(:,idx.pub)=="in";
idxes_j2=comp_j.vars(:,idx.pub)=='out';
alias_i2=comp_i.vars(idxes_i2,idx.vctg+1);
alias_j2=comp_j.vars(idxes_j2,idx.vctg+1);
vars_i2=comp_i.vars(idxes_i2,idx.var);
vars_j2=comp_j.vars(idxes_j2,idx.var);
end
% Map vars
[~,ivars_i,ivars_j]=intersect(alias_i,alias_j,'stable');
[~,ivars_i2,ivars_j2]=intersect(alias_i2,alias_j2,'stable');
if ~isempty(ivars_i)||~isempty(ivars_i2)
n=n+1;
model.vmaps(n).name=[comp_i.name,comp_j.name];
if ~isempty(ivars_i)&&~isempty(ivars_i2)
model.vmaps(n).vpairs=[vars_i(ivars_i),vars_j(ivars_j);vars_i2(ivars_i2),vars_j2(ivars_j2);];
elseif ~isempty(ivars_i)
model.vmaps(n).vpairs=[vars_i(ivars_i),vars_j(ivars_j);];
else
model.vmaps(n).vpairs=[vars_i2(ivars_i2),vars_j2(ivars_j2);];
end
end
end
end
end
end