This repository has been archived by the owner. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added an alignment script, added a load function in main for alignmen…
…t script data
- Loading branch information
1 parent
92b16af
commit f16fcdd
Showing
6 changed files
with
600 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
clear all | ||
close all | ||
clc | ||
%% Set Paramters | ||
inFolder = 'D:\data\Leona'; | ||
outFolder = 'D:\data\out'; | ||
binningFactor = [1, 1, 2]; | ||
nSubIm = 1000;%number of subImages for med align | ||
referenceChannel = 0; %0: green, 1: red; reference channel for shift corrections | ||
numShiftCorrections = 1; %number of shift corrections | ||
%% Load Files | ||
currentFolder = pwd; | ||
cd(inFolder); | ||
folders = dir; | ||
|
||
for k = length(folders):-1:1 | ||
% remove non-folders | ||
if ~folders(k).isdir | ||
folders(k) = [ ]; | ||
continue | ||
end | ||
% remove folders starting with . | ||
fname = folders(k).name; | ||
if fname(1) == '.' | ||
folders(k) = [ ]; | ||
end | ||
end | ||
|
||
numFolders = length(folders); | ||
|
||
%iterate through all folders | ||
for f = 1:numFolders | ||
%go into folder | ||
cd(inFolder); | ||
cd(folders(f).name); | ||
greenFiles = dir('ChanA_*_*.tif'); | ||
redFiles = dir('ChanB_*_*.tif'); | ||
%check if fileNumber is the same | ||
if size(greenFiles, 1) ~= size(redFiles, 1) | ||
error(['Unequal number of files in trial folder "', folders(f).name, '"!!']); | ||
end | ||
numFiles = size(greenFiles, 1); | ||
|
||
%load first red frame to get resolution and being able to preallocate | ||
temp = readTiffNoStack(redFiles(1).name); | ||
res = size(temp); | ||
|
||
%preallocate data_r | ||
data_r = zeros(res(1), res(2), numFiles); | ||
data_g = zeros(res(1), res(2), numFiles); | ||
%load data | ||
disp(['Loading Files In Folder ', folders(f).name]); | ||
parfor i=1:numFiles | ||
data_r(:,:,i) = readTiffNoStack(redFiles(i).name); | ||
data_g(:,:,i) = readTiffNoStack(greenFiles(i).name); | ||
end | ||
disp(['Shift Correction Of Files In Folder ', folders(f).name]); | ||
|
||
%save stacks in memory | ||
cd(outFolder); | ||
disp(['Write Files Of Folder ', folders(f).name ' in ' outFolder ' to RAM']); | ||
data_r_complete{f} = data_r; | ||
data_g_complete{f} = data_g; | ||
end | ||
|
||
%go back to old current folder | ||
cd(currentFolder); | ||
clear data_r data_g | ||
data_r = data_r_complete; | ||
data_g = data_g_complete; | ||
%create big array | ||
numFrames = 0; | ||
numFrameList = zeros(1,numFolders); | ||
for i = 1:numFolders | ||
numFrames = numFrames + size(data_g{i},3); | ||
numFrameList(i+1) = numFrames; | ||
end | ||
|
||
%preallocate data | ||
data_r_array = zeros(size(data_g{i},1),size(data_g{i},2),numFrames, class(data_r{1})); | ||
data_g_array = zeros(size(data_g{i},1),size(data_g{i},2),numFrames, class(data_g{1})); | ||
for i = 1:numFolders | ||
data_r_array(:,:,(numFrameList(i)+1):numFrameList(i+1)) = data_r{i}; | ||
data_r{i} = []; | ||
data_g_array(:,:,(numFrameList(i)+1):numFrameList(i+1)) = data_g{i}; | ||
data_g{i} = []; | ||
end | ||
clear data_r_complete data_g_complete data_r data_g | ||
data_r = data_r_array; | ||
data_g = data_g_array; | ||
clear data_g_array data_r_array | ||
%% Do Shift Correction | ||
wb = waitbar(0, 'Performing Shift Correction'); | ||
|
||
for j = 1:numShiftCorrections | ||
if referenceChannel | ||
referenceChannelData = data_r; | ||
else | ||
referenceChannelData = data_g; | ||
end | ||
%Med Align | ||
parfor f=1:size(referenceChannelData,3) | ||
referenceChannelData(:,:,f) = imgaussfilt(referenceChannelData(:,:,f),2); | ||
end | ||
[rs, cs] = medAlign(referenceChannelData, nSubIm, 50, -inf); | ||
%shift red and green channel | ||
waitbar(j/numShiftCorrections, wb, 'Performing Med Align..'); | ||
parfor i = 1:size(data_g,3) | ||
data_r(:,:,i) = circshift(data_r(:,:,i),[rs(i),cs(i)]); | ||
data_g(:,:,i) = circshift(data_g(:,:,i),[rs(i),cs(i)]); | ||
end | ||
end | ||
%matVis(data_r,data_g); | ||
%% Export Files | ||
%save trial files | ||
for i=1:numFolders | ||
waitbar(i/numFolders, wb, 'Saving Trial Tiff Files..'); | ||
%binning | ||
data_r_save = uint16(binning(data_r(:,:,numFrameList(i)+1:numFrameList(i+1)), binningFactor, 'mean')); | ||
data_g_save = uint16(binning(data_g(:,:,numFrameList(i)+1:numFrameList(i+1)), binningFactor, 'mean')); | ||
|
||
saveastiff(data_r_save, [outFolder filesep folders(i).name '_r.tif']); | ||
saveastiff(data_g_save, [outFolder filesep folders(i).name '_g.tif']); | ||
end | ||
|
||
%save big tiff file | ||
data_r = uint16(binning(data_r, binningFactor, 'mean')); | ||
data_g = uint16(binning(data_g, binningFactor, 'mean')); | ||
disp('Saving Big Data Set..'); | ||
waitbar(0, wb, 'Saving Big Tiff Files..'); | ||
options.big = true; | ||
saveastiff(data_r, [outFolder filesep 'complete_r.tif'], options); | ||
waitbar(0.5, wb, 'Saving Big Tiff Files..'); | ||
saveastiff(data_g, [outFolder filesep 'complete_g.tif'], options); | ||
%% | ||
close(wb); | ||
disp('Finished Script!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
function [data_g_array, data_r_array, numFrameList, fn_green, fn_red, greenFiles, redFiles] = CellSortLoadFilesFromAlignmentScript(outFolder) | ||
greenFiles = dir('*_g*.tif*'); | ||
redFiles = dir('*_r*.tif*'); | ||
%delete complete file from file list | ||
greenFiles(end)=[]; | ||
redFiles(end)=[]; | ||
|
||
fn_green = greenFiles(1).name; | ||
fn_red = redFiles(1).name; | ||
%load files | ||
numberGreen = size(greenFiles,1); | ||
numberRed = size(redFiles,1); | ||
if numberGreen ~= numberRed | ||
error('Unequal number of red and green channel files!!!'); | ||
end | ||
|
||
%load trials | ||
disp('Loading Trial Files..'); | ||
parfor i = 1:numberGreen | ||
data_r{i} = readTiff(redFiles(i).name); | ||
data_g{i} = readTiff(greenFiles(i).name); | ||
end | ||
|
||
%put them in single arrays | ||
numFrames = 0; | ||
numFrameList = zeros(1,numberGreen); | ||
for i = 1:numberGreen | ||
numFrames = numFrames + size(data_g{i},3); | ||
numFrameList(i+1) = numFrames; | ||
end | ||
|
||
%preallocate data | ||
data_r_array = zeros(size(data_g{i},1),size(data_g{i},2),numFrames, class(data_r{1})); | ||
data_g_array = zeros(size(data_g{i},1),size(data_g{i},2),numFrames, class(data_g{1})); | ||
disp('Creating Big Data Set..'); | ||
for i = 1:numberGreen | ||
data_r_array(:,:,(numFrameList(i)+1):numFrameList(i+1)) = data_r{i}; | ||
data_r{i} = []; | ||
data_g_array(:,:,(numFrameList(i)+1):numFrameList(i+1)) = data_g{i}; | ||
data_g{i} = []; | ||
end | ||
|
||
%calculate quality plot | ||
reference = mean(data_r_array, 3); | ||
qualityPlot = zeros(1,size(data_r_array, 3)); | ||
parfor i = 1:size(data_r_array,3) | ||
qualityPlot(i) = sqrt(squeeze(mean(mean((cast(data_r_array(:,:,i),'like', reference) - reference).^2,1),2))); | ||
end | ||
figure('Name','Quality Plot'); | ||
plot(qualityPlot); | ||
for i=1:numberGreen | ||
vline(numFrameList(i), 'black', greenFiles(i).name); | ||
end | ||
title('Quality Of Shift Correction'); | ||
xlabel('frame'); | ||
ylabel('std of frame'); | ||
|
||
disp('Finished Loading Trial Files!'); | ||
end |
Oops, something went wrong.