Macro to meassure cytoplasm to nucleus translocation

Hello everyone, and thank you for a nice and constructive forum.

I am trying to setup a macro to meassure the staining intensity in the nucleus, and in the viscinity outside. I have set it up now, so that the macro stops after the nucleus meassure so I can paste it into a spreadsheet, and again, after the band region around the nucleus has been meassured. THen I calculate the ratio manually in excell, however it is tedious for >3000 images…
I hope one of you experts in here can help me optimize it to make it more convenient.
At first, I tried to set it up to give me the two columns of data I want (mean of nucleus and mean of band around nucleus) next to each other, but I cannot figure out how to do this. I get the first column, and since both are “mean gray value”, the second line of data is printed underneath and not very helpful when I want to pair the two sets of data…
Obviously, if I could get this pair of data in separate columns paired to the same cell from which they came, this would be a good start. Next, if ImageJ can do the calculation of dividing these two in a third column, this would make my day!

thank you in advance! Looking very much forward to reading responses :slight_smile:
Below is an example of a responder well (green nuclei) and control. 20201211_E8_SlideC_6h_C4_CTR_0_2.tif (1.0 MB)

20201211_E8_SlideC_6h_C3_CTR_100_11.tif (1.0 MB)

the code so far:
run(“Split Channels”);
run(“Gaussian Blur…”, “sigma=2”);
setAutoThreshold(“Default dark”);
//does thresholding, sets to binary 0 or 255;
setOption(“BlackBackground”, false);
run(“Convert to Mask”);
run(“Fill Holes”);
run(“Watershed”);
//Segmentation of 1st pic, DAPI
run(“Set Measurements…”, “mean decimal = 3”);
run(“Analyze Particles…”, “size=5-Infinity pixel show=[Overlay Masks] display exclude add in_situ”);
//Creates ROIs from Nuclei
close();
run(“From ROI Manager”);
Table.deleteRows(0, 10000);
//deletes meassurements from Dapi ch which are unnecessary
roiManager(“Measure”);
waitForUser(“copy mean nuclear results”, “then click OK”);
run(“Clear Results”);
counts=roiManager(“count”);
for(j=0; j<counts; j++) {
roiManager(“Select”, j);
run(“Make Band…”, “band=0.05”);
roiManager(“Update”);
}

roiManager(“Deselect”);
roiManager(“Measure”);
waitForUser(“copy mean cytoplasmic Intensity”, “then click OK”);
close();
run(“Clear Results”);
run(“From ROI Manager”);
roiManager(“Deselect”);
roiManager(“Measure”);
for (k = 0; k < nResults; k + +)
{
MI + = getResult(‘Mean’, k);
}
{
AMI = (MI/k);//calculates mean intensity
print(getTitle());
print(“Count:” +k);
print (“Avg. Intensity:” + AMI);
}
roiManager(“Deselect”);
roiManager(“Delete”);
Table.deleteRows(0, 10000);
// closes all images before the next iteration
while(nImages > 0) close();

p.s. the final part is a meassure of the red channel I added to get that data too, but not part of today’s problem perse :wink:

Hi Helena,

It is possible to create extra columns in the results table by defining new column names. I have modified your macro code to rename the column showing the nucleus measurements from the default ‘Mean’ to ‘Nucleus’. The code then measures the band around each nucleus using the getStatitistics command and adds that measurement to a second column in the results table labelled ‘Band’. It then also calculates the ratio of the band to the nucleus and adds the value to a third column of the results table. This is all done within the loop that creates the band for each ROI.
At the end of the measurements, each column is summarised. I have made a few other small changes such as removing the ‘Display’ option in the first ‘Analyze Particles’ as you don’t want the DAPI measurements in the results table and delete them straight afterwards anyway.

Hope this helps with your workflow.
Cheers,
Volko

run("Split Channels");
run("Gaussian Blur...", "sigma=2");
setAutoThreshold("Default dark");
//does thresholding, sets to binary 0 or 255;
setOption("BlackBackground", false);
run("Convert to Mask");
run("Fill Holes");
run("Watershed");
//Segmentation of 1st pic, DAPI
run("Set Measurements...", "mean decimal = 3");
// removed 'display' as you don't need the measurements at this stage
run("Analyze Particles...", "size=5-Infinity pixel show=[Overlay Masks] exclude add in_situ");   
//Creates ROIs from Nuclei
close();
run("From ROI Manager");
//next line not needed anymore as the earlier Analyze Particle dosen't create measurements
//Table.deleteRows(0, 10000);   
//deletes meassurements from Dapi ch which are unnecessary
//you could have also used run("Clear Results"); will clear results table rather than just a fixed number of rows

