IMAGE QUANTITATION SCRIPTS

ImageJ_#1 Check_File_Names

//Check there is for every red image a green image and ROI


title = "Check Files";
msg = "Choose Folder containing images";
waitForUser(title, msg);

  path = getDirectory("Choose a Directory ");

red="Ex_mCherry_572"
green="Ex_GFP_470"
Dialog.create("File Labels");
Dialog.addString("Red:", red);
Dialog.addString("Green:", green);
Dialog.show();
red= Dialog.getString();
green = Dialog.getString();

     list = getFileList(path);
     Array.show(list);
     for (i=0; i<list.length; i++) {
     	filematch="undetermined";
		ROImatch="undetermined";
        if (matches(list[i], ".*"+red+".*")){
        	dot = indexOf(list[i], red);
        	PartAconditionName = substring(list[i], 0, dot);
     		EndOfConditionName =  lastIndexOf(list[i], red)+lengthOf(red);
		    PartBconditionName = substring(list[i],EndOfConditionName, lastIndexOf(list[i], "."));
		    condition=PartAconditionName+PartBconditionName;
		    
			for (k=0; k<list.length; k++){
	        	if (matches(list[k], PartAconditionName+green+PartBconditionName+".tif")){
	        		filematch = "YES"; print("match:" +PartAconditionName+green+PartBconditionName);
	           	} }
			if (filematch != "YES"){
				filematch = "NO"; print("No green channel image:" + PartAconditionName+green+PartBconditionName);
				};			
			    };		
		  }
 
________
ImageJ_#2 Initialize_Slice_Table


setOption("ExpandableArrays", true);
//create list of conditions
title = "Check Files";
msg = "Choose Decon Folder.";
waitForUser(title, msg);
  
print("Choose image folder"); 

  path = getDirectory("Choose a Directory ");
  count = 1;

red="Ex_mCherry_572"
green="Ex_GFP_470"
Dialog.create("File Labels");
Dialog.addMessage("											"); 
Dialog.addString("Red:", red);
Dialog.addString("Green:", green);
Dialog.show();
red= Dialog.getString();
green = Dialog.getString();

condition_names = newArray;
condition="empty";


     
     list = getFileList(path);
     for (i=0; i<list.length; i++) {
     	filematch="undetermined";
		ROImatch="undetermined";
        if (matches(list[i], ".*"+red+".*"+".tif")){
        	dot = indexOf(list[i], red); 
     		PartAconditionName = substring(list[i], 0, dot);
     		EndOfConditionName =  lastIndexOf(list[i], red)+lengthOf(red);
		    PartBconditionName = substring(list[i],EndOfConditionName, lastIndexOf(list[i], "."));
		    condition=PartAconditionName+PartBconditionName;
		    print(condition);
			condition_names = Array.concat(condition_names, condition);
			Array.show(condition_names);	
		  }
  		}

  path2 = File.getParent(path) + File.separator + "Analysis" + File.separator;
print(path);
print(path2);






//Create Table to hold slices
 ImageSlices = "ImageSlices.csv";
    Table.create(ImageSlices);
    for (n=0, row=0; n<condition_names.length; n+=1, row++) {
     Table.set("Condition", row, NaN);
     Table.set("Start_Slice", row,NaN);
     Table.set("End_Slice", row, NaN);
     Table.setColumn("Condition", condition_names);
    }
  Table.update;
  Table.save(path2+"ImageSlices.csv");

_________
ImageJ_#3 GetCompositeImages


//Check there is for every red image a green image and ROI
call("java.lang.System.gc");
setOption("ExpandableArrays", true);
//Automatically retreive unprocessed ROIs
title = "Check Files";
msg = "Choose Expt. Folder";
waitForUser(title, msg);

  path = getDirectory("Choose a Directory ");

red="Ex_mCherry_572"
green="Ex_GFP_470"
Dialog.create("File Labels");
Dialog.addString("Red:", red);
Dialog.addString("Green:", green);
Dialog.show();
red= Dialog.getString();
green = Dialog.getString();

