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

   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 instead. See exportToTabDelimited
               for details regarding that functionality. A dialog window
               will open if no file name is specified.
   sortIds     logical whether metabolites, reactions and genes should be
               sorted alphabetically by their identifiers (opt, default
               false)

   The resulting Excel file can be used with importExcelModel/SBMLFromExcel
   for modelling or to generate a SBML file.

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

Generated by m2html © 2005