Skip to content

Instantly share code, notes, and snippets.

@CellKai
Last active September 5, 2018 10:41
Show Gist options
  • Save CellKai/427e324e9529cc02a90018d878aff3fa to your computer and use it in GitHub Desktop.
Save CellKai/427e324e9529cc02a90018d878aff3fa to your computer and use it in GitHub Desktop.
QuPath script that segments two populations of cells based on different channels
import static qupath.lib.scripting.QPEx.*
setImageType('FLUORESCENCE');
// ch1 = A594 (PVcells, cell staining), ch2 = a488 (cell stain to identify brain region layer 4, do not quantify), ch3 = dapi
// make sure all regions are selected so measurments are performed within them
selectAnnotations();
// run the watershed segmentation on channel 4 (dapi), get nucleus and cell objects
runPlugin('qupath.imagej.detect.nuclei.WatershedCellDetection', '{"detectionImageFluorescence": 3, "requestedPixelSizeMicrons": 0.0, "backgroundRadiusMicrons": 8.0, "medianRadiusMicrons": 0.0, "sigmaMicrons": 1.5, "minAreaMicrons": 10.0, "maxAreaMicrons": 400.0, "threshold": 100.0, "watershedPostProcess": true, "cellExpansionMicrons": 5.0, "includeNuclei": true, "smoothBoundaries": true, "makeMeasurements": true}');
// Create the classes
negative = getPathClass('Negative', getColorRGB(0,0,255)) // blue
ch1Positive = getPathClass('Ch1 positive', getColorRGB(0, 255, 0)) // green
// Get cells & reset all the classifications
def allcells = getCellObjects()
resetDetectionClassifications()
allcells.each {it.setPathClass(negative)}
// run the watershed segmentation on channel 1(dapi) to get the positive cells
runPlugin('qupath.imagej.detect.nuclei.WatershedCellDetection', '{"detectionImageFluorescence": 1, "requestedPixelSizeMicrons": 0.0, "backgroundRadiusMicrons": 8.0, "medianRadiusMicrons": 0.0, "sigmaMicrons": 1.5, "minAreaMicrons": 10.0, "maxAreaMicrons": 400.0, "threshold": 400.0, "watershedPostProcess": true, "cellExpansionMicrons": 0.1, "includeNuclei": true, "smoothBoundaries": true, "makeMeasurements": true}');
def pvcells = getCellObjects()
resetDetectionClassifications()
pvcells.each {it.setPathClass(ch1Positive)}
// update number of positives to avoid overcounting
// ch1Pos = cells.findAll {it.getPathClass().getName() == "Ch1 positive"}
fireHierarchyUpdate();
print allcells.size()
print pvcells.size()
// Create Output Path for results
path = buildFilePath(PROJECT_BASE_DIR, 'export')
// Make directory in case the directory does not exist
mkdirs(path)
// Make a results filename
fileName = 'classification-result.txt'
// Write results to a file
file = new File(path, fileName)
delimiter = '\t'
// check if the file exists and if not, create it
if(!file.exists() ) {
file.text = 'Image Name' << delimiter <<
'Ch1 Positive' << delimiter <<
'Total Cells' << delimiter << System.lineSeparator()
}
// Append the results to the end of the file
file << getProjectEntry().getImageName() << delimiter <<
pvcells.size() << delimiter <<
allcells.size() << delimiter << System.lineSeparator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment