/* * * Functions existing: * - Auto cell counts using either stardist or auto-thresholds * (note stardist required to be installed) * - Excludes ROI if requested * - Calculates histogram with number of bins requested * - Histogram fills full 16 bit range (0,65535) can be altered on line 83 * - Mask images saved if requested * * * */ var autoCellCount = true; var useStardist = true; var nuclearChannel = 2; var signalChannel = 1; var interactive = true; //allows ROIs to be drawn and excluded var nBins = 1024; var fileTypeExt = "czi"; //filetype of images var saveMasks = true; //save masks with nuclei circled in yellow, excluded region in red var table = ""; var dir1 = "/data/"; dir1 = getDirectory("Choose source directory"); flist = getFileList(dir1); setTool("Oval"); dir2 = dir1 + "output/" if(!File.exists(dir2)){ File.makeDirectory(dir2); } nCells = countCZIs(flist,fileTypeExt); run("Close All"); for(i=0;i 0){ roiManager("Set Color","red"); roiManager("Show All without labels"); run("Flatten"); close("mask"); rename("mask"); } if(saveMasks){ saveAs("TIF",dir2 + shortfname + "_mask.tif"); } run("Close All"); } } function done(){ for(i=0;i<5;i++){ beep(); wait(50); } showMessage("Beep", "Done. Table has been saved"); beep(); } } function getHist(fname,nCells){ closeRoiManager(); selectWindow(fname); run("Duplicate...","title=signal duplicate channels="+signalChannel+"-"+signalChannel); waitForUser("Draw region to exclude (if any)"); if(selectionType()!=-1){ roiManager("Add"); run("Make Inverse"); } histMin = 0; histMax = 65535; getHistogram(values, counts, nBins, histMin, histMax); cols = newArray("Bin","nCells"); //Setup custom results table Table_Heading = "Histogram Results"; if(!isOpen(Table_Heading)){ columns = Array.concat(cols,values); table = generateTable(Table_Heading,columns); } res = newArray(fname,nCells); res = Array.concat(res,counts); saveTable(Table_Heading); return res; } function countNuclei(fname){ selectWindow(fname); run("Duplicate...","duplicate title=nucs channels="+nuclearChannel+"-"+nuclearChannel); if(useStardist){ run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'nucs', 'modelChoice':'Versatile (fluorescent nuclei)', 'normalizeInput':'true', 'percentileBottom':'1.3000000000000003', 'percentileTop':'99.89999999999999', 'probThresh':'0.45', 'nmsThresh':'0.55', 'outputType':'Both', 'nTiles':'1', 'excludeBoundary':'2', 'roiPosition':'Automatic', 'verbose':'false', 'showCsbdeepProgress':'false', 'showProbAndDist':'false'], process=[false]"); nCells = roiManager("Count"); close("Label Image"); }else{ resetMinAndMax(); run("8-bit"); run("Subtract Background...", "rolling=50"); run("Auto Local Threshold", "method=Phansalkar radius=15 parameter_1=0 parameter_2=0 white"); run("Median...", "radius=4"); run("Watershed"); run("Analyze Particles...", "size=26-Infinity display clear include summarize add"); nCells = roiManager("Count"); } close("nucs"); return nCells; } function countCZIs(flist,extension){ ///called countCZI but will count number of files with whatever extension is specified nCzis=0; titleString = "Bin"; for(i=0;i