From 34e3b5fdb2b5877960b65765411ea7f7d1620b5e Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Mon, 26 Mar 2012 21:29:17 +0000
Subject: [PATCH] Add methods getCamera, getEmissionTag, getExitationTag,
 getExpCam, getFilterRef, getLightSRef, getWaveLength, getChannelDescription
 from Matt.

SVN: 24786
---
 .../imagereaders/bioformats/FlexHelper.java   | 126 ++++++++++++++++--
 .../bioformats/FlexHelperTest.java            |  17 ++-
 2 files changed, 129 insertions(+), 14 deletions(-)

diff --git a/image_readers/source/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelper.java b/image_readers/source/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelper.java
index d1ef872d845..c03cc4d713a 100644
--- a/image_readers/source/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelper.java
+++ b/image_readers/source/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelper.java
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package ch.systemsx.cisd.imagereaders.bioformats;
 
 import java.io.StringReader;
@@ -44,9 +43,9 @@ import ch.systemsx.cisd.common.geometry.SpatialPoint;
 /**
  * A helper class with utility methods that parse metadata from FLEX files.
  * <p>
- * Clients can implement their own utility methods on the top of FlexHelper, by parsing the 
- * metadata XML as returned by {@link #getMetadata()} or by directly executing XPath query 
- * via {@link #selectByXpathQuery(String)}.
+ * Clients can implement their own utility methods on the top of FlexHelper, by parsing the metadata
+ * XML as returned by {@link #getMetadata()} or by directly executing XPath query via
+ * {@link #selectByXpathQuery(String)}.
  * 
  * @author Kaloyan Enimanev
  */
@@ -61,6 +60,21 @@ public class FlexHelper
     private static final String SELECT_TILE_FOR_IMAGE =
             "//Images/Image[@BufferNo='%s']/Sublayout/text()";
 
+    private static final String SELECT_FILTER_REF_FOR_IMAGE =
+            "//Images/Image[@BufferNo='%s']/FilterCombinationRef/text()";
+
+    private static final String SELECT_LIGHTS_REF_FOR_IMAGE =
+            "//Images/Image[@BufferNo='%s']/LightSourceCombinationRef/text()";
+
+    private static final String SELECT_EMISSION_FOR_IMAGE =
+            "//FilterCombinations/FilterCombination[@ID='%s']/SliderRef[@ID='%s']";
+
+    private static final String SELECT_LASER_FOR_IMAGE =
+            "//LightSourceCombinations/LightSourceCombination[@ID='%s']/LightSourceRef/@ID";
+
+    private static final String SELECT_EXCITATION_FOR_IMAGE =
+            "//LightSources/LightSource[@ID='%s']/Wavelength/text()";
+
     private static final String SELECT_TILE_XCOORDS = "//Sublayouts/Sublayout/Field/OffsetX/text()";
 
     private static final String SELECT_TILE_YCOORDS = "//Sublayouts/Sublayout/Field/OffsetY/text()";
@@ -95,7 +109,7 @@ public class FlexHelper
         String tile = nodeList.item(0).getNodeValue();
         return Integer.parseInt(tile.trim());
     }
-    
+
     public String getChannelCode(int imageIdx)
     {
         NodeList nodeList = selectByXpathQuery(SELECT_CHANNELS);
@@ -106,6 +120,100 @@ public class FlexHelper
         return nodeList.item(imageIdx).getNodeValue();
     }
 