incomplete_condition=newArray;
incomplete_PartAconditionName=newArray;
incomplete_PartBconditionName=newArray;
count=0;
     list = getFileList(path + "Decon");
     for (i=0; i<list.length; i++) {
     	run("Close All");
     	filematch="undetermined";
		ROImatch="undetermined";
        if (matches(list[i], ".*"+red+".*"+".tif")){
        	dot = indexOf(list[i], red); 
     		PartAconditionName = substring(list[i], 0, dot);
     		EndOfConditionName =  lastIndexOf(list[i], red)+lengthOf(red);
		    PartBconditionName = substring(list[i],EndOfConditionName, lastIndexOf(list[i], "."));
		    condition=PartAconditionName+PartBconditionName;
		    print(condition);
			for (k=0; k<list.length; k++){
	        	if (matches(list[k], PartAconditionName+green+PartBconditionName+".tif")){
	        		filematch = "YES";
	           	} }
			if (filematch != "YES"){
				filematch = "NO"; print("No green channel image:" + PartAconditionName+green+PartBconditionName);
				};					
			Analysis = path + "Analysis" + File.separator;
			print(Analysis);
  			Analysislist = getFileList(Analysis);
  			Array.show(Analysislist);		    
			for (m=0; m<Analysislist.length; m++){			 
				 if (matches(Analysislist[m], condition+"_inner_ROI.zip")){
	        		ROImatch = "YES";
	        		print("ROI done"+condition);	        		
	           		};
	           	};
	        if (ROImatch != "YES"){
				print("No ROI: " + condition);
				incomplete_condition = Array.concat(incomplete_condition, condition);
				Array.show(incomplete_condition);
				incomplete_PartAconditionName=Array.concat(incomplete_PartAconditionName, PartAconditionName);
				incomplete_PartBconditionName=Array.concat(incomplete_PartBconditionName, PartBconditionName);
			    };
			
		  };
     };
numm=incomplete_condition.length;
for(k=0; k<incomplete_condition.length; k++)
{
		Array.show(incomplete_condition);
		Array.show(incomplete_PartBconditionName);
	  	condition = incomplete_condition[k];
	  	PartAconditionName=incomplete_PartAconditionName[k];
		PartBconditionName=incomplete_PartBconditionName[k];
	  	print("condition:"+condition);
	
	open(path+File.separator+"Decon"+File.separator+PartAconditionName+red+PartBconditionName+".tif");
	print(path+File.separator+"Decon"+File.separator+PartAconditionName+red+PartBconditionName+".tif");
	Red = getTitle();
	
	open(path+File.separator+"Decon"+File.separator+PartAconditionName+green+PartBconditionName+".tif");
	print(path+File.separator+"Decon"+File.separator+PartAconditionName+green+PartBconditionName+".tif");
	Green = getTitle();
	
	print(Red);
	print(Green);
	
	run("Merge Channels...", "c1=["+Red+"] c2=["+Green+"] create keep");
	Composite = getTitle();
	rename(Composite+"_"+condition);
	Composite = getTitle();
	selectWindow(Red);
	close(Red);
	selectWindow(Composite);
	
	title = "Check focus";
	msg = "Check which slices are in focus then click \"OK\".";
	waitForUser(title, msg);
	
	selectWindow(Composite);
	
	//specify slices and save to table
	End = nSlices/2 -1;
	Dialog.create("Slice");
	Dialog.addMessage("Keep slices");
	Start= Dialog.addNumber("Start:", 1);
	End = Dialog.addNumber("End:", End);
	Bad = Dialog.addCheckbox("Bad Image", false);
	Dialog.show();
	start= Dialog.getNumber();
	end= Dialog.getNumber();
	bad= Dialog.getCheckbox();
	if(bad==true)
	{
		print("THIS WAS A BAD IMAGE");
		
		incomplete_condition = Array.deleteIndex(incomplete_condition, k);
		incomplete_PartAconditionName=Array.deleteIndex(incomplete_PartAconditionName,k);
		incomplete_PartBconditionName=Array.deleteIndex(incomplete_PartBconditionName,k);
		Table.open(path+"/Analysis/"+"ImageSlices.csv");
		Table.deleteRows(k, k);
		Table.update;
		Table.save(path+"/Analysis/"+"ImageSlices.csv");
		
		close(Composite);
		run("Close All");
		k=k-1;
		
		
		//I dont think condition names are used for anything 
	}
	if(bad==false)
	{
	Table.open(path+"/Analysis/"+"ImageSlices.csv");
	dot=Table.getColumn("Condition");
	print(count);
	Table.set("Condition",count,condition);
	Table.set("Start_Slice", count, start);
	Table.set("End_Slice", count, end) ;
	Table.update;
	Table.save(path+"/Analysis/"+"ImageSlices.csv");
	  function index(a, value) { 
      for (i=0; i<a.length; i++) 
          if (a[i]==value) return i; 
      return -1; 
  	} 
	  selectWindow(Composite);
	run("Duplicate...", "duplicate slices=start-end"); 
	Composite = getTitle();
	close(Composite);
	saveAs("Tiff", path+"/Composite/"+condition+"_Composite.tif");
	Composite = getTitle();
	count++; 
	run("Close All");
	}
}


