Project

General

Profile

Files » MRI_Leaf_Infection_Tools.txt

Analyze virus infections in leaves - Volker Baecker, 02/02/2012 03:19 PM

 
/**
* MRI Leaf Infection Tools
*
* written 2012 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
*/

var helpURL = "http://dev.mri.cnrs.fr/wiki/imagej-macros/Leaf_Infection_Tools";
var greenChannel = "GFP";
var redChannel = "RFP";
var thresholdingMethod = "Huang";
var isCalculateCorrelation = true;
var isCountAreas = true;
var minSize = 10;

macro "Unused Tool - C037" { }

macro "MRI Leaf Infection Tools Help Action Tool - Cf00D18D19D28D29D38D73D74D83D84D9aD9bDaaDabCfffD00D01D0dD0eD0fD10D1eD1fD20D2eD2fD30D3eD3fD40D4fD50D5fD60D6fD70D80D90Da0Db0DbfDc0Dc1Dc2Dc3Dc4DceDcfDd0Dd1Dd2Dd3Dd4DddDdeDdfDe0De1De2De3De4DebDecDedDeeDefDf0Df1Df2Df3Df4Df5Df9DfaDfbDfcDfdDfeDffC0f0D02D03D06D07D08D09D0aD0cD11D12D13D14D1cD1dD21D22D23D24D25D2bD2cD2dD31D33D34D35D3bD3cD3dD41D44D45D46D4aD4bD4cD4eD51D52D53D56D57D59D5aD5bD5eD61D62D63D64D67D69D6aD6dD6eD71D72D75D79D7cD7dD7eD7fD81D82D85D86D8bD8cD8dD8fD91D93D94D95D96D97D99D9cD9fDa1Da6Da7Da9DaeDafDb1Db2Db3Db4Db7Db9DbaDbdDbeDc5Dc7Dc9DccDcdDd5DdaDdbDdcDe5De8De9DeaDf6Df8C888D04D05D0bD15D16D17D1aD1bD26D27D2aD32D36D37D39D3aD42D43D47D48D49D4dD54D55D58D5cD5dD65D66D68D6bD6cD76D77D78D7aD7bD87D88D89D8aD8eD92D98D9dD9eDa2Da3Da4Da5Da8DacDadDb5Db6Db8DbbDbcDc6Dc8DcaDcbDd6Dd7Dd8Dd9De6De7Df7"{
run('URL...', 'url='+helpURL);
}

macro "adjust display Action Tool - C037T4d14a" {
run("Grays");
run("Enhance Contrast", "saturated=0.35");
}

macro "measure Action Tool - C037T4d14m" {
setBatchMode(true);
title = getTitle();
inDir = getDirectory("image");
outDir = inDir + "/" + "control";
if (!File.exists(outDir)) File.makeDirectory(outDir);
titleRed = replace(title, greenChannel, redChannel);
run("Set Measurements...", "area perimeter display redirect=None decimal=3");
nr = roiManager("count");
correlation = newArray(nr / 2);
numberOfAreasGreen = newArray(nr / 2);
numberOfAreasRed = newArray(nr / 2);
for (i=0; i<nr; i=i+2) {
leafNr = (i / 2) + 1;
print("\\Update:" + "analyzing leaf " + leafNr + " of " + (nr / 2));
if (isCalculateCorrelation) {
r = calculateCorrelation(title, titleRed, leafNr);
correlation[leafNr-1] = r;
}
selectWindow(title);
end = lastIndexOf(title, ".");
currentTitle = substring(title, 0, end) + "-" +leafNr + ".tif";
end = lastIndexOf(titleRed, ".");
currentTitleRed = substring(titleRed, 0, end) + "-" + leafNr + ".tif";
roiManager("Select", i);
run("Duplicate...", "title=" + currentTitle);
// leaf
roiManager("Select", i+1);
run("Clear Outside");
run("Measure");
// green
setAutoThreshold(thresholdingMethod + " dark");
run("Create Selection");
run("Measure");
run("Clear Outside");
// red
selectWindow(titleRed);
roiManager("Select", i);
run("Duplicate...", "title=" + currentTitleRed);
roiManager("Select", i+1);
run("Clear Outside");
setAutoThreshold(thresholdingMethod + " dark");
run("Create Selection");
run("Measure");
run("Clear Outside");
// red AND green
imageCalculator("Min create", currentTitle, currentTitleRed);
setThreshold(1, 65535);
run("Create Selection");
run("Measure");
end = lastIndexOf(title, ".");
overlayTitle = substring(title, 0, end-3) + "ovr-" + leafNr + ".tif";
rename(overlayTitle);
run("Merge Channels...", "red="+currentTitleRed + " green=" + currentTitle +" blue=*None* gray=*None* keep");
roiManager("Select", i+1);
run("Draw");
selectWindow(overlayTitle);
selectWindow("RGB");
run("Restore Selection");
run("Draw");
end = lastIndexOf(title, ".");
controlTitle = substring(title, 0, end-3) + "CTL-" + leafNr + ".jpg";
saveAs("Jpeg", outDir + "/" + controlTitle);
close();
selectWindow(currentTitle);
close();
selectWindow(currentTitleRed);
close();
selectWindow(overlayTitle);
close();
if (isCountAreas) {
a = countNumberOfAreasGreen(title, leafNr);
numberOfAreasGreen[leafNr-1] = a;
a = countNumberOfAreasRed(titleRed, leafNr);
numberOfAreasRed[leafNr-1] = a;
}

}
// copy to new table
tableName = title+"-measurements";
run("Table...", "name="+ tableName +" width=800 height=600");
tableName = "["+tableName+"]";
headings = "\\Headings:leaf\tleaf-area\t"+greenChannel+"-area\t"+redChannel+"-area\toverlap-area\tleaf-perim.\t"+greenChannel+"-perim.\t"+redChannel+"-perim.\toverlap-perim.";
if (isCalculateCorrelation) headings = headings + "\tR";
if (isCountAreas) headings = headings + "\tareas-green\tareas-red";
print(tableName, headings);
for (i=0; i<nResults; i=i+4) {
image = getResultLabel(i+1);
leafArea = getResult("Area", i);
leafPerimeter = getResult("Perim.", i);
greenArea = getResult("Area", i+1);
greenPerimeter = getResult("Perim.", i+1);
redArea = getResult("Area", i+2);
redPerimeter = getResult("Perim.", i+2);
overlapArea = getResult("Area", i+3);
overlapPerimeter = getResult("Perim.", i+3);
line = ""+image+"\t"+leafArea +"\t"+greenArea +"\t"+redArea +"\t"+overlapArea + "\t" +leafPerimeter+"\t"+greenPerimeter+"\t"+redPerimeter+"\t"+overlapPerimeter;
if (isCalculateCorrelation) {
r = correlation[i/4];
line = line + "\t" + r;
}
if (isCountAreas) {
greenAreas = numberOfAreasGreen[i/4];
redAreas = numberOfAreasRed[i/4];
line = line + "\t" + greenAreas+"\t" + redAreas;
}
print(tableName, line);
}
selectWindow("Results");
run("Close");
beep();
print("FINISHED");
setBatchMode("exit and display");
}

