writeCbModel

PURPOSE ^

writeCbModel Write out COBRA models in various formats

SYNOPSIS ^

function writeCbModel(model,format,fileName,compSymbolList,compNameList,sbmlLevel,sbmlVersion)

DESCRIPTION ^

writeCbModel Write out COBRA models in various formats

 writeCbModel(model,format,fileName,compSymbolList,compNameList,sbmlLevel,sbmlVersion)

INPUTS
 model             Standard COBRA model structure
 format            File format to be used ('text','xls' or 'sbml')

OPTIONAL INPUTS
 fileName          File name for output file (optional, default opens
                   dialog box)
 compSymbolList    List of compartment symbols
 compNameList      List of compartment names corresponding to compSymbolList
 sbmlLevel         SBML Level (default = 2)
 sbmlVersion       SBML Version (default = 1)

 Markus Herrgard 2/5/07
 Ines Thiele 01/10 - Added more options for field to write in xls format
 Richard Que (3/17/10) Added ability to specify compartment names and
                       symbols

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function writeCbModel(model,format,fileName,compSymbolList,compNameList,sbmlLevel,sbmlVersion)
0002 %writeCbModel Write out COBRA models in various formats
0003 %
0004 % writeCbModel(model,format,fileName,compSymbolList,compNameList,sbmlLevel,sbmlVersion)
0005 %
0006 %INPUTS
0007 % model             Standard COBRA model structure
0008 % format            File format to be used ('text','xls' or 'sbml')
0009 %
0010 %OPTIONAL INPUTS
0011 % fileName          File name for output file (optional, default opens
0012 %                   dialog box)
0013 % compSymbolList    List of compartment symbols
0014 % compNameList      List of compartment names corresponding to compSymbolList
0015 % sbmlLevel         SBML Level (default = 2)
0016 % sbmlVersion       SBML Version (default = 1)
0017 %
0018 % Markus Herrgard 2/5/07
0019 % Ines Thiele 01/10 - Added more options for field to write in xls format
0020 % Richard Que (3/17/10) Added ability to specify compartment names and
0021 %                       symbols
0022 
0023 if nargin < 4
0024     compSymbolList = {};
0025     compNameList = {};
0026 end
0027 if nargin < 6
0028     sbmlLevel = 2;
0029     sbmlVersion = 1;
0030 end
0031 
0032 [nMets,nRxns] = size(model.S);
0033 
0034 formulas = printRxnFormula(model,model.rxns,false,false,false,1,false);
0035 
0036 %% Open a dialog to select file name
0037 if (nargin < 3 & ~strcmp(format,'sbml'))
0038     switch format
0039         case 'xls'
0040             [fileNameFull,filePath] = uiputfile({'*.xls'});
0041         case {'text','txt'}
0042             [fileNameFull,filePath] = uiputfile({'*.txt'});
0043         case 'xml'
0044             [fileNameFull,filePath] = uiputfile({'*.xml'});
0045         otherwise
0046             [fileNameFull,filePath] = uiputfile({'*'});
0047     end
0048     if (fileNameFull)
0049         [t1,t2,t3,t4,tokens] = regexp(fileNameFull,'(\w*)\.(\w*)');
0050         fileName = [filePath tokens{1}{1}];
0051         switch tokens{1}{2}
0052             case 'xls'
0053                 format = 'xls';
0054             case 'txt'
0055                 format = 'text';
0056                 fileName = [fileName '.txt'];
0057             case 'xml'
0058                 format = 'sbml';
0059 %                 fprintf('Note that you will be asked to supply the file name again (this is a feature, not a bug)');
0060             otherwise
0061                 format = 'unknown';
0062         end
0063     else
0064         return;
0065     end
0066 end
0067 switch format
0068     %% Text file
0069     case {'text','txt'}
0070         fid = fopen(fileName,'w');
0071         fprintf(fid,'Rxn name\t');
0072         if (isfield(model,'rxnNames'))
0073             fprintf(fid,'Rxn description\t');
0074         end
0075         fprintf(fid,'Formula\t');
0076         if (isfield(model,'grRules'))
0077             fprintf(fid,'Gene-reaction association\t');
0078         end
0079         fprintf(fid,'Reversible\tLB\tUB\tObjective\n');
0080         for i = 1:nRxns
0081             fprintf(fid,'%s\t',model.rxns{i});
0082             if (isfield(model,'rxnNames'))
0083                 fprintf(fid,'%s\t',model.rxnNames{i});
0084             end
0085             fprintf(fid,'%s\t',formulas{i});
0086             if (isfield(model,'grRules'))
0087                 fprintf(fid,'%s\t',model.grRules{i});
0088             end
0089             fprintf(fid,'%d\t%6.2f\t%6.2f\t%6.2f\n',model.rev(i),model.lb(i),model.ub(i),model.c(i));
0090         end
0091         fprintf(fid,'Metabolite name\tMetabolite description\tMetabolite formula\n');
0092         for i = 1:nMets
0093             fprintf(fid,'%s',model.mets{i});
0094             if isfield(model,'metNames')
0095                 fprintf(fid,'\t%s',model.metNames{i});
0096             end
0097             if isfield(model,'metFormulas')
0098                 fprintf(fid,'\t%s',model.metFormulas{i});
0099             end
0100             fprintf(fid,'\n');
0101         end
0102         fclose(fid);
0103         %% Excel file
0104     case 'xls'
0105         tmpData{1,1} = 'Rxn name';
0106         tmpData{1,2} = 'Rxn description';
0107         baseInd = 3;
0108         tmpData{1,baseInd} = 'Formula';
0109         tmpData{1,baseInd+1} = 'Gene-reaction association';
0110         tmpData{1,baseInd+2} = 'Genes';
0111         tmpData{1,baseInd+3} = 'Proteins';
0112         tmpData{1,baseInd+4} = 'Subsystem';
0113         tmpData{1,baseInd+5} = 'Reversible';
0114         tmpData{1,baseInd+6} = 'LB';
0115         tmpData{1,baseInd+7} = 'UB';
0116         tmpData{1,baseInd+8} = 'Objective';
0117         tmpData{1,baseInd+9} = 'Confidence Score';
0118         tmpData{1,baseInd+10} = 'EC Number';
0119         tmpData{1,baseInd+11} = 'Notes';
0120         tmpData{1,baseInd+12} = 'References';
0121         for i = 1:nRxns
0122             tmpData{i+1,1} = chopForExcel(model.rxns{i});
0123             if (isfield(model,'rxnNames'))
0124                 tmpData{i+1,2} = chopForExcel(model.rxnNames{i});
0125             else
0126                 tmpData{i+1,2} =  '';
0127             end
0128             
0129             tmpData{i+1,baseInd} = chopForExcel(formulas{i});
0130             if (isfield(model,'geneNameRules'))
0131                 tmpData{i+1,baseInd+1} = chopForExcel(model.geneNameRules{i});
0132             elseif (isfield(model,'grRules'))
0133                 tmpData{i+1,baseInd+1} = chopForExcel(model.grRules{i});
0134             else
0135                 tmpData{i+1,baseInd+1} = '';
0136             end
0137             if (isfield(model,'geneNames'))
0138                 geneNames = model.geneNames(model.rxnGeneMat(i,:) == 1);
0139                 tmpData{i+1,baseInd+2} = constructGeneStr(geneNames);
0140             elseif (isfield(model,'genes'))
0141                 geneNames = model.genes(model.rxnGeneMat(i,:) == 1);
0142                 tmpData{i+1,baseInd+2} = constructGeneStr(geneNames);
0143             else
0144                 tmpData{i+1,baseInd+2} = '';
0145             end
0146             if (isfield(model,'proteins'))
0147                 tmpData{i+1,baseInd+3} = chopForExcel(model.proteins{i});
0148             else
0149                 tmpData{i+1,baseInd+3} = '';
0150             end
0151             if (isfield(model,'subSystems'))
0152                 tmpData{i+1,baseInd+4} = chopForExcel(char(model.subSystems{i}));
0153             else
0154                 tmpData{i+1,baseInd+4} = '';
0155             end
0156             tmpData{i+1,baseInd+5} = model.rev(i)*1.0;
0157             tmpData{i+1,baseInd+6} = model.lb(i);
0158             tmpData{i+1,baseInd+7} = model.ub(i);
0159             tmpData{i+1,baseInd+8} = model.c(i);
0160             if (isfield(model,'confidenceScores'))                
0161                 tmpData{i+1,baseInd+9} =  chopForExcel(num2str(model.confidenceScores{i}));           
0162             else
0163                 tmpData{i+1,baseInd+9} = '';
0164             end
0165             if (isfield(model,'rxnECNumbers'))
0166                 tmpData{i+1,baseInd+10} = chopForExcel(model.rxnECNumbers{i});
0167             else
0168                 tmpData{i+1,baseInd+10} = '';
0169             end
0170             if (isfield(model,'rxnNotes'))
0171                 tmpData{i+1,baseInd+11} = chopForExcel(char(model.rxnNotes{i}));
0172             else
0173                 tmpData{i+1,baseInd+11} = '';
0174             end
0175             if (isfield(model,'rxnReferences'))
0176                 tmpData{i+1,baseInd+12} = chopForExcel(char(model.rxnReferences{i}));
0177             else
0178                 tmpData{i+1,baseInd+12} = '';
0179             end
0180         end
0181         %keyboard
0182         xlswrite(fileName,tmpData,'reactions');
0183         if isfield(model,'metNames')
0184             tmpMetData{1,1} = 'Metabolite name';
0185             tmpMetData{1,2} = 'Metabolite description';
0186             tmpMetData{1,3} = 'Metabolite neutral formula';
0187             tmpMetData{1,4} = 'Metabolite charged formula';
0188             tmpMetData{1,5} = 'Metabolite charge';
0189             tmpMetData{1,6} = 'Metabolite Compartment';
0190             tmpMetData{1,7} = 'Metabolite KEGGID';
0191             tmpMetData{1,8} = 'Metabolite PubChemID';
0192             tmpMetData{1,9} = 'Metabolite CheBI ID';
0193             tmpMetData{1,10} = 'Metabolite Inchi String';
0194             tmpMetData{1,11} = 'Metabolite Smile';
0195             for i = 1:nMets
0196                 tmpMetData{i+1,1} = chopForExcel(model.mets{i});
0197                 tmpMetData{i+1,2} = chopForExcel(model.metNames{i});
0198                 if isfield(model,'metFormulasNeutral')
0199                     tmpMetData{i+1,3} = chopForExcel(model.metFormulasNeutral{i});
0200                 else
0201                     tmpMetData{i+1,3} = '';
0202                 end
0203                 if isfield(model,'metFormulas')
0204                     tmpMetData{i+1,4} = chopForExcel(model.metFormulas{i});
0205                 else
0206                     tmpMetData{i+1,4} = '';
0207                 end
0208                 if isfield(model,'metCharge')
0209                     tmpMetData{i+1,5} = chopForExcel(model.metCharge(i));
0210                 else
0211                     tmpMetData{i+1,5} = '';
0212                 end
0213                 if isfield(model,'metCompartment')
0214                     tmpMetData{i+1,6} = chopForExcel(model.metCompartment{i});
0215                 else
0216                     tmpMetData{i+1,6} = '';
0217                 end
0218                 if isfield(model,'metKEGGID')
0219                     tmpMetData{i+1,7} = chopForExcel(model.metKEGGID{i});
0220                 else
0221                     tmpMetData{i+1,7} = '';
0222                 end
0223                 if isfield(model,'metPubChemID')
0224                     if iscell(model.metPubChemID(i))                        
0225                     tmpMetData{i+1,8} = chopForExcel(model.metPubChemID{i});
0226                     else
0227                     tmpMetData{i+1,8} = chopForExcel(model.metPubChemID(i));
0228                     end
0229                 else
0230                     tmpMetData{i+1,8} = '';
0231                 end
0232                 if isfield(model,'metChEBIID')
0233                  
0234                     tmpMetData{i+1,9} = chopForExcel(model.metChEBIID(i));
0235                 else
0236                     tmpMetData{i+1,9} = '';
0237                 end
0238                 if isfield(model,'metInchiString')
0239                     tmpMetData{i+1,10} = chopForExcel(model.metInchiString{i});
0240                 else
0241                     tmpMetData{i+1,10} = '';
0242                 end
0243                 if isfield(model,'metSmiles')
0244                     tmpMetData{i+1,11} = chopForExcel(model.metSmiles{i});
0245                 else
0246                     tmpMetData{i+1,11} = '';
0247                 end
0248             end
0249             xlswrite(fileName,tmpMetData,'metabolites');
0250         else
0251             xlswrite(fileName,model.mets,'metabolites');
0252         end
0253         %% SBML
0254     case 'sbml'
0255         sbmlModel = convertCobraToSBML(model,sbmlLevel,sbmlVersion,compSymbolList,compNameList);
0256         if exist('fileName','var')&&~isempty(fileName)
0257             OutputSBML(sbmlModel,fileName);
0258         else
0259             OutputSBML(sbmlModel);
0260         end
0261         %% Unknown
0262     otherwise
0263         error('Unknown file format');
0264 end
0265 
0266 %% Chop strings for excel output
0267 function strOut = chopForExcel(str)
0268 
0269 if (length(str) > 5000)
0270     strOut = str(1:5000);
0271     fprintf('String longer than 5000 characters - truncated for Excel output\n%s\n',str);
0272 else
0273     strOut = str;
0274 end
0275 
0276 %% Construct gene name string
0277 function geneStr = constructGeneStr(geneNames)
0278 
0279 geneStr = '';
0280 for i = 1:length(geneNames)
0281     geneStr = [geneStr ' ' geneNames{i}];
0282 end
0283 geneStr = strtrim(geneStr);

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003