From 0ef1e10ed6f288da36f5c1e11c1fd5efb7138d46 Mon Sep 17 00:00:00 2001
From: Jurjen Broeke <jurjen.broeke@vu.nl>
Date: Thu, 7 Mar 2024 09:39:21 +0100
Subject: [PATCH] minor update

fixed implementation of importing ROIs.
added additional checks on user interaction (WIP).
---
 sCaSpA.m | 56 +++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/sCaSpA.m b/sCaSpA.m
index 3893020..330b5cd 100644
--- a/sCaSpA.m
+++ b/sCaSpA.m
@@ -174,11 +174,12 @@ classdef sCaSpA < matlab.apps.AppBase
     properties (Access = private)
         majVer = 1;
         % major versions - 230228 = 1 -> finish build the main interface, it will guide the user to the main steps of the analysis
-        minVer = 3;
+        minVer = 4;
         % minor versions - 230228 = 0 -> basic functionality implemented
         %                - 230320 = 1 -> add a new column for labeling the ROIs, one filter for the trace, and one filter for the FOV
         %                - 230405 = 2 -> Implemented loading *.nd2 files
         %                - 230425 = 3 -> Start implementation for cell clustering and out of network analysis
+        %                - 240307 = 4 -> implemented ROI loading from ImageJ RoiSet files
         dailyBuilt = 3;
         % bug fixes      - 230425 = 0 -> Several bug fixes and new implementations
         %                - 230504 = 1 -> Add batch import for movies
@@ -502,41 +503,62 @@ classdef sCaSpA < matlab.apps.AppBase
         
         function ImportROIs(app)
             roiPath = uigetdir(app.options.LastPath, 'Select ROI folder');
-            fileExt = '*.csv';
+            if roiPath == 0
+                % user canceled
+                return;
+            end
+            % use the zip extension to select RoiSets derived from ImageJ
+            fileExt = '*.zip';
             roiFiles = dir(fullfile(roiPath, fileExt));
+            
+            % perform a check to see if any files are present
+            if numel(roiFiles) == 0
+                return;
+            end
+
             togglePointer(app);
             figure(app.UIFigure);
             try
-                % Get the name info
-                nameList = regexprep({roiFiles.name}, fileExt, '');
+                % Get the name info (remove the asterisk for replacement)
+                nameList = regexprep({roiFiles.name}, fileExt(2:end), '');
                 nameParts = regexp(nameList, '_', 'split')';
                 nFiles = numel(nameList);
                 allRois = cell(size(app.imgT,1), 1);
                 for f = 1:nFiles
-                    % Match the ROI to the expetimentID
-                    expID = [nameParts{f}{2} '_' nameParts{f}{4}];
+                    % Match the ROI to the experimentID, compensate for
+                    % RoiSet_ prefix if required
+                    if matches(nameParts{f}{1}, 'RoiSet')
+                        expID = [nameParts{f}{3} '_' nameParts{f}{5}];
+                    else
+                        expID = [nameParts{f}{2} '_' nameParts{f}{4}];
+                    end
                     cellFltr = matches(app.dicT.ExperimentID, expID);
+                    if sum(cellFltr) == 0
+                        % this experiment ID could not be found, skip
+                        continue;
+                    end
                     % Extract the ROIs
                     if contains(fileExt, '*.zip')
-                        tempRoi = ReadImageJROI(fullfile({roiFiles(f).folder}, {roiFiles(f).name}));
-                        tempRoi = cellfun(@(x) x.vnRectBounds, tempRoi{:}, 'UniformOutput', false);
+                        tempRoi = ReadImageJROI(fullfile(roiFiles(f).folder, roiFiles(f).name));
+                        tempRoi = cellfun(@(x) x.vnRectBounds, tempRoi, 'UniformOutput', false);
                     else
-                        tempRoi = ReadImageJROI(fullfile({roiFiles(f).folder}, {roiFiles(f).name}));
+                        tempRoi = ReadImageJROI(fullfile(roiFiles(f).folder, roiFiles(f).name));
                     end
                     % Get the center coordinate
                     tempRoi = cellfun(@(x) round([mean(x([2 4])), mean(x([1 3]))]), tempRoi, 'UniformOutput', false);
                     % Add the ROI to the right cells
                     allRois{cellFltr} = cell2mat(tempRoi');
-                    % Load the data
-                    getIntensity(app)
                 end
+                %assign the updated ROIs
                 app.dicT.RoiSet = allRois;
+                % Load the data
+                getIntensity(app)
                 togglePointer(app)
             catch ME
-                delete(hWait);
                 togglePointer(app);
                 disp(ME)
                 errordlg('Failed to import the RoiSet. Please check command window for details', 'Import ROIs failed');
+                return;
             end
             modifyROIs(app, 'Modify')
         end
@@ -1025,11 +1047,15 @@ classdef sCaSpA < matlab.apps.AppBase
                     tempDIC = contains(app.dicT.CellID, app.DropDownDIC.Value);
                     allRoi = app.dicT.RoiSet{tempDIC};
                     nRoi = size(allRoi,1);
+                    if nRoi == 0
+                        % there are no ROIs, so stop processing
+                        return;
+                    end
                     % Delete the existing mask and replace it
                     app.patchMask = [];
                     delete(findobj(app.UIAxesDIC, 'Type', 'Patch'));
                     delete(findobj(app.UIAxesMovie, 'Type', 'Patch'));
-                    for r=1:nRoi
+                    for r=nRoi:-1:1
                         if strcmp(app.options.RoiShape, 'Square')
                             tempRoi = [allRoi(r,:) - app.options.RoiSize; allRoi(r,:) + app.options.RoiSize];
                             roiX = [tempRoi(1,1) tempRoi(2,1) tempRoi(2,1) tempRoi(1,1)];
@@ -2398,7 +2424,7 @@ classdef sCaSpA < matlab.apps.AppBase
                 %hWait = varargin{2};
             end
             for i = 1:nImages
-                tic
+                %tic
                 nRoi = size(tempRoi,1);
                 hWait.Message = sprintf('Getting %d ROIs intensity in FOV: %s', nRoi, app.imgT.CellID{imgFltr(i)});
                 imgInfo = app.imgT.ImgProperties(imgFltr(i),:);
@@ -2433,7 +2459,7 @@ classdef sCaSpA < matlab.apps.AppBase
                 % Since we already loaded the data anyway, save the StdDev
                 tempImg = std(double(imgData),[],3);
                 app.stdT{tempDIC} = tempImg;
-                loadTime = toc;
+                %loadTime = toc;
             end
             if nargin < 3
                 close(hWait);
-- 
GitLab