% PARSE_REGENERATING_FRAGMENT studies the properties of the vascular tissue during regeneration % % Blanchoud Group, UNIFR % Simon Blanchoud % 06/12/2021 function parse_regenerating_fragments % load the required packages pkg load matgeom pkg load image % Absolute path to the directory containing the segmentation files fdir = '/home/guest/data/Nathalie/Quantifications/*.roi'; files = glob(fdir); nfiles = length(files); % Structures to store the data all_tunic = cell(nfiles, 2, 1); all_ampullae = cell(nfiles, 2, 4); all_vessels = cell(nfiles, 2, 2); all_evals = cell(nfiles, 2, 4); all_quants = cell(nfiles, 2, 4); % Display? plot_segmentations = true; % Loop through all files for i=1:nfiles % Extract the parts of the filename [dir, name, ext] = fileparts (files{i}); filename = fullfile(dir, name); % Inform the user on the progress disp(['Processing: ' name]); % The actual files imfile = [filename '.tif']; ijfile = [filename '.zip']; umfile = [filename '.txt']; tgfile = [filename '.roi']; % Extract the segmentations ROIs = ReadImageJROI(ijfile); [props, data] = analyze_ROI(ROIs, 'Slice', 'Area', 'Length', 'Centroid'); % Setup the various thresholds scale = csvread(umfile); tdist = (450/scale); rdist = ( 50/scale); pdist = ( 30/scale); vdist = ( 10/scale); % Measure the size of the dataset nframes = max(props(:,1)); ntracks = length(data); % Prepare some temporary variables start = props(:,1) 1); tri = tri(~tiny,:); edges = [tri(:,[1 2]); tri(:,[1 3]); tri(:,[2 3])]; [edges] = unique(edges, 'rows'); bads = ismember(edges, props(tiny,2:3), 'rows'); edges = edges(~bads,:); dist = bsxfun(@minus, pts(:,1), pts(:,1).').^2 + bsxfun(@minus, pts(:,2), pts(:,2).').^2; ind = sub2ind(size(dist), edges(:,1), edges(:,2)); edist = dist(ind); edges = edges(edist <= thresh2,:); periph = outer_edges(pts, edges, thresh); nneigh = histc(edges(:),[1:size(pts,1)]); return end % Identify the periphery of a network function periph = outer_edges(pts, edges) npts = size(pts, 1); sets = zeros(npts, 1); nsets = 0; dist = sqrt((pts(edges(:,1),1) - pts(edges(:,2),1)).^2 + (pts(edges(:,1),2) - pts(edges(:,2),2)).^2); periph = []; for i=1:npts first = find(sets==0, 1); if (isempty(first)) break; end nsets = nsets + 1; sets(first) = nsets; curr_set = first; for j=1:npts conx = ismember(edges, curr_set); conx = edges(any(conx, 2), :); new_set = unique(conx(:)); sets(new_set) = nsets; if numel(curr_set) == numel(new_set) break else curr_set = new_set; end end end pindx = [1:npts].'; for i=1:nsets curr = (sets==i); tmp = pts(curr,:); if (size(tmp,1)<=3) full = pindx(curr); else pcurr = pindx(curr); convs = pcurr(convhull(tmp(:,1), tmp(:,2))); full = []; for j=1:length(convs)-1 if ~any(all(edges==convs(j) | edges==convs(j+1), 2)) [d, p] = distancePointEdge(tmp, [pts(convs(j),:) pts(convs(j+1),:)]); d(d==0) = NaN; [v,dindx] = min(d); midpt = pcurr(dindx); p1 = grShortestPath(pts, edges, convs(j), midpt, dist); p2 = grShortestPath(pts, edges, midpt, convs(j+1), dist); full = [full; p1(1:end-1); p2(1:end-1)]; else full = [full; convs(j)]; end end end periph = [periph; pts(full([1:end 1]),:); NaN(1,2);]; end return end % Fuse close points function merged = merge_points(pts, thresh) merged = pts; if (size(pts, 1) < 2) return; end thresh = thresh*thresh; dist = bsxfun(@minus, pts(:,1), pts(:,1).').^2 + bsxfun(@minus, pts(:,2), pts(:,2).').^2; groups = {}; npts = size(pts,1); candidates = [1:npts]; for i=1:npts if isempty(candidates) break; end ncands = size(candidates, 1); cluster = false(ncands,1); cluster(1) = true; num = 1; for j=1:ncands d = dist(candidates(cluster), candidates); cluster = any(d