Home > functions > internal > dsLocateModelFiles.m

dsLocateModelFiles

PURPOSE ^

LOCATEMODELFILES - locate mechanism files associated with DynaSim specifications.

SYNOPSIS ^

function [paths,files] = dsLocateModelFiles(input)

DESCRIPTION ^

LOCATEMODELFILES - locate mechanism files associated with DynaSim specifications.

 Usage:
   [paths,files]=dsLocateModelFiles(input)

 Input: DynaSim specification or model structure or string or cell array of
        strings listing mechanism names or files.

 Outputs:
   - paths: unique paths to mechanism files
   - files: full names of files containing mechanism sub-models

 See also (used by): dsParseModelEquations, dsCheckHostPaths, dsCreateBatch

 Author: Jason Sherfey, PhD <jssherfey@gmail.com>
 Copyright (C) 2016 Jason Sherfey, Boston University, USA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [paths,files] = dsLocateModelFiles(input)
0002 %LOCATEMODELFILES - locate mechanism files associated with DynaSim specifications.
0003 %
0004 % Usage:
0005 %   [paths,files]=dsLocateModelFiles(input)
0006 %
0007 % Input: DynaSim specification or model structure or string or cell array of
0008 %        strings listing mechanism names or files.
0009 %
0010 % Outputs:
0011 %   - paths: unique paths to mechanism files
0012 %   - files: full names of files containing mechanism sub-models
0013 %
0014 % See also (used by): dsParseModelEquations, dsCheckHostPaths, dsCreateBatch
0015 %
0016 % Author: Jason Sherfey, PhD <jssherfey@gmail.com>
0017 % Copyright (C) 2016 Jason Sherfey, Boston University, USA
0018 
0019 % First looks for absolute path , then in current dir, then ds models dir,
0020 % then in ds models sub dirs, then in full matlab path. First looks for string,
0021 % then extensions .eqns, .mech, .txt, and .m (in that order).
0022 
0023 % extract list of mechanisms from input
0024 if ischar(input)
0025   % convert to cell array of strings
0026   mechanism_list={input};
0027 elseif iscellstr(input)
0028   mechanism_list=input;
0029 elseif isstruct(input)
0030   if isfield(input,'specification')
0031     % extract specification from DynaSim model structure
0032     input=input.specification;
0033   elseif isfield(input,'model') && isfield(input.model,'specification') 
0034     % extract specification from DynaSim data structure
0035     input=input.model.specification;
0036   elseif isfield(input,'base_model') && isfield(input.base_model,'specification') 
0037     % extract specification from DynaSim studyinfo structure
0038     input=input.base_model.specification;
0039   else
0040     % this is probably a specification structure
0041   end
0042   
0043   mechanism_list={};
0044   if isfield(input,'populations') && isstruct(input.populations)
0045     % extract mechanism_list from populations in DynaSim specification structure
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     % add equations to mechanism_list in case it contains a .eqns file
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     % extract mechanism_list connections in DynaSim specification structure
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 %     m={input.connections.mechanism_list};
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   % remove mechanisms present in specification structure
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 % remove @ pointers from mechanism identifiers
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 % exclude elements with non-word characters (these are not file names)
0114 keep = cellfun(@isempty,regexp(mechanism_list,'[^\w\.\-/]'));
0115 mechanism_list = mechanism_list(keep);
0116 
0117 % search in dynasim toolbox model directory
0118 dynasim_path = dsGetRootPath();
0119 model_dir = fullfile(dynasim_path,'models'); % models dir is at root level
0120 search_paths = regexp(genpath(model_dir),pathsep,'split'); % look in all dynasim directories
0121 
0122 % add current directory
0123 search_paths=cat(2,pwd,search_paths); % look in current directory first
0124 
0125 % exclude .git directories
0126 keep = cellfun(@isempty,regexp(search_paths,'.git'));
0127 search_paths = search_paths(keep);
0128 
0129 % remove empty paths
0130 keep = ~cellfun(@isempty,search_paths);
0131 search_paths = search_paths(keep);
0132 
0133 % % add single empty path (in case mech given as absolute path)
0134 % search_paths = cat(2,{''},search_paths);
0135 
0136 % get num paths and increment 1 for when searching without specific path
0137 num_paths=length(search_paths)+1;
0138 
0139 % locate mechanism files
0140 files={};
0141 if iscellstr(mechanism_list)
0142   for f = 1:length(mechanism_list) % loop over mechanisms
0143     for s = 1:num_paths
0144       % determine name of mechanism file (assuming recommended extensions)
0145       if s == num_paths
0146         % if not found in paths, just use the mech string as given
0147         mech = mechanism_list{f};
0148       else
0149         % prepend current search path to mech string
0150         mech = fullfile(search_paths{s}, mechanism_list{f});
0151       end
0152       
0153       % see if any extension matches current mech string
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       % use 'which' to get full filename of file in Matlab path
0169       file = which(file);
0170       
0171       if s == num_paths && isempty(file)
0172         % looked through all paths at this point
0173         % if still empty, then wrong name or not on path
0174         warning('Could not find mechanism ''%s'' on path (with standard extensions).', mech);
0175       end
0176       
0177       if ~isempty(file)
0178         % add mech filepath to files cell array
0179         files{end+1}=file;
0180         
0181         % stop searching through paths for mech
0182         break;
0183       end
0184     end
0185   end
0186 end
0187 
0188 % extract unique paths to mechanism files
0189 paths = unique_wrapper(cellfun(@fileparts2,files,'uni',0),'stable');

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005