//flam RNA-FISH analysis in Fiji

//detects flam RNA FISH foci in OSCs and determines their localisation in the nucleus vs cytosol
//single-plane image of OSCs stained with DAPI (C1), RNA FISH probes against flam (C3) and oligo dT (C2) 

run("Duplicate...", "duplicate");
img=getTitle()

run("Split Channels");

selectWindow("C4-"+img)
close()


//make mask out of nuclei, using Difference of Gaussian (DoG) filter
selectWindow("C1-"+img);rename("1");
	run("Duplicate...", " ");rename("2");
	
	selectWindow("1");
	run("Gaussian Blur...", "sigma=5 scaled");
	selectWindow("2");
	run("Gaussian Blur...", "sigma=0.6 scaled");
	imageCalculator("Subtract create", "2", "1");
	rename("DoG");
	selectWindow("1");close();
	selectWindow("2");close();
	
	selectWindow("DoG");
	run("Auto Threshold", "method=Huang2 white");
	setOption("BlackBackground", false);
	run("Convert to Mask");
	run("Create Selection");
	roiManager("Add");

//identify maxima, count the total number and count the number for each nucleus
selectWindow("C3-"+img);rename("3");
	run("Duplicate...", " ");rename("4");
	
	selectWindow("3");
	run("Gaussian Blur...", "sigma=1");
	selectWindow("4");
	run("Gaussian Blur...", "sigma=8");
	imageCalculator("Subtract create", "3", "4");
	rename("DoG2");
	selectWindow("3");close();
	selectWindow("4");close();
	selectWindow("DoG2");

	run("Find Maxima...", "noise=500 output=Count");
	saveAs("Results", "/path/rnafish_analyser/tot_maxima.csv");
	run("Clear Results");
	
	roiManager("Select", 0);
	roiManager("Split");
	VAR = roiManager("Count");
	
	for(i=1; i<VAR; i++){
		roiManager("Select", i);
		run("Find Maxima...", "noise=500 output=Count");
		saveAs("Results", "/path/rnafish_analyser/nuc_dots_"+i+".csv");
		run("Clear Results");
		}
		
	roiManager("Deselect");
	roiManager("Delete");

//make mask out of cytosol using oligo dT channel and count the total number of maxima
selectWindow("C2-"+img);rename("5");
	run("Duplicate...", " ");rename("6");
	
	selectWindow("5");
	run("Gaussian Blur...", "sigma=6 scaled");
	selectWindow("6");
	run("Gaussian Blur...", "sigma=1 scaled");
	imageCalculator("Subtract create", "6", "5");
	rename("DoG3");
	selectWindow("5");close();
	selectWindow("6");close();
	
	selectWindow("DoG3");
	run("Auto Threshold", "method=Huang2 white");
	setOption("BlackBackground", false);
	run("Invert")
	run("Fill Holes");
	run("Convert to Mask");
	run("Create Selection");
	roiManager("Add");
	
//identify maxima, count total number and count the number for each cell
	selectWindow("DoG2")
	roiManager("Select", 0);
	roiManager("Split");
	VAR2 = roiManager("Count");
	
	for(i=1; i<VAR2; i++){
		roiManager("Select", i);
		run("Find Maxima...", "noise=500 output=Count");
		saveAs("Results", "/path/rnafish_analyser/cell_dots_"+i+".csv");
		run("Clear Results");
		}
		
	roiManager("Deselect");
	roiManager("Delete");

close()

waitForUser("Happy with the results? Hit OK")

close()
close()

//the number of cytosolic foci is calculated by subtracting the number of foci assigned to the nuclei from the total number of foci in the cell

