From ae3c338aee3a180b5ebd7c950b07d109166b67c4 Mon Sep 17 00:00:00 2001 From: cramakri <cramakri> Date: Wed, 10 Aug 2011 20:21:12 +0000 Subject: [PATCH] LMS-2437 Add new put method to the screening facade and the Matlab API. SVN: 22463 --- screening/source/java/OpenBISScreeningML.java | 107 ++++++++++++ .../v1/IScreeningOpenbisServiceFacade.java | 18 ++ .../api/v1/ScreeningOpenbisServiceFacade.java | 41 +++-- .../java/OpenBISScreeningMLTest.java | 165 ++++++++++++++---- 4 files changed, 277 insertions(+), 54 deletions(-) diff --git a/screening/source/java/OpenBISScreeningML.java b/screening/source/java/OpenBISScreeningML.java index 4c974d6d7d3..b95a5dff57b 100644 --- a/screening/source/java/OpenBISScreeningML.java +++ b/screening/source/java/OpenBISScreeningML.java @@ -1001,6 +1001,103 @@ public class OpenBISScreeningML } } + /** + * Uploads specified data set for specified plate. The data set code will be returned. + * <p> + * Matlab example: + * + * <pre> + * % Upload data set /path/to/my-data-set with properties DESCRIPTION and NUMBER for + * % plate P005 in space SPACE + * % with data set 201007091122-928 as the parent + * properties = {'DESCRIPTION' 'hello example'; 'NUMBER' 3.14} + * parents = {'201007091122-928' } + * datasetcode = OpenBISScreeningML.uploadDataSetForPlateAndParents('/SPACE/P005', parents, '/path/to/my-data-set', 'HCS_IMAGE', properties) + * </pre> + * + * @param augmentedPlateCode The augmented plate code. + * @param parentDataSetCodeObjects The codes of the parents of this data set + * @param dataSetFilePath Path to the data set file/folder to be uploaded. + * @param dataSetType Data set type. + * @param dataSetProperties A two dimensional array where the first column contains the property + * codes and the second column the corresponding property values. + */ + public static Object uploadDataSetForPlateAndParents(String augmentedPlateCode, + Object[] parentDataSetCodeObjects, String dataSetFilePath, String dataSetType, + Object[][] dataSetProperties) + { + checkLoggedIn(); + Plate plateIdentifier = getPlate(augmentedPlateCode); + List<String> dataSetCodes = createStringList(parentDataSetCodeObjects); + File dataSetFile = new File(dataSetFilePath); + if (dataSetFile.exists() == false) + { + throw new RuntimeException("Unknown data set file path '" + dataSetFilePath + "'."); + } + try + { + Map<String, String> map = createMap(dataSetProperties); + IDataSetDss dataSet = + openbis.putDataSet(plateIdentifier, dataSetFile, new NewDataSetMetadataDTO( + dataSetType, map, dataSetCodes)); + return dataSet.getCode(); + } catch (Exception ex) + { + throw new RuntimeException("Couldn't upload data set for plate '" + augmentedPlateCode + + "'.", ex); + } + } + + /** + * Uploads a data set to the specified experiment, setting the data set parents. The data set + * code will be returned. + * <p> + * Matlab example: + * + * <pre> + * % Upload data set /path/to/my-data-set with property DESCRIPTION and N + * % to experiment E103 in project PROJECT and space SPACE + * % with data set 201007091122-928 as the parent + * properties = {'DESCRIPTION' 'hello example' } + * parents = {'201007091122-928' } + * datasetcode = OpenBISScreeningML.uploadDataSetWithParents('/SPACE/PROJECT/E103', parents, '/path/to/my-data-set', 'HCS_IMAGE', properties) + * </pre> + * + * @param augmentedExperimentCode The augmented experiment code. + * @param parentDataSetCodeObjects The codes of the parents of this data set + * @param dataSetFilePath Path to the data set file/folder to be uploaded. + * @param dataSetType Data set type. + * @param dataSetProperties A two dimensional array where the first column contains the property + * codes and the second column the corresponding property values. + */ + public static Object uploadDataSetForExperimentAndParents(String augmentedExperimentCode, + Object[] parentDataSetCodeObjects, String dataSetFilePath, String dataSetType, + Object[][] dataSetProperties) + { + checkLoggedIn(); + ExperimentIdentifier experimentIdentifier = + getExperimentIdentifierOrFail(augmentedExperimentCode); + List<String> dataSetCodes = createStringList(parentDataSetCodeObjects); + File dataSetFile = new File(dataSetFilePath); + if (dataSetFile.exists() == false) + { + throw new RuntimeException("Unknown data set file path '" + dataSetFilePath + "'."); + } + try + { + Map<String, String> map = createMap(dataSetProperties); + IDataSetDss dataSet = + openbis.putDataSet(experimentIdentifier, dataSetFile, + new NewDataSetMetadataDTO(dataSetType, map, dataSetCodes)); + return dataSet.getCode(); + } catch (Exception ex) + { + throw new RuntimeException("Couldn't upload data set for experiment '" + + augmentedExperimentCode + "' and parents '" + + Arrays.toString(parentDataSetCodeObjects) + "'.", ex); + } + } + private static Map<String, String> createMap(Object[][] properties) { Map<String, String> map = new HashMap<String, String>(); @@ -1015,6 +1112,16 @@ public class OpenBISScreeningML return map; } + private static List<String> createStringList(Object[] identifiers) + { + List<String> list = new ArrayList<String>(); + for (Object identifier : identifiers) + { + list.add(identifier.toString()); + } + return list; + } + // // Images // diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/IScreeningOpenbisServiceFacade.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/IScreeningOpenbisServiceFacade.java index b029222220d..9c3e3a67154 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/IScreeningOpenbisServiceFacade.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/IScreeningOpenbisServiceFacade.java @@ -291,6 +291,24 @@ public interface IScreeningOpenbisServiceFacade NewDataSetMetadataDTO dataSetMetadataOrNull) throws IllegalStateException, EnvironmentFailureException, IOException; + /** + * Upload a new data set to the DSS for a plate. + * + * @param experimentIdentifier Identifier of a experiment that should become owner of the new + * data set + * @param dataSetFile A file or folder containing the data + * @param dataSetMetadataOrNull The optional metadata overriding server defaults for the new + * data set + * @return A proxy to the newly added data set + * @throws IllegalStateException Thrown if the user has not yet been authenticated. + * @throws EnvironmentFailureException Thrown in cases where it is not possible to connect to + * the server. + * @throws IOException when accessing the data set file or folder fails + */ + public IDataSetDss putDataSet(ExperimentIdentifier experimentIdentifier, File dataSetFile, + NewDataSetMetadataDTO dataSetMetadataOrNull) throws IllegalStateException, + EnvironmentFailureException, IOException; + /** * Converts the given list of {@link PlateWellReferenceWithDatasets} into a list of * {@link FeatureVectorDatasetWellReference}. diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/ScreeningOpenbisServiceFacade.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/ScreeningOpenbisServiceFacade.java index 9ea143675ab..490ed0fcef0 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/ScreeningOpenbisServiceFacade.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/api/v1/ScreeningOpenbisServiceFacade.java @@ -559,8 +559,8 @@ public class ScreeningOpenbisServiceFacade implements IScreeningOpenbisServiceFa } public List<ch.systemsx.cisd.openbis.dss.client.api.v1.DataSet> getFullDataSets( - PlateIdentifier plateIdentifier, - IDataSetFilter dataSetFilter) throws IllegalStateException, EnvironmentFailureException + PlateIdentifier plateIdentifier, IDataSetFilter dataSetFilter) + throws IllegalStateException, EnvironmentFailureException { checkASMinimalMinorVersion("getPlateSample", PlateIdentifier.class); Sample sample = openbisScreeningServer.getPlateSample(sessionToken, plateIdentifier); @@ -615,19 +615,6 @@ public class ScreeningOpenbisServiceFacade implements IScreeningOpenbisServiceFa return openbisScreeningServer.getWellSample(sessionToken, wellIdentifier); } - /** - * Upload a new data set to the DSS for a plate. - * - * @param plateIdentifier Identifier of a plate that should become owner of the new data set - * @param dataSetFile A file or folder containing the data - * @param dataSetMetadataOrNull The optional metadata overriding server defaults for the new - * data set - * @return A proxy to the newly added data set - * @throws IllegalStateException Thrown if the user has not yet been authenticated. - * @throws EnvironmentFailureException Thrown in cases where it is not possible to connect to - * the server. - * @throws IOException when accessing the data set file or folder fails - */ public IDataSetDss putDataSet(PlateIdentifier plateIdentifier, File dataSetFile, NewDataSetMetadataDTO dataSetMetadataOrNull) throws IllegalStateException, EnvironmentFailureException, IOException @@ -636,14 +623,33 @@ public class ScreeningOpenbisServiceFacade implements IScreeningOpenbisServiceFa return createDataSetDss(sample, dataSetMetadataOrNull, dataSetFile); } - private IDataSetDss createDataSetDss(Sample sample, - NewDataSetMetadataDTO dataSetMetadataOrNull, File dataSetFile) throws IOException + public IDataSetDss putDataSet(ExperimentIdentifier experimentIdentifier, File dataSetFile, + NewDataSetMetadataDTO dataSetMetadataOrNull) throws IllegalStateException, + EnvironmentFailureException, IOException { + final DataSetOwner dataSetOwner = + new DataSetOwner(DataSetOwnerType.EXPERIMENT, + experimentIdentifier.getAugmentedCode()); final NewDataSetMetadataDTO dataSetMetadata = (dataSetMetadataOrNull == null) ? new NewDataSetMetadataDTO() : dataSetMetadataOrNull; + return createDatasetDss(dataSetMetadata, dataSetFile, dataSetOwner); + } + + private IDataSetDss createDataSetDss(Sample sample, + NewDataSetMetadataDTO dataSetMetadataOrNull, File dataSetFile) throws IOException + { final DataSetOwner dataSetOwner = new DataSetOwner(DataSetOwnerType.SAMPLE, sample.getIdentifier()); + final NewDataSetMetadataDTO dataSetMetadata = + (dataSetMetadataOrNull == null) ? new NewDataSetMetadataDTO() + : dataSetMetadataOrNull; + return createDatasetDss(dataSetMetadata, dataSetFile, dataSetOwner); + } + + private IDataSetDss createDatasetDss(NewDataSetMetadataDTO dataSetMetadata, File dataSetFile, + final DataSetOwner dataSetOwner) throws IOException + { final String dataSetFolderNameOrNull = dataSetFile.isDirectory() ? dataSetFile.getName() : null; final List<FileInfoDssDTO> fileInfos = getFileInfosForPath(dataSetFile); @@ -1643,7 +1649,6 @@ public class ScreeningOpenbisServiceFacade implements IScreeningOpenbisServiceFa } }); } - private void processImagesStreamUnchecked(final IPlateImageHandler plateImageHandler, List<PlateImageReference> references, final InputStream stream) { diff --git a/screening/sourceTest/java/OpenBISScreeningMLTest.java b/screening/sourceTest/java/OpenBISScreeningMLTest.java index 11df591f5b2..9842828faf8 100644 --- a/screening/sourceTest/java/OpenBISScreeningMLTest.java +++ b/screening/sourceTest/java/OpenBISScreeningMLTest.java @@ -76,7 +76,8 @@ import ch.systemsx.cisd.openbis.plugin.screening.shared.api.v1.dto.WellPosition; */ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase { - private static final PlateIdentifier PLATE_1 = PlateIdentifier.createFromAugmentedCode("/S/PLATE-1"); + private static final PlateIdentifier PLATE_1 = PlateIdentifier + .createFromAugmentedCode("/S/PLATE-1"); private static final FilenameFilter FILTER_TEMP_DIR = new FilenameFilter() { @@ -318,7 +319,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase assertEquals(1, plates.length); context.assertIsSatisfied(); } - + @Test public void testListPlatesByExperimentAndAnalysisProcedure() { @@ -363,20 +364,19 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase will(returnValue(Arrays.asList("F1", "F2"))); } }); - + Object[][] features = OpenBISScreeningML.listFeatures("/S/P/E1", null); - + assertEquals("F1", features[0][0]); assertEquals("F2", features[1][0]); assertEquals(2, features.length); context.assertIsSatisfied(); } - + @Test public void testGetFeatureMatrixForGene() { - final MaterialIdentifier gene = new MaterialIdentifier( - MaterialTypeIdentifier.GENE, "GEN1"); + final MaterialIdentifier gene = new MaterialIdentifier(MaterialTypeIdentifier.GENE, "GEN1"); context.checking(new Expectations() { { @@ -393,16 +393,16 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase new double[] { 1.5, 42 }); FeatureVectorDatasetReference fds2 = - new FeatureVectorDatasetReference("ds2", "MY-TYPE", "", p2, - ExperimentIdentifier.createFromAugmentedCode("/S/P/E"), - Geometry.createFromCartesianDimensions(3, 2), new Date(4711), - null, null); + new FeatureVectorDatasetReference("ds2", "MY-TYPE", "", p2, + ExperimentIdentifier.createFromAugmentedCode("/S/P/E"), + Geometry.createFromCartesianDimensions(3, 2), new Date(4711), + null, null); FeatureVectorDatasetWellReference well2 = - new FeatureVectorDatasetWellReference(fds2, new WellPosition(2, 7)); + new FeatureVectorDatasetWellReference(fds2, new WellPosition(2, 7)); FeatureVectorWithDescription f2 = - new FeatureVectorWithDescription(well2, Arrays.asList("F1", "F2"), - new double[] - { -3, 7.125 }); + new FeatureVectorWithDescription(well2, Arrays.asList("F1", "F2"), + new double[] + { -3, 7.125 }); will(returnValue(Arrays.asList(f1, f2))); } }); @@ -410,14 +410,14 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase Object[][][][] matrix = OpenBISScreeningML.getFeatureMatrix("GEN1", null, null); assertEquals(Double.NaN, matrix[0][0][0][0]); - assertEquals(1.5, matrix[0][0][1][0]); + assertEquals(1.5, matrix[0][0][1][0]); assertEquals(Double.NaN, matrix[0][1][0][0]); assertEquals(Double.NaN, matrix[0][1][1][0]); assertEquals(Double.NaN, matrix[0][2][0][0]); - assertEquals(42.0, matrix[0][2][1][0]); - assertEquals(-3.0, matrix[0][0][0][1]); + assertEquals(42.0, matrix[0][2][1][0]); + assertEquals(-3.0, matrix[0][0][0][1]); assertEquals(Double.NaN, matrix[0][0][1][1]); - assertEquals(7.125, matrix[0][1][0][1]); + assertEquals(7.125, matrix[0][1][0][1]); assertEquals(Double.NaN, matrix[0][1][1][1]); assertEquals(Double.NaN, matrix[0][2][0][1]); assertEquals(Double.NaN, matrix[0][2][1][1]); @@ -436,7 +436,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase assertEquals(3, matrix.length); context.assertIsSatisfied(); } - + @Test public void testGetEmptyFeatureMatrixForPlate() { @@ -457,14 +457,14 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase assertEquals(0, matrix[2].length); context.assertIsSatisfied(); } - + @Test public void testGetFeatureMatrixForPlate() { context.checking(new Expectations() { { - one(openbis).loadFeaturesForPlates(Arrays.asList(PLATE_1), + one(openbis).loadFeaturesForPlates(Arrays.asList(PLATE_1), Arrays.asList("F1", "F2", "F3"), null); FeatureVectorDatasetReference ref = new FeatureVectorDatasetReference("ds1", "MY-TYPE", "", PLATE_1, @@ -487,7 +487,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase Object[][][][] matrix = OpenBISScreeningML.getFeatureMatrixForPlate("/S/PLATE-1", null, new String[] { "F1", "F2", "F3" }); - + assertPlateFeatures("[a, 1.5]", matrix[0][0]); assertPlateFeatures("[42.5, 42.0]", matrix[0][1]); assertEquals(2, matrix[0].length); @@ -503,7 +503,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase assertEquals(2, matrix[2].length); context.assertIsSatisfied(); } - + private void assertPlateFeatures(String expectedFeatures, Object[][] plateFeatures) { List<Object> list = new ArrayList<Object>(); @@ -514,7 +514,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase } assertEquals(expectedFeatures, list.toString()); } - + @Test public void testGetWellProperties() { @@ -577,13 +577,14 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase @Test public void testListDataSetsFiles() { - final RecordingMatcher<IDataSetFilter> filterMatcher = new RecordingMatcher<IDataSetFilter>(); + final RecordingMatcher<IDataSetFilter> filterMatcher = + new RecordingMatcher<IDataSetFilter>(); context.checking(new Expectations() { { one(openbis).getDataSets(with(p1), with(filterMatcher)); will(returnValue(Arrays.asList(ds1, ds2))); - + one(ds1).listFiles("/", true); will(returnValue(new FileInfoDssDTO[] { new FileInfoDssDTO("a", "a", true, -1), @@ -598,7 +599,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase will(returnValue("ds2")); } }); - + Object[][][] files = OpenBISScreeningML.listDataSetsFiles(p1.getAugmentedCode(), ".*"); assertEquals("Type:.*", filterMatcher.recordedObject().toString()); @@ -609,7 +610,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase assertEquals(2, files.length); context.assertIsSatisfied(); } - + @Test public void testLoadDataSetFile() { @@ -618,18 +619,18 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase { one(openbis).getDataSet("ds1"); will(returnValue(ds1)); - + one(ds1).getLinkOrCopyOfContent("root", tempDir, "a/b/c"); will(returnValue(new File("data"))); } }); - + Object file = OpenBISScreeningML.loadDataSetFile("ds1", "a/b/c", "root"); - + assertEquals("data", file); context.assertIsSatisfied(); } - + @Test public void testLoadDataSets() { @@ -640,7 +641,8 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase ds2Folder.mkdirs(); final String datasetTypePattern = "blablaCode"; final String mountPoint = "/mount/openbis/store"; - final RecordingMatcher<IDataSetFilter> filterMatcher = new RecordingMatcher<IDataSetFilter>(); + final RecordingMatcher<IDataSetFilter> filterMatcher = + new RecordingMatcher<IDataSetFilter>(); final DataSet dataSet1 = createDataSet("ds-1", ds1); final DataSet dataSet2 = createDataSet("ds-2", ds2); @@ -719,7 +721,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase assertEquals(2, result.length); context.assertIsSatisfied(); } - + @Test public void testUpdateDataSet() { @@ -763,6 +765,97 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase context.assertIsSatisfied(); } + @Test + public void testUploadDataSetForPlateAndParents() + { + final File dataSetFolder = + new File(OpenBISScreeningML.tempDir, OpenBISScreeningML.DATASETS_FOLDER); + final File ds1Folder = new File(dataSetFolder, "ds-1"); + ds1Folder.mkdirs(); + final RecordingMatcher<NewDataSetMetadataDTO> metaDataMatcher = + new RecordingMatcher<NewDataSetMetadataDTO>(); + context.checking(new Expectations() + { + { + Map<String, String> properties = new LinkedHashMap<String, String>(); + properties.put("A", "42"); + properties.put("B", "43"); + try + { + one(openbis).putDataSet(with(new Plate("PLATE-1", "S", "s-1", eId1)), + with(ds1Folder), with(metaDataMatcher)); + will(returnValue(ds1)); + } catch (Exception ex) + { + throw CheckedExceptionTunnel.wrapIfNecessary(ex); + } + + one(ds1).getCode(); + will(returnValue("DS-1")); + } + }); + + Object code = OpenBISScreeningML.uploadDataSetForPlateAndParents("/S/PLATE-1", new Object[] + { "DATA-SET-CODE1", "DATA-SET-CODE2" }, ds1Folder.getPath(), "my-type", new Object[][] + { + { "A", "42" }, + { "B", "43" } }); + + assertEquals("DS-1", code); + assertEquals("my-type", metaDataMatcher.recordedObject().tryDataSetType()); + assertEquals("{A=42, B=43}", metaDataMatcher.recordedObject().getProperties().toString()); + assertEquals("[DATA-SET-CODE1, DATA-SET-CODE2]", metaDataMatcher.recordedObject() + .getParentDataSetCodes().toString()); + context.assertIsSatisfied(); + } + + @Test + public void testUploadDataSetForExperimentAndParents() + { + final File dataSetFolder = + new File(OpenBISScreeningML.tempDir, OpenBISScreeningML.DATASETS_FOLDER); + final File ds1Folder = new File(dataSetFolder, "ds-1"); + ds1Folder.mkdirs(); + final RecordingMatcher<NewDataSetMetadataDTO> metaDataMatcher = + new RecordingMatcher<NewDataSetMetadataDTO>(); + context.checking(new Expectations() + { + { + Map<String, String> properties = new LinkedHashMap<String, String>(); + properties.put("A", "42"); + properties.put("B", "43"); + try + { + one(openbis).putDataSet( + with(new ExperimentIdentifier("S", "P", "E1", "e-1")), + with(ds1Folder), with(metaDataMatcher)); + will(returnValue(ds1)); + } catch (Exception ex) + { + throw CheckedExceptionTunnel.wrapIfNecessary(ex); + } + + one(ds1).getCode(); + will(returnValue("DS-1")); + } + }); + + Object code = + OpenBISScreeningML.uploadDataSetForExperimentAndParents("/S/P/E1", new Object[] + { "DATA-SET-CODE1", "DATA-SET-CODE2" }, ds1Folder.getPath(), "my-type", + new Object[][] + { + { "A", "42" }, + { "B", "43" } }); + + assertEquals("DS-1", code); + assertEquals("my-type", metaDataMatcher.recordedObject().tryDataSetType()); + assertEquals("{A=42, B=43}", metaDataMatcher.recordedObject().getProperties().toString()); + assertEquals("[DATA-SET-CODE1, DATA-SET-CODE2]", metaDataMatcher.recordedObject() + .getParentDataSetCodes().toString()); + context.assertIsSatisfied(); + } + @Test public void testLoadImages() { @@ -1009,7 +1102,7 @@ public class OpenBISScreeningMLTest extends AbstractFileSystemTestCase dsInitializer.putProperty(propKey, properties.get(propKey)); } - ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet metadata = + ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet metadata = new ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet(dsInitializer); return new DataSet(null, null, metadata, dataSetDss); } -- GitLab