Home > io > exportForGit.m

exportForGit

PURPOSE ^

exportForGit

SYNOPSIS ^

function out=exportForGit(model,prefix,path,formats,mainBranchFlag,subDirs,cobraText)

DESCRIPTION ^

 exportForGit
   Generates a directory structure and populates this with model files, ready
   to be commited to a Git(Hub) maintained model repository. Writes the model
   as SBML L3V1 FBCv2 (both XML and YAML), COBRA text, Matlab MAT-file
   orthologies in KEGG

   model               model structure in RAVEN format that should be exported
   prefix              prefix for all filenames (opt, default 'model')
   path                path where the directory structure should be generated
                       and populated with all files (opt, default to current
                       working directory)
   formats             cell array of strings specifying in what file formats
                       the model should be exported (opt, default to all
                       formats as {'mat', 'txt', 'xlsx', 'xml', 'yml'})
   mainBranchFlag      logical, if true, function will error if RAVEN (and
                       COBRA if detected) is/are not on the main branch.
                       (opt, default false)
   subDirs             logical, whether model files for each file format 
                       should be written in its own subdirectory, with
                       'model' as parent directory, in accordance to the
                       standard-GEM repository format. If false, all files
                       are stored in the same folder. (opt, default true)
   cobraText           logical, whether the txt file should be in COBRA
                       Toolbox format using metabolite IDs, instead of
                       metabolite names and compartments. (opt, default
                       false)

   Usage: exportForGit(model,prefix,path,formats,mainBranchFlag)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function out=exportForGit(model,prefix,path,formats,mainBranchFlag,subDirs,cobraText)