roiManager("Measure");
Table.renameColumn("Mean", "Nucleus");					//rename column 'Mean' to 'Nucleus'  
//waitForUser("copy mean nuclear results", "then click OK");
//don't clear results table
//run("Clear Results");
counts=roiManager("count");
for(j=0; j<counts; j++) {
	roiManager("Select", j);
	run("Make Band...", "band=0.05");
	roiManager("Update");
	getStatistics(area, mean, min, max, std, histogram); //measure band
	setResult("Band",j,mean);							 //write mean band intensity to new column 'Band' in results table
	setResult("Ratio",j,mean/getResult("Nucleus"));		 //calculate ratio of band to nuclues and write it to column 'Ratio'
};
run("Summarize");										//Summarise results table

/*  This should no longer be required
roiManager("Deselect");
roiManager("Measure");
waitForUser("copy mean cytoplasmic Intensity", "then click OK");
close();
run("Clear Results");
run("From ROI Manager");
roiManager("Deselect");
roiManager("Measure");
for (k = 0; k < nResults; k + +)
{
MI + = getResult("Mean", k);
}
{
AMI = (MI/k);//calculates mean intensity
*/
print(getTitle());
print("Count:" +nResults);        						
print ("Avg. Intensity:" + getResult("Nucleus",nResults-4));
//}

roiManager("Deselect");
roiManager("Delete");
//Table.deleteRows(0, 10000);
// closes all images before the next iteration
while(nImages > 0) close();

[quote=“Volko, post:2, topic:46586”]

Dear Volko,
thank you so much for taking your time to look at my problem, and even within 24hours - very much appreciated :slight_smile: !
I ran your optimized code, and indeed I get the two columns I asked for. However, the calculation of the ratio is off somehow. I tried different combinations of dividing numbers in the two columns ("nucleus" and "band"), to try and work out what is actually being calculated, but without success. Nevertheless, from this optimized results table, I can easily do the division in a spreadsheet afterwards. 

I put in one of the readouts here if you are interested:
![translocation macro_Volko|264x500](upload://whjlEHQIFqGJrDgbExOie2Jka9S.jpeg) 

thanks again! And also for the cleanup you did on the code ;)
1 Like

1 Like

Hi Helena,
Sorry, my mistake - I forgot to define the row when reading the value from the results table to calculate the ratio (the line setResult(“Ratio”,j,mean/getResult(“Nucleus”,j)); was missing the j in the getResult command). So, the macro used the last value from the list to calculate the ratios, which resulted in the odd results. Below is an updated macro that fixes the issue.
Cheers,
Volko

run("Split Channels");
run("Gaussian Blur...", "sigma=2");
setAutoThreshold("Default dark");
//does thresholding, sets to binary 0 or 255;
setOption("BlackBackground", false);
run("Convert to Mask");
run("Fill Holes");
run("Watershed");
//Segmentation of 1st pic, DAPI
run("Set Measurements...", "mean decimal = 3");
run("Analyze Particles...", "size=5-Infinity pixel show=[Overlay Masks] exclude add in_situ");   
//Creates ROIs from Nuclei
close();
run("From ROI Manager");

roiManager("Measure");
Table.renameColumn("Mean", "Nucleus");					//rename column 'Mean' to 'Nucleus'  
counts=roiManager("count");
for(j=0; j<counts; j++) {
	roiManager("Select", j);
	run("Make Band...", "band=0.05");
	roiManager("Update");
	getStatistics(area, mean, min, max, std, histogram); //measure band
	setResult("Band",j,mean);							 //write mean band intensity to new column 'Band' in results table
	setResult("Ratio",j,mean/getResult("Nucleus",j));	//calculate ratio of band to nuclues and write it to column 'Ratio'
};

run("Summarize");										//Summarise results table


print(getTitle());
print("Count:" +nResults);        						
print ("Avg. Intensity:" + getResult("Nucleus",nResults-4));


roiManager("Deselect");
roiManager("Delete");
//Table.deleteRows(0, 10000);
// closes all images before the next iteration
while(nImages > 0) close();
2 Likes

Fantastic,
thank you so much for your time and help!