From 8dece9ea7a12aa6ade3893c640183f6add84fe99 Mon Sep 17 00:00:00 2001
From: tpylak <tpylak>
Date: Tue, 17 Aug 2010 10:50:26 +0000
Subject: [PATCH] LMS-1645 dynamix: parse timepoints correctly during dataset
 upload

SVN: 17473
---
 .../etl/dynamix/HCSImageFileExtractor.java    | 53 ++++++++++++++++---
 .../dynamix/HCSImageFileExtractorTest.java    | 44 +++++++++++++++
 2 files changed, 91 insertions(+), 6 deletions(-)
 create mode 100644 screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractorTest.java

diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractor.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractor.java
index 76f3e513e42..4361be170e0 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractor.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractor.java
@@ -17,8 +17,11 @@
 package ch.systemsx.cisd.openbis.dss.etl.dynamix;
 
 import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -28,7 +31,9 @@ import java.util.Set;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang.StringUtils;
 
+import ch.rinn.restrictions.Private;
 import ch.systemsx.cisd.bds.hcs.Location;
+import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
 import ch.systemsx.cisd.openbis.dss.etl.AbstractHCSImageFileExtractor;
 import ch.systemsx.cisd.openbis.dss.etl.AcquiredPlateImage;
 import ch.systemsx.cisd.openbis.dss.etl.HCSImageFileExtractionResult.Channel;
@@ -43,6 +48,8 @@ import ch.systemsx.cisd.openbis.plugin.screening.shared.basic.dto.WellLocation;
  */
 public class HCSImageFileExtractor extends AbstractHCSImageFileExtractor
 {
+    private static final String DYNAMIX_TOKEN_SEPARATOR = "_";
+
     private static final String POSITION_MAPPING_FILE_NAME = "pos2loc.tsv";
 
     private final List<String> channelNames;
@@ -87,7 +94,7 @@ public class HCSImageFileExtractor extends AbstractHCSImageFileExtractor
      */
     protected final Location tryGetPlateLocation(final String plateLocation)
     {
-        final String[] tokens = StringUtils.split(plateLocation, "_");
+        final String[] tokens = StringUtils.split(plateLocation, DYNAMIX_TOKEN_SEPARATOR);
         Integer row = new Integer(tokens[0]);
         Integer column = new Integer(tokens[1]);
         return Location.tryCreateLocationFromRowAndColumn(row, column);
@@ -102,8 +109,8 @@ public class HCSImageFileExtractor extends AbstractHCSImageFileExtractor
     @Override
     protected final ImageFileInfo tryExtractImageInfo(File imageFile, SampleIdentifier datasetSample)
     {
-        final String baseName = FilenameUtils.getBaseName(imageFile.getPath());
-        final String[] tokens = StringUtils.split(baseName, "_");
+        String baseName = FilenameUtils.getBaseName(imageFile.getPath());
+        String[] tokens = StringUtils.split(baseName, DYNAMIX_TOKEN_SEPARATOR);
         if (tokens == null || tokens.length != 5)
         {
             if (operationLog.isDebugEnabled())
@@ -119,14 +126,48 @@ public class HCSImageFileExtractor extends AbstractHCSImageFileExtractor
         // "left_dia_pos100_t20100227_152439.tif"
         ImageFileInfo info = new ImageFileInfo();
         // row_column - will be parsed later. It's unnecessary and should be refactored.
-        info.setPlateLocationToken(wellLocation.getRow() + "_" + wellLocation.getColumn());
+        info.setPlateLocationToken(wellLocation.getRow() + DYNAMIX_TOKEN_SEPARATOR
+                + wellLocation.getColumn());
         info.setWellLocationToken(null);
         info.setChannelToken(tokens[1]);
 
+        long timepoint = getSecondsFromFirstMeasurement(imageFile, tokens);
+        info.setTimepointToken("" + timepoint);
+        return info;
+    }
+
+    private static long getSecondsFromFirstMeasurement(File imageFile, String[] tokens)
+    {
         File[] images = imageFile.getParentFile().listFiles();
         Arrays.sort(images);
-        info.setTimepointToken("" + Arrays.asList(images).indexOf(imageFile));
-        return info;
+        String firstMeasurementFilePath = images[0].getPath();
+
+        String firstMeasurementFileBaseName = FilenameUtils.getBaseName(firstMeasurementFilePath);
+        String[] firstMeasurementTokens =
+                StringUtils.split(firstMeasurementFileBaseName, DYNAMIX_TOKEN_SEPARATOR);
+
+        return getSecondsFromFirstMeasurement(tokens, firstMeasurementTokens);
+    }
+
+    @Private
+    static long getSecondsFromFirstMeasurement(String[] tokens, String[] firstMeasurementTokens)
+    {
+        Date firstMeasurementDate = parseDate(firstMeasurementTokens);
+        Date thisMeasurementDate = parseDate(tokens);
+        return (thisMeasurementDate.getTime() - firstMeasurementDate.getTime()) / 1000;
+    }
+
+    private static Date parseDate(String[] tokens)
+    {
+        // t20100227_152439 -> 20100227152439
+        String dateToken = tokens[3].substring(1) + tokens[4];
+        try
+        {
+            return new SimpleDateFormat("yyyymmddhhmmss").parse(dateToken);
+        } catch (ParseException ex)
+        {
+            throw new EnvironmentFailureException("Cannot parse the data in the file name: " + ex);
+        }
     }
 
     private WellLocation getWellLocation(File imageFile, final String[] tokens)
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractorTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractorTest.java
new file mode 100644
index 00000000000..338d9d5adae
--- /dev/null
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/etl/dynamix/HCSImageFileExtractorTest.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.dss.etl.dynamix;
+
+import org.testng.AssertJUnit;
+import org.testng.annotations.Test;
+
+import ch.rinn.restrictions.Friend;
+
+/**
+ * Tests of {@link HCSImageFileExtractor}
+ * 
+ * @author Tomasz Pylak
+ */
+@Friend(toClasses = HCSImageFileExtractor.class)
+public class HCSImageFileExtractorTest extends AssertJUnit
+{
+    @Test
+    public void testParseTimepoint()
+    {
+        String[] tokensThisMeasurement = new String[]
+            { "left", "dia", "pos100", "t20100227", "162542" };
+        String[] tokensFirstMeasurement = new String[]
+            { "left", "dia", "pos100", "t20100227", "152439" };
+        long secondsFromFirstMeasurement =
+                HCSImageFileExtractor.getSecondsFromFirstMeasurement(tokensThisMeasurement,
+                        tokensFirstMeasurement);
+        assertEquals(3 + 60 + 3600, secondsFromFirstMeasurement);
+    }
+}
-- 
GitLab