diff --git a/workflow/apps/matlab/easy/EASYconsole.m b/workflow/apps/matlab/easy/EASYconsole.m index f747d047..5a4d1ecc 100644 --- a/workflow/apps/matlab/easy/EASYconsole.m +++ b/workflow/apps/matlab/easy/EASYconsole.m @@ -3,10 +3,9 @@ function varargout = EASYconsole(varargin) global easyDir - global easySuffix global scansDir global easyResultsDir - global easyResultsDirName + global easyProject global fotosResultsDir global figsResultsDir global pointMapsResultsDir @@ -41,38 +40,52 @@ function varargout = EASYconsole(varargin) fprintf('This script name: %s\n', easyFileName); % Set scansDir (project scans directory) intelligently - if exist('PROJECT', 'var') && ~isempty(getenv('PROJECT')) - scansDir=getenv('PROJECT'); + + if exist('PROJECT_SCANS_DIR', 'var') && ~isempty(getenv('PROJECT_SCANS_DIR')) + scansDir=fullfile(getenv('PROJECT_SCANS_DIR')); + if exist(scansDir, 'dir') + fprintf('Using scans directory: %s from environment variable PROJECT_SCANS_DIR\n', scansDir); + disp('This usually indicates that we are in module mode'); + if ~exist('PROJECT', 'var') || isempty(getenv('PROJECT')) + dirInfo=fileparts(scansDir); + project=dirInfo.name; + end + else + disp('WARNING: PROJECT_SCANS_DIR does not exist'); + end + elseif exist('PROJECT', 'var') && ~isempty(getenv('PROJECT')) % for standalone mode + % scansDir=fullfile(parentDir, '..', '..', 'scans', getenv('PROJECT') 'out', 'easy', getenv('PROJECT')); + scansDir=fullfile(parentDir, '..', '..', 'scans', getenv('PROJECT')); + project=getenv('PROJECT'); fprintf('Using project path: %s from environment variable PROJECT\n', scansDir); disp('This usually indicates that we are in standalone mode'); - elseif exist('PROJECT_SCANS_DIR', 'var') && ~isempty(getenv('PROJECT_SCANS_DIR')) - scansDir=getenv('PROJECT_SCANS_DIR'); - fprintf('Using scans directory: %s from environment variable PROJECT_SCANS_DIR\n', scansDir); - disp('This usually indicates that we are in module mode'); else % TODO Lots of this is hardcoded logic, this TODO is just a reminder to change this block % when changing EASY and other variables in the parent script + fprintf('WARNING: Running in standalone mode without PROJECT or PROJECT_SCANS_DIR environment variables (not recommended)\n'); fprintf('Beginning parent scans directory search\n'); - fprintf('This usually indicates that we are in stand-alone mode without PROJECT or PROJECT_SCANS_DIR environment variables\n'); dirsToScan={ - fullfile(parentDir,'..', '..', 'scans'), + fullfile(parentDir, '..', '..', 'scans'), fullfile(parentDir, '..', '..', 'ExpJobs'), - fullfile('/mnt/data/scans'), - fullfile('/mnt/data/ExpJobs'), + fullfile('mnt','data','scans'), + fullfile('mnt','data', 'ExpJobs'), fullfile(parentDir, '..', '..', 'templates', 'scans-demo') }; for i=1:length(dirsToScan) d=dirsToScan(i); if exist(d, 'dir') subDirs=dir(d); - if ~isempty(subDirs) - fprintf('Found a non-empty parent scans directory in our list: %s\n', d); - fprintf('Scanning inside for a project scan directory\n'); - [~, sortedIndices]=sort(datenum({dirs.date}), 'descend'); % sort by newest first - sortedDirs=dirs{sortedIndices}; - scansDir=sortedDirs{1}; - fprintf('Selected newest project scans directory: %s\n', scansDir); + pattern='^\d{6}_.*_.*'; + matchedDirs={dirs(regexp(subDirs.name, pattern)).name}; + if ~isempty(matchedDirs) + fprintf('Found a non-empty scans directory in our list: %s\n', char(d); + fprintf('Setting scansDir to %s\n', char(d)); + scansDir=fullfile(d); end + fprintf('Scanning inside %s for a project directory\n', char(d)); + sortedMatchedDirs=sortrows(matchedDirs); + project=sortedMatchedDirs{0}; % select the latest dir (by date prefix) + fprintf('Selected latest project directory %s\n', char(project)); end end end @@ -97,37 +110,12 @@ function varargout = EASYconsole(varargin) fprintf('Using EASY script directory: %s from hardcoded default\n', easyDir); end - % If we don't have tan EASY_SUFFIX from the module, generate it from scansDir - if exist('EASY_SUFFIX', 'var') && ~isempty(getenv('EASY_SUFFIX')) - easySuffix=getenv('EASY_SUFFIX'); - else - % The following is a way to parse the project name from the scansDir - [ ~, dirName]=fileparts(scansDir); - parts=strsplit(dirName, '_'); - scansDate=parts{1}; - scansUserName=parts{2}; - easySuffix=strjoin(parts(3:end), '_'); - % Might as well check this too for fun - if ~strcmp(userName, scansUserName) - disp('WARNING: userName does not match scansUserName'); - disp("This usually means that you are attempting to run an EASY analysis on another user's project data scans"); - end - % For happiness - if strcmp(todayStr, scansDate) - disp("Early bird gets the worm"); - end - end - if (exist('EASY_RESULTS_DIR', 'var') && ~isempty(getenv('EASY_RESULTS_DIR'))) easyResultsDir=fullfile(getenv('EASY_RESULTS_DIR')); - if exist(easyResultsDir, 'dir') - fprintf('WARNING: EASY results dir %s already exists\n', easyResultsDir); - disp('Files in this directory may be overwritten'); - end fprintf('Using output directory: %s from environment variable EASY_RESULTS_DIR\n', easyResultsDir); else - easyResultsDirName=strcat('Results_',todayStr,'_',userName,'_',easySuffix); - easyResultsDir=fullfile(scansDir,easyResultsDirName); + easyProject=strcat(todayStr,'_',userName,'_', project); + easyResultsDir=fullfile(parentDir, '..', '..', 'out', project, 'easy', easyProject); if exist(easyResultsDir, 'dir') fprintf('WARNING: EASY results dir %s already exists\n', easyResultsDir); disp('Files in this directory may be overwritten') @@ -140,12 +128,12 @@ function varargout = EASYconsole(varargin) fprintf('Using drug media file: %s from environment variable MASTER_PLATE_FILE\n', masterPlateFile); else % Try to find MasterPlate_ file on our own - mp=fullfile(scansDir,'MasterPlateFiles',strcat('MasterPlate_', easySuffix,'.xlsx')); + mp=fullfile(scansDir, strcat('MasterPlate_', project,'.xlsx')); if exist(mp, 'file') masterPlateFile=mp; fprintf('Using drug media file: %s from internal logic\n', masterPlateFile); else - fprintf('WARNING: Have you created a MasterPlate_ file in %s/MasterPlateFiles/?\n', scansDir); + fprintf('WARNING: Have you created a MasterPlate_ file in %s/easy_in/?\n', scansDir); end end @@ -153,8 +141,7 @@ function varargout = EASYconsole(varargin) drugMediaFile=fullfile(getenv('DRUG_MEDIA_FILE')); fprintf('Using drug media file: %s from environment variable DRUG_MEDIA_FILE\n', drugMediaFile); else - % Try to find MasterPlate_ file on our own - dm=fullfile(scansDir,'MasterPlateFiles',strcat('DrugMedia_', easySuffix,'.xlsx')); + dm=fullfile(scansDir,'easy_in',strcat('DrugMedia_', project,'.xlsx')); if exist(mp, 'file') drugMediaFile=dm; fprintf('Using drug media file: %s from internal logic\n', drugMediaFile); @@ -163,12 +150,9 @@ function varargout = EASYconsole(varargin) end end - matDir=fullfile(easyResultsDir,'matResults'); - if ~exist(matDir, 'dir') - mkdir(matDir); - end - matFile=fullfile(matDir,strcat(easyResultsDirName,'.mat')); - % Pulled these out of par4GblFnc8c + matDir=fullfile(easyResultsDir); + matFile=fullfile(matDir,easyResultsDirName,'.mat'); + mpdmFile=fullfile(matDir,'MPDM.mat'); printResultsDir=fullfile(easyResultsDir,'PrintResults'); fotosResultsDir=fullfile(easyResultsDir,'Fotos'); figsResultsDir=fullfile(easyResultsDir,'figs'); @@ -176,8 +160,7 @@ function varargout = EASYconsole(varargin) pointMapsFile=fullfile(pointMapsResultsDir,'NImParameters.mat'); oldPointMapsFile=fullfile(pointMapsResultsDir,'ImParameters.mat'); searchRangeFile=fullfile(fotosResultsDir,'CSearchRange.mat'); - mpdmFile=fullfile(matDir,'MPDM.mat'); - + % Decent time to print some helpful vars if debug disp('Vars at end of main loop:') @@ -190,6 +173,7 @@ function varargout = EASYconsole(varargin) if exist(searchRangeFile, 'file') searchRangeNum=load(searchRangeFile); end + % Add easyDir to the MATLAB path % I have not idea if this is necessary or works but theoretically should % reduce directory scoping issues when calling scripts w/o a path @@ -197,12 +181,13 @@ function varargout = EASYconsole(varargin) % GUI interface design gui_Singleton=1; - gui_State=struct( 'gui_Name', mfilename, ... - 'gui_Singleton', gui_Singleton, ... - 'gui_OpeningFcn', @EASYconsole_OpeningFcn, ... - 'gui_OutputFcn', @EASYconsole_OutputFcn, ... - 'gui_LayoutFcn', [] , ... - 'gui_Callback', []); + gui_State=struct(... + 'gui_Name', mfilename, ... + 'gui_Singleton', gui_Singleton, ... + 'gui_OpeningFcn', @EASYconsole_OpeningFcn, ... + 'gui_OutputFcn', @EASYconsole_OutputFcn, ... + 'gui_LayoutFcn', [] , ... + 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback=str2func(varargin{1}); end @@ -256,15 +241,15 @@ function varargout = EASYconsole_OutputFcn(~, ~, handles) varargout{1}=handles.output; end -%% CONSOLE BUTTON INTERFACES %% +% Console button interfaces % File Button Interface function FileMenu_Callback(~, ~, ~) - %returnStartDir + % returnStartDir end % Load Experiment Button Interface function LoadExp_Callback(~, ~, ~) - %returnStartDir + % returnStartDir end % New Experiment Button Interface @@ -333,7 +318,7 @@ function NewExpDat_Callback(~, ~, ~) end % set the title for fhconsole depending on existence - if exist('easyResultsDir','var')&&~isempty(easyResultsDir) + if exist('easyResultsDir','var') && ~isempty(easyResultsDir) set(fhconsole,'Name',sprintf('EASYconsole - %s', easyResultsDir)); else set(fhconsole,'Name','EASYconsole - Master Plate directory not selected.');