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 else
0076     COBRAver = [];
0077 end
0078 %Retrieve libSBML version:
0079 [ravenDir,prevDir]=findRAVENroot();
0080 try % 5.17.0 and newer
0081     libSBMLver=OutputSBML_RAVEN;
0082     libSBMLver=libSBMLver.libSBML_version_string;
0083 catch % before 5.17.0
0084     fid = fopen('tempModelForLibSBMLversion.xml','w+');
0085     fclose(fid);
0086     evalc('[~,~,libSBMLver]=TranslateSBML_RAVEN(''tempModelForLibSBMLversion.xml'',0,0)');
0087     libSBMLver=libSBMLver.libSBML_version_string;
0088     delete('tempModelForLibSBMLversion.xml');
0089 end
0090 
0091 % Make models folder, no warnings if folder already exists
0092 if subDirs
0093     path=fullfile(path,'model');
0094     filePath=strcat(path,filesep,{'txt','yml','mat','xlsx','xml'});
0095     [~,~,~]=mkdir(path);
0096     for i = 1:length(formats)
0097         [~,~,~]=mkdir(fullfile(path,formats{i}));
0098     end
0099 else
0100     filePath=cell(1,5); filePath(:)={path};
0101 end
0102 
0103 
0104 % Write TXT format
0105 if ismember('txt', formats)
0106     fid=fopen(fullfile(filePath{1},strcat(prefix,'.txt')),'w');
0107     if COBRAtext==true
0108         eqns=constructEquations(model,model.rxns,false,false,false);
0109         eqns=strrep(eqns,' => ','  -> ');
0110         eqns=strrep(eqns,' <=> ','  <=> ');
0111         eqns=regexprep(eqns,'> $','>');
0112         grRules=regexprep(model.grRules,'\((?!\()','( ');
0113         grRules=regexprep(grRules,'(?<!\))\)',' )');
0114     else
0115         eqns=constructEquations(model,model.rxns);
0116         grRules=model.grRules;
0117     end
0118     fprintf(fid, 'Rxn name\tFormula\tGene-reaction association\tLB\tUB\tObjective\n');
0119     for i = 1:numel(model.rxns)
0120         fprintf(fid, '%s\t', model.rxns{i});
0121         fprintf(fid, '%s \t', eqns{i});
0122         fprintf(fid, '%s\t', grRules{i});
0123         fprintf(fid, '%6.2f\t%6.2f\t%6.2f\n', model.lb(i), model.ub(i), model.c(i));
0124     end
0125     fclose(fid);
0126 end
0127 
0128 % Write YML format
0129 if ismember('yml', formats)
0130     writeYAMLmodel(model,fullfile(filePath{2},strcat(prefix,'.yml')));
0131 end
0132 
0133 % Write MAT format
0134 if ismember('mat', formats)
0135     save(fullfile(filePath{3},strcat(prefix,'.mat')),'model');
0136 end
0137 
0138 % Write XLSX format
0139 if ismember('xlsx', formats)
0140     exportToExcelFormat(model,fullfile(filePath{4},strcat(prefix,'.xlsx')));
0141 end
0142 
0143 % Write XML format
0144 if ismember('xml', formats)
0145         exportModel(model,fullfile(filePath{5},strcat(prefix,'.xml')),neverPrefixIDs);
0146 end
0147 
0148 %Save file with versions:
0149 fid = fopen(fullfile(path,'dependencies.txt'),'wt');
0150 fprintf(fid,['MATLAB\t' version '\n']);
0151 fprintf(fid,['libSBML\t' libSBMLver '\n']);
0152 fprintf(fid,['RAVEN_toolbox\t' RAVENver '\n']);
0153 if ~isempty(COBRAver)
0154     fprintf(fid,['COBRA_toolbox\t' COBRAver '\n']);
0155 end
0156 if isfield(model,'modelVersion')
0157     fields = fieldnames(model.modelVersion);
0158     for i = 1:length(fields)
0159         value = model.modelVersion.(fields{i});
0160         fprintf(fid,[fields{i} '\t' num2str(value) '\n']);
0161     end
0162 end
0163 fclose(fid);
0164 end

Generated by m2html © 2005