Project

General

Profile

Files » Batch_Split_Channels_Tool.txt

Volker Baecker, 02/05/2014 05:19 PM

 
/**
* Batch splt channels
*
* Allows to split the channels of images with an arbitrary number of dimensions. The tool works
* in batch mode on all images (of two possible types) in a given folder. The results of images type 1
* will be written into folders sim/c1, sim/c2, etc. those of type 2 into folders widefield/c1, widefield/c2, etc.
*
* If you have only one image type use the first image type to set it.
*
* written 2014 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
*/

var _IMAGE_TYPE1 = "SIR_ALX.dv";
var _IMAGE_TYPE2 = "PRJ_ALX.dv";
var _RESULT_FOLDER1 = "sim";
var _RESULT_FOLDER2 = "widefield";
var _PROJECTION_METHOD = "Max Intensity";

var _DIR;
var _MAX_PROGRESS;
var _CURRENT_PROGRESS;

var helpURL = "http://dev.mri.cnrs.fr/projects/imagej-macros/wiki/Batch_Split_Channels";

macro "batch split channels [f8]" {
batchSplitChannels();
}

macro "batch split channels (f8) Action Tool - C000D10D11D12D13D14D15D16D17D18D19D1aD1bD20D21D22D23D24D25D26D27D28D29D2aD30D31D32D33D34D35D36D37D38D39D3fD40D41D42D43D44D45D46D47D48D4eD4fD50D51D52D53D54D55D56D57D5dD5eD5fD60D61D62D63D64D65D66D6cD6dD6eD6fD70D71D72D73D74D75D7bD7cD7dD7eD7fD80D81D82D83D84D8aD8bD8cD8dD8eD8fD90D91D92D93D99D9aD9bD9cD9dD9eD9fDa0Da1Da2Da8Da9DaaDabDacDadDaeDafDb0Db1Db8Db9DbaDbbDbcDbdDbeDbfDc0Dc7Dc8Dc9DcaDcbDccDcdDceDcfDd6Dd7Dd8Dd9DdaDdbDdcDddDdeDdfDe5De6De7De8De9DeaDebDecDedDeeDefDf4Df5Df6Df7Df8Df9DfaDfbDfcDfdDfeDffC000D1fC788D95C334D1cDc6Dd0Dd5De4CbbcDc5Dd4C333D2bD3aD49D58D67D76Da3Db2Dc1CaabDb3Dc2Dd1C888D00D01D02D03D04D05D06D07D08D09D0aD0bD0cD0fCddeDc4Dd3C333D3eD4dD5cD6bD7aD89D98Da7CaabD3dD5bD6aD79D88D97De0C455Df0Df2CcddD2dD3cD4bD5aD69D78D87Da5Db4Dc3Dd2De2CabbD2cD3bD4aD59D68D77D86Da6CddeDb5C222Df3C99aD2eCccdD96CabbDa4De3C555Df1C011D2fDb7C888D0eC233D85D94CaaaD4cCcddDe1C677D1eC9aaD1dDb6C899D0d" {
batchSplitChannels();
}

macro "batch split channels (f8) Action Tool Options" {
Dialog.create("Batch Split Channels Options");
Dialog.addString("image type 1", _IMAGE_TYPE1);
Dialog.addString("image type 2", _IMAGE_TYPE2);
Dialog.addString("result folder 1", _RESULT_FOLDER1);
Dialog.addString("result folder 2", _RESULT_FOLDER2);
Dialog.addChoice("projection method", newArray("Average Intensity", "Max Intensity", "Min Intensity", "Sum Slices", "Standard Deviation", "Median"), _PROJECTION_METHOD);
Dialog.addMessage("Press the help button below to open the online help!");
Dialog.addHelp(helpURL);
Dialog.show();
_IMAGE_TYPE1 = Dialog.getString();
_IMAGE_TYPE2 = Dialog.getString();
_RESULT_FOLDER1 = Dialog.getString();
_RESULT_FOLDER2 = Dialog.getString();
_PROJECTION_METHOD = Dialog.getChoice();
}

function batchSplitChannels() {
setBatchMode(true);
print("\\Clear");
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
ts = toString(dayOfMonth) + "-" + (month+1) + "-" + year + " " + hour + ":" + minute + ":" + second + "." + msec;
IJ.log("STARTED batch split channels: " + ts);
_DIR = getDirectory("select the input folder");
files = getFileList(_DIR);
splitChannelsOfFiles(files);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
ts = toString(dayOfMonth) + "-" + (month+1) + "-" + year + " " + hour + ":" + minute + ":" + second + "." + msec;
IJ.log("FINISHED batch split channels: " + ts);
setBatchMode("exit & display");
}

function splitChannelsOfFiles(files) {
filesType1 = filterInterestingFiles(files, _IMAGE_TYPE1);
filesType2 = filterInterestingFiles(files, _IMAGE_TYPE2);
_MAX_PROGRESS = filesType1.length + filesType2.length;
if (_MAX_PROGRESS ==0) {
IJ.log("No files selected! Macro stopped.");
return;
}
_CURRENT_PROGRESS = 0;
if (filesType1.length>0) File.makeDirectory(_DIR + "/" + _RESULT_FOLDER1);
if (filesType2.length>0) File.makeDirectory(_DIR + "/" + _RESULT_FOLDER2);
splitAll(filesType1, _DIR + "/" + _RESULT_FOLDER1) ;
splitAll(filesType2, _DIR + "/" + _RESULT_FOLDER2) ;
}

function splitAll(files, targetFolder) {
for (i=0; i<files.length; i++) {
print("\\Update1:Loading file " + (_CURRENT_PROGRESS+1) + " of " + _MAX_PROGRESS + " - " + files[i]);
run("Bio-Formats Importer", "open=["+ _DIR + files[i] + "] color_mode=Default view=Hyperstack stack_order=XYCZT");
print("\\Update1:Processing file " + (_CURRENT_PROGRESS+1) + " of " + _MAX_PROGRESS + " - " + files[i]);
getDimensions(width, height, channels, slices, frames);
run("Split Channels");
for (d=channels; d>0; d--) {
if (!File.exists(targetFolder+"C" + toString(d))) File.makeDirectory(targetFolder+"/" + "C" + toString(d));
title = getTitle();
parts = split(title, ".");
save(targetFolder + "/C" + toString(d) + "/" + parts[0] + ".tif");
if (slices>1) {
run("Z Project...", "start=1 stop="+slices+" projection=" + "[" + _PROJECTION_METHOD + "]");
title = getTitle();
parts = split(title, ".");
save(targetFolder + "/C" + toString(d) + "/" + parts[0] + ".tif");
close();
}
close();
}
_CURRENT_PROGRESS = _CURRENT_PROGRESS + 1;
}
}

function filterInterestingFiles(files, suffix) {
counter = 0;
for (i=0; i<files.length; i++) {
if (endsWith(files[i], suffix)) {
counter = counter + 1;
}
}
result_files = newArray(counter);
index = 0;
for (i=0; i<files.length; i++) {
if (endsWith(files[i], suffix)) {
result_files[index] = files[i];
index = index + 1;
}
}
return result_files;
}
(2-2/3)