0001 function writeYAMLmodel(model,fileName,preserveQuotes,sortIds)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 if nargin<2|| isempty(fileName)
0017 [fileName, pathName] = uiputfile({'*.yml;*.yaml'}, 'Select file for model export',[model.id '.yml']);
0018 if fileName == 0
0019 error('You should provide a file location')
0020 else
0021 fileName = fullfile(pathName,fileName);
0022 end
0023 end
0024 fileName=char(fileName);
0025
0026 if nargin < 3
0027 preserveQuotes = true;
0028 end
0029 if nargin < 4
0030 sortIds = false;
0031 end
0032 if ~endsWith(fileName,{'.yml','.yaml'})
0033 fileName = strcat(fileName,'.yml');
0034 end
0035
0036
0037 if isfield(model,'rules')
0038 model = ravenCobraWrapper(model);
0039 end
0040
0041
0042 if sortIds == true
0043 model = sortIdentifiers(model);
0044 end
0045
0046
0047 if isfield(model,'metMiriams')
0048 [model.newMetMiriams,model.newMetMiriamNames] = extractMiriam(model.metMiriams);
0049 end
0050 if isfield(model,'rxnMiriams')
0051 [model.newRxnMiriams,model.newRxnMiriamNames] = extractMiriam(model.rxnMiriams);
0052 end
0053 if isfield(model,'geneMiriams')
0054 [model.newGeneMiriams,model.newGeneMiriamNames] = extractMiriam(model.geneMiriams);
0055 end
0056 if isfield(model,'compMiriams')
0057 [model.newCompMiriams,model.newCompMiriamNames] = extractMiriam(model.compMiriams);
0058 end
0059
0060
0061 fid = fopen(fileName,'wt');
0062 if fid == -1
0063 error(['Cannot write to ' fileName ', does the directory exist?'])
0064 end
0065 fprintf(fid,'---\n!!omap\n');
0066
0067
0068 writeMetadata(model,fid);
0069
0070
0071 fprintf(fid,'- metabolites:\n');
0072 for i = 1:length(model.mets)
0073 fprintf(fid,' - !!omap\n');
0074 writeField(model, fid, 'mets', 'txt', i, ' - id', preserveQuotes)
0075 writeField(model, fid, 'metNames', 'txt', i, ' - name', preserveQuotes)
0076 writeField(model, fid, 'metComps', 'txt', i, ' - compartment', preserveQuotes)
0077 writeField(model, fid, 'metFormulas', 'txt', i, ' - formula', preserveQuotes)
0078 writeField(model, fid, 'metCharges', 'num', i, ' - charge', preserveQuotes)
0079 writeField(model, fid, 'inchis', 'txt', i, ' - inchis', preserveQuotes)
0080 writeField(model, fid, 'metSmiles', 'txt', i, ' - smiles', preserveQuotes)
0081 writeField(model, fid, 'metMiriams', 'txt', i, ' - annotation', preserveQuotes)
0082 writeField(model, fid, 'metDeltaG', 'num', i, ' - deltaG', preserveQuotes)
0083 writeField(model, fid, 'metNotes', 'txt', i, ' - notes', preserveQuotes)
0084 writeField(model, fid, 'metFrom', 'txt', i, ' - metFrom', preserveQuotes)
0085 end
0086
0087
0088 fprintf(fid,'- reactions:\n');
0089 for i = 1:length(model.rxns)
0090 fprintf(fid,' - !!omap\n');
0091 writeField(model, fid, 'rxns', 'txt', i, ' - id', preserveQuotes)
0092 writeField(model, fid, 'rxnNames', 'txt', i, ' - name', preserveQuotes)
0093 writeField(model, fid, 'S', 'txt', i, ' - metabolites', preserveQuotes)
0094 writeField(model, fid, 'lb', 'num', i, ' - lower_bound', preserveQuotes)
0095 writeField(model, fid, 'ub', 'num', i, ' - upper_bound', preserveQuotes)
0096 writeField(model, fid, 'grRules', 'txt', i, ' - gene_reaction_rule', preserveQuotes)
0097 writeField(model, fid, 'rxnFrom', 'txt', i, ' - rxnFrom', preserveQuotes)
0098 if model.c(i)~=0
0099 writeField(model, fid, 'c', 'num', i, ' - objective_coefficient', preserveQuotes)
0100 end
0101 writeField(model, fid, 'eccodes', 'txt', i, ' - eccodes', preserveQuotes)
0102 writeField(model, fid, 'rxnReferences', 'txt', i, ' - references', preserveQuotes)
0103 writeField(model, fid, 'subSystems', 'txt', i, ' - subsystem', preserveQuotes)
0104 writeField(model, fid, 'rxnMiriams', 'txt', i, ' - annotation', preserveQuotes)
0105 writeField(model, fid, 'rxnDeltaG', 'num', i, ' - deltaG', preserveQuotes)
0106 writeField(model, fid, 'rxnConfidenceScores', 'num', i, ' - confidence_score', preserveQuotes)
0107 writeField(model, fid, 'rxnNotes', 'txt', i, ' - rxnNotes', preserveQuotes)
0108 end
0109
0110
0111 fprintf(fid,'- genes:\n');
0112 for i = 1:length(model.genes)
0113 fprintf(fid,' - !!omap\n');
0114 writeField(model, fid, 'genes', 'txt', i, ' - id', preserveQuotes)
0115 writeField(model, fid, 'geneShortNames', 'txt', i, ' - name', preserveQuotes)
0116 writeField(model, fid, 'proteins', 'txt', i, ' - protein', preserveQuotes)
0117 writeField(model, fid, 'geneMiriams', 'txt', i, ' - annotation', preserveQuotes)
0118 end
0119
0120
0121 fprintf(fid,'- compartments: !!omap\n');
0122 for i = 1:length(model.comps)
0123 writeField(model, fid, 'compNames', 'txt', i, ['- ' model.comps{i}], preserveQuotes)
0124 writeField(model, fid, 'compMiriams', 'txt', i, '- annotation', preserveQuotes)
0125 end
0126
0127
0128
0129 if isfield(model,'ec')
0130 fprintf(fid,'- ec-rxns:\n');
0131 for i = 1:length(model.ec.rxns)
0132 fprintf(fid,' - !!omap\n');
0133 writeField(model.ec, fid, 'rxns', 'txt', i, '- id', preserveQuotes)
0134 writeField(model.ec, fid, 'kcat', 'num', i, '- kcat', preserveQuotes)
0135 writeField(model.ec, fid, 'source', 'txt', i, '- source', preserveQuotes)
0136 writeField(model.ec, fid, 'notes', 'txt', i, '- notes', preserveQuotes)
0137 writeField(model.ec, fid, 'eccodes', 'txt', i, '- eccodes', preserveQuotes)
0138 writeField(model.ec, fid, 'rxnEnzMat', 'txt', i, '- enzymes', preserveQuotes)
0139 end
0140
0141 fprintf(fid,'- ec-enzymes:\n');
0142 for i = 1:length(model.ec.genes)
0143 fprintf(fid,' - !!omap\n');
0144 writeField(model.ec, fid, 'genes', 'txt', i, '- genes', preserveQuotes)
0145 writeField(model.ec, fid, 'enzymes', 'txt', i, '- enzymes', preserveQuotes)
0146 writeField(model.ec, fid, 'mw', 'num', i, '- mw', preserveQuotes)
0147 writeField(model.ec, fid, 'sequence', 'txt', i, '- sequence', preserveQuotes)
0148 writeField(model.ec, fid, 'concs', 'num', i, '- concs', preserveQuotes)
0149 end
0150 end
0151
0152
0153 fclose(fid);
0154
0155 end
0156
0157 function writeField(model,fid,fieldName,type,pos,name,preserveQuotes)
0158
0159
0160
0161
0162 if isfield(model,fieldName)
0163 if strcmp(fieldName,'metComps')
0164
0165 fieldName = 'comps';
0166 pos = model.metComps(pos);
0167 end
0168
0169 field = model.(fieldName);
0170
0171 if strcmp(fieldName,'metMiriams')
0172 if ~isempty(model.metMiriams{pos})
0173 fprintf(fid,' %s: !!omap\n',name);
0174 for i=1:size(model.newMetMiriams,2)
0175
0176
0177 if ~isempty(model.newMetMiriams{pos,i})
0178
0179
0180
0181 writeField(model, fid, 'newMetMiriams', 'txt', pos, [' - ' model.newMetMiriamNames{i} '_' sprintf('%d',i)], preserveQuotes)
0182 end
0183 end
0184 end
0185
0186 elseif strcmp(fieldName,'rxnMiriams')
0187 if ~isempty(model.rxnMiriams{pos})
0188 fprintf(fid,' %s: !!omap\n',name);
0189 for i=1:size(model.newRxnMiriams,2)
0190 if ~isempty(model.newRxnMiriams{pos,i})
0191 writeField(model, fid, 'newRxnMiriams', 'txt', pos, [' - ' model.newRxnMiriamNames{i} '_' sprintf('%d',i)], preserveQuotes)
0192 end
0193 end
0194 end
0195
0196 elseif strcmp(fieldName,'geneMiriams')
0197 if ~isempty(model.geneMiriams{pos})
0198 fprintf(fid,' %s: !!omap\n',name);
0199 for i=1:size(model.newGeneMiriams,2)
0200 if ~isempty(model.newGeneMiriams{pos,i})
0201 writeField(model, fid, 'newGeneMiriams', 'txt', pos, [' - ' model.newGeneMiriamNames{i} '_' sprintf('%d',i)], preserveQuotes)
0202 end
0203 end
0204 end
0205
0206 elseif strcmp(fieldName,'compMiriams')
0207 if ~isempty(model.compMiriams{pos})
0208 fprintf(fid,' %s: !!omap\n',name);
0209 for i=1:size(model.newCompMiriams,2)
0210 if ~isempty(model.newCompMiriams{pos,i})
0211 writeField(model, fid, 'newCompMiriams', 'txt', pos, [' - ' model.newCompMiriamNames{i} '_' sprintf('%d',i)], preserveQuotes)
0212 end
0213 end
0214 end
0215
0216 elseif strcmp(fieldName,'S')
0217
0218 fprintf(fid,' %s: !!omap\n',name);
0219 if sum(field(:,pos) ~= 0) > 0
0220 model.mets = model.mets(field(:,pos) ~= 0);
0221 model.coeffs = field(field(:,pos) ~= 0,pos);
0222
0223 [model.mets,order] = sort(model.mets);
0224 model.coeffs = model.coeffs(order);
0225 for i = 1:length(model.mets)
0226 writeField(model, fid, 'coeffs', 'num', i, [' - ' model.mets{i}], preserveQuotes)
0227 end
0228 end
0229
0230 elseif strcmp(fieldName,'rxnEnzMat')
0231
0232 fprintf(fid,' %s: !!omap\n',name);
0233 if sum(field(pos,:) ~= 0) > 0
0234 model.enzymes = model.enzymes(field(pos,:) ~= 0);
0235 model.coeffs = field(pos,field(pos,:) ~= 0);
0236
0237 [model.enzymes,order] = sort(model.enzymes);
0238 model.coeffs = model.coeffs(order);
0239 for i = 1:length(model.enzymes)
0240 writeField(model, fid, 'coeffs', 'num', i, [' - ' model.enzymes{i}], preserveQuotes)
0241 end
0242 end
0243
0244 elseif sum(strcmp({'subSystems','newMetMiriams','newRxnMiriams','newGeneMiriams','newCompMiriams','eccodes'},fieldName)) > 0
0245
0246 if strcmp(fieldName,'subSystems')
0247 list = field{pos};
0248 if isempty(list)
0249 return
0250 end
0251 elseif strcmp(fieldName,'newMetMiriams')
0252 index = str2double(regexprep(name,'^.+_',''));
0253 name = regexprep(name,'_\d+$','');
0254 list = strsplit(model.newMetMiriams{pos,index},'; ');
0255 elseif strcmp(fieldName,'newRxnMiriams')
0256 index = str2double(regexprep(name,'^.+_',''));
0257 name = regexprep(name,'_\d+$','');
0258 list = strsplit(model.newRxnMiriams{pos,index},'; ');
0259 elseif strcmp(fieldName,'newGeneMiriams')
0260 index = str2double(regexprep(name,'^.+_',''));
0261 name = regexprep(name,'_\d+$','');
0262 list = strsplit(model.newGeneMiriams{pos,index},'; ');
0263 elseif strcmp(fieldName,'newCompMiriams')
0264 index = str2double(regexprep(name,'^.+_',''));
0265 name = regexprep(name,'_\d+$','');
0266 list = strsplit(model.newCompMiriams{pos,index},'; ');
0267 elseif ~isempty(field{pos})
0268 list = strrep(field{pos},' ','');
0269 list = strsplit(list,';');
0270 else
0271 return
0272 end
0273 list=strip(list);
0274
0275 if length(list) == 1 && ~strcmp(list{1},'') && ~strcmp(fieldName,'subSystems')
0276 if preserveQuotes
0277 list = ['"' list{1} '"'];
0278 end
0279 if iscell(list)
0280 list=list{1};
0281 end
0282 fprintf(fid,' %s: %s\n',name,list);
0283 elseif ischar(list) && strcmp(fieldName,'subSystems')
0284 if preserveQuotes
0285 list = ['"' list '"'];
0286 end
0287 fprintf(fid,' %s: %s\n',name,list);
0288 elseif length(list) > 1 || strcmp(fieldName,'subSystems')
0289 if preserveQuotes
0290 for j=1:numel(list)
0291 list{j} = ['"' list{j} '"'];
0292 end
0293 end
0294 fprintf(fid,' %s:\n',name);
0295 for i = 1:length(list)
0296 fprintf(fid,'%s - %s\n',regexprep(name,'(^\s*).*','$1'),list{i});
0297 end
0298 end
0299
0300 elseif sum(pos) > 0
0301
0302 if strcmp(type,'txt')
0303 value = field{pos};
0304 if preserveQuotes && ~isempty(value)
0305 value = ['"',value,'"'];
0306 end
0307 elseif strcmp(type,'num')
0308 if isnan(field(pos))
0309 value = [];
0310 else
0311 value = sprintf('%.15g',full(field(pos)));
0312 end
0313 end
0314 if ~isempty(value)
0315 fprintf(fid,' %s: %s\n',name,value);
0316 end
0317 end
0318 end
0319 end
0320
0321 function writeMetadata(model,fid)
0322
0323
0324
0325
0326 fprintf(fid, '- metaData:\n');
0327 if isfield(model,'id')
0328 fprintf(fid, ' id: "%s"\n', model.id);
0329 else
0330 fprintf(fid, ' id: "blankID"\n');
0331 end
0332 if isfield(model,'name')
0333 fprintf(fid, ' name: "%s"\n',model.name);
0334 else
0335 fprintf(fid, ' name: "blankName"\n');
0336 end
0337 if isfield(model,'version')
0338 fprintf(fid, ' version: "%s"\n',model.version);
0339 end
0340 fprintf(fid, ' date: "%s"\n',datestr(now,29));
0341 if isfield(model,'annotation')
0342 if isfield(model.annotation,'defaultLB')
0343 fprintf(fid, ' defaultLB: "%g"\n', model.annotation.defaultLB);
0344 end
0345 if isfield(model.annotation,'defaultUB')
0346 fprintf(fid, ' defaultUB: "%g"\n', model.annotation.defaultUB);
0347 end
0348 if isfield(model.annotation,'givenName')
0349 fprintf(fid, ' givenName: "%s"\n', model.annotation.givenName);
0350 end
0351 if isfield(model.annotation,'familyName')
0352 fprintf(fid, ' familyName: "%s"\n', model.annotation.familyName);
0353 end
0354 if isfield(model.annotation,'authors')
0355 fprintf(fid, ' authors: "%s"\n', model.annotation.authors);
0356 end
0357 if isfield(model.annotation,'email')
0358 fprintf(fid, ' email: "%s"\n', model.annotation.email);
0359 end
0360 if isfield(model.annotation,'organization')
0361 fprintf(fid, ' organization: "%s"\n',model.annotation.organization);
0362 end
0363 if isfield(model.annotation,'taxonomy')
0364 fprintf(fid, ' taxonomy: "%s"\n', model.annotation.taxonomy);
0365 end
0366 if isfield(model.annotation,'note')
0367 fprintf(fid, ' note: "%s"\n', model.annotation.note);
0368 end
0369 if isfield(model.annotation,'sourceUrl')
0370 fprintf(fid, ' sourceUrl: "%s"\n', model.annotation.sourceUrl);
0371 end
0372 end
0373 if isfield(model,'ec')
0374 if model.ec.geckoLight
0375 geckoLight = 'true';
0376 else
0377 geckoLight = 'false';
0378 end
0379 fprintf(fid,' geckoLight: "%s"\n',geckoLight);
0380 end
0381 end