0001 function [paths,files] = dsLocateModelFiles(input)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 if ischar(input)
0025
0026 mechanism_list={input};
0027 elseif iscellstr(input)
0028 mechanism_list=input;
0029 elseif isstruct(input)
0030 if isfield(input,'specification')
0031
0032 input=input.specification;
0033 elseif isfield(input,'model') && isfield(input.model,'specification')
0034
0035 input=input.model.specification;
0036 elseif isfield(input,'base_model') && isfield(input.base_model,'specification')
0037
0038 input=input.base_model.specification;
0039 else
0040
0041 end
0042
0043 mechanism_list={};
0044 if isfield(input,'populations') && isstruct(input.populations)
0045
0046
0047 m={};
0048 for i=1:length(input.populations)
0049 for j=1:length(input.populations(i).mechanism_list)
0050 name=input.populations(i).mechanism_list{j};
0051 if ~isfield(input.populations,'mechanisms') ...
0052 || isempty(input.populations(i).mechanisms) ...
0053 || ~ismember(name,{input.populations(i).mechanisms.name})
0054 m=cat(2,name,m);
0055 end
0056 end
0057 end
0058
0059 if ~isempty(m)
0060 if iscell(m{1})
0061 m=unique_wrapper([m{:}],'stable');
0062 else
0063 m=unique_wrapper(m,'stable');
0064 end
0065 mechanism_list=cat(2,mechanism_list,m);
0066 end
0067
0068
0069 for i=1:length(input.populations)
0070 if ~isempty(input.populations(i).equations)
0071 mechanism_list{end+1}=input.populations(i).equations;
0072 end
0073 end
0074 end
0075
0076 if isfield(input,'connections') && isstruct(input.connections)
0077
0078 m={};
0079 for i=1:length(input.connections)
0080 for j=1:length(input.connections(i).mechanism_list)
0081 name=input.connections(i).mechanism_list{j};
0082 if ~isfield(input.connections,'mechanisms') ...
0083 || isempty(input.connections(i).mechanisms) ...
0084 || ~ismember(name,{input.connections(i).mechanisms.name})
0085 m=cat(2,name,m);
0086 end
0087 end
0088 end
0089
0090 if ~isempty(m)
0091 if iscell(m{1})
0092 m=unique_wrapper([m{:}],'stable');
0093 else
0094 m=unique_wrapper(m,'stable');
0095 end
0096 mechanism_list=cat(2,mechanism_list,m);
0097 end
0098 end
0099
0100
0101 if ~isempty(mechanism_list) && isfield(input,'mechanisms') && isfield(input.mechanisms,'name')
0102 mech_names=regexp(mechanism_list,'^[^@]+','match');
0103 mechanism_list=mechanism_list(~ismember([mech_names{:}],{input.mechanisms.name}));
0104 end
0105 end
0106
0107
0108 if any(~cellfun(@isempty,regexp(mechanism_list,'@','once')))
0109 mechanism_list=regexp(mechanism_list,'^([^@]+)@?','tokens','once');
0110 mechanism_list=[mechanism_list{:}];
0111 end
0112
0113
0114 keep = cellfun(@isempty,regexp(mechanism_list,'[^\w\.\-/]'));
0115 mechanism_list = mechanism_list(keep);
0116
0117
0118 dynasim_path = dsGetRootPath();
0119 model_dir = fullfile(dynasim_path,'models');
0120 search_paths = regexp(genpath(model_dir),pathsep,'split');
0121
0122
0123 search_paths=cat(2,pwd,search_paths);
0124
0125
0126 keep = cellfun(@isempty,regexp(search_paths,'.git'));
0127 search_paths = search_paths(keep);
0128
0129
0130 keep = ~cellfun(@isempty,search_paths);
0131 search_paths = search_paths(keep);
0132
0133
0134
0135
0136
0137 num_paths=length(search_paths)+1;
0138
0139
0140 files={};
0141 if iscellstr(mechanism_list)
0142 for f = 1:length(mechanism_list)
0143 for s = 1:num_paths
0144
0145 if s == num_paths
0146
0147 mech = mechanism_list{f};
0148 else
0149
0150 mech = fullfile(search_paths{s}, mechanism_list{f});
0151 end
0152
0153
0154 if exist(mech,'file')
0155 file = mech;
0156 elseif exist([mech '.eqns'],'file')
0157 file = [mech '.eqns'];
0158 elseif exist([mech '.mech'],'file')
0159 file = [mech '.mech'];
0160 elseif exist([mech '.txt'],'file')
0161 file = [mech '.txt'];
0162 elseif exist([mech '.m'],'file')
0163 file = [mech '.m'];
0164 else
0165 file = '';
0166 end
0167
0168
0169 file = which(file);
0170
0171 if s == num_paths && isempty(file)
0172
0173
0174 warning('Could not find mechanism ''%s'' on path (with standard extensions).', mech);
0175 end
0176
0177 if ~isempty(file)
0178
0179 files{end+1}=file;
0180
0181
0182 break;
0183 end
0184 end
0185 end
0186 end
0187
0188
0189 paths = unique_wrapper(cellfun(@fileparts2,files,'uni',0),'stable');