From ec96ebc537b13cf8a74b2760f10b98456d163cbe Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 23 Nov 2018 09:57:00 +0100
Subject: [PATCH] proper recon element is used when annotating by name

---
 .../services/annotators/ElementAnnotator.java |  5 +++
 .../services/annotators/ReconAnnotator.java   | 37 ++++++++++++++++++-
 .../annotators/ReconAnnotatorTest.java        | 15 ++++++++
 .../lcsb/mapviewer/model/map/MiriamType.java  |  9 ++++-
 4 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java
index 520c32d4f3..909d574132 100644
--- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java
+++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java
@@ -523,6 +523,11 @@ public abstract class ElementAnnotator extends CachableInterface {
     element.addMiriamData(md);
   }
 
+  protected void addRhea(BioEntity element, String value) {
+    MiriamData md = createMiriamData(MiriamType.RHEA, value);
+    element.addMiriamData(md);
+  }
+
   /**
    * Sets {@link BioEntity#getFormula()}.
    * 
diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java
index 8cf0fd072e..110a1bab6d 100644
--- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java
+++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java
@@ -7,6 +7,7 @@ import java.util.Map.Entry;
 
 import org.apache.log4j.Logger;
 
+import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
@@ -142,7 +143,7 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
           JsonElement jelement = new JsonParser().parse(content);
           JsonObject jobject = jelement.getAsJsonObject();
           if (jobject.get("results").getAsJsonArray().size() > 0) {
-            jobject = jobject.get("results").getAsJsonArray().get(0).getAsJsonObject();
+            jobject = getBestMatchForAbbreviation(id, jobject.get("results").getAsJsonArray());
 
             for (Entry<String, JsonElement> entry : jobject.entrySet()) {
               String key = entry.getKey();
@@ -294,9 +295,18 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
                 continue;
               } else if (key.equals("casRegistry")) {
                 addCas(annotatedObject, value);
+              } else if (key.equals("rhea")) {
+                addRhea(annotatedObject, value);
               } else if (key.equals("epa_id")) {
                 // for now we don't handle it
                 continue;
+              } else if (key.equals("biocyc")) {
+                // for now we don't handle it
+                continue;
+              } else if (key.equals("createdDate")) {
+                continue;
+              } else if (key.equals("updatedDate")) {
+                continue;
               } else {
                 unknown = true;
               }
@@ -316,6 +326,31 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
     }
   }
 
+  private JsonObject getBestMatchForAbbreviation(String id, JsonArray asJsonArray) {
+    String match = null;
+    JsonObject result = null;
+    for (JsonElement element : asJsonArray) {
+      if (((JsonObject) element).get("abbreviation") != null) {
+        String objAbreviation = ((JsonObject) element).get("abbreviation").getAsString();
+        if (match == null) {
+          match = objAbreviation;
+          result = (JsonObject) element;
+        } else if (match.length() > objAbreviation.length()) {
+          match = objAbreviation;
+          result = (JsonObject) element;
+        } else if (id.equalsIgnoreCase(objAbreviation)) {
+          match = objAbreviation;
+          result = (JsonObject) element;
+        }
+      } else if (match == null) {
+        logger.warn("No abbreviation found for element: " + element);
+        result = (JsonObject) element;
+      }
+    }
+    return result;
+  }
+
+
   @Override
   public String getCommonName() {
     return "Recon annotator";
diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java
index 3fe3c8a56a..47f4ec4024 100644
--- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java
+++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java
@@ -189,6 +189,21 @@ public class ReconAnnotatorTest extends AnnotationTestFunctions {
     }
   }
 
+  @Test
+  public void testAnnotatingForOxygen() throws Exception {
+    try {
+      SimpleMolecule molecule = new SimpleMolecule("id");
+      molecule.setName("o2");
+      reconAnnotator.annotateElement(molecule);
+      assertTrue(molecule.getMiriamData().size() > 0);
+      assertEquals(0, getWarnings().size());
+      assertEquals("O2", molecule.getFormula());
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
   @Test
   public void testAnnotatingWithWhitespace() throws Exception {
     try {
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java b/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java
index 40a92c1e2d..ebd5dabeed 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java
@@ -469,7 +469,7 @@ public enum MiriamType {
    */
   VMH_METABOLITE("VMH metabolite", //
       "https://vmh.uni.lu/", //
-      "urn:miriam:vmhmetabolite", //
+      new String[] { "urn:miriam:vmhmetabolite", "http://identifiers.org/vmhmetabolite/" }, //
       new Class<?>[] { Chemical.class }, "MIR:00000636"),
 
   /**
@@ -477,7 +477,7 @@ public enum MiriamType {
    */
   VMH_REACTION("VMH reaction", //
       "https://vmh.uni.lu/", //
-      "urn:miriam:vmhreaction", //
+      new String[] { "urn:miriam:vmhreaction", "http://identifiers.org/vmhreaction/" }, //
       new Class<?>[] { Reaction.class }, "MIR:00000640"),
 
   /**
@@ -741,6 +741,11 @@ public enum MiriamType {
             foundType = type;
             foundUri = uri;
           }
+        } else if (miriamUri.startsWith(uri) && uri.endsWith("/")) {
+          if (uri.length() > foundUri.length()) {
+            foundType = type;
+            foundUri = uri;
+          }
         }
       }
     }
-- 
GitLab