+    public String getChannelDescription(int imageIdx)
+    {
+        String out =
+                getChannelCode(imageIdx) + " - Ex:" + getExcitationTag(imageIdx) + "nm Em:"
+                        + getEmissionTag(imageIdx) + "nm";
+        return out;
+    }
+
+    public String getExpCam(int imageIdx)
+    {
+        NodeList nodeList = selectByXpathQuery(SELECT_CHANNELS);
+        if (imageIdx < 0 || nodeList.getLength() <= imageIdx)
+        {
+            throw new IllegalArgumentException("No image can be matched to idx=" + imageIdx);
+        }
+        return nodeList.item(imageIdx).getNodeValue();
+    }
+
+    public String getFilterRef(int imageIdx)
+    {
+        String query = String.format(SELECT_FILTER_REF_FOR_IMAGE, imageIdx);
+        NodeList nodeList = selectByXpathQuery(query);
+        if (nodeList.getLength() == 0)
+        {
+            throw new IllegalArgumentException("No image can be matched to idx=" + imageIdx);
+        }
+        String filterRef = nodeList.item(0).getNodeValue();
+        return filterRef.trim();
+    }
+
+    public String getLightSRef(int imageIdx)
+    {
+        String query = String.format(SELECT_LIGHTS_REF_FOR_IMAGE, imageIdx);
+        NodeList nodeList = selectByXpathQuery(query);
+        if (nodeList.getLength() == 0)
+        {
+            throw new IllegalArgumentException("No image can be matched to idx=" + imageIdx);
+        }
+        String filterRef = nodeList.item(0).getNodeValue();
+        return filterRef.trim();
+    }
+
+    public String getCamera(int imageIdx)
+    {
+        String expCam = getExpCam(imageIdx);
+        String cam = "Camera" + expCam.split("Cam")[1];
+        return cam.trim();
+    }
+
+    public String getEmissionTag(int imageIdx)
+    {
+        String query =
+                String.format(SELECT_EMISSION_FOR_IMAGE, getFilterRef(imageIdx),
+                        getCamera(imageIdx));
+        NodeList nodeList = selectByXpathQuery(query);
+        if (nodeList.getLength() == 0)
+        {
+            throw new IllegalArgumentException("No image can be matched to idx=" + imageIdx);
+        }
+        String emissionTag = nodeList.item(0).getAttributes().getNamedItem("Filter").getNodeValue();
+        return emissionTag.trim();
+
+    }
+
+    public String getExcitationTag(int imageIdx)
+    {
+        String query = String.format(SELECT_LASER_FOR_IMAGE, getLightSRef(imageIdx));
+        NodeList nodeList = selectByXpathQuery(query);
+        if (nodeList.getLength() == 0)
+        {
+            throw new IllegalArgumentException("No image can be matched to idx=" + imageIdx);
+        }
+        String out = "";
+        for (int i = 0; i < nodeList.getLength(); i++)
+        {
+            out = out + getWaveLength(nodeList.item(i).getNodeValue()) + ",";
+        }
+        out = out.substring(0, out.length() - 1);
+        return out;
+
+    }
+
+    public String getWaveLength(String laser)
+    {
+        String query = String.format(SELECT_EXCITATION_FOR_IMAGE, laser);
+        NodeList nodeList = selectByXpathQuery(query);
+        if (nodeList.getLength() == 0)
+        {
+            throw new IllegalArgumentException("No image can be matched to idx=" + laser);
+        }
+        String waveLength = nodeList.item(0).getNodeValue();
+        return waveLength.trim();
+    }
+
     public Map<Integer, SpatialPoint> getTileCoordinates()
     {
         Map<Integer, SpatialPoint> points = new HashMap<Integer, SpatialPoint>();
@@ -193,11 +301,11 @@ public class FlexHelper
             return dBF.newDocumentBuilder().parse(is);
         } catch (Exception e)
         {
-            final String errorMessage = String.format(
-                    "Failed to parse FLEX metadata :\n\n%s\n\nisn't a well formed XML document. %s",
-                    value, e.getMessage());
+            final String errorMessage =
+                    String.format(
+                            "Failed to parse FLEX metadata :\n\n%s\n\nisn't a well formed XML document. %s",
+                            value, e.getMessage());
             throw new IllegalArgumentException(errorMessage);
         }
     }
-
 }
diff --git a/image_readers/sourceTest/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelperTest.java b/image_readers/sourceTest/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelperTest.java
index 6722a621ad4..1fcb3fb6a9d 100644
--- a/image_readers/sourceTest/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelperTest.java
+++ b/image_readers/sourceTest/java/ch/systemsx/cisd/imagereaders/bioformats/FlexHelperTest.java
@@ -39,6 +39,8 @@ public class FlexHelperTest extends ImageReaderTestCase
 
         assertEquals(1, helper.getTileNumber(0));
         assertEquals("Exp1Cam1", helper.getChannelCode(0));
+        assertEquals("488", helper.getExcitationTag(0));
+        assertEquals("565/40", helper.getEmissionTag(0));
 
         Map<Integer, SpatialPoint> tileCoordinates = helper.getTileCoordinates();
         assertEquals(1, tileCoordinates.size());
@@ -59,16 +61,21 @@ public class FlexHelperTest extends ImageReaderTestCase
             { "Exp1", "Exp1", "Exp2" };
         final String[] CAMS = new String[]
             { "Cam1", "Cam3", "Cam2" };
-        
+        final String[] EXCITATION = new String[]
+            { "561,405", "561,405", "488" };
+        final String[] EMISSION = new String[]
+            { "450/50", "690/70", "565/40" };
+
         for (int i = 0; i < 12; i++)
         {
             int parsedTile = helper.getTileNumber(i);
-            String parsedChannelName = helper.getChannelCode(i);
             assertEquals((i / 3) + 1, parsedTile);
-            String channelName = EXPOSURES[ i % 3 ] + CAMS[ i % 3 ];
-            assertEquals(channelName, parsedChannelName);
+            String channelName = EXPOSURES[i % 3] + CAMS[i % 3];
+            assertEquals(channelName, helper.getChannelCode(i));
+            assertEquals(EXCITATION[i % 3], helper.getExcitationTag(i));
+            assertEquals(EMISSION[i % 3], helper.getEmissionTag(i));
         }
-        
+
         Map<Integer, SpatialPoint> tileCoordinates = helper.getTileCoordinates();
         assertEquals(4, tileCoordinates.size());
         assertEquals(new SpatialPoint(4.44E-4, 6.61E-4), tileCoordinates.get(1));
-- 
GitLab