Skip to content

Commit

Permalink
Orphaned images and containers (#77)
Browse files Browse the repository at this point in the history
* Add direct getters for orphaned images / datasets / plates
* Add tests
  • Loading branch information
Rdornier authored Feb 23, 2024
1 parent dff1242 commit ec8d569
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
123 changes: 123 additions & 0 deletions src/main/java/fr/igred/omero/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,50 @@ public List<DatasetWrapper> getDatasets(String name)
}


/**
* Gets all orphaned datasets available from OMERO owned by a given user.
*
* @param experimenter The user.
*
* @return See above.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws OMEROServerError Server error.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<DatasetWrapper> getOrphanedDatasets(ExperimenterWrapper experimenter)
throws ServiceException, ExecutionException, OMEROServerError, AccessException {
String template = "select dataset from Dataset as dataset" +
" join fetch dataset.details.owner as o" +
" where o.id = %d" +
" and not exists (select obl from ProjectDatasetLink as obl where obl.child = dataset.id) ";
String query = String.format(template, experimenter.getId());
Long[] ids = this.findByQuery(query)
.stream()
.map(IObject::getId)
.map(RLong::getValue)
.toArray(Long[]::new);
return getDatasets(ids);
}


/**
* Gets all orphaned datasets available from OMERO owned by the current user.
*
* @return See above.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws OMEROServerError Server error.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<DatasetWrapper> getOrphanedDatasets()
throws ServiceException, ExecutionException, OMEROServerError, AccessException {
return getOrphanedDatasets(getUser());
}


/**
* Returns the image with the specified ID from OMERO.
*
Expand Down Expand Up @@ -387,6 +431,40 @@ public List<ImageWrapper> getImages(String name)
}


/**
* Gets all orphaned images owned by the specified user.
*
* @return See above.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<ImageWrapper> getOrphanedImages(ExperimenterWrapper experimenter)
throws ServiceException, AccessException, ExecutionException {
Collection<ImageData> images = ExceptionHandler.of(getBrowseFacility(),
bf -> bf.getOrphanedImages(getCtx(), experimenter.getId()))
.handleOMEROException("Cannot get orphaned images")
.get();
return wrap(images, ImageWrapper::new);
}


/**
* Gets all orphaned images owned by the current user.
*
* @return See above.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<ImageWrapper> getOrphanedImages()
throws ServiceException, AccessException, ExecutionException {
return getOrphanedImages(getUser());
}


/**
* Gets all images with the name specified inside projects and datasets with the given names.
*
Expand Down Expand Up @@ -728,6 +806,51 @@ public List<PlateWrapper> getPlates(ExperimenterWrapper experimenter)
}


/**
* Gets all orphaned plates available from OMERO owned by a given user.
*
* @param experimenter The user.
*
* @return See above.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws OMEROServerError Server error.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<PlateWrapper> getOrphanedPlates(ExperimenterWrapper experimenter)
throws ServiceException, ExecutionException, OMEROServerError, AccessException {
String template = "select plate from Plate as plate" +
" join fetch plate.details.owner as o" +
" where o.id = %d" +
" and not exists (select obl from " +
" ScreenPlateLink as obl where obl.child = plate.id) ";
String query = String.format(template, experimenter.getId());
Long[] ids = this.findByQuery(query)
.stream()
.map(IObject::getId)
.map(RLong::getValue)
.toArray(Long[]::new);
return getPlates(ids);
}


/**
* Gets all orphaned plates available from OMERO for the current user.
*
* @return See above.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws OMEROServerError Server error.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<PlateWrapper> getOrphanedPlates()
throws ServiceException, ExecutionException, OMEROServerError, AccessException {
return getOrphanedPlates(getUser());
}


/**
* Gets the well with the specified id from OMERO.
*
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/fr/igred/omero/repository/DatasetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,22 @@ void testSetDescription() throws Exception {
assertEquals(description, client.getDataset(DATASET1.id).getDescription());
}


@Test
void testCreateOrphanedDatasetAndDeleteIt() throws Exception {
String name = "To delete";

DatasetWrapper dataset = new DatasetWrapper(name, "Dataset which will be deleted");
dataset.saveAndUpdate(client);
long id = dataset.getId();

List<DatasetWrapper> orphaned = client.getOrphanedDatasets();
client.delete(orphaned);

assertEquals(1, orphaned.size());
assertEquals(name, orphaned.get(0).getName());

assertThrows(NoSuchElementException.class, () -> client.getDataset(id));
}

}
20 changes: 20 additions & 0 deletions src/test/java/fr/igred/omero/repository/ImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import ij.process.ImageStatistics;
import loci.plugins.BF;
import omero.constants.metadata.NSCLIENTMAPANNOTATION;
import omero.gateway.model.ImageData;
import omero.gateway.model.MapAnnotationData;
import omero.model.NamedValue;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -693,4 +694,23 @@ void testDownload() throws Exception {
}


@Test
void testCreateOrphanedImageAndDeleteIt() throws Exception {
String name = "To delete";

ImageWrapper image = new ImageWrapper(new ImageData());
image.setName(name);
image.saveAndUpdate(client);
long id = image.getId();

List<ImageWrapper> orphaned = client.getOrphanedImages();
client.delete(orphaned);

assertEquals(1, orphaned.size());
assertEquals(name, orphaned.get(0).getName());

assertThrows(NoSuchElementException.class, () -> client.getImage(id));
}


}
22 changes: 22 additions & 0 deletions src/test/java/fr/igred/omero/repository/PlateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

import fr.igred.omero.UserTest;
import fr.igred.omero.annotations.TagAnnotationWrapper;
import omero.gateway.model.PlateData;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.NoSuchElementException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;


class PlateTest extends UserTest {
Expand Down Expand Up @@ -213,4 +216,23 @@ void testGetWellOriginY() throws Exception {
assertEquals(origin, plate.getWellOriginY(null).getValue(), Double.MIN_VALUE);
}


@Test
void testCreateOrphanedPlateAndDeleteIt() throws Exception {
String name = "To delete";

PlateWrapper plate = new PlateWrapper(new PlateData());
plate.setName(name);
plate.saveAndUpdate(client);
long id = plate.getId();

List<PlateWrapper> orphaned = client.getOrphanedPlates();
client.delete(orphaned);

assertEquals(1, orphaned.size());
assertEquals(name, orphaned.get(0).getName());

assertThrows(NoSuchElementException.class, () -> client.getPlate(id));
}

}

0 comments on commit ec8d569

Please sign in to comment.