//Surface_protein_v_cleft_counting.ijm
	//BEFORE RUNNING, CHECK THAT SURFACE PROTEIN IS CORRECT (command+F)
	//get input and output directories	
	indir = getDirectory("select a source folder");
	outdir = getDirectory("select a destination folder");
	
	//get the image list
 	list = getFileList(indir);

	//start batch loop
	for(i=0; i<list.length; i++){
		
		run("Close All");
		
		//open image, grab the file name
 		open(indir + list[i]);
		nameonly = File.nameWithoutExtension;

		//split channels and rename each channels with static name
		run("Split Channels");

		image = "C1-"+ list[i]; 
		selectWindow(image);
		run("Z Project...", "projection=[Max Intensity]");
		rename("DAPI_MIP");
		
		image = "C2-"+ list[i]; 	
		selectWindow(image);
		run("Z Project...", "projection=[Max Intensity]");
		rename("REX1_MIP");
		
		image = "C3-"+ list[i]; 
		selectWindow(image);
		run("Z Project...", "projection=[Max Intensity]");
		rename("EMP3_MIP");

		//create masks
			//make cell mask w/ gaussian blur
			imageCalculator("Add create", "EMP3_MIP","REX1_MIP");
			run("Gaussian Blur...", "sigma=2");
			setAutoThreshold("Default dark");
			run("Convert to Mask");
			rename("cellmask");

			//REX1 clefts mask (local threshold)
			selectWindow("REX1_MIP");
			run("Duplicate...", " ");
			setOption("ScaleConversions", true);
			run("8-bit");
			run("Auto Local Threshold", "method=Niblack radius=5 parameter_1=0 parameter_2=0 white");
			run("Watershed");
			rename("REX1mask_clefts");
			
			//REX1 clefts mask (default threshold to remove noise from local threshold)
			selectWindow("REX1_MIP");
			run("Duplicate...", " ");
			run("8-bit");
			rename("REX1mask_blur");
			run("Gaussian Blur...", "sigma=2");
			setAutoThreshold("Default dark");
			//run("Threshold...");
			run("Convert to Mask");
			imageCalculator("AND create", "REX1mask_clefts","REX1mask_blur");
			selectWindow("Result of REX1mask_clefts");
			rename("REX1mask");
		
		//on the original image, make a binary, watershed, analyse particles
		selectWindow("REX1mask");
		rename(nameonly+"REX1mask");
		run("Set Measurements...", "area mean redirect=REX1_MIP decimal=3");
		run("Analyze Particles...", "size=10-Infinity pixel show=[Masks] summarize");
		run("Invert LUT");
		run("Convert to Mask");
		rename("REX1_merge_");
		
		selectWindow("cellmask");
		rename(nameonly+"EMP3mask");
		run("Set Measurements...", "area mean redirect=EMP3_MIP decimal=3");
		run("Analyze Particles...", "show=[Bare Outlines] summarize");
		run("Invert LUT");
		run("Convert to Mask");
		rename("EMP3outline");
		imageCalculator("Add", "EMP3_MIP","EMP3outline");
		rename("EMP3_merge_");

		//make proof
		run("Merge Channels...", "c1=REX1_MIP c2=EMP3_MIP c3=DAPI_MIP create keep");
		run("Stack to RGB");
		rename("_merge_");
		
		run("Images to Stack", "name=[] title=_merge_ use");
		run("Make Montage...", "columns=3 rows=1 scale=1 border=1");
		run("Scale Bar...", "width=10 height=1 font=6 color=Gray background=None location=[Lower Right] hide overlay");
		setFont("Serif",8 , "antiliased");
		setColor(0, 255, 0);
		x=175; y=100;
		drawString("EMP3", x, y);
		setColor(255, 0, 0);
		x=75; y=100;
		drawString("REX1", x, y);
		setColor(255, 255, 255);
		x=275; y=100;
		drawString("Merge", x, y);	
		run("Flatten");
		saveAs("tif", outdir+nameonly+"_proof");

		//clear up workspace, memory
		run("Close All");
		run("Collect Garbage");
	}