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

Generated by m2html © 2005