Skip to content

Commit

Permalink
feat: convertCharArray function
Browse files Browse the repository at this point in the history
- all functions in this commit are tested by a unit_test
  • Loading branch information
edkerk committed May 1, 2022
1 parent 1ad1109 commit ff462fd
Show file tree
Hide file tree
Showing 45 changed files with 785 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Use MATLAB style diff
*.m text diff=matlab
# Mark as binary to prevent EOL changes. Also no need to track
mafft-linux*/* binary
mafft.bat binary
# Files/folders that should not be included when downloading repo as ZIP
.gitattributes export-ignore
.gitignore export-ignore
Expand Down
2 changes: 2 additions & 0 deletions core/addExchangeRxns.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

if nargin<3
mets=model.mets;
elseif ~islogical(mets) && ~isnumeric(mets)
mets=convertCharArray(mets);
end
J=getIndexes(model,mets,'mets',false);
mets=model.mets(J);
Expand Down
12 changes: 3 additions & 9 deletions core/addGenesRaven.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@
if ~isfield(genesToAdd,'genes')
EM='genes is a required field in genesToAdd';
dispEM(EM);
end

if ~iscellstr(genesToAdd.genes)
EM='genesToAdd.genes must be a cell array of strings';
dispEM(EM);
else
genesToAdd.genes=convertCharArray(genesToAdd.genes);
end

%Number of genes
Expand Down Expand Up @@ -68,14 +65,11 @@

%Some more checks and if they pass then add each field to the structure
if isfield(genesToAdd,'geneShortNames')
genesToAdd.geneShortNames=convertCharArray(genesToAdd.geneShortNames);
if numel(genesToAdd.geneShortNames)~=nGenes
EM='genesToAdd.geneShortNames must have the same number of elements as genesToAdd.genes';
dispEM(EM);
end
if ~iscellstr(genesToAdd.geneShortNames)
EM='genesToAdd.geneShortNames must be a cell array of strings';
dispEM(EM);
end
%Add empty field if it doesn't exist
if ~isfield(newModel,'geneShortNames')
newModel.geneShortNames=largeFiller;
Expand Down
43 changes: 9 additions & 34 deletions core/addMets.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,19 @@
%Check some stuff regarding the required fields
if ~isfield(metsToAdd,'mets')
metsToAdd.mets=generateNewIds(newModel,'mets',prefix,numel(metsToAdd.metNames));
else
metsToAdd.mets=convertCharArray(metsToAdd.mets);
end
if ~isfield(metsToAdd,'metNames')
metsToAdd.metNames=metsToAdd.mets;
else
metsToAdd.metNames=convertCharArray(metsToAdd.metNames);
end
if ~isfield(metsToAdd,'compartments')
EM='compartments is a required field in metsToAdd';
dispEM(EM);
end
if ischar(metsToAdd.mets)
metsToAdd.mets={metsToAdd.mets};
elseif ~iscellstr(metsToAdd.mets)
EM='metsToAdd.mets must be a cell array of strings';
dispEM(EM);
end
if ischar(metsToAdd.metNames)
metsToAdd.metNames={metsToAdd.metNames};
elseif ~iscellstr(metsToAdd.metNames)
EM='metsToAdd.metNames must be a cell array of strings';
dispEM(EM);
end
if ~iscellstr(metsToAdd.compartments)
if ischar(metsToAdd.compartments)

This comment has been minimized.

Copy link
@JonathanRob

JonathanRob May 26, 2022

Collaborator

fyi, removing this part of the code now requires that the compartments field is the same length as the number of metabolites to add. So I recommend removing the following sentence from this function's header:

If this is a string rather than a cell array it is assumed that all mets are in that compartment

This comment has been minimized.

Copy link
@edkerk

edkerk May 26, 2022

Author Member

That was not intended, will fix!

temp=cell(numel(metsToAdd.mets),1);
temp(:)={metsToAdd.compartments};
metsToAdd.compartments=temp;
else
EM='metsToAdd.compartments must be a cell array of strings';
dispEM(EM);
end
else
metsToAdd.compartments=convertCharArray(metsToAdd.compartments);
end

%Number of metabolites
Expand Down Expand Up @@ -195,14 +179,11 @@
end

if isfield(metsToAdd,'inchis')
metsToAdd.inchis=convertCharArray(metsToAdd.inchis);
if numel(metsToAdd.inchis)~=nMets
EM='metsToAdd.inchis must have the same number of elements as metsToAdd.mets';
dispEM(EM);
end
if ~iscellstr(metsToAdd.inchis)
EM='metsToAdd.inchis must be a cell array of strings';
dispEM(EM);
end
%Add empty field if it doesn't exist
if ~isfield(newModel,'inchis')
newModel.inchis=largeFiller;
Expand All @@ -216,14 +197,11 @@
end

if isfield(metsToAdd,'metFormulas')
metsToAdd.metFormulas=convertCharArray(metsToAdd.metFormulas);
if numel(metsToAdd.metFormulas)~=nMets
EM='metsToAdd.metFormulas must have the same number of elements as metsToAdd.mets';
dispEM(EM);
end
if ~iscellstr(metsToAdd.metFormulas)
EM='metsToAdd.metFormulas must be a cell array of strings';
dispEM(EM);
end
%Add empty field if it doesn't exist
if ~isfield(newModel,'metFormulas')
newModel.metFormulas=largeFiller;
Expand Down Expand Up @@ -257,14 +235,11 @@
end

if isfield(metsToAdd,'metNotes')
metsToAdd.metNotes=convertCharArray(metsToAdd.metNotes);
if numel(metsToAdd.metNotes)~=nMets
EM='metsToAdd.metNotes must have the same number of elements as metsToAdd.mets';
dispEM(EM);
end
if ~iscellstr(metsToAdd.metNotes)
EM='metsToAdd.metNotes must be a cell array of strings';
dispEM(EM);
end
%Add empty field if it doesn't exist
if ~isfield(newModel,'metNotes')
newModel.metNotes=largeFiller;
Expand Down
32 changes: 13 additions & 19 deletions core/addRxns.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
end

if allowNewGenes & isfield(rxnsToAdd,'grRules')
genesToAdd.genes = strjoin(rxnsToAdd.grRules);
genesToAdd.genes = strjoin(convertCharArray(rxnsToAdd.grRules));
genesToAdd.genes = regexp(genesToAdd.genes,' |)|(|and|or','split'); % Remove all grRule punctuation
genesToAdd.genes = genesToAdd.genes(~cellfun(@isempty,genesToAdd.genes)); % Remove spaces and empty genes
genesToAdd.genes = setdiff(unique(genesToAdd.genes),model.genes); % Only keep new genes
Expand Down Expand Up @@ -161,7 +161,8 @@
if ~isfield(rxnsToAdd,'rxns')
EM='rxns is a required field in rxnsToAdd';
dispEM(EM);
elseif iscell(rxnsToAdd.rxns)
else
rxnsToAdd.rxns=convertCharArray(rxnsToAdd.rxns);
%To fit with some later printing
rxnsToAdd.rxns=rxnsToAdd.rxns(:);
end
Expand All @@ -175,30 +176,16 @@
dispEM(EM);
end

if ~iscellstr(rxnsToAdd.rxns) && ~ischar(rxnsToAdd.rxns)
%It could also be a string, but it's not encouraged
EM='rxnsToAdd.rxns must be a cell array of strings';
dispEM(EM);
else
rxnsToAdd.rxns=cellstr(rxnsToAdd.rxns);
end

%Normal case: equations provided
if isfield(rxnsToAdd,'equations')
if ~iscellstr(rxnsToAdd.equations) && ~ischar(rxnsToAdd.equations)
%It could also be a string, but it's not encouraged
EM='rxnsToAdd.equations must be a cell array of strings';
dispEM(EM);
else
rxnsToAdd.equations=cellstr(rxnsToAdd.equations);
end

rxnsToAdd.equations=convertCharArray(rxnsToAdd.equations);

%Alternative case: mets+stoichiometry provided
else
%In the case of 1 rxn added (array of strings + vector), transform to
%cells of length=1:
if iscellstr(rxnsToAdd.mets)
rxnsToAdd.mets = {rxnsToAdd.mets};
rxnsToAdd.mets={rxnsToAdd.mets};
end
if isnumeric(rxnsToAdd.stoichCoeffs)
rxnsToAdd.stoichCoeffs = {rxnsToAdd.stoichCoeffs};
Expand Down Expand Up @@ -258,6 +245,7 @@
newModel.rxns=[newModel.rxns;rxnsToAdd.rxns(:)];

if isfield(rxnsToAdd,'rxnNames')
rxnsToAdd.rxnNames=convertCharArray(rxnsToAdd.rxnNames);
if numel(rxnsToAdd.rxnNames)~=nRxns
EM='rxnsToAdd.rxnNames must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand Down Expand Up @@ -341,6 +329,7 @@
end

if isfield(rxnsToAdd,'eccodes')
rxnsToAdd.eccodes=convertCharArray(rxnsToAdd.eccodes);
if numel(rxnsToAdd.eccodes)~=nRxns
EM='rxnsToAdd.eccodes must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand Down Expand Up @@ -415,6 +404,7 @@
end
end
if isfield(rxnsToAdd,'grRules')
rxnsToAdd.grRules=convertCharArray(rxnsToAdd.grRules);
if numel(rxnsToAdd.grRules)~=nRxns
EM='rxnsToAdd.grRules must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand All @@ -432,6 +422,7 @@
end

if isfield(rxnsToAdd,'rxnFrom')
rxnsToAdd.rxnFrom=convertCharArray(rxnsToAdd.rxnFrom);
if numel(rxnsToAdd.rxnFrom)~=nRxns
EM='rxnsToAdd.rxnFrom must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand All @@ -449,6 +440,7 @@
end

if isfield(rxnsToAdd,'rxnNotes')
rxnsToAdd.rxnNotes=convertCharArray(rxnsToAdd.rxnNotes);
if numel(rxnsToAdd.rxnNotes)~=nRxns
EM='rxnsToAdd.rxnNotes must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand All @@ -466,6 +458,7 @@
end

if isfield(rxnsToAdd,'rxnReferences')
rxnsToAdd.rxnReferences=convertCharArray(rxnsToAdd.rxnReferences);
if numel(rxnsToAdd.rxnReferences)~=nRxns
EM='rxnsToAdd.rxnReferences must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand All @@ -483,6 +476,7 @@
end

if isfield(rxnsToAdd,'pwys')
rxnsToAdd.pwys=convertCharArray(rxnsToAdd.pwys);
if numel(rxnsToAdd.pwys)~=nRxns
EM='rxnsToAdd.pwys must have the same number of elements as rxnsToAdd.rxns';
dispEM(EM);
Expand Down
31 changes: 17 additions & 14 deletions core/addRxnsGenesMets.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
%
% model draft model where reactions should be copied to
% sourceModel model where reactions and metabolites are sourced from
% rxns cell array with reaction IDs (from source model)
% string allowed if only one reaction is added
% rxns cell array with reaction IDs (from source model). Can also
% be string if only one reaction is added
% addGene three options:
% false no genes are annotated to the new reactions
% true grRules ared copied from the sourceModel and
Expand All @@ -16,10 +16,12 @@
% array, and any new genes are added when
% required
% (opt, default false)
% rxnNote string explaining why reactions were copied to model,
% is included as newModel.rxnNotes (opt, default
% rxnNote cell array with strings explaining why reactions were copied
% to the model, to be included as newModel.rxnNotes. Can also
% be string if same rxnNotes should be added for each new
% reaction, or only one reaction is to be added (opt, default
% 'Added via addRxnsAndMets()')
% confidence double specifying confidence score for all reactions.
% confidence integer specifying confidence score for all reactions.
% 4: biochemical data: direct evidence from enzymes
% assays
% 3: genetic data: knockout/-in or overexpression
Expand All @@ -45,19 +47,21 @@
if nargin<6
confidence=0;
end
rxns=convertCharArray(rxns);
if nargin<5
rxnNote='Added via addRxnsGenesMets()';
rxnNote={'Added via addRxnsGenesMets()'};
else
rxnNote=convertCharArray(rxnNote);
if numel(rxnNote)==1 && numel(rxns)>1
rxnNoteArray=cell(numel(rxns),1);
rxnNoteArray{:}=rxnNote;
rxnNote=rxnNoteArray;
end
end
if nargin<4
addGene=false;
end

%If the supplied object is a character array, then convert it to a cell
%array
if ischar(rxns)
rxns={rxns};
end

% Obtain indexes of reactions in source model
[notNewRxn,oldRxn]=ismember(rxns,model.rxns);
rxns=rxns(~notNewRxn);
Expand Down Expand Up @@ -150,8 +154,7 @@
rxnToAdd.rxns=sourceModel.rxns(rxnIdx);
rxnToAdd.lb=sourceModel.lb(rxnIdx);
rxnToAdd.ub=sourceModel.ub(rxnIdx);
rxnToAdd.rxnNotes=cell(1,numel(rxnToAdd.rxns));
rxnToAdd.rxnNotes(:)={rxnNote};
rxnToAdd.rxnNotes(:)=rxnNote(~notNewRxn);
rxnToAdd.rxnConfidenceScores=NaN(1,numel(rxnToAdd.rxns));
if ~isnumeric(confidence)
EM='confidence score must be numeric';
Expand Down
14 changes: 4 additions & 10 deletions core/addTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
EM='fromComps must have exactly one match in model.comps';
dispEM(EM);
end
if ischar(toComps)
toComps={toComps};
end
toComps=convertCharArray(toComps);
[I, toIDs]=ismember(toComps,model.comps);
if ~all(I)
EM='All compartments in toComps must have a match in model.comps';
Expand All @@ -43,12 +41,11 @@
if nargin<4
%Find all metabolites in fromComp
metNames=model.metNames(model.metComps==fromID);
end

%If an empty set was given
if isempty(metNames)
elseif isempty(metNames)
%Find all metabolites in fromComp
metNames=model.metNames(ismember(model.metComps,model.comps(fromID)));
else
metNames=convertCharArray(metNames);
end

if nargin<5
Expand All @@ -64,9 +61,6 @@
end

%Check that the names are unique
if ischar(metNames)
metNames={metNames};
end
if numel(unique(metNames))~=numel(metNames)
dispEM('Not all metabolite names are unique');
end
Expand Down
9 changes: 2 additions & 7 deletions core/buildEquation.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
% buildEquation
% Construct single equation string for a given reaction
%
% mets string array with metabolites involved in the reaction.
% mets cell array with metabolites involved in the reaction.
% stoichCoeffs vector with corresponding stoichiometric coeffs.
% isrev logical indicating if the reaction is or not
% reversible.
Expand All @@ -11,12 +11,7 @@
%
% Usage: equationString=buildEquation(mets,stoichCoeffs,isrev)

if ~iscellstr(mets) && ~ischar(mets)
EM = 'mets must be a cell array of strings';
dispEM(EM);
else
mets = cellstr(mets);
end
mets=convertCharArray(mets);
if ~isnumeric(stoichCoeffs)
EM = 'stoichCoeffs must be a numeric vector';
dispEM(EM);
Expand Down
2 changes: 2 additions & 0 deletions core/canConsume.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

if nargin<2
mets=model.mets;
elseif ~islogical(mets) && ~isnumeric(mets)
mets=convertCharArray(mets);
end

[model, rxns]=addExchangeRxns(model,'in',mets);
Expand Down
2 changes: 2 additions & 0 deletions core/canProduce.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

if nargin<2
mets=model.mets;
elseif ~islogical(mets) && ~isnumeric(mets)
mets=convertCharArray(mets);
end

[model, rxns]=addExchangeRxns(model,'out',mets);
Expand Down
Loading

0 comments on commit ff462fd

Please sign in to comment.