Skip to content

Commit

Permalink
Merge pull request #170 from SysBioChalmers/fix/YamlQuotes
Browse files Browse the repository at this point in the history
fix: systematically add double quote to string elements in Yaml file
  • Loading branch information
haowang-bioinfo authored May 18, 2020
2 parents 9752326 + 524ca3c commit 82d430f
Show file tree
Hide file tree
Showing 4 changed files with 134,879 additions and 134,849 deletions.
68 changes: 45 additions & 23 deletions code/io/importHumanYaml.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
function model=importHumanYaml(yamlFilename)
function model=importHumanYaml(yamlFilename, silentMode)
% importHumanYaml
% Imports a yaml file matching (roughly) the cobrapy yaml structure
%
% Input:
% yamlFile a file in yaml model structure. As defined in HumanGEM, the
% yaml file contains 5 sections: metaData, metabolites,
% reactions, genes and compartments
% silentMode set as true to turn off notificaiton messages (opt, default
% false)
%
% Output:
% model a model structure
%
% Usage: model=importYaml(yamlFilename)
% Usage: model=importYaml(yamlFilename, silentMode)
%
% Hao Wang, 2019-07-26
% Hao Wang, 2020-05-17
%
% This function is to reverse engineer the RAVEN function `writeYaml`
%

if nargin < 2
silentMode = false;
end

if ~(exist(yamlFilename,'file')==2)
error('Yaml file %s cannot be found',string(yamlFilename));
end
Expand Down Expand Up @@ -65,7 +71,9 @@
% Load Yaml format model

fid = fopen(yamlFilename);
fprintf('Start importing...\n');
if ~silentMode
fprintf('Start importing...\n');
end

section = 0;
while ~feof(fid)
Expand All @@ -74,7 +82,9 @@

% import metaData
if isequal(tline, '- metaData:')
fprintf('\tmetaData\n');
if ~silentMode
fprintf('\tmetaData\n');
end
section = 1;
end

Expand Down Expand Up @@ -112,19 +122,21 @@

% import metabolites:
if isequal(tline, '- metabolites:')
fprintf('\tmetabolites\n');
if ~silentMode
fprintf('\tmetabolites\n');
end
section = 2;
end

if section == 2
if numel(tline) > 10 && isequal(tline(1:10),' - id: ')
model.mets = [model.mets; tline(11:end)];
if startsWith(tline,' - id: ')
model = readFieldElement(model, tline, 'mets', ' - id: ');

elseif numel(tline) > 12 && isequal(tline(1:12),' - name: ')
model.metNames = [model.metNames; tline(14:end-1)];
elseif startsWith(tline,' - name: ')
model = readFieldElement(model, tline, 'metNames', ' - name: ');

elseif numel(tline) > 19 && isequal(tline(1:19),' - compartment: ')
model.metComps = [model.metComps; tline(20:end)];
elseif startsWith(tline,' - compartment: ')
model = readFieldElement(model, tline, 'metComps', ' - compartment: ');

elseif startsWith(tline,' - formula: ')
model = readFieldElement(model, tline, 'metFormulas',' - formula: ');
Expand All @@ -144,7 +156,9 @@

% import reactions:
if isequal(tline, '- reactions:')
fprintf('\treactions\n');
if ~silentMode
fprintf('\treactions\n');
end
section = 3;
readSubsystems = false;
readEquation = false;
Expand All @@ -153,8 +167,8 @@

if section == 3
if startsWith(tline,' - id: ')
model.rxns = [model.rxns; tline(11:end)];
rxnId = tline(11:end);
model = readFieldElement(model, tline, 'rxns',' - id: ');
rxnId = tline(12:end-1);

elseif startsWith(tline,' - name: ')
model = readFieldElement(model, tline, 'rxnNames',' - name: ');
Expand Down Expand Up @@ -198,7 +212,7 @@
rightEquation = '';
else
if readSubsystems
subSystems = [subSystems; tline(11:end)];
subSystems = [subSystems; tline(12:end-1)];

% resolve the equation
elseif readEquation
Expand All @@ -225,23 +239,27 @@

% import genes:
if isequal(tline, '- genes:')
fprintf('\tgenes\n');
if ~silentMode
fprintf('\tgenes\n');
end
section = 4;
end

if section == 4 && numel(tline) > 10 && isequal(tline(1:10),' - id: ')
model.genes = [model.genes; tline(11:end)];
if section == 4 && startsWith(tline,' - id: ')
model = readFieldElement(model, tline, 'genes',' - id: ');
end


% import compartments:
if isequal(tline, '- compartments: !!omap')
fprintf('\tcompartments\n');
if ~silentMode
fprintf('\tcompartments\n');
end
section = 5;
end

if section == 5 && numel(tline) > 7 && isequal(tline(1:6),' - ')
str = split(tline(7:end),': ');
str = split(tline(7:end-1),': "');
model.comps = [model.comps; str{1}];
model.compNames = [model.compNames; str{2}];
end
Expand All @@ -251,7 +269,9 @@


% follow-up data processing
fprintf('\nimporting completed\nfollow-up processing...');
if ~silentMode
fprintf('\nimporting completed\nfollow-up processing...');
end
[~, model.metComps] = ismember(model.metComps, model.comps);
model.metCharges = int64(str2double(model.metCharges));
model.lb = str2double(model.lb);
Expand Down Expand Up @@ -286,7 +306,9 @@
% dealing with the `unconstrained` field for other models!
model.unconstrained = double(endsWith(model.mets, 'x'));

fprintf(' Done!\n');
if ~silentMode
fprintf(' Done!\n');
end

end

Expand Down
12 changes: 4 additions & 8 deletions code/io/writeHumanYaml.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function writeHumanYaml(model,name)
%
%
% Jonathan Robinson, 2019-03-14
% Hao Wang, 2019-09-15
% Hao Wang, 2020-05-17
%

%{
Expand Down Expand Up @@ -220,19 +220,15 @@ function writeField(model,fid,fieldName,type,pos,name)
elseif length(list) > 1 || strcmp(fieldName,'subSystems')
fprintf(fid,[' ' name ':\n']);
for i = 1:length(list)
fprintf(fid,[' - ' list{i} '\n']);
fprintf(fid,[' - "' list{i} '"\n']);
end
end

elseif sum(pos) > 0
% all other fields
if strcmp(type,'txt')
if strcmp(name,'- name')
% enclose all "names" in double quotes
value = ['"',field{pos},'"'];
else
value = field{pos};
end
% enclose all string elements with double quotes
value = ['"',field{pos},'"'];
elseif strcmp(type,'num')
if isnan(field(pos))
value = [];
Expand Down
16 changes: 14 additions & 2 deletions code/test/testYamlConversion.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
function status = testYamlConversion(model)
function status = testYamlConversion
% test the functions for yaml import/export see if the conversion process
% changes the model content
%
% Usage: status = testYamlConversion
%
% Hao Wang, 2020-05-17
%

% Get model path
[ST, I]=dbstack('-completenames');
modelPath=fileparts(fileparts(fileparts(ST(I).file)));

% Import yaml model
ymlFile=fullfile(modelPath,'modelFiles','yml','HumanGEM.yml');
model = importHumanYaml(ymlFile, true);

% make sure there is no intermediate Yaml file under the current folder
warning('off', 'MATLAB:DELETE:FileNotFound')
Expand All @@ -12,7 +24,7 @@

% export to yml and then import back
writeHumanYaml(model,'testYamlConversion.yml');
importedHumanGEM = importHumanYaml('testYamlConversion.yml');
importedHumanGEM = importHumanYaml('testYamlConversion.yml', true);

% remove intermediate Yaml file
delete testYamlConversion.yml;
Expand Down
Loading

0 comments on commit 82d430f

Please sign in to comment.