0002 % exportForGit
0003 %   Generates a directory structure and populates this with model files, ready
0004 %   to be commited to a Git(Hub) maintained model repository. Writes the model
0005 %   as SBML L3V1 FBCv2 (both XML and YAML), COBRA text, Matlab MAT-file
0006 %   orthologies in KEGG
0007 %
0008 %   model               model structure in RAVEN format that should be exported
0009 %   prefix              prefix for all filenames (opt, default 'model')
0010 %   path                path where the directory structure should be generated
0011 %                       and populated with all files (opt, default to current
0012 %                       working directory)
0013 %   formats             cell array of strings specifying in what file formats
0014 %                       the model should be exported (opt, default to all
0015 %                       formats as {'mat', 'txt', 'xlsx', 'xml', 'yml'})
0016 %   mainBranchFlag      logical, if true, function will error if RAVEN (and
0017 %                       COBRA if detected) is/are not on the main branch.
0018 %                       (opt, default false)
0019 %   subDirs             logical, whether model files for each file format
0020 %                       should be written in its own subdirectory, with
0021 %                       'model' as parent directory, in accordance to the
0022 %                       standard-GEM repository format. If false, all files
0023 %                       are stored in the same folder. (opt, default true)
0024 %   cobraText           logical, whether the txt file should be in COBRA
0025 %                       Toolbox format using metabolite IDs, instead of
0026 %                       metabolite names and compartments. (opt, default
0027 %                       false)
0028 %
0029 %   Usage: exportForGit(model,prefix,path,formats,mainBranchFlag)
0030 if nargin<7
0031     cobraText=false;
0032 end
0033 if nargin<6
0034     subDirs=true;
0035 end
0036 if nargin<5
0037     mainBranchFlag=false;
0038 end
0039 if nargin<4 || isempty(formats)
0040     formats={'mat', 'txt', 'xlsx', 'xml', 'yml'};
0041 else
0042     formats=convertCharArray(formats);
0043 end
0044 if any(~ismember(formats, {'mat', 'txt', 'xlsx', 'xml', 'yml'}))
0045     EM='Unknown file format defined. Only mat, txt, xlsx, xml and yml are allowed file formats.';
0046     error(EM)
0047 end
0048 if nargin<3
0049     path='.';
0050 else
0051     path=char(path);
0052 end
0053 if nargin<2
0054     prefix='model';
0055 else
0056     prefix=char(prefix);
0057 end
0058 
0059 %Sort reactions, metabolites and genes alphabetically
0060 model=sortIdentifiers(model);
0061 
0062 %Get versions or commits of toolboxes:
0063 RAVENver = getToolboxVersion('RAVEN','ravenCobraWrapper.m',mainBranchFlag);
0064 COBRAver = getToolboxVersion('COBRA','initCobraToolbox.m',mainBranchFlag);
0065 
0066 %Retrieve libSBML version:
0067 [ravenDir,prevDir]=findRAVENroot();
0068 cd(fullfile(ravenDir,'software','libSBML'));
0069 try % 5.17.0 and newer
0070     libSBMLver=OutputSBML;
0071     libSBMLver=libSBMLver.libSBML_version_string;
0072 catch % before 5.17.0
0073     fid = fopen('tempModelForLibSBMLversion.xml','w+');
0074     fclose(fid);
0075     evalc('[~,~,libSBMLver]=TranslateSBML(''tempModelForLibSBMLversion.xml'',0,0)');
0076     libSBMLver=libSBMLver.libSBML_version_string;
0077     delete('tempModelForLibSBMLversion.xml');
0078 end
0079 cd(prevDir)
0080 
0081 % Make models folder, no warnings if folder already exists
0082 if subDirs
0083     path=fullfile(path,'model');
0084     filePath=strcat(path,filesep,{'txt','yml','mat','xlsx','xml'});
0085     [~,~,~]=mkdir(path);
0086     for i = 1:length(formats)
0087         [~,~,~]=mkdir(fullfile(path,formats{i}));
0088     end
0089 else
0090     filePath=cell(1,5); filePath(:)={path};
0091 end
0092 
0093 
0094 % Write TXT format
0095 if ismember('txt', formats)
0096     fid=fopen(fullfile(filePath{1},strcat(prefix,'.txt')),'w');
0097     if cobraText==true
0098         eqns=constructEquations(model,model.rxns,false,false,false);
0099         eqns=strrep(eqns,' => ','  -> ');
0100         eqns=strrep(eqns,' <=> ','  <=> ');
0101         eqns=regexprep(eqns,'> $','>');
0102         grRules=regexprep(model.grRules,'\((?!\()','( ');
0103         grRules=regexprep(grRules,'(?<!\))\)',' )');
0104     else
0105         eqns=constructEquations(model,model.rxns);
0106         grRules=model.grRules;
0107     end
0108     fprintf(fid, 'Rxn name\tFormula\tGene-reaction association\tLB\tUB\tObjective\n');
0109     for i = 1:numel(model.rxns)
0110         fprintf(fid, '%s\t', model.rxns{i});
0111         fprintf(fid, '%s \t', eqns{i});
0112         fprintf(fid, '%s\t', grRules{i});
0113         fprintf(fid, '%6.2f\t%6.2f\t%6.2f\n', model.lb(i), model.ub(i), model.c(i));
0114     end
0115     fclose(fid);
0116 end
0117 
0118 % Write YML format
0119 if ismember('yml', formats)
0120     writeYAMLmodel(model,fullfile(filePath{2},strcat(prefix,'.yml')));
0121 end
0122 
0123 % Write MAT format
0124 if ismember('mat', formats)
0125     save(fullfile(filePath{3},strcat(prefix,'.mat')),'model');
0126 end
0127 
0128 % Write XLSX format
0129 if ismember('xlsx', formats)
0130     exportToExcelFormat(model,fullfile(filePath{4},strcat(prefix,'.xlsx')));
0131 end
0132 
0133 % Write XML format
0134 if ismember('xml', formats)
0135         exportModel(model,fullfile(filePath{5},strcat(prefix,'.xml')));
0136 end
0137 
0138 %Save file with versions:
0139 fid = fopen(fullfile(path,'dependencies.txt'),'wt');
0140 fprintf(fid,['MATLAB\t' version '\n']);
0141 fprintf(fid,['libSBML\t' libSBMLver '\n']);
0142 fprintf(fid,['RAVEN_toolbox\t' RAVENver '\n']);
0143 if ~isempty(COBRAver)
0144     fprintf(fid,['COBRA_toolbox\t' COBRAver '\n']);
0145 end
0146 if isfield(model,'modelVersion')
0147     fields = fieldnames(model.modelVersion);
0148     for i = 1:length(fields)
0149         value = model.modelVersion.(fields{i});
0150         fprintf(fid,[fields{i} '\t' num2str(value) '\n']);
0151     end
0152 end
0153 fclose(fid);
0154 end

Generated by m2html © 2005