Skip to content

Instantly share code, notes, and snippets.

@NicoKiaru
Created February 10, 2022 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicoKiaru/25369ed257d1be68554f7da3023da52c to your computer and use it in GitHub Desktop.
Save NicoKiaru/25369ed257d1be68554f7da3023da52c to your computer and use it in GitHub Desktop.
Re-import ABBA regions then filter out regions based on name #BIOP #ABBA #QuPath
// Modify the sequence of functions according to your need:
// - reImportAbbaRegions()
// - clearAllExcept(['Left: TH', 'Left: MB'])
// - clearRight()
// - clearLeft()
//
reImportAbbaRegions() // Erase and re import regions
// First way of selecting regions : make a text file with each region that you want to keep, one region per line, take care with spaces!
// def listOfRegions = new File("C:/Users/chiarutt/Downloads/ListRegions.txt") as String[]
// Second way : modify the list below
def listOfRegions=[
'Left: PL5',
'Left: PL6a',
'Right: PL5',
'Right: PL6a',
'Left: ILA1',
'Left: ILA2/3',
'Left: ILA5',
'Left: ILA6a',
'Right: ACAd1',
'Right: ACAd2/3',
'Right: ACAd5',
'Right: ACAd6a',
'Right: ACAv1'
]
// print them out
listOfRegions.each {
println it
}
clearAllExcept(listOfRegions)
// ------------- FUNCTIONS ------------------------
// 0 - Re-import regions
// Necessary import, requires qupath-extension-abba, see: https://github.com/BIOP/qupath-biop-extensions
def reImportAbbaRegions() {
clearAllObjects();
ImageData imageData = getCurrentImageData()
List<String> atlasNames = AtlasTools.getAvailableAtlasRegistration(imageData);
if (atlasNames.size()==0) {
logger.error("No atlas registration found."); // TODO : show an error message for the user
return;
}
String atlasName = atlasNames.get(0);
if (atlasNames.size()>1) {
logger.warn("Several atlases registration have been found. Importing atlas: "+atlasName);
}
Path ontologyPath = Paths.get(Projects.getBaseDirectory(getProject()).getAbsolutePath(), atlasName+"-Ontology.json");
AtlasOntology ontology = AtlasHelper.openOntologyFromJsonFile(ontologyPath.toString());
// Get naming possibilities
String namingProperty = "acronym"
/* Dialogs.showChoiceDialog("Regions names",
"Please select the property for naming the imported regions.",
AtlasTools.getNamingProperties(ontology).toArray(new String[0]),
"ID");*/
ontology.setNamingProperty(namingProperty);
AtlasTools.loadWarpedAtlasAnnotations(ontology, imageData, atlasName, true);
}
// 1 - Remove right region
def clearRight() {
removeObjects(getAnnotationObjects().findAll{it.getName().equals('Root')}, true)
removeObjects(getAnnotationObjects().findAll{it.getPathClass().toString().contains('Right:')}, false)
}
// 2 - Remove left region
def clearLeft() {
removeObjects(getAnnotationObjects().findAll{it.getName().equals('Root')}, true)
removeObjects(getAnnotationObjects().findAll{it.getPathClass().toString().contains('Left:')}, false)
}
// 3 - Delete All Regions except the ones on the list
def clearAllExcept(regionsToKeep) {
removeObjects(
getAnnotationObjects()
.findAll{!(regionsToKeep.contains(it.getPathClass().toString()))}, true)
}
// 4 - Print all regions
//getAnnotationObjects().each{println(it.getPathClass().toString())}
import qupath.ext.biop.abba.struct.AtlasHelper
import qupath.ext.biop.abba.struct.AtlasOntology
import qupath.ext.biop.abba.AtlasTools
import qupath.lib.images.ImageData
import java.nio.file.Path
import java.nio.file.Paths
import java.util.List
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment