Home > RAVEN > exportToExcelFormat.m

exportToExcelFormat

PURPOSE ^

exportToExcelFormat

SYNOPSIS ^

function exportToExcelFormat(model,filename)

DESCRIPTION ^

 exportToExcelFormat
   Exports a model structure to the Microsoft Excel model format

   model       a model structure
   filename    file name of the Excel file. Both xls and xlsx are 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

   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)

   Rasmus Agren, 2013-10-09

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function exportToExcelFormat(model,filename)
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. Both xls and xlsx are 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
0011 %
0012 %   The resulting Excel file can be used with importExcelModel/SBMLFromExcel
0013 %   for modelling or to generate a SBML file.
0014 %
0015 %   NOTE: No checks are made regarding the correctness of the model. Use
0016 %         checkModelStruct to identify problems in the model structure
0017 %
0018 %   Usage: exportToExcelFormat(model,filename)
0019 %
0020 %   Rasmus Agren, 2013-10-09
0021 %
0022 
0023 [filePath, A, B]=fileparts(filename);
0024 
0025 if ~any(filePath)
0026    filePath=pwd; 
0027 end
0028 
0029 %If a path was used call on exportToTabDelimited instead
0030 if ~any(A) || ~any(B)
0031     exportToTabDelimited(model,filename);
0032     return;
0033 end
0034 
0035 %Adds the required classes to the Java path
0036 [ST, I]=dbstack('-completenames');
0037 ravenPath=fileparts(ST(I).file);
0038 poiPATH=fullfile(ravenPath,'software','apache-poi');
0039 javaaddpath(fullfile(poiPATH,'dom4j-1.6.1.jar'));
0040 javaaddpath(fullfile(poiPATH,'poi-3.8-20120326.jar'));
0041 javaaddpath(fullfile(poiPATH,'poi-ooxml-3.8-20120326.jar'));
0042 javaaddpath(fullfile(poiPATH,'poi-ooxml-schemas-3.8-20120326.jar'));
0043 javaaddpath(fullfile(poiPATH,'xmlbeans-2.3.0.jar'));
0044 
0045 %Import required classes from Apache POI
0046 import org.apache.poi.ss.usermodel.*;
0047 import org.apache.poi.hssf.usermodel.*;
0048 import org.apache.poi.xssf.usermodel.*;
0049 import org.apache.poi.ss.usermodel.*;
0050 import org.apache.poi.ss.util.*;
0051 import java.io.File;
0052 import java.io.FileOutputStream;
0053 import java.io.IOException;
0054 
0055 %If the folder doesn't exist then create it
0056 if ~exist(filePath,'dir')
0057     mkdir(filePath);
0058 end
0059 
0060 %Remove the file if it already exist
0061 if exist(filename,'file')
0062     delete(filename);
0063 end
0064 
0065 %Check that the file endings are correct
0066 if ~strcmpi(B,'.XLS') && ~strcmpi(B,'.XLSX')
0067    dispEM('The file name must end in .xls or .xlsx'); 
0068 end
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     %Also do some parsing here
0076     rules=model.grRules;
0077     rules=strrep(rules,'(','');
0078     rules=strrep(rules,')','');
0079     rules=strrep(rules,' and ',':');
0080     rules=strrep(rules,' or ',';');
0081 else
0082     rules=[];
0083 end
0084 
0085 %Check if the model has default upper/lower bounds. This determines if
0086 %those values should be printed or not
0087 hasDefaultLB=false;
0088 hasDefaultUB=false;
0089 if isfield(model,'annotation')
0090     if isfield(model.annotation,'defaultLB')
0091        hasDefaultLB=true; 
0092     end
0093     if isfield(model.annotation,'defaultUB')
0094        hasDefaultUB=true; 
0095     end
0096 end
0097 
0098 %Create the workbook
0099 if strcmpi(B,'.XLS')
0100     wb=HSSFWorkbook();
0101 else
0102     wb=XSSFWorkbook();
0103 end
0104 
0105 %Add the RXNS sheet
0106 s=wb.createSheet();
0107 wb.setSheetName(0, 'RXNS');
0108 
0109 %Create the header row
0110 headers={'#';'ID';'NAME';'EQUATION';'EC-NUMBER';'GENE ASSOCIATION';'LOWER BOUND';'UPPER BOUND';'OBJECTIVE';'COMPARTMENT';'MIRIAM';'SUBSYSTEM';'REPLACEMENT ID'};
0111 r=s.createRow(0);
0112 for i=0:numel(headers)-1
0113     c=r.createCell(i);
0114     c.setCellValue(headers{i+1});    
0115 end
0116 
0117 %Then fill in the sheet
0118 for i=1:numel(model.rxns)
0119     r=s.createRow(i);
0120     
0121     c=r.createCell(1);
0122     c.setCellValue(model.rxns{i});
0123     
0124     if isfield(model,'rxnNames')
0125         c=r.createCell(2);
0126         c.setCellValue(model.rxnNames{i});
0127     end
0128 
0129     c=r.createCell(3);
0130     c.setCellValue(model.equations{i});
0131     
0132     if isfield(model,'eccodes')
0133         c=r.createCell(4);
0134         c.setCellValue(model.eccodes{i});
0135     end
0136     
0137     if ~isempty(rules)
0138         c=r.createCell(5);
0139         c.setCellValue(rules{i});
0140     end
0141     
0142     if isfield(model,'lb')
0143         if hasDefaultLB==true
0144             if model.rev(i)==1
0145                 %If reversible, print only if different than defaultLB
0146                 if model.lb(i) ~= model.annotation.defaultLB
0147                     c=r.createCell(6);
0148                     c.setCellValue(model.lb(i));
0149                 end
0150             else
0151                 %If irreversible, print only for non-zero values
0152                 if model.lb(i)~=0
0153                     c=r.createCell(6);
0154                     c.setCellValue(model.lb(i));
0155                 end
0156             end
0157         else
0158             c=r.createCell(6);
0159             c.setCellValue(model.lb(i));
0160         end
0161     end
0162     if isfield(model,'ub')
0163         if hasDefaultUB==true
0164             if model.ub(i) ~= model.annotation.defaultUB
0165                 c=r.createCell(6);
0166                 c.setCellValue(model.ub(i));
0167             end
0168         else
0169             c=r.createCell(6);
0170             c.setCellValue(model.ub(i));
0171         end
0172     end
0173     if isfield(model,'c')
0174         if model.c(i)~=0
0175             c=r.createCell(8);
0176             c.setCellValue(model.c(i));
0177         end
0178     end
0179     if isfield(model,'rxnComps')
0180         c=r.createCell(9);
0181         c.setCellValue(model.comps{model.rxnComps(i)});
0182     end
0183     if isfield(model,'rxnMiriams')
0184        if ~isempty(model.rxnMiriams{i})
0185            toPrint=[];
0186            for j=1:numel(model.rxnMiriams{i}.name)
0187                toPrint=[toPrint strtrim(model.rxnMiriams{i}.name{j}) ':' strtrim(model.rxnMiriams{i}.value{j}) ';']; 
0188            end
0189            c=r.createCell(10);
0190            c.setCellValue(toPrint(1:end-1));
0191        end 
0192     end
0193     if isfield(model,'subSystems')
0194         c=r.createCell(11);
0195         c.setCellValue(model.subSystems{i});
0196     end
0197 end
0198 
0199 %Add the METS sheet
0200 s=wb.createSheet();
0201 wb.setSheetName(1, 'METS');
0202 
0203 %Create the header row
0204 headers={'#';'ID';'NAME';'UNCONSTRAINED';'MIRIAM';'COMPOSITION';'InChI';'COMPARTMENT';'REPLACEMENT ID'};
0205 r=s.createRow(0);
0206 for i=0:numel(headers)-1
0207     c=r.createCell(i);
0208     c.setCellValue(headers{i+1});    
0209 end
0210 
0211 for i=1:numel(model.mets)
0212     r=s.createRow(i);
0213     
0214     c=r.createCell(1);
0215     c.setCellValue([model.metNames{i} '[' model.comps{model.metComps(i)} ']']);
0216     
0217     if isfield(model,'metNames')
0218         c=r.createCell(2);
0219         c.setCellValue(model.metNames{i});
0220     end
0221     
0222     if isfield(model,'unconstrained')
0223         if model.unconstrained(i)~=0
0224             c=r.createCell(3);
0225             c.setCellValue(true);
0226         end
0227     end
0228     
0229     if isfield(model,'metMiriams')
0230        if ~isempty(model.metMiriams{i})
0231            toPrint=[];
0232            for j=1:numel(model.metMiriams{i}.name)
0233                toPrint=[toPrint strtrim(model.metMiriams{i}.name{j}) ':' strtrim(model.metMiriams{i}.value{j}) ';']; 
0234            end
0235            c=r.createCell(4);
0236            c.setCellValue(toPrint(1:end-1));
0237        end 
0238     end
0239     
0240     if isfield(model,'metFormulas')
0241         c=r.createCell(5);
0242         c.setCellValue(model.metFormulas{i});
0243     end
0244     
0245     if isfield(model,'inchis')
0246         c=r.createCell(6);
0247         c.setCellValue(model.inchis{i});
0248     end
0249     
0250     if isfield(model,'metComps')
0251         c=r.createCell(7);
0252         c.setCellValue(model.comps{model.metComps(i)});
0253     end
0254     
0255     c=r.createCell(8);
0256     c.setCellValue(model.mets{i});
0257 end
0258 
0259 %Add the COMPS sheet
0260 s=wb.createSheet();
0261 wb.setSheetName(2, 'COMPS');
0262 
0263 %Create the header row
0264 headers={'#';'ABBREVIATION';'NAME';'INSIDE';'MIRIAM'};
0265 r=s.createRow(0);
0266 for i=0:numel(headers)-1
0267     c=r.createCell(i);
0268     c.setCellValue(headers{i+1});    
0269 end
0270 
0271 for i=1:numel(model.comps)
0272     r=s.createRow(i);
0273     
0274     c=r.createCell(1);
0275     c.setCellValue(model.comps{i});
0276     
0277     if isfield(model,'compNames')
0278         c=r.createCell(2);
0279         c.setCellValue(model.compNames{i});
0280     end
0281     
0282     if isfield(model,'compOutside')
0283         c=r.createCell(3);
0284         c.setCellValue(model.compOutside{i});
0285     end
0286     
0287     if isfield(model,'compMiriams')
0288        if ~isempty(model.compMiriams{i})
0289            toPrint=[];
0290            for j=1:numel(model.compMiriams{i}.name)
0291                toPrint=[toPrint strtrim(model.compMiriams{i}.name{j}) ':' strtrim(model.compMiriams{i}.value{j}) ';']; 
0292            end
0293            c=r.createCell(4);
0294            c.setCellValue(toPrint(1:end-1));
0295        end 
0296     end
0297 end
0298 
0299 %Add the GENES sheet
0300 addedGeneSheet=-1; %This is to get the sheet numbering right if no genes are added
0301 if isfield(model,'genes')
0302     s=wb.createSheet();
0303     wb.setSheetName(3, 'GENES');
0304     addedGeneSheet=0;
0305 
0306     %Create the header row
0307     headers={'#';'NAME';'MIRIAM';'SHORT NAME';'COMPARTMENT'};
0308     r=s.createRow(0);
0309     for i=0:numel(headers)-1
0310         c=r.createCell(i);
0311         c.setCellValue(headers{i+1});    
0312     end
0313 
0314     for i=1:numel(model.genes)
0315         r=s.createRow(i);
0316 
0317         c=r.createCell(1);
0318         c.setCellValue(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                c=r.createCell(2);
0327                c.setCellValue(toPrint(1:end-1));
0328            end 
0329        end
0330        
0331        if isfield(model,'geneComps')
0332            c=r.createCell(4);
0333            c.setCellValue(model.comps{model.geneComps(i)});
0334        end
0335     end
0336 end
0337 
0338 %Add the MODEL sheet
0339 s=wb.createSheet();
0340 wb.setSheetName(4-addedGeneSheet, 'MODEL');
0341 
0342 %Create the header row
0343 headers={'#';'ID';'DESCRIPTION';'DEFAULT LOWER';'DEFAULT UPPER';'CONTACT GIVEN NAME';'CONTACT FAMILY NAME';'CONTACT EMAIL';'ORGANIZATION';'TAXONOMY';'NOTES'};
0344 r=s.createRow(0);
0345 for i=0:numel(headers)-1
0346     c=r.createCell(i);
0347     c.setCellValue(headers{i+1});    
0348 end
0349 
0350 %Add some default stuff if needed
0351 if ~isfield(model,'annotation')
0352    model.annotation.familyName='Agren';
0353    model.annotation.givenName='Rasmus';
0354    model.annotation.email='rasmus.j.agren@gmail.com';
0355    model.annotation.organization='Chalmers University of Technology';
0356 end
0357 
0358 r=s.createRow(1);
0359 a=model.annotation;
0360 
0361 if isfield(model,'id')
0362     c=r.createCell(1);
0363     c.setCellValue(model.id);
0364 end
0365 if isfield(model,'description')
0366     c=r.createCell(2);
0367     c.setCellValue(model.description);
0368 end
0369 if isfield(a,'defaultLB')
0370     c=r.createCell(3);
0371     c.setCellValue(a.defaultLB);
0372 end
0373 if isfield(a,'defaultUB')
0374     c=r.createCell(4);
0375     c.setCellValue(a.defaultUB);
0376 end
0377 if isfield(a,'givenName')
0378     c=r.createCell(5);
0379     c.setCellValue(a.givenName);
0380 end
0381 if isfield(a,'familyName')
0382     c=r.createCell(6);
0383     c.setCellValue(a.familyName);
0384 end
0385 if isfield(a,'email')
0386     c=r.createCell(7);
0387     c.setCellValue(a.email);
0388 end
0389 if isfield(a,'organization')
0390     c=r.createCell(8);
0391     c.setCellValue(a.organization);
0392 end
0393 if isfield(a,'taxonomy')
0394     c=r.createCell(9);
0395     c.setCellValue(a.taxonomy);
0396 end
0397 if isfield(a,'note')
0398     c=r.createCell(10);
0399     c.setCellValue(a.note);
0400 end
0401         
0402 %Open the output stream
0403 out = FileOutputStream(filename);
0404 wb.write(out);
0405 out.close();
0406 end

Generated on Mon 06-Jan-2014 14:58:12 by m2html © 2005