diff --git a/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java b/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java index ea865fc9c27ed036c0b9344460b8bc0641deab84..d734dde6b11f847a980e248335873a19c7f49935 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java +++ b/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java @@ -142,6 +142,25 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement checkLocation(getWellGeometry(), wellLocation); } + private final IDirectory getImageRootDirectoryNode(final File imageRootDirectory) + { + final IDirectory originalDataDirectory = getOriginalDataDirectory(); + final String imageRootDirName = imageRootDirectory.getName(); + // If not already present, move the 'imageRootDirectory' to 'original' data directory. + if (originalDataDirectory.tryGetNode(imageRootDirName) == null) + { + originalDataDirectory.addFile(imageRootDirectory, null, true); + } + final INode imageRootNode = originalDataDirectory.tryGetNode(imageRootDirName); + if (imageRootNode == null) + { + throw new DataStructureException(String.format( + "No image root directory named '%s' could be found in the original directory.", imageRootDirName)); + } + assert imageRootNode instanceof IDirectory : "Image root node must be a directory."; + return (IDirectory) imageRootNode; + } + // // IHCSFormattedData // @@ -162,9 +181,11 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement } } - public final NodePath addStandardNode(final File imageFile, int channel, final Location plateLocation, - final Location wellLocation) throws DataStructureException + public final NodePath addStandardNode(final File imageRootDirectory, final String imageRelativePath, + final int channel, final Location plateLocation, final Location wellLocation) throws DataStructureException { + assert imageRootDirectory != null : "Given image root directory can not be null."; + assert imageRelativePath != null : "Given image relative path can not be null."; INode node = tryGetStandardNodeAt(channel, plateLocation, wellLocation); if (node != null) { @@ -172,7 +193,6 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement "A node already exists at channel %d, plate location '%s' and well location '%s'.", channel, plateLocation, wellLocation)); } - assert imageFile != null : "Given original file name can not be null."; final IDirectory standardDir = getStandardDataDirectory(); final IDirectory channelDir = Utilities.getOrCreateSubDirectory(standardDir, getChannelName(channel)); final IDirectory plateRowDir = Utilities.getOrCreateSubDirectory(channelDir, getPlateRowDirName(plateLocation)); @@ -181,16 +201,18 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement final String wellFileName = createWellFileName(wellLocation); if (containsOriginalData()) { - final INode originalNode = getOriginalDataDirectory().addFile(imageFile, null, true); - if (originalNode == null) + final IDirectory imageRootDirectoryNode = getImageRootDirectoryNode(imageRootDirectory); + final INode imageNode = imageRootDirectoryNode.tryGetNode(imageRelativePath); + if (imageNode == null) { - throw new DataStructureException(String.format("No original node with name '%s' could be found.", - imageFile)); + throw new DataStructureException(String.format( + "No image node with path '%s' could be found in the original directory.", imageRelativePath)); } - node = plateColumnDir.tryAddLink(wellFileName, originalNode); + node = plateColumnDir.tryAddLink(wellFileName, imageNode); } else { - node = plateColumnDir.addFile(imageFile, wellFileName, true); + // Copies the file. So we are able to undo the operation. + node = plateColumnDir.addFile(new File(imageRootDirectory, imageRelativePath), wellFileName, false); } if (node == null) { @@ -198,7 +220,7 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement String .format( "Original file name '%s' could not be added at channel %d, plate location '%s' and well location '%s'.", - imageFile, channel, plateLocation, wellLocation)); + imageRelativePath, channel, plateLocation, wellLocation)); } final char sep = Constants.PATH_SEPARATOR; final String standardNodePath = diff --git a/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java b/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java index bd3eaf39cecf45ed7e219529e6443aba983253cd..7e42df5d01e144a124c10525cb541a2237a1e8f1 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java +++ b/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java @@ -44,13 +44,16 @@ public interface IHCSImageFormattedData extends IFormattedData /** * Adds a new image file at given coordinates. * + * @param imageRootDirectory the root directory where the image is located. This usually is the incoming data set + * directory. + * @param imageRelativePath relative path (to <var>imageRootDirectory</var>) name of the image file that is going + * to be added in the <code>standard</code> directory. * @return the new <code>INode</code> just added (encapsulated in returned <code>NodePath</code>) with its path * in the <code>standard</code> directory. Never returns <code>null</code>. - * @param imageFile name of the image file that is going to be added in the <code>standard</code> directory. * @throws DataStructureException if a node already exists at given coordinates. */ - public NodePath addStandardNode(final File imageFile, final int channel, final Location plateLocation, - final Location wellLocation) throws DataStructureException; + public NodePath addStandardNode(final File imageRootDirectory, final String imageRelativePath, final int channel, + final Location plateLocation, final Location wellLocation) throws DataStructureException; // // Helper classes diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java index 9edccc78314479b636265ff049ced8019c87b3fb..a5b54ec75bb9e8fd8aa63019943fba03628ac931 100644 --- a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java +++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java @@ -54,6 +54,10 @@ import ch.systemsx.cisd.common.utilities.AbstractFileSystemTestCase; public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase { + private static final String IMAGE_ROOT_DIRECTORY_NAME = "CP001-1"; + + private static final String ORIGINAL_TIFF = "original.tiff"; + static final Location PLATE_LOCATION = new Location(2, 1); static final Location WELL_LOCATION = new Location(1, 2); @@ -68,7 +72,11 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase private IDirectory standardNode; - private IDirectory leafDirectory; + private IDirectory originalNode; + + private IDirectory standardLeafDirectory; + + private File imageRootDirectory; private final void prepareAndCreateFormattedData() { @@ -92,10 +100,19 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase private final void prepareStandardNode() { - standardNode = NodeFactory.createDirectoryNode(workingDirectory); + final File standardDirectory = new File(workingDirectory, DataStructureV1_0.DIR_STANDARD); + standardDirectory.mkdir(); + standardNode = NodeFactory.createDirectoryNode(standardDirectory); final IDirectory channel1 = standardNode.makeDirectory(Channel.CHANNEL + "1"); final IDirectory row1 = channel1.makeDirectory(HCSImageFormattedData.ROW + "1"); - leafDirectory = row1.makeDirectory(HCSImageFormattedData.COLUMN + "2"); + standardLeafDirectory = row1.makeDirectory(HCSImageFormattedData.COLUMN + "2"); + } + + private final void prepareOriginalNode() + { + final File originalDirectory = new File(workingDirectory, DataStructureV1_0.DIR_ORIGINAL); + originalDirectory.mkdir(); + originalNode = NodeFactory.createDirectoryNode(originalDirectory); } private final void addParameterCheckExpectations(final Expectations exp) @@ -110,6 +127,14 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase exp.will(exp.returnValue(geometry)); } + private final void prepareImageRootDirectory() throws IOException + { + imageRootDirectory = new File(workingDirectory, IMAGE_ROOT_DIRECTORY_NAME); + imageRootDirectory.mkdir(); + final File originalFile = new File(imageRootDirectory, ORIGINAL_TIFF); + FileUtils.writeStringToFile(originalFile, "This is my original file..."); + } + // // AbstractFileSystemTestCase // @@ -122,6 +147,7 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase context = new Mockery(); prepareAndCreateFormattedData(); prepareStandardNode(); + prepareOriginalNode(); } @AfterMethod @@ -169,11 +195,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase @Test public final void testAddStandardNodeWithOriginalData() throws IOException { - final String originalFilePath = "original.txt"; - final File originalFile = new File(workingDirectory, originalFilePath); - FileUtils.writeStringToFile(originalFile, "This is my original file..."); + prepareImageRootDirectory(); final int channelIndex = 1; - final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory); context.checking(new Expectations() { { @@ -190,29 +213,27 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase } }); final NodePath nodePath = - formattedData.addStandardNode(originalFile, channelIndex, PLATE_LOCATION, WELL_LOCATION); + formattedData.addStandardNode(imageRootDirectory, ORIGINAL_TIFF, channelIndex, PLATE_LOCATION, + WELL_LOCATION); final String standardFileName = "row2_column1.tiff"; assertNotNull(nodePath); assertTrue(nodePath.node instanceof ILink); assertEquals(standardFileName, nodePath.node.getName()); // Look at the standard leaf directory if the node is there as well. - final INode standardFile = leafDirectory.tryGetNode(standardFileName); + final INode standardFile = standardLeafDirectory.tryGetNode(standardFileName); assertNotNull(standardFile); assertTrue(standardFile instanceof IFile); assertEquals(standardFileName, standardFile.getName()); // Node should still be in the 'original' directory. - assertNotNull(originalNode.tryGetNode(originalFilePath)); + assertNotNull(((IDirectory) originalNode.tryGetNode(IMAGE_ROOT_DIRECTORY_NAME)).tryGetNode(ORIGINAL_TIFF)); context.assertIsSatisfied(); } @Test public final void testAddStandardNodeWithoutOriginalData() throws IOException { - final String originalFilePath = "original.tiff"; - final File originalFile = new File(workingDirectory, originalFilePath); - FileUtils.writeStringToFile(originalFile, "This is my original file..."); + prepareImageRootDirectory(); final int channelIndex = 1; - final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory); context.checking(new Expectations() { { @@ -226,18 +247,19 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase } }); final NodePath nodePath = - formattedData.addStandardNode(originalFile, channelIndex, PLATE_LOCATION, WELL_LOCATION); + formattedData.addStandardNode(imageRootDirectory, ORIGINAL_TIFF, channelIndex, PLATE_LOCATION, + WELL_LOCATION); final String standardFileName = "row2_column1.tiff"; assertNotNull(nodePath); assertTrue(nodePath.node instanceof IFile); assertEquals(standardFileName, nodePath.node.getName()); // Look at the standard leaf directory if the node is there as well. - final INode standardFile = leafDirectory.tryGetNode(standardFileName); + final INode standardFile = standardLeafDirectory.tryGetNode(standardFileName); assertNotNull(standardFile); assertTrue(standardFile instanceof IFile); assertEquals(standardFileName, standardFile.getName()); // Node should no longer be in the 'original' directory. - assertNull(originalNode.tryGetNode(originalFilePath)); + assertNull(originalNode.tryGetNode(IMAGE_ROOT_DIRECTORY_NAME)); context.assertIsSatisfied(); } @@ -265,7 +287,7 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase { final File file = new File(workingDirectory, "row2_column1.tiff"); FileUtils.writeStringToFile(file, "This is my original file..."); - leafDirectory.addFile(file, null, true); + standardLeafDirectory.addFile(file, null, true); final int channelIndex = 1; context.checking(new Expectations() {