0001 function exportToExcelFormat(model,filename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 if ~isfield(model,'id') || ~isfield(model,'description')
0030 throw(MException('','The model must have "id" and "description" fields'));
0031 end
0032 if ~any(model.id)
0033 throw(MException('','The model must have an "id" field'));
0034 end
0035 if ~any(model.description)
0036 fprintf('WARNING: The "description" field is empty. Uses the id as description.\n');
0037 model.description=model.id;
0038 end
0039
0040
0041
0042 I=find(isinf(model.lb));
0043 J=find(isinf(model.lb));
0044 if any(I) || any(J)
0045 fprintf(['WARNING: Some reactions have +/- Inf as bounds. '...
0046 'This is not supported by the Excel exporter. Such bounds are changed to +/- 1000 instead.\n']);
0047 model.lb(I(model.lb(I)<0))=-1000;
0048 model.lb(I(model.lb(I)>0))=1000;
0049 model.ub(J(model.ub(J)<0))=-1000;
0050 model.ub(J(model.ub(J)>0))=1000;
0051 end
0052
0053
0054 fprintf(['NOTE: Reactions and genes have no compartment in the model structure, but must be assigned to a '...
0055 'compartment to adhere to SBML standards. Therefore the first compartment in model.comps is '...
0056 'used. This is just for annotation and has no effect on the functionality of the model.\n']);
0057 [filePath A B]=fileparts(filename);
0058
0059 if ~any(filePath)
0060 filePath=pwd;
0061 end
0062
0063 if any(A) && any(B)
0064 excelFile=[filePath '\' A B];
0065 else
0066 excelFile=[];
0067 end
0068
0069 if filePath(end)~='\'
0070 filePath=[filePath '\'];
0071 end
0072
0073
0074 if ~exist(filePath,'dir')
0075 mkdir(filePath);
0076 end
0077
0078
0079
0080 model.rxns=strtrim(model.rxns);
0081 model.mets=strtrim(model.mets);
0082 model.metName=strtrim(model.metNames);
0083 model.rxnNames=strtrim(model.rxnNames);
0084 model.compNames=strtrim(model.compNames);
0085
0086
0087
0088
0089 model.equations=constructEquations(model,model.rxns,true);
0090
0091
0092 rxnFile=fopen(fullfile(filePath,'excelRxns.txt'),'wt');
0093
0094
0095 fprintf(rxnFile,'#\tRXNID\tNAME\tEQUATION\tEC-NUMBER\tGENE ASSOCIATION\tLOWER BOUND\tUPPER BOUND\tOBJECTIVE\tCOMPARTMENT\tSUBSYSTEM\tSBO TERM\tREPLACEMENT ID\n');
0096
0097
0098 if isfield(model,'eccodes');
0099 model.eccodes=strtrim(model.eccodes);
0100 printEC=true;
0101 else
0102 printEC=false;
0103 end
0104
0105
0106 if isfield(model,'grRules');
0107 model.grRules=strtrim(model.grRules);
0108 printRules=true;
0109
0110
0111 rules=model.grRules;
0112 rules=strrep(rules,'(','');
0113 rules=strrep(rules,')','');
0114 rules=strrep(rules,' and ',':');
0115 rules=strrep(rules,' or ',';');
0116 else
0117 printRules=false;
0118 end
0119
0120
0121 if isfield(model,'subSystems');
0122 model.subSystems=strtrim(model.subSystems);
0123 printSubSystems=true;
0124 else
0125 printSubSystems=false;
0126 end
0127
0128
0129 for i=1:numel(model.rxns)
0130 fprintf(rxnFile,['\t' model.rxns{i} '\t' model.rxnNames{i} '\t' model.equations{i} '\t']);
0131
0132 if printEC==true
0133 fprintf(rxnFile,[model.eccodes{i} '\t']);
0134 else
0135 fprintf(rxnFile,'\t');
0136 end
0137
0138 if printRules==true
0139 fprintf(rxnFile,[rules{i} '\t']);
0140 else
0141 fprintf(rxnFile,'\t');
0142 end
0143
0144
0145 fprintf(rxnFile,[num2str(model.lb(i)) '\t' num2str(model.ub(i)) '\t']);
0146
0147 if model.c(i)~=0
0148 fprintf(rxnFile,[num2str(model.c(i)) '\t' ]);
0149 else
0150 fprintf(rxnFile,'\t');
0151 end
0152
0153 fprintf(rxnFile,[model.comps{1} '\t']);
0154
0155 if printSubSystems==true
0156 fprintf(rxnFile,[model.subSystems{i} '\t']);
0157 else
0158 fprintf(rxnFile,'\t');
0159 end
0160
0161
0162 fprintf(rxnFile,'\t\t');
0163
0164 fprintf(rxnFile,'\n');
0165 end
0166
0167 fclose(rxnFile);
0168
0169
0170 metFile=fopen(fullfile(filePath,'excelMets.txt'),'wt');
0171
0172
0173 if isfield(model,'unconstrained')
0174 printUnconstrained=true;
0175 else
0176 printUnconstrained=false;
0177 end
0178
0179
0180 if isfield(model,'metMiriams')
0181 printMiriam=true;
0182 else
0183 printMiriam=false;
0184 end
0185
0186
0187 if isfield(model,'metFormulas')
0188 model.metFormulas=strtrim(model.metFormulas);
0189 printFormulas=true;
0190 else
0191 printFormulas=false;
0192 end
0193
0194
0195 if isfield(model,'inchis')
0196 model.inchis=strtrim(model.inchis);
0197 printInchis=true;
0198 else
0199 printInchis=false;
0200 end
0201
0202
0203 fprintf(metFile,'#\tMETID\tMETNAME\tUNCONSTRAINED\tMIRIAM\tCOMPOSITION\tInChI\tCOMPARTMENT\tREPLACEMENT ID\tMETS FIELD\n');
0204
0205
0206 for i=1:numel(model.mets)
0207 fprintf(metFile,['\t' model.metNames{i} '[' model.comps{model.metComps(i)} ']\t' model.metNames{i} '\t']);
0208
0209 if printUnconstrained==true
0210 if model.unconstrained(i)~=0
0211 fprintf(metFile,'true\t');
0212 else
0213 fprintf(metFile,'\t');
0214 end
0215 else
0216 fprintf(metFile,'\t');
0217 end
0218
0219 if printMiriam==true
0220 if ~isempty(model.metMiriams{i})
0221 fprintf(metFile,[strtrim(model.metMiriams{i}.name{1}) ':' strtrim(model.metMiriams{i}.value{1}) '\t']);
0222 else
0223 fprintf(metFile,'\t');
0224 end
0225 else
0226 fprintf(metFile,'\t');
0227 end
0228
0229 if printFormulas==true
0230
0231 if printInchis==false
0232 fprintf(metFile,[model.metFormulas{i} '\t']);
0233 else
0234
0235
0236 if isempty(model.inchis{i})
0237 fprintf(metFile,[model.metFormulas{i} '\t']);
0238 else
0239 fprintf(metFile,'\t');
0240 end
0241 end
0242 else
0243 fprintf(metFile,'\t');
0244 end
0245
0246 if printInchis==true
0247 fprintf(metFile,[model.inchis{i} '\t']);
0248 else
0249 fprintf(metFile,'\t');
0250 end
0251
0252 fprintf(metFile,[model.comps{model.metComps(i)} '\t']);
0253
0254
0255
0256 fprintf(metFile,['m' int2str(i) '\t']);
0257
0258
0259
0260 fprintf(metFile,[model.mets{i} '\t']);
0261
0262 fprintf(metFile,'\n');
0263 end
0264
0265 fclose(metFile);
0266
0267 if isfield(model,'genes')
0268
0269 geneFile=fopen(fullfile(filePath,'excelGenes.txt'),'wt');
0270
0271
0272 if isfield(model,'geneMiriams')
0273 printMiriams=true;
0274 else
0275 printMiriams=false;
0276 end
0277
0278
0279 if isfield(model,'geneShortNames');
0280 model.geneShortNames=strtrim(model.geneShortNames);
0281 printShortNames=true;
0282 else
0283 printShortNames=false;
0284 end
0285
0286
0287 fprintf(geneFile,'#\tGENE NAME\tGENE ID 1\tGENE ID 2\tSHORT NAME\tCOMPARTMENT\tKEGG MAPS\n');
0288
0289
0290 for i=1:numel(model.genes)
0291 fprintf(geneFile,['\t' model.genes{i} '\t']);
0292
0293 if printMiriams==true
0294
0295
0296 if ~isempty(model.geneMiriams{i})
0297 nonKegg=1:numel(model.geneMiriams{i}.name);
0298 kegg=strmatch('kegg.pathway',strtrim(model.geneMiriams{i}.name),'exact');
0299 nonKegg(kegg)=[];
0300
0301 if ~isempty(nonKegg)
0302
0303
0304 fprintf(geneFile,[strtrim(model.geneMiriams{i}.name{nonKegg(1)}) ':' strtrim(model.geneMiriams{i}.value{nonKegg(1)}) '\t']);
0305
0306 if numel(nonKegg)>1
0307 fprintf(geneFile,[strtrim(model.geneMiriams{i}.name{nonKegg(2)}) ':' strtrim(model.geneMiriams{i}.value{nonKegg(2)}) '\t']);
0308 else
0309 fprintf(geneFile,'\t');
0310 end
0311 else
0312
0313 fprintf(geneFile,'\t\t');
0314 end
0315 else
0316 fprintf(geneFile,'\t\t');
0317 end
0318 else
0319 fprintf(geneFile,'\t\t');
0320 end
0321
0322 if printShortNames==true
0323 fprintf(geneFile,[model.geneShortNames{i} '\t']);
0324 else
0325 fprintf(geneFile,'\t');
0326 end
0327
0328
0329 fprintf(geneFile,[model.comps{1} '\t']);
0330
0331 if printMiriams==true
0332
0333
0334 if ~isempty(model.geneMiriams{i})
0335 kegg=strmatch('kegg.pathway',strtrim(model.geneMiriams{i}.name),'exact');
0336 if ~isempty(kegg)
0337 for j=1:numel(kegg)
0338 if j<numel(kegg)
0339 pad=':';
0340 else
0341 pad='';
0342 end
0343 fprintf(geneFile,[strtrim(model.geneMiriams{i}.value{kegg(j)}) pad]);
0344 end
0345 else
0346
0347 fprintf(geneFile,'\t');
0348 end
0349 else
0350 fprintf(geneFile,'\t');
0351 end
0352 else
0353 fprintf(geneFile,'\t');
0354 end
0355
0356 fprintf(geneFile,'\n');
0357 end
0358 fclose(geneFile);
0359 end
0360
0361 if isfield(model,'id')
0362
0363 modelFile=fopen(fullfile(filePath,'excelModel.txt'),'wt');
0364
0365
0366 fprintf(geneFile,'#\tMODELID\tMODELNAME\tDEFAULT LOWER\tDEFAULT UPPER\tCONTACT GIVEN NAME\tCONTACT FAMILY NAME\tCONTACT EMAIL\tORGANIZATION\tTAXONOMY\tNOTES\n');
0367
0368
0369
0370 fprintf(geneFile,['\t' model.id '\t' model.description '\t' num2str(min(model.lb)) '\t' num2str(max(model.ub)) '\tRasmus\tAgren\trasmus.agren@chalmers.se\tChalmers University of Technology\t9606\t\n']);
0371 fclose(modelFile);
0372 end
0373
0374 if isfield(model,'comps')
0375 if ~isfield(model,'compOutside')
0376 model.compOutside=cell(numel(model.comps),1);
0377 model.compOutside(:)={''};
0378 end
0379
0380 compsFile=fopen(fullfile(filePath,'excelComps.txt'),'wt');
0381
0382
0383 fprintf(compsFile,'#\tCOMPABBREV\tCOMPNAME\tINSIDE\tGO TERM\n');
0384
0385 for i=1:numel(model.comps)
0386 fprintf(compsFile,['\t' model.comps{i} '\t' model.compNames{i} '\t' model.compOutside{i} '\t\n']);
0387 end
0388 fclose(compsFile);
0389 end
0390
0391
0392
0393 if any(excelFile)
0394 foundError=false;
0395 textFiles={'excelRxns.txt' 'excelMets.txt' 'excelGenes.txt' 'excelComps.txt' 'excelModel.txt'};
0396 formatStrings={'%s%s%s%s%s%s%n%n%n%s%s%s%s' '%s%s%s%s%s%s%s%s%s%s' '%s%s%s%s%s%s%s' '%s%s%s%s%s' '%s%s%s%n%n%s%s%s%s%s%s'};
0397 sheets={'RXNS' 'METS' 'GENES' 'COMPS' 'MODEL'};
0398 captions={{'#' 'RXNID' 'NAME' 'EQUATION' 'EC-NUMBER' 'GENE ASSOCIATION' 'LOWER BOUND' 'UPPER BOUND' 'OBJECTIVE' 'COMPARTMENT' 'SUBSYSTEM' 'SBO TERM' 'REPLACEMENT ID'};...
0399 {'#' 'METID' 'METNAME' 'UNCONSTRAINED' 'MIRIAM' 'COMPOSITION' 'InChI' 'COMPARTMENT' 'REPLACEMENT ID' 'METS FIELD'};...
0400 {'#' 'GENE NAME' 'GENE ID 1' 'GENE ID 2' 'SHORT NAME' 'COMPARTMENT' 'KEGG MAPS'};...
0401 {'#' 'COMPABBREV' 'COMPNAME' 'INSIDE' 'GO TERM'};...
0402 {'#' 'MODELID' 'MODELNAME' 'DEFAULT LOWER' 'DEFAULT UPPER' 'CONTACT GIVEN NAME' 'CONTACT FAMILY NAME' 'CONTACT EMAIL' 'ORGANIZATION' 'TAXONOMY' 'NOTES'}};
0403 for i=1:numel(textFiles)
0404 fid=fopen(fullfile(filePath,textFiles{i}),'r');
0405 C = textscan(fid,formatStrings{i},'Delimiter','\t','Headerlines',1,'Whitespace','','BufSize',15000);
0406 fclose(fid);
0407
0408
0409
0410 cellArray=[];
0411 for j=1:numel(C)
0412
0413 if iscell(C{j})
0414
0415
0416 cellArray=[cellArray C{j}];
0417 else
0418 cellArray=[cellArray num2cell(C{j})];
0419 end
0420 end
0421
0422
0423 cellArray=[captions{i};cellArray];
0424 errorFlag=xlswrite(excelFile,cellArray,sheets{i});
0425
0426 if errorFlag==0
0427 fprintf('There was an error in writing the Excel file. Keeping the text files in the specified directory');
0428 foundError=true;
0429 end
0430 end
0431
0432
0433 if foundError==false
0434 for i=1:numel(textFiles)
0435 delete(fullfile(filePath,textFiles{i}));
0436 end
0437 end
0438 end
0439 end