macro 'measure Action Tool Options' {
Dialog.create("Leaf Infection Tool Options");
Dialog.addString("green channel", greenChannel);
Dialog.addString("red channel", redChannel);
Dialog.addChoice("thresholding method", newArray("Default", "Huang", "Intermodes", "IsoData", "IJ_IsoData", "Li", "MaxEntropy", "Mean", "MinError", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle", "Yen"), thresholdingMethod);
Dialog.addCheckbox("calculate correlation", isCalculateCorrelation);
Dialog.addCheckbox("count areas", isCountAreas);
Dialog.addNumber("min. size", minSize);
Dialog.show();
greenChannel = Dialog.getString();
redChannel = Dialog.getString();
thresholdingMethod = Dialog.getChoice();
isCalculateCorrelation = Dialog.getCheckbox();
isCountAreas = Dialog.getCheckbox();
minSize = Dialog.getNumber();
}

function calculateCorrelation(title, titleRed, leafNr) {
selectWindow(title);
end = lastIndexOf(title, ".");
currentTitle = substring(title, 0, end) + "-" +leafNr + ".tif";
end = lastIndexOf(titleRed, ".");
currentTitleRed = substring(titleRed, 0, end) + "-" + leafNr + ".tif";
roiManager("Select", i);
run("Duplicate...", "title=" + currentTitle);
// leaf
roiManager("Select", i+1);
selectWindow(titleRed);
roiManager("Select", i);
run("Duplicate...", "title=" + currentTitleRed);
roiManager("Select", i+1);
correlation = call("analysis.CorrelationCalculator.correlation", currentTitle, currentTitleRed);
selectWindow(currentTitle);
close();
selectWindow(currentTitleRed);
close();
return correlation;
}

function countNumberOfAreasGreen(title, leafNr) {
selectWindow(title);
end = lastIndexOf(title, ".");
currentTitle = substring(title, 0, end) + "-" +leafNr + ".tif";
roiManager("Select", i);
run("Duplicate...", "title=" + currentTitle);
// leaf
roiManager("Select", i+1);
run("Clear Outside");
setAutoThreshold(thresholdingMethod + " dark");
IJ.renameResults("tmp-res");
run("Analyze Particles...", "size="+minSize+"-Infinity pixel circularity=0.00-1.00 show=Outlines display");
result = nResults;
selectWindow("Results");
run("Close");
selectWindow("Drawing of " + currentTitle);
controlTitle = substring(title, 0, end-3) + "CTL-areas-green-" + leafNr + ".jpg";
saveAs("Jpeg", outDir + "/" + controlTitle);
close();
selectWindow("tmp-res");
IJ.renameResults("Results");
selectWindow(currentTitle);
close();
return result;
}

function countNumberOfAreasRed(title, leafNr) {
selectWindow(title);
end = lastIndexOf(title, ".");
currentTitle = substring(title, 0, end) + "-" +leafNr + ".tif";
roiManager("Select", i);
run("Duplicate...", "title=" + currentTitle);
// leaf
roiManager("Select", i+1);
run("Clear Outside");
setAutoThreshold(thresholdingMethod + " dark");
IJ.renameResults("tmp-res");
run("Analyze Particles...", "size="+minSize+"-Infinity pixel circularity=0.00-1.00 show=Outlines display");
result = nResults;
selectWindow("Results");
run("Close");
selectWindow("Drawing of " + currentTitle);
controlTitle = substring(title, 0, end-3) + "CTL-areas-red-" + leafNr + ".jpg";
saveAs("Jpeg", outDir + "/" + controlTitle);
close();
selectWindow("tmp-res");
IJ.renameResults("Results");
selectWindow(currentTitle);
close();
return result;
}
(1-1/5)