Home > io > exportToExcelFormat.m

exportToExcelFormat

PURPOSE ^

exportToExcelFormat

SYNOPSIS ^

function exportToExcelFormat(model,fileName,sortIds)

DESCRIPTION ^

 exportToExcelFormat
   Exports a model structure to the Microsoft Excel model format

 Input:
   model       a model structure
   fileName    file name of the Excel file. Only xlsx format is supported.
               In order to preserve backward compatibility this could also
               be only a path, in which case the model is exported to a
               set of tab-delimited text files via exportToTabDelimited.
               A dialog window will open if fileName is empty.
   sortIds     logical whether metabolites, reactions and genes should be
               sorted alphabetically by their identifiers (optional,
               default false)

 No checks are made regarding the correctness of the model. Use
 checkModelStruct to identify problems in the model structure.

 Usage: exportToExcelFormat(model, fileName, sortIds)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function exportToExcelFormat(model,fileName,sortIds)
0002 % exportToExcelFormat
0003 %   Exports a model structure to the Microsoft Excel model format
0004 %
0005 % Input:
0006 %   model       a model structure
0007 %   fileName    file name of the Excel file. Only xlsx format is supported.
0008 %               In order to preserve backward compatibility this could also
0009 %               be only a path, in which case the model is exported to a
0010 %               set of tab-delimited text files via exportToTabDelimited.
0011 %               A dialog window will open if fileName is empty.
0012 %   sortIds     logical whether metabolites, reactions and genes should be
0013 %               sorted alphabetically by their identifiers (optional,
0014 %               default false)
0015 %
0016 % No checks are made regarding the correctness of the model. Use
0017 % checkModelStruct to identify problems in the model structure.
0018 %
0019 % Usage: exportToExcelFormat(model, fileName, sortIds)
0020 
0021 if nargin<2 || isempty(fileName)
0022     [fileName, pathName] = uiputfile('*.xlsx', 'Select file for model export',[model.id '.xlsx']);
0023     if fileName == 0
0024         error('You should provide a file location')
0025     else
0026         fileName = fullfile(pathName,fileName);
0027     end
0028 end
0029 fileName=char(fileName);
0030 if nargin<3
0031     sortIds=false;
0032 end
0033 if sortIds==true
0034     model=sortIdentifiers(model);
0035 end
0036 
0037 addList = matlab.addons.installedAddons;
0038 if any(strcmpi(addList.Name,'Text Analytics Toolbox'))
0039     error(['exportToExcelFormat is incompatible with MATLAB Text Analytics Toolbox. ' ...
0040            'Further instructions => https://github.com/SysBioChalmers/RAVEN/issues/55#issuecomment-1514369299'])
0041 end
0042 
0043 [~, A, B]=fileparts(fileName);
0044 
0045 %If a path was used call on exportToTabDelimited instead
0046 if ~any(A) || ~any(B)
0047     exportToTabDelimited(model,fileName);
0048     return;
0049 end
0050 
0051 if ~strcmpi(B,'.xlsx')
0052     EM='As of RAVEN version 1.9, only export to xlsx format is supported';
0053     dispEM(EM);
0054 end
0055 
0056 import java.io.File;
0057 import java.io.FileOutputStream;
0058 import java.io.IOException;
0059 
0060 %Remove the output file if it already exists
0061 if isfile(fileName)
0062     delete(fileName);
0063 end
0064 
0065 %Load an empty workbook
0066 wb=loadWorkbook(fileName,true);
0067 
0068 %Construct equations
0069 model.equations=constructEquations(model,model.rxns,true);
0070 
0071 %Check if it should print genes
0072 if isfield(model,'grRules')
0073     rules=model.grRules;
0074 else
0075     rules=[];
0076 end
0077 
0078 %Check if the model has default upper/lower bounds. This determines if
0079 %those values should be printed or not
0080 hasDefaultLB=false;
0081 hasDefaultUB=false;
0082 if isfield(model,'annotation')
0083     if isfield(model.annotation,'defaultLB')
0084         hasDefaultLB=true;
0085     end
0086     if isfield(model.annotation,'defaultUB')
0087         hasDefaultUB=true;
0088     end
0089 end
0090 
0091 %Add the RXNS sheet
0092 
0093 %Create the header row
0094 headers={'#';'ID';'NAME';'EQUATION';'EC-NUMBER';'GENE ASSOCIATION';'LOWER BOUND';'UPPER BOUND';'OBJECTIVE';'COMPARTMENT';'MIRIAM';'SUBSYSTEM';'REPLACEMENT ID';'NOTE';'REFERENCE';'CONFIDENCE SCORE'};
0095 
0096 %Add empty comments
0097 emptyColumn=cell(numel(model.rxns),1);
0098 rxnSheet=emptyColumn;
0099 
0100 %Add the model fields
0101 rxnSheet=[rxnSheet model.rxns];
0102 
0103 if isfield(model,'rxnNames')
0104     rxnSheet=[rxnSheet model.rxnNames];
0105 else
0106     rxnSheet=[rxnSheet emptyColumn];
0107 end
0108 
0109 rxnSheet=[rxnSheet model.equations];
0110 
0111 if isfield(model,'eccodes')
0112     rxnSheet=[rxnSheet model.eccodes];
0113 else
0114     rxnSheet=[rxnSheet emptyColumn];
0115 end
0116 
0117 if ~isempty(rules)
0118     rxnSheet=[rxnSheet rules];
0119 else
0120     rxnSheet=[rxnSheet emptyColumn];
0121 end
0122 
0123 lb=emptyColumn;
0124 ub=emptyColumn;
0125 objective=emptyColumn;
0126 rxnMiriams=emptyColumn;
0127 
0128 for i=1:numel(model.rxns)
0129     if isfield(model,'lb')
0130         if hasDefaultLB==true
0131             if model.rev(i)==1
0132                 %If reversible, print only if different than defaultLB
0133                 if model.lb(i) ~= model.annotation.defaultLB
0134                     lb{i}=model.lb(i);
0135                 end
0136             else
0137                 %If irreversible, print only for non-zero values
0138                 if model.lb(i)~=0
0139                     lb{i}=model.lb(i);
0140                 end
0141             end
0142         else
0143             lb{i}=model.lb(i);
0144         end
0145     end
0146     
0147     if isfield(model,'ub')
0148         if hasDefaultUB==true
0149             if model.ub(i) ~= model.annotation.defaultUB
0150                 ub{i}=model.ub(i);
0151             end
0152         else
0153             ub{i}=model.ub(i);
0154         end
0155     end
0156     
0157     if isfield(model,'c')
0158         if model.c(i)~=0
0159             objective{i}=model.c(i);
0160         end
0161     end
0162     
0163     if isfield(model,'rxnMiriams')
0164         if ~isempty(model.rxnMiriams{i})
0165             toPrint=[];
0166             for j=1:numel(model.rxnMiriams{i}.name)
0167                 toPrint=[toPrint strtrim(model.rxnMiriams{i}.name{j}) '/' strtrim(model.rxnMiriams{i}.value{j}) ';'];
0168             end
0169             rxnMiriams{i}=toPrint(1:end-1);
0170         end
0171     end
0172 end
0173 
0174 rxnSheet=[rxnSheet lb];
0175 rxnSheet=[rxnSheet ub];
0176 rxnSheet=[rxnSheet objective];
0177 
0178 if isfield(model,'rxnComps')
0179     rxnSheet=[rxnSheet model.comps(model.rxnComps)];
0180 else
0181     rxnSheet=[rxnSheet emptyColumn];
0182 end
0183 
0184 rxnSheet=[rxnSheet rxnMiriams];
0185 
0186 subsystems='';
0187 if isfield(model,'subSystems')
0188     for i=1:numel(model.subSystems)
0189         if ~isempty(model.subSystems{i,1})
0190             subsystems{i,1}=strjoin(model.subSystems{i,1},';');
0191         else
0192             subsystems{i,1}='';
0193         end
0194     end
0195     rxnSheet=[rxnSheet subsystems];
0196 else
0197     rxnSheet=[rxnSheet emptyColumn];
0198 end
0199 
0200 %For REPLACEMENT ID which isn't in the model
0201 rxnSheet=[rxnSheet emptyColumn];
0202 
0203 if isfield(model,'rxnNotes')
0204     rxnSheet=[rxnSheet model.rxnNotes];
0205 else
0206     rxnSheet=[rxnSheet emptyColumn];
0207 end
0208 
0209 if isfield(model,'rxnReferences')
0210     rxnSheet=[rxnSheet model.rxnReferences];
0211 else
0212     rxnSheet=[rxnSheet emptyColumn];
0213 end
0214 
0215 if isfield(model,'rxnConfidenceScores')
0216     rxnSheet=[rxnSheet num2cell(model.rxnConfidenceScores)];
0217 else
0218     rxnSheet=[rxnSheet emptyColumn];
0219 end
0220 
0221 wb=writeSheet(wb,'RXNS',0,headers,[],rxnSheet);
0222 
0223 headers={'#';'ID';'NAME';'UNCONSTRAINED';'MIRIAM';'COMPOSITION';'InChI';'COMPARTMENT';'REPLACEMENT ID';'CHARGE'};
0224 
0225 metSheet=cell(numel(model.mets),numel(headers));
0226 
0227 for i=1:numel(model.mets)
0228     metSheet{i,2}=[model.metNames{i} '[' model.comps{model.metComps(i)} ']'];
0229     
0230     if isfield(model,'metNames')
0231         metSheet(i,3)=model.metNames(i);
0232     end
0233     
0234     if isfield(model,'unconstrained')
0235         if model.unconstrained(i)~=0
0236             metSheet{i,4}=true;
0237         end
0238     end
0239     
0240     if isfield(model,'metMiriams')
0241         if ~isempty(model.metMiriams{i})
0242             toPrint=[];
0243             for j=1:numel(model.metMiriams{i}.name)
0244                 toPrint=[toPrint strtrim(model.metMiriams{i}.name{j}) '/' strtrim(model.metMiriams{i}.value{j}) ';'];
0245             end
0246             metSheet{i,5}=toPrint(1:end-1);
0247         end
0248     end
0249     
0250     % Making sure that only these metFormulas are exported, which don't
0251     % have InChI strings
0252     if isfield(model,'metFormulas')
0253         if isfield(model,'inchis')
0254             if isempty(model.inchis{i})
0255                 metSheet(i,6)=model.metFormulas(i);
0256             end
0257         else
0258             metSheet(i,6)=model.metFormulas(i);
0259         end
0260     end
0261     
0262     if isfield(model,'inchis')
0263         metSheet(i,7)=model.inchis(i);
0264     end
0265     
0266     if isfield(model,'metComps')
0267         metSheet(i,8)=model.comps(model.metComps(i));
0268     end
0269     
0270     metSheet(i,9)=model.mets(i);
0271     
0272     if isfield(model,'metCharges')
0273         metSheet{i,10}=model.metCharges(i);
0274     end
0275 end
0276 
0277 wb=writeSheet(wb,'METS',1,headers,[],metSheet);
0278 
0279 %Add the COMPS sheet
0280 
0281 %Create the header row
0282 headers={'#';'ABBREVIATION';'NAME';'INSIDE';'MIRIAM'};
0283 
0284 compSheet=cell(numel(model.comps),numel(headers));
0285 
0286 for i=1:numel(model.comps)
0287     compSheet(i,2)=model.comps(i);
0288     
0289     if isfield(model,'compNames')
0290         compSheet(i,3)=model.compNames(i);
0291     end
0292     
0293     if isfield(model,'compOutside')
0294         compSheet(i,4)=model.compOutside(i);
0295     end
0296     
0297     if isfield(model,'compMiriams')
0298         if ~isempty(model.compMiriams{i})
0299             toPrint=[];
0300             for j=1:numel(model.compMiriams{i}.name)
0301                 toPrint=[toPrint strtrim(model.compMiriams{i}.name{j}) '/' strtrim(model.compMiriams{i}.value{j}) ';'];
0302             end
0303             compSheet{i,5}=toPrint(1:end-1);
0304         end
0305     end
0306 end
0307 
0308 wb=writeSheet(wb,'COMPS',2,headers,[],compSheet);
0309 
0310 %Add the GENES sheet
0311 if isfield(model,'genes')
0312     %Create the header row
0313     headers={'#';'NAME';'MIRIAM';'SHORT NAME';'COMPARTMENT'};
0314     
0315     geneSheet=cell(numel(model.genes),numel(headers));
0316     
0317     for i=1:numel(model.genes)
0318         geneSheet(i,2)=model.genes(i);
0319         
0320         if isfield(model,'geneMiriams')
0321             if ~isempty(model.geneMiriams{i})
0322                 toPrint=[];
0323                 for j=1:numel(model.geneMiriams{i}.name)
0324                     toPrint=[toPrint strtrim(model.geneMiriams{i}.name{j}) '/' strtrim(model.geneMiriams{i}.value{j}) ';'];
0325                 end
0326                 geneSheet{i,3}=toPrint(1:end-1);
0327             end
0328         end
0329         if isfield(model,'geneShortNames')
0330             geneSheet(i,4)=model.geneShortNames(i);
0331         end
0332         if isfield(model,'geneComps')
0333             geneSheet(i,5)=model.comps(model.geneComps(i));
0334         end
0335     end
0336     
0337     wb=writeSheet(wb,'GENES',3,headers,[],geneSheet);
0338 end
0339 
0340 %Add the MODEL sheet
0341 
0342 %Create the header row
0343 headers={'#';'ID';'NAME';'TAXONOMY';'DEFAULT LOWER';'DEFAULT UPPER';'CONTACT GIVEN NAME';'CONTACT FAMILY NAME';'CONTACT EMAIL';'ORGANIZATION';'NOTES'};
0344 
0345 modelSheet=cell(1,numel(headers));
0346 
0347 if ~isfield(model,'annotation')
0348     model.annotation = [];
0349 end
0350 
0351 if isfield(model,'id')
0352     modelSheet{1,2}=model.id;
0353 else
0354     modelSheet{1,2}='blankID';
0355 end
0356 if isfield(model,'name')
0357     modelSheet{1,3}=model.name;
0358 else
0359     modelSheet{1,3}='blankName';
0360 end
0361 if isfield(model.annotation,'taxonomy')
0362     modelSheet{1,4}=model.annotation.taxonomy;
0363 end
0364 if isfield(model.annotation,'defaultLB')
0365     modelSheet{1,5}=model.annotation.defaultLB;
0366 end
0367 if isfield(model.annotation,'defaultUB')
0368     modelSheet{1,6}=model.annotation.defaultUB;
0369 end
0370 if isfield(model.annotation,'givenName')
0371     modelSheet{1,7}=model.annotation.givenName;
0372 end
0373 if isfield(model.annotation,'familyName')
0374     modelSheet{1,8}=model.annotation.familyName;
0375 end
0376 if isfield(model.annotation,'email')
0377     modelSheet{1,9}=model.annotation.email;
0378 end
0379 if isfield(model.annotation,'organization')
0380     modelSheet{1,10}=model.annotation.organization;
0381 end
0382 if isfield(model.annotation,'note')
0383     modelSheet{1,11}=model.annotation.note;
0384 end
0385 
0386 if isfield(model,'genes')
0387     wb=writeSheet(wb,'MODEL',4,headers,[],modelSheet);
0388 else
0389     wb=writeSheet(wb,'MODEL',3,headers,[],modelSheet);
0390 end
0391 
0392 %Open the output stream
0393 out = FileOutputStream(fileName);
0394 wb.write(out);
0395 out.close();
0396 end

Generated by m2html © 2005