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)

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

Generated by m2html © 2005