Home > io > readYAMLmodel.m

readYAMLmodel

PURPOSE ^

readYAMLmodel

SYNOPSIS ^

function model=readYAMLmodel(fileName, verbose)

DESCRIPTION ^

 readYAMLmodel
   Reads a yaml file matching (roughly) the cobrapy yaml structure

   Input:
   fileName    a model file in yaml file format. A dialog window will open
               if no file name is specified.
   verbose     set as true to monitor progress (opt, default false)

   Output:
   model       a model structure

   Usage: model = readYAMLmodel(fileName, verbose)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function model=readYAMLmodel(fileName, verbose)
0002 % readYAMLmodel
0003 %   Reads a yaml file matching (roughly) the cobrapy yaml structure
0004 %
0005 %   Input:
0006 %   fileName    a model file in yaml file format. A dialog window will open
0007 %               if no file name is specified.
0008 %   verbose     set as true to monitor progress (opt, default false)
0009 %
0010 %   Output:
0011 %   model       a model structure
0012 %
0013 %   Usage: model = readYAMLmodel(fileName, verbose)
0014 if nargin<1 || isempty(fileName)
0015     [fileName, pathName] = uigetfile({'*.yml;*.yaml'}, 'Please select the model file');
0016     if fileName == 0
0017         error('You should select a model file')
0018     else
0019         fileName = fullfile(pathName,fileName);
0020     end
0021 end
0022 if nargin < 2
0023     verbose = false;
0024 end
0025 
0026 if ~isfile(fileName)
0027     error('Yaml file %s cannot be found', string(fileName));
0028 end
0029 
0030 if verLessThan('matlab','9.9') %readlines introduced 2020b
0031     fid=fopen(fileName);
0032     line_raw=cell(1000000,1);
0033     i=1;
0034     while ~feof(fid)
0035         line_raw{i}=fgetl(fid);
0036         i=i+1;
0037     end
0038     line_raw(i:end)=[];
0039     line_raw=string(line_raw);
0040     fclose(fid);
0041 else
0042     line_raw=readlines(fileName);
0043 end
0044 % If entry is broken of multiple lines, concatenate. Assumes at least 6
0045 % leading spaces to avoid metaData to be concatenated.
0046 newLine=regexp(line_raw,'^ {6,}([\w\(\)].*)','tokens');
0047 brokenLine=find(~cellfun(@isempty,newLine));
0048 for i=1:numel(brokenLine)
0049     line_raw{brokenLine(i)-1} = strjoin({line_raw{brokenLine(i)-1},char(newLine{brokenLine(i)}{1})},' ');
0050 end
0051 line_raw(brokenLine)=[];
0052 
0053 line_key = regexprep(line_raw,'^ *-? ([^:]+)(:).*','$1');
0054 line_key = regexprep(line_key,'(.*!!omap)|(---)|( {4,}.*)','');
0055 
0056 line_value = regexprep(line_raw, '.*:$','');
0057 line_value = regexprep(line_value, '[^":]+: "?(.+)"?$','$1');
0058 line_value = regexprep(line_value, '(")|(^ {4,}- )','');
0059 
0060 modelFields =   {'id',char();...
0061                'name',char();...
0062         'description',char();...
0063             'version',char();...
0064                'date',char();...
0065          'annotation',struct();...
0066                'rxns',{};...
0067            'rxnNames',{};...
0068                'mets',{};...
0069            'metNames',{};...
0070                   'S',sparse([]);...
0071                  'lb',{};... %Changed to double in the end.
0072                  'ub',{};... %Changed to double in the end.
0073                 'rev',{};... %Changed to double in the end.
0074                   'c',[];...
0075                   'b',cell(0,0);... %Changed to double in the end.
0076               'genes',cell(0,0);...
0077             'grRules',cell(0,0);...
0078          'rxnGeneMat',sparse([]);...
0079            'rxnComps',cell(0,0);... %Changed to double in the end.
0080          'subSystems',cell(0,0);...
0081             'eccodes',cell(0,0);...
0082          'rxnMiriams',cell(0,0);...
0083           'rxnDeltaG',{};... %Changed to double in the end.
0084            'rxnNotes',cell(0,0);...
0085       'rxnReferences',cell(0,0);...
0086 'rxnConfidenceScores',cell(0,0);...
0087            'metComps',cell(0,0);... %Changed to double in the end.
0088              'inchis',cell(0,0);...
0089           'metSmiles',cell(0,0);...
0090         'metFormulas',cell(0,0);...
0091          'metMiriams',cell(0,0);...
0092           'metDeltaG',{};... %Changed to double in the end.
0093          'metCharges',cell(0,0);... %Changed to double in the end.
0094            'metNotes',cell(0,0);...
0095               'comps',cell(0,0);...
0096           'compNames',cell(0,0);...
0097         'compOutside',cell(0,0);...
0098           'geneComps',cell(0,0);... %Changed to double in the end.
0099         'geneMiriams',cell(0,0);...
0100      'geneShortNames',cell(0,0);...
0101       'unconstrained',cell(0,0);... %Changed to double in the end.
0102             'metFrom',cell(0,0);...
0103             'rxnFrom',cell(0,0)};
0104 for i=1:size(modelFields,1)
0105     model.(modelFields{i,1})=modelFields{i,2};
0106 end
0107 
0108 % If GECKO model
0109 if any(contains(line_key,'geckoLight'))
0110     isGECKO=true;
0111     ecFields = {'geckoLight', false;...
0112                       'rxns', {};...
0113                       'kcat', {};...
0114                     'source', cell(0,0);...
0115                      'notes', cell(0,0);...
0116                    'eccodes', cell(0,0);...
0117                      'genes', cell(0,0);...
0118                    'enzymes', cell(0,0);...
0119                         'mw', cell(0,0);...
0120                   'sequence', cell(0,0);...
0121                      'concs', cell(0,0);...
0122                  'rxnEnzMat', []};
0123     for i=1:size(ecFields,1)
0124         model.ec.(ecFields{i,1})=ecFields{i,2};
0125     end
0126     ecGecko=cell(25000,2);      ecGeckoNo=1;
0127     enzStoich=cell(100000,3);   enzStoichNo=1;
0128 else
0129     isGECKO=false;
0130 end
0131 
0132 section = 0;
0133 metMiriams=cell(100000,3);   metMirNo=1;
0134 rxnMiriams=cell(100000,3);   rxnMirNo=1;
0135 geneMiriams=cell(100000,3);  genMirNo=1;
0136 subSystems=cell(100000,2);   subSysNo=1;
0137 eccodes=cell(100000,2);      ecCodeNo=1;
0138 equations=cell(100000,3);   equatiNo=1;
0139 
0140 for i=1:numel(line_key)
0141     tline_raw = line_raw{i};
0142     tline_key = line_key{i};
0143     tline_value = line_value{i};
0144     % import different sections
0145     switch tline_raw
0146         case '- metaData:'
0147             section = 1;
0148             if verbose
0149                 fprintf('\t%d\n', section);
0150             end
0151             continue % Go to next line
0152         case '- metabolites:'
0153             section = 2;
0154             if verbose
0155                 fprintf('\t%d\n', section);
0156             end
0157             pos=0;
0158             continue
0159         case '- reactions:'
0160             section = 3;
0161             if verbose
0162                 fprintf('\t%d\n', section);
0163             end
0164             pos=0;
0165             continue
0166         case '- genes:'
0167             section = 4;
0168             if verbose
0169                 fprintf('\t%d\n', section);
0170             end
0171             pos=0;
0172             continue
0173         case '- compartments: !!omap'
0174             section = 5;
0175             if verbose
0176                 fprintf('\t%d\n', section);
0177             end
0178             pos=0;
0179             continue
0180         case '- ec-rxns:'
0181             section = 6;
0182             if verbose
0183                 fprintf('\t%d\n', section);
0184             end
0185             pos=0;
0186             continue
0187         case '- ec-enzymes:'
0188             section = 7;
0189             if verbose
0190                 fprintf('\t%d\n', section);
0191             end
0192             pos=0;
0193             continue
0194     end
0195 
0196     % skip over empty keys
0197     if isempty(tline_raw) || (isempty(tline_key) && contains(tline_raw,'!!omap'))
0198         continue;
0199     end
0200     
0201     % import metaData
0202     if section == 1
0203         switch tline_key
0204             case {'short_name','id'} %short_name used by human-GEM
0205                 model.id = tline_value;
0206             case 'name'
0207                 model.name = tline_value;                
0208             case 'full_name' %used by human-GEM
0209                 model.description = tline_value;
0210             case 'version'
0211                 model.version = tline_value;
0212             case 'date'
0213                 model.date = tline_value;                
0214             case 'taxonomy'
0215                 model.annotation.taxonomy = tline_value;
0216             case {'description','note'} %description used by human-GEM
0217                 model.annotation.note = tline_value;
0218             case 'github'
0219                 model.annotation.sourceUrl = tline_value;
0220             case 'givenName'
0221                 model.annotation.givenName = tline_value;
0222             case 'familyName'
0223                 model.annotation.familyName = tline_value;
0224             case 'authors'
0225                 model.annotation.authorList = tline_value;
0226             case 'email'
0227                 model.annotation.email = tline_value;
0228             case 'organization'
0229                 model.annotation.organization = tline_value;
0230             case 'geckoLight'
0231                 if strcmp(tline_value,'true')
0232                     model.ec.geckoLight = true;
0233                 end
0234         end; continue
0235     end
0236 
0237     % import metabolites:
0238     if section == 2
0239         switch tline_key
0240             case 'id'
0241                 pos = pos + 1;
0242                 model = readFieldValue(model, 'mets', tline_value,pos);
0243                 readList=''; miriamKey='';
0244             case 'name'
0245                 model = readFieldValue(model, 'metNames', tline_value, pos);
0246                 readList=''; miriamKey='';
0247             case 'compartment'
0248                 model = readFieldValue(model, 'metComps', tline_value, pos);
0249                 readList=''; miriamKey='';
0250             case 'formula'
0251                 model = readFieldValue(model, 'metFormulas', tline_value, pos);
0252                 readList=''; miriamKey='';
0253             case 'charge'
0254                 model = readFieldValue(model, 'metCharges', tline_value, pos);
0255                 readList=''; miriamKey='';
0256             case 'notes'
0257                 model = readFieldValue(model, 'metNotes', tline_value, pos);
0258                 readList=''; miriamKey='';
0259             case 'inchis'
0260                 model = readFieldValue(model, 'inchis', tline_value, pos);
0261                 readList=''; miriamKey='';
0262             case 'smiles'
0263                 model = readFieldValue(model, 'metSmiles', tline_value, pos);
0264                 readList=''; miriamKey='';    
0265             case 'deltaG'
0266                 model = readFieldValue(model, 'metDeltaG', tline_value, pos);
0267                 readList=''; miriamKey='';                                
0268             case 'metFrom'
0269                 model = readFieldValue(model, 'metFrom', tline_value, pos);
0270                 readList=''; miriamKey='';
0271             case 'annotation'
0272                 readList = 'annotation';
0273             otherwise
0274                 switch readList
0275                     case 'annotation'
0276                         [metMiriams, miriamKey] = gatherAnnotation(pos,metMiriams,tline_key,tline_value,miriamKey,metMirNo);
0277                         metMirNo = metMirNo + 1;
0278                     otherwise
0279                         error(['Unknown entry in yaml file: ' tline_raw])
0280                 end
0281         end; continue
0282     end
0283 
0284     % import reactions:
0285     if section == 3
0286         switch tline_key
0287             case 'id'
0288                 pos = pos + 1;
0289                 model = readFieldValue(model, 'rxns', tline_value,pos);
0290                 readList=''; miriamKey='';
0291             case 'name'
0292                 model = readFieldValue(model, 'rxnNames', tline_value, pos);
0293                 readList=''; miriamKey='';
0294             case 'lower_bound'
0295                 model.lb(pos,1) = {tline_value};
0296                 readList=''; miriamKey='';
0297             case 'upper_bound'
0298                 model.ub(pos,1) = {tline_value};
0299                 readList=''; miriamKey='';
0300             case 'rev'
0301                 model.rev(pos,1) = {tline_value};
0302                 readList=''; miriamKey='';
0303             case 'gene_reaction_rule'
0304                 model = readFieldValue(model, 'grRules', tline_value, pos);
0305                 readList=''; miriamKey='';
0306             case 'rxnNotes'
0307                 model = readFieldValue(model, 'rxnNotes', tline_value, pos);
0308                 readList=''; miriamKey='';
0309             case 'rxnFrom'
0310                 model = readFieldValue(model, 'rxnFrom', tline_value, pos);
0311                 readList=''; miriamKey='';
0312             case 'deltaG'
0313                 model = readFieldValue(model, 'rxnDeltaG', tline_value, pos);
0314                 readList=''; miriamKey='';                
0315             case 'objective_coefficient'
0316                 model.c(pos,1) = 1;
0317                 readList=''; miriamKey='';
0318             case 'references'
0319                 model = readFieldValue(model, 'rxnReferences', tline_value, pos);
0320                 readList=''; miriamKey='';
0321             case 'confidence_score'
0322                 model = readFieldValue(model, 'rxnConfidenceScores', tline_value, pos);
0323                 readList=''; miriamKey='';
0324             case 'eccodes'
0325                 if isempty(tline_value)
0326                     readList = 'eccodes';
0327                 else
0328                     eccodes(ecCodeNo,1:2)={pos,tline_value};
0329                     ecCodeNo=ecCodeNo+1;
0330                 end
0331             case 'subsystem'
0332                 if isempty(tline_value)
0333                     readList = 'subsystem';
0334                 else
0335                     subSystems(subSysNo,1:2)={pos,tline_value};
0336                     subSysNo=subSysNo+1;
0337                 end
0338             case 'metabolites'
0339                 readList = 'equation';
0340             case 'annotation'
0341                 readList = 'annotation';
0342                 
0343             otherwise
0344                 switch readList
0345                     case 'eccodes'
0346                         eccodes(ecCodeNo,1:2)={pos,regexprep(tline_value,'^ +- "?(.*)"?$','$1')};
0347                         ecCodeNo=ecCodeNo+1;
0348                     case 'subsystem'
0349                         subSystems(subSysNo,1:2)={pos,regexprep(tline_value,'^ +- "?(.*)"?$','$1')};
0350                         subSysNo=subSysNo+1;
0351                     case 'annotation'
0352                         [rxnMiriams, miriamKey,rxnMirNo] = gatherAnnotation(pos,rxnMiriams,tline_key,tline_value,miriamKey,rxnMirNo);
0353                         rxnMirNo=rxnMirNo+1;
0354                     case 'equation'
0355                         coeff = sscanf(tline_value,'%f');
0356                         equations(equatiNo,1:3)={pos,tline_key,coeff};
0357                         equatiNo=equatiNo+1;
0358                     otherwise
0359                         error(['Unknown entry in yaml file: ' tline_raw])
0360                 end                
0361         end; continue
0362     end
0363 
0364     % import genes:
0365     if section == 4
0366         switch tline_key
0367             case 'id'
0368                 pos = pos + 1;
0369                 model = readFieldValue(model, 'genes', tline_value, pos);
0370                 readList = '';
0371                 miriamKey = '';
0372             case 'name'
0373                 model = readFieldValue(model, 'geneShortNames', tline_value, pos);
0374             case 'annotation'
0375                 readList = 'annotation';
0376             otherwise
0377                 switch readList
0378                     case 'annotation'
0379                         [geneMiriams, miriamKey] = gatherAnnotation(pos,geneMiriams,tline_key,tline_value,miriamKey,genMirNo);
0380                         genMirNo = genMirNo + 1;
0381                     otherwise
0382                         error(['Unknown entry in yaml file: ' tline_raw])
0383                 end
0384         end; continue
0385     end
0386 
0387     % import compartments:
0388     if section == 5
0389         model.comps(end+1,1) = {tline_key};
0390         model.compNames(end+1,1) = {tline_value};
0391     end
0392 
0393     % import ec reaction info
0394     if section == 6
0395         switch tline_key
0396             case 'id'
0397                 pos = pos + 1;
0398                 model.ec = readFieldValue(model.ec, 'rxns', tline_value, pos);
0399                 readList='';
0400             case 'kcat'
0401                 model.ec = readFieldValue(model.ec, 'kcat', tline_value, pos);
0402                 readList='';
0403             case 'source'
0404                 model.ec = readFieldValue(model.ec, 'source', tline_value, pos);
0405                 readList='';
0406             case 'notes'
0407                 model.ec = readFieldValue(model.ec, 'notes', tline_value, pos);
0408                 readList='';
0409             case 'eccodes'
0410                 if isempty(tline_value)
0411                     readList = 'eccodes';
0412                 else
0413                     ecGecko(ecGeckoNo,1:2)={pos,tline_value};
0414                     ecGeckoNo=ecGeckoNo+1;
0415                 end
0416             case 'enzymes'
0417                 readList = 'enzStoich';
0418             otherwise
0419                 switch readList
0420                     case 'eccodes'
0421                         ecGecko(ecGeckoNo,1:2)={pos,regexprep(tline_value,'^ +- "?(.*)"?$','$1')};
0422                         ecGeckoNo=ecGeckoNo+1;
0423                     case 'enzStoich'
0424                         coeff = sscanf(tline_value,'%f');
0425                         enzStoich(enzStoichNo,1:3)={pos,tline_key,coeff};
0426                         enzStoichNo=enzStoichNo+1;
0427                     otherwise
0428                         error(['Unknown entry in yaml file: ' tline_raw])
0429                 end
0430         end; continue
0431     end
0432 
0433     % import ec enzyme info
0434     if section == 7
0435         switch tline_key
0436             case 'genes'
0437                 pos = pos + 1;
0438                 model.ec = readFieldValue(model.ec, 'genes', tline_value, pos);
0439             case 'enzymes'
0440                 model.ec = readFieldValue(model.ec, 'enzymes', tline_value, pos);
0441             case 'mw'
0442                 model.ec = readFieldValue(model.ec, 'mw', tline_value, pos);
0443             case 'sequence'
0444                 model.ec = readFieldValue(model.ec, 'sequence', tline_value, pos);
0445             case 'concs'
0446                 model.ec = readFieldValue(model.ec, 'concs', tline_value, pos);
0447             otherwise
0448                 error(['Unknown entry in yaml file: ' tline_raw])
0449         end; continue
0450     end
0451 end
0452 
0453 %Parse annotations
0454 if ~isempty(metMiriams)
0455     locs = cell2mat(metMiriams(:,1));
0456     for i=unique(locs)'
0457         model.metMiriams{i,1}.name=metMiriams(locs==i,2);
0458         model.metMiriams{i,1}.value=metMiriams(locs==i,3);
0459     end
0460 end
0461 if ~isempty(rxnMiriams)
0462     locs = cell2mat(rxnMiriams(:,1));
0463     for i=unique(locs)'
0464         model.rxnMiriams{i,1}.name=rxnMiriams(locs==i,2);
0465         model.rxnMiriams{i,1}.value=rxnMiriams(locs==i,3);
0466     end
0467 end
0468 if ~isempty(geneMiriams)
0469     locs = cell2mat(geneMiriams(:,1));
0470     for i=unique(locs)'
0471         model.geneMiriams{i,1}.name=geneMiriams(locs==i,2);
0472         model.geneMiriams{i,1}.value=geneMiriams(locs==i,3);
0473     end
0474 end
0475 
0476 %Parse subSystems
0477 if ~isempty(subSystems)
0478     locs = cell2mat(subSystems(:,1));
0479     for i=unique(locs)'
0480         model.subSystems{i,1}=subSystems(locs==i,2);
0481     end
0482 end
0483 
0484 %Parse ec-codes
0485 if ~isempty(eccodes)
0486     locs = cell2mat(eccodes(:,1));
0487     for i=unique(locs)'
0488         eccodesCat=strjoin(eccodes(locs==i,2),';');
0489         model.eccodes{i,1}=eccodesCat;
0490     end
0491     emptyEc=cellfun(@isempty,model.eccodes);
0492     model.eccodes(emptyEc)={''};
0493 end
0494 
0495 % follow-up data processing
0496 if verbose
0497     fprintf('\nimporting completed\nfollow-up processing...');
0498 end
0499 [~, model.metComps] = ismember(model.metComps, model.comps);
0500 [~, model.geneComps] = ismember(model.geneComps, model.comps);
0501 [~, model.rxnComps] = ismember(model.rxnComps, model.comps);
0502 
0503 % Fill S-matrix
0504 rxnIdx = cellfun(@isempty, equations(:,1));
0505 equations(rxnIdx,:) = '';
0506 rxnIdx = cell2mat(equations(:,1));
0507 [~,metIdx] = ismember(equations(:,2),model.mets);
0508 coeffs = cell2mat(equations(:,3));
0509 model.S=sparse(max(metIdx),max(rxnIdx));
0510 linearIndices = sub2ind([max(metIdx), max(rxnIdx)],metIdx,rxnIdx);
0511 model.S(linearIndices) = coeffs;
0512 
0513 % Convert strings to numeric
0514 model.metCharges = str2double(model.metCharges);
0515 model.lb = str2double(model.lb);
0516 model.ub = str2double(model.ub);
0517 model.rxnConfidenceScores = str2double(model.rxnConfidenceScores);
0518 model.b = zeros(length(model.mets),1);
0519 model.metDeltaG = str2double(model.metDeltaG);
0520 model.rxnDeltaG = str2double(model.rxnDeltaG);
0521 
0522 % Fill some other fields
0523 model.annotation.defaultLB = min(model.lb);
0524 model.annotation.defaultUB = max(model.ub);
0525 if numel(model.lb)<numel(model.rxns) %No LB reported = min
0526     model.lb(end+1:numel(model.rxns)-numel(model.lb),1) = double(model.annotation.defaultLB);
0527 end
0528 if numel(model.ub)<numel(model.rxns) %No UB reported = max
0529     model.ub(end+1:numel(model.rxns)-numel(model.ub),1) = double(model.annotation.defaultUB);
0530 end
0531 if ~all(cellfun(@isempty,model.rev))
0532     model.rev = str2double(model.rev);
0533 else
0534     model.rev = [];
0535 end
0536 if numel(model.rev)<numel(model.rxns) %No rev reported, assume from LB and UB
0537     model.rev(end+1:numel(model.rxns)-numel(model.rev),1) = double(model.lb<0 & model.ub>0);
0538 end
0539 
0540 % Remove empty fields, otherwise fill to correct length
0541 % Reactions
0542 for i={'rxnNames','grRules','eccodes','rxnNotes','rxnReferences',...
0543        'rxnFrom','subSystems','rxnMiriams'} % Empty strings
0544    model = emptyOrFill(model,i{1},{''},'rxns');
0545 end
0546 for i={'c'} % Zeros
0547    model = emptyOrFill(model,i{1},0,'rxns',true);
0548 end
0549 for i={'rxnConfidenceScores','rxnDeltaG'} % NaNs
0550    model = emptyOrFill(model,i{1},NaN,'rxns');
0551 end
0552 for i={'rxnComps'} % Ones, assume first compartment
0553    model = emptyOrFill(model,i{1},1,'rxns');
0554 end
0555 % Metabolites
0556 for i={'metNames','inchis','metFormulas','metMiriams','metFrom','metSmiles','metNotes'} % Empty strings
0557    model = emptyOrFill(model,i{1},{''},'mets');
0558 end
0559 for i={'metCharges','unconstrained'} % Zeros
0560    model = emptyOrFill(model,i{1},0,'mets');
0561 end
0562 for i={'metDeltaG'} % % NaNs
0563     model = emptyOrFill(model,i{1},NaN,'mets');
0564  end
0565 for i={'metComps'} % Ones, assume first compartment
0566    model = emptyOrFill(model,i{1},1,'mets');
0567 end
0568 % Genes
0569 for i={'geneMiriams','geneShortNames'} % Empty strings
0570    model = emptyOrFill(model,i{1},{''},'genes');
0571 end
0572 for i={'geneComps'} % Ones, assume first compartment
0573    model = emptyOrFill(model,i{1},1,'genes');
0574 end
0575 % Comps
0576 for i={'compNames'} % Empty strings
0577    model = emptyOrFill(model,i{1},{''},'comps');
0578 end
0579 for i={'compOutside'} % First comp
0580    model = emptyOrFill(model,i{1},model.comps{1},'comps');
0581 end
0582 % Single fields are kept, even if empty
0583 % for i={'description','name','version','date','annotation'}
0584 %     if isempty(model.(i{1}))
0585 %         model = rmfield(model,i{1});
0586 %     end
0587 % end
0588 
0589 % Make rxnGeneMat fields and map to the existing model.genes field
0590 [genes, rxnGeneMat] = getGenesFromGrRules(model.grRules);
0591 model.rxnGeneMat = sparse(numel(model.rxns),numel(model.genes));
0592 [~,geneOrder] = ismember(genes,model.genes);
0593 if any(geneOrder == 0)
0594     error(['The grRules includes the following gene(s), that are not in '...
0595            'the list of model genes: ', genes{~geneOrder}])
0596 end
0597 model.rxnGeneMat(:,geneOrder) = rxnGeneMat;
0598 
0599 % Finalize GECKO model
0600 if isGECKO
0601     % Fill in empty fields and empty entries
0602     for i={'kcat','source','notes','eccodes'} % Even keep empty
0603         model.ec = emptyOrFill(model.ec,i{1},{''},'rxns',true);
0604     end
0605     for i={'enzymes','mw','sequence'}
0606         model.ec = emptyOrFill(model.ec,i{1},{''},'genes',true);
0607     end
0608     model.ec = emptyOrFill(model.ec,'concs',{'NaN'},'genes',true);
0609     model.ec = emptyOrFill(model.ec,'kcat',{'0'},'genes',true);
0610     % Change string to double
0611     for i={'kcat','mw','concs'}
0612         if isfield(model.ec,i{1})
0613             model.ec.(i{1}) = str2double(model.ec.(i{1}));
0614         end
0615     end
0616     % Fill rxnEnzMat
0617     rxnIdx              = cellfun(@isempty, enzStoich(:,1));
0618     enzStoich(rxnIdx,:) = '';
0619     rxnIdx              = cell2mat(enzStoich(:,1));
0620     [~,enzIdx]          = ismember(enzStoich(:,2),model.ec.enzymes);
0621     coeffs              = cell2mat(enzStoich(:,3));
0622     model.ec.rxnEnzMat  = zeros(max(rxnIdx), max(enzIdx));
0623     linearIndices       = sub2ind([max(rxnIdx), max(enzIdx)], rxnIdx, enzIdx);
0624     model.ec.rxnEnzMat(linearIndices) = coeffs;
0625     %Parse ec-codes
0626     if ~isempty(ecGecko)
0627         locs = cell2mat(ecGecko(:,1));
0628         for i=unique(locs)'
0629             ecGeckoCat=strjoin(ecGecko(locs==i,2),';');
0630             model.ec.eccodes{i,1}=ecGeckoCat;
0631         end
0632         emptyEc=cellfun(@isempty,model.ec.eccodes);
0633         model.ec.eccodes(emptyEc)={''};
0634     end
0635 end
0636 
0637 if verbose
0638     fprintf(' Done!\n');
0639 end
0640 end
0641 
0642 function model = emptyOrFill(model,field,emptyEntry,type,keepEmpty)
0643 if nargin<5
0644     keepEmpty=false;
0645 end
0646 if isnumeric(emptyEntry)
0647     emptyCells=isempty(model.(field));
0648 else
0649     emptyCells=cellfun(@isempty,model.(field));
0650 end
0651 if all(emptyCells) && ~keepEmpty
0652     model = rmfield(model, field);
0653 elseif numel(model.(field))<numel(model.(type))
0654     model.(field)(end+1:numel(model.(type)),1)=emptyEntry;
0655 end
0656 end
0657 
0658 function model = readFieldValue(model, fieldName, value, pos)
0659 if numel(model.(fieldName))<pos-1
0660     model.(fieldName)(end+1:pos,1) = {''};
0661 end
0662 model.(fieldName)(pos,1) = {value};
0663 end
0664 
0665 function [miriams, miriamKey,entryNumber] = gatherAnnotation(pos,miriams,key,value,miriamKey,entryNumber)
0666 if isempty(key)
0667     key=miriamKey;
0668 else
0669     miriamKey=key;
0670 end
0671 if ~isempty(value)
0672     miriams(entryNumber,1:3) = {pos, key, strip(value)};
0673 else
0674     entryNumber = entryNumber - 1;
0675 end
0676 end

Generated by m2html © 2005