-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathgenerateNewIds.m
More file actions
executable file
·61 lines (55 loc) · 1.83 KB
/
Copy pathgenerateNewIds.m
File metadata and controls
executable file
·61 lines (55 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function newIds=generateNewIds(model,type,prefix,quantity,numLength)
% generateNewIds
% Generates a list of new metabolite or reaction ids, sequentially
% numbered with a defined prefix. The model is queried for the highest
% existing number of that type of id.
%
% model model structure
% type string specifying type of id, 'rxns' or 'mets'
% prefix string specifying prefix to be used in all ids. E.g. 's_'
% or 'r_'.
% quantity number of new ids that should be generated (optional, default 1)
% numLength length of numerical part of id. E.g. 4 gives ids like
% r_0001 and 6 gives ids like r_000001. If the prefix is
% already used in the model, then the model-defined length
% will be used instead. (optional, default 4)
%
% Usage: newIds=generateNewIds(model,type,prefix,quantity,numLength)
%
type=char(type);
prefix=char(prefix);
if type=='rxns'
existingIds=model.rxns;
elseif type=='mets'
existingIds=model.mets;
else
error('type should be either ''rxns'' or ''mets''.')
end
if nargin<4
quantity=1;
end
if nargin<5
numLength=4;
end
% Subset only existingIds that have the prefix
existingIds=existingIds(~cellfun(@isempty,regexp(existingIds,['^' prefix])));
if ~isempty(existingIds)
existingIds=regexprep(existingIds,['^' prefix],'');
existingIds=sort(existingIds);
lastId=existingIds{end};
numLength=length(lastId);
lastId=str2double(lastId);
else
lastId=0;
fprintf(['No ' type ' ids with prefix "' prefix ...
'" currently exist in the model. The first new id will be "' ...
[prefix,num2str(1,['%0' num2str(numLength) 'd'])] '"\n'],'%s')
end
if isnan(lastId)
lastId = 0;
end
newIds=cell(quantity,1);
for k=1:quantity
newIds{k}=[prefix,num2str(k+lastId,['%0' num2str(numLength) 'd'])];
end
end