// Jan Wisniewski, EIB/NCI/NIH, Bethesda, MD //Detects cell colonies, counts and measures their average size //gathers results into a custom table print("Colony Identification and Counting Macro\nJan Wisniewski, Experimental Immunology Branch\nNational Cancer Institute, NIH, Bethesda, Maryland"); getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec); print(""); print("run: ", month, "/", dayOfMonth, "/", year, " at ", hour, ":", minute); print(""); output=getDirectory("Choose a Destination Folder"); input=getDirectory("Select a Source Directory"); list=getFileList(input); title1 = "Analysis_Results"; title2 = "["+title1+"]"; f=title2; run("New... ", "name="+title2+" type=Table"); print(f,"\\Headings:Image\tChannel\tName\tMin._Detection_Limit(um2)\t#_of_Detected_Colonies\tAverage_Size"); run("Bio-Formats (Windowless)"); rename("x"); getDimensions(width, height, channels, slices, frames); chn=channels; if(chn>1) {run("Split Channels"); run("Tile"); selectWindow("C1-x"); run("Enhance Contrast", "saturated=0.10"); selectWindow("C2-x"); run("Enhance Contrast", "saturated=0.10"); if(chn==3) {selectWindow("C3-x"); run("Enhance Contrast", "saturated=0.10"); } Dialog.create("Channel setup"); itemx=newArray("C1-x", "C2-x", "C3-x"); Dialog.addRadioButtonGroup("Which channel will you use?", itemx, 1, 3, "C1-x") Dialog.addString("Name", ""); items=newArray("Grays", "Blue", "Cyan", "Green", "Yellow", "Red", "Magenta"); Dialog.addChoice("Display color", items, "Red"); //Dialog.addNumber("Size reduction factor:", 2); Dialog.addNumber("ROI diameter(fraction of whole image)", 0.90); Dialog.show(); chnid=Dialog.getRadioButton(); chname=Dialog.getString(); chcol=Dialog.getChoice(); chres=2; diam=Dialog.getNumber(); selectWindow(chnid); run("Duplicate...", "title=x"); close("C1-x"); close("C2-x"); if(chn==3) {close("C3-x"); } } else {run("Enhance Contrast", "saturated=0.10"); Dialog.create("Channel setup"); Dialog.addString("Name", ""); items=newArray("Grays", "Blue", "Cyan", "Green", "Yellow", "Red", "Magenta"); Dialog.addChoice("Display color", items, "Red"); //Dialog.addNumber("Size reduction factor:", 2); Dialog.addNumber("ROI diameter(fraction of whole image)", 0.90); Dialog.show(); chnid="x"; chname=Dialog.getString(); chcol=Dialog.getChoice(); chres=2; diam=Dialog.getNumber(); } redsz=width/chres; roisz=redsz*diam; strt=(redsz-roisz)/2; print(width, chnid, chname, chcol, chres, diam, redsz, roisz, strt); run("Resample", "factor=chres factor_0=chres"); run("Enhance Contrast", "saturated=0.10"); rename(chname); run(chcol); close("x"); myDir = output+"temporary"+File.separator; File.makeDirectory(myDir); saveAs("Tiff", myDir+"temp1.tif"); rename(chname); run("Duplicate...", " "); rename("mask"); run("Multiply...", "value=0"); selectWindow(chname); makeOval(strt, strt, roisz, roisz); run("Copy"); run("Select None"); selectWindow("mask"); run("Paste"); run("Select None"); setAutoThreshold("Default dark"); run("Threshold..."); waitForUser("Adjust threshold"); getThreshold(lower, upper); thrlow=lower; thrup=upper; setOption("BlackBackground", true); run("Convert to Mask"); run("Maximum...", "radius=15"); run("Fill Holes"); run("Watershed"); saveAs("Tiff", myDir+"temp2.tif"); rename("mask"); Dialog.create("Select minimum size limit"); Dialog.addMessage("Minimum colony size (um2) = factor x 100000"); Dialog.addNumber("Factor value:", 1); Dialog.show(); lmt1=Dialog.getNumber(); szlimit=100000*lmt1; print(szlimit); run("Analyze Particles...", "size=szlimit-Infinity circularity=0.33-1.00 show=Outlines summarize in_situ"); run("Green"); run("Maximum...", "radius=2"); selectWindow(chname); run("RGB Color"); selectWindow("mask"); run("RGB Color"); run("Images to Stack", "name=Stack title=[] use"); run("Z Project...", "projection=[Sum Slices]"); try1=getBoolean("Were colonies selected properly?", "Yes, proceed to analysis", "No, try different value"); close("Stack"); close("SUM_Stack"); if(try1==1) {tlist=getFileList(myDir); for (h=0; h1) {run("Split Channels"); selectWindow(chnid); run("Duplicate...", "title=x"); close("C1-x"); close("C2-x"); if(chn==3) {close("C3-x"); } } run("Resample", "factor=chres factor_0=chres"); run("Enhance Contrast", "saturated=0.10"); run(chcol); rename(chname); close("x"); run("Duplicate...", "mask"); makeOval(strt, strt, roisz, roisz); run("Clear Outside"); run("Select None"); setThreshold(thrlow, thrup); run("Convert to Mask"); run("Maximum...", "radius=15"); run("Fill Holes"); run("Watershed"); rename(ttl); run("Analyze Particles...", "size=szlimit-Infinity circularity=0.33-1.00 show=Outlines summarize in_situ"); Table.rename("Summary", "Results"); colnumb=getResult("Count"); avesz=getResult("Average Size"); close("Results"); selectWindow("Analysis_Results"); print(f,ttl+"\t"+chnid+"\t"+chname+"\t"+szlimit+"\t"+colnumb+"\t"+avesz); run("Green"); run("Maximum...", "radius=2"); run("RGB Color"); selectWindow(chname); run("RGB Color"); run("Duplicate...", "circle"); run("Select All"); setBackgroundColor(0, 0, 0); run("Clear", "slice"); run("Select None"); makeOval(strt, strt, roisz, roisz); run("Flatten"); run("Maximum...", "radius=2"); close("circle"); run("Images to Stack", "name=Stack title=[] use"); run("Z Project...", "projection=[Max Intensity]"); saveAs("Tiff", output+ttl+"_detected_colonies"); close(); close(); close("Stack"); } getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec); //Table.rename("Summary", "Results"); //saveAs("Results", output+"Results"+"_"+month+"_"+dayOfMonth+"_"+year+"_"+hour+"_"+minute+".csv"); //close("Results"); selectWindow("Analysis_Results"); saveAs("Text", output+"Analysis_Results"+"_"+month+"_"+dayOfMonth+"_"+year+"_"+hour+"_"+minute+".csv"); close("Analysis_Results"); //selectWindow("Log"); //saveAs("Text", output+"Log"+"_"+month+"_"+dayOfMonth+"_"+year+"_"+hour+"_"+minute+".txt"); close("Log");