Home > io > exportForGit.m

exportForGit

PURPOSE ^

exportForGit

SYNOPSIS ^

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

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 (optional, default 'model')
   path                path where the directory structure should be
                       generated and populated with all files (optional,
                       default to current working directory)
   formats             cell array of strings specifying in what file
                       formats the model should be exported (optional,
                       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.
                       (optional, 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. (optional, default
                       true)
   COBRAtext           logical, whether the txt file should be in COBRA
                       Toolbox format using metabolite IDs, instead of
                       metabolite names and compartments. (optional,
                       default false)
   neverPrefixIDs      true if prefixes are never added to identifiers,
                       even if start with e.g. digits. This might result
                       in invalid SBML files (optional, default false)

 Usage: exportForGit(model,prefix,path,formats,mainBranchFlag,subDirs,COBRAtext,COBRAstyle)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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

Generated by m2html © 2005