__________________
ImageJ_#4 Autosegment_ROI
//Assumes ROI is opened with composite image

////////////////////////////////////////
//This needs to be a new macro
title = "Check Files";
msg = "Choose Expt. Folder";
waitForUser(title, msg);
  path = getDirectory("Choose a Directory ");
title = "Check Files";
mainpath= path
Table.open(path+"/Analysis/"+"ImageSlices.csv");
		Incomplete_condition =newArray;
		Incomplete_condition=Table.getColumn("Condition");
		Array.show(Incomplete_condition);
for(k=0;k<Incomplete_condition.length;k++)
{
	run("Close All");
	if(roiManager("Count")!=0)
	{
		roiManager("Deselect");
		roiManager("Delete");
	}
	condition = Incomplete_condition[k];
	print(path);

	open(path+File.separator+"OUTMASK"+File.separator+"imagej"+File.separator+condition+"_Composite.tif.tif");
	mask =getTitle();
	print(mask);
	print(path+File.separator+"OUTMASK"+File.separator+"imagej"+File.separator+condition+"_Composite.tif.tif");
	open(path+File.separator+"Composite"+File.separator+condition+"_Composite.tif");
	Composite =getTitle();
///////////
	selectWindow(mask);
	run("Analyze Particles...", "size=1000-Infinity show=Nothing exclude add");
	selectWindow(Composite);
	roiManager("Show All with labels");
	title = "Check ROI";
	msg = "Check ROI'S then click \"OK\".";
	waitForUser(title, msg);
//////////////

	for (i = 0; i < roiManager("count"); i++) {
	roiManager("Select", i);
	run("Enlarge...", "enlarge=-5 pixel");
	roiManager("Update");
	run("Interpolate", "interval=8 smooth");
	roiManager("Update");
	roiManager("Rename", "Inner "+i+1);
	roiManager("Set Color", "yellow");
	roiManager("Set Line Width", 0);
	}
	selectWindow(Composite);
	roiManager("Show All with labels");

	roiManager("deselect");
	roiManager("Save", path+"/Analysis/"+condition+"_inner_ROI.zip");

	run("Colors...", "foreground=white background=black selection=yellow");

	n=roiManager("count");

	for (i = 0; i < roiManager("count"); i++) {
	roiManager("Select", i);
	roiManager("Rename", "Inner "+i+1);
	}

	s = getInfo("image.filename");
	dot = indexOf(s, "_Composite");
	if (dot >= 0) s = substring(s, 0, dot);

	 {
	  path3 =  getInfo("image.directory");
	  path2 = File.getParent(path3) + "/Analysis/";
	  call("ij.io.OpenDialog.setDefaultDirectory", path2);
	 }

	roiManager("deselect");
	roiManager("Save", path2+s+"_inner_ROI.zip");
}
run("Close All");
	if(roiManager("Count")!=0)
	{
		roiManager("Deselect");
		roiManager("Delete");
	}


______________________
ImageJ_#5. Measure_PM_and_Inner


setOption("ExpandableArrays", true);


title = "Check Files";
msg = "Choose Expt. Folder";
waitForUser(title, msg);

path = getDirectory("Choose a Directory ");

red="Ex_mCherry_572"
Dialog.create("File Labels");
Dialog.addString("Red:", red);
Dialog.show();
red= Dialog.getString();

Conditions=newArray;
FileNames=newArray;
  
list = getFileList(path + "Composite");

  for (i=0; i<list.length; i++) {
	loc = indexOf(list[i]," ",0) +1;
	condition=substring(list[i],0, lastIndexOf(list[i],"_"));
	part1name=substring(list[i],0, loc);
	part2name=substring(list[i],loc, lastIndexOf(list[i],"_"));
	FileNames[i]= part1name+red+part2name+".tif";
	Conditions=Array.concat(Conditions,condition);
 	}

Array.show(Conditions);
Array.show(FileNames);

for (koo=0; koo<Conditions.length; koo++) {
	call("java.lang.System.gc");
	open(path+"Decon"+File.separator+FileNames[koo]);
	Red = getTitle();
	open(path+"Analysis"+File.separator+Conditions[koo]+"_inner_ROI.zip");
	condition=Conditions[koo];
// Draw PM and Inner
	run("Duplicate...", "duplicate");
	run("Colors...", "foreground=white background=black selection=red");
	
	for (i = 0; i < roiManager("count"); i++) {
	roiManager("Select", i);
	roiManager("Rename", "Inner "+i+1);
	}
	
	n= roiManager("count");
	for (i = 0; i < n; i++) {
	roiManager("Select", i);
	run("Enlarge...", "enlarge=8 pixel");
	roiManager("Add");
	}
	
	for (i = 0; i < n; i++) {
	b= i+n;
	roiManager("Select", b);
	roiManager("Rename", "Outer "+i+1);
	}
	
	for (i = 0; i < n; i++) {
	c= i+n;
	newImage("Untitled", "8-bit black", 1344, 1024, 1);
	roiManager("Show None");
	roiManager("Deselect");
	roiManager("Select", c);
	run("Fill", "slice");
	roiManager("Select", i);
	run("Clear", "slice");
	run("Select None");
	run("Invert");
	run("Create Selection");
	roiManager("Add");
	close();
	}
	
	for (i = 0; i < n; i++) {
	d=2*n+i;
	roiManager("Select", d);
	roiManager("Rename", "PM "+i+1);
	}
		
	roiManager("deselect");
	roiManager("Save", path+"Analysis"+File.separator+condition+"_PM_and_inner_ROI.zip");
	run("Select None");
	close();

	
//Remove out of focus slices
	Table.open(path+"Analysis"+File.separator +"ImageSlices.csv");
	dot=Table.getColumn("Condition");
	rowIndex = index(dot, condition);
	print(rowIndex);
	start=Table.get("Start_Slice", rowIndex);
	end= Table.get("End_Slice", rowIndex) ;
	  function index(a, value) { 
	      for (i=0; i<a.length; i++) 
	          if (a[i]==value) return i; 
	      return -1; 
	  } 
	print(start);
	print(end);
	
	
	run("Duplicate...", "duplicate range=start-end"); 
	Red2 = getTitle();
	close(Red);
	selectWindow(Red2);
	Red=getTitle();

//initialise results table
	b=roiManager("count")/3;
	 CustomResultsTable = "Custom_Results_Table";
	  Table.create(CustomResultsTable);
	    for (n=0, row=0; n<b; n+=1, row++) {
	     Table.set("Cell no.", row, NaN);
	     Table.set("Inner_Int.Den", row, NaN);
	     Table.set("PM_Int.Den", row, NaN);
	     Table.set("PM/Total_Int.Den", row, NaN);
	     Table.set("Inner_Area", row, NaN);
	     Table.set("PM_Area", row, NaN);
	     Table.set("Inner_Mean", row, NaN);
	     Table.set("PM_Mean", row, NaN);
	     Table.set("Inner_Std.Dev", row, NaN);
	     Table.set("PM_Std.Den", row, NaN);
	     Table.set("Background value", row, NaN);
	    }
	  Table.update;
	  Table.save(path +  "Analysis" + File.separator + condition + "_Results.csv");

//Calculate background
	selectWindow(Red);
	n=roiManager("count")/3;
	for (i = 0; i < n; i++) {
		Inner = i;
		Outer = n+i;
		PM = 2*n+i;
		roiManager("Select", Outer);
		run("Duplicate...", "duplicate");
		run("Duplicate...", "duplicate");
		roiManager("Select", Inner);
		run("Clear Outside", "stack");
		
		slicebackgrounds=newArray;
			for (k=1; k<=nSlices; k++) { 
			 	setSlice(k);
			    requires ("1.42j"); 
			    w = getWidth; 
			    h = getHeight; 
			    a = newArray(w*h); 
			    d = 0; 
			    for (y=0; y<h; y++) 
			       for (x=0; x<w; x++) 
			           a[d++] = getPixel(x,y); 
				Array.sort(a);Array.show(a);
				print(a.length);
				c=newArray;
				for  (f=0; f<a.length; f++){
					if (a[f]!= 0){c=Array.concat(c,a[f]);}
					};
				Array.sort(c);Array.show(c);
				e = (c[c.length*0.50]);
				slicebackgrounds=Array.concat(slicebackgrounds,e);
			}
		back = "";
		for  (h=0; h<slicebackgrounds.length; h++){
			back= back + slicebackgrounds[h] + " ";};
		print(back);
		close();		
		
//Measure regions
			for (k=1; k<=nSlices; k++) { 
			 	setSlice(k);
			 	b=slicebackgrounds[k-1];
			 	print(b);
				run("Select None");
				run("Subtract...", "value=b");
				run("Min...", "value=0");}
	
			run("Z Project...", "projection=[Sum Slices]");
			run("Clear Results");
				
			roiManager("Select", Inner);
			run("Measure");
			Inner_Int_Den=getResult("IntDen", 0);
			Inner_Area=getResult("Area", 0);
			Inner_Mean=getResult("Mean", 0);
			Inner_StdDev=getResult("StdDev", 0);
			run("Clear Results");
				
			roiManager("Select", PM);
			run("Measure");
			PM_Int_Den=getResult("IntDen", 0);
			PM_Area=getResult("Area", 0);
			PM_Mean=getResult("Mean", 0);
			PM_StdDev=getResult("StdDev", 0);	
			run("Clear Results");
	
			PM_Total_Int_Den = PM_Int_Den / (Inner_Int_Den +PM_Int_Den)*100;
			//put in table	
			row = i;
			selectWindow("Custom_Results_Table");
			Table.set("Cell no.", row, i+1);
		    Table.set("Inner_Int.Den", row, Inner_Int_Den);
		    Table.set("PM_Int.Den", row, PM_Int_Den);
		    Table.set("PM/Total_Int.Den", row, PM_Total_Int_Den);
		    Table.set("Inner_Area", row, Inner_Area);
		    Table.set("PM_Area", row, PM_Area);
		    Table.set("Inner_Mean", row, Inner_Mean);
		    Table.set("PM_Mean", row, PM_Mean);
		    Table.set("Inner_Std.Dev", row, Inner_StdDev);
		    Table.set("PM_Std.Den", row, PM_StdDev);
		    Table.set("Background value", row, back);
	  	Table.update;
	  	Table.save(path +  "Analysis" + File.separator + condition + "_Results.csv");

		close();
		close();

	
		
	}
    if (isOpen("ROI Manager")) {
     selectWindow("ROI Manager");
     run("Close");
    
  }
  close();

	}
