JAL-2755 canonicalise the database source for a dbref before resolving a database...
[jalview.git] / src / jalview / util / DBRefUtils.java
index d43f5bc..bded248 100755 (executable)
@@ -29,7 +29,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -66,6 +65,9 @@ public class DBRefUtils
     canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
     canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
 
+    canonicalSourceNameLookup.put("ensemblgenomes",
+            DBRefSource.ENSEMBLGENOMES);
+
     // Make sure we have lowercase entries for all canonical string lookups
     Set<String> keys = canonicalSourceNameLookup.keySet();
     for (String k : keys)
@@ -101,14 +103,14 @@ public class DBRefUtils
     HashSet<String> srcs = new HashSet<String>();
     for (String src : sources)
     {
-      srcs.add(src);
+      srcs.add(src.toUpperCase());
     }
 
     List<DBRefEntry> res = new ArrayList<DBRefEntry>();
     for (DBRefEntry dbr : dbrefs)
     {
       String source = getCanonicalName(dbr.getSource());
-      if (srcs.contains(source))
+      if (srcs.contains(source.toUpperCase()))
       {
         res.add(dbr);
       }
@@ -141,8 +143,8 @@ public class DBRefUtils
       return false;
     }
     String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
-    return coordsys == null ? false : coordsys.equals(dBRefEntry
-            .getSource());
+    return coordsys == null ? false
+            : coordsys.equals(dBRefEntry.getSource());
   }
 
   /**
@@ -161,6 +163,20 @@ public class DBRefUtils
       return null;
     }
     String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
+    if (canonical==null)
+    {
+      if (source.toLowerCase().startsWith("ensembl"))
+      {
+        canonical = DBRefSource.ENSEMBL;
+        for (String ensembls: new String[] { "Protists","Plants","Bacteria","Fungi","Metazoa"})
+        {
+          if (source.toLowerCase().endsWith(ensembls.toLowerCase()))
+          {
+            canonical = DBRefSource.ENSEMBLGENOMES;
+          }
+        }
+      }
+    }
     return canonical == null ? source : canonical;
   }
 
@@ -258,9 +274,8 @@ public class DBRefUtils
           if (refa.getAccessionId() == null
                   || refb.getAccessionId().equals(refa.getAccessionId()))
           {
-            if (refa.getMap() == null
-                    || (refb.getMap() != null && refb.getMap().equals(
-                            refa.getMap())))
+            if (refa.getMap() == null || (refb.getMap() != null
+                    && refb.getMap().equals(refa.getMap())))
             {
               return true;
             }
@@ -302,19 +317,18 @@ public class DBRefUtils
     @Override
     public boolean matches(DBRefEntry refa, DBRefEntry refb)
     {
-      if (refa.getSource() != null
-              && refb.getSource() != null
+      if (refa.getSource() != null && refb.getSource() != null
               && DBRefUtils.getCanonicalName(refb.getSource()).equals(
                       DBRefUtils.getCanonicalName(refa.getSource())))
       {
         // We dont care about version
         if (refa.getAccessionId() != null && refb.getAccessionId() != null
-        // FIXME should be && not || here?
+                // FIXME should be && not || here?
                 || refb.getAccessionId().equals(refa.getAccessionId()))
         {
           if ((refa.getMap() == null || refb.getMap() == null)
-                  || (refa.getMap() != null && refb.getMap() != null && refb
-                          .getMap().equals(refa.getMap())))
+                  || (refa.getMap() != null && refb.getMap() != null
+                          && refb.getMap().equals(refa.getMap())))
           {
             return true;
           }
@@ -335,8 +349,7 @@ public class DBRefUtils
     @Override
     public boolean matches(DBRefEntry refa, DBRefEntry refb)
     {
-      if (refa.getSource() != null
-              && refb.getSource() != null
+      if (refa.getSource() != null && refb.getSource() != null
               && DBRefUtils.getCanonicalName(refb.getSource()).equals(
                       DBRefUtils.getCanonicalName(refa.getSource())))
       {
@@ -347,11 +360,12 @@ public class DBRefUtils
           if ((refa.getMap() == null && refb.getMap() == null)
                   || (refa.getMap() != null && refb.getMap() != null))
           {
-            if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
+            if ((refb.getMap().getMap() == null
+                    && refa.getMap().getMap() == null)
                     || (refb.getMap().getMap() != null
-                            && refa.getMap().getMap() != null && refb
-                            .getMap().getMap().getInverse()
-                            .equals(refa.getMap().getMap())))
+                            && refa.getMap().getMap() != null
+                            && refb.getMap().getMap().getInverse()
+                                    .equals(refa.getMap().getMap())))
             {
               return true;
             }
@@ -373,8 +387,7 @@ public class DBRefUtils
     @Override
     public boolean matches(DBRefEntry refa, DBRefEntry refb)
     {
-      if (refa.getSource() != null
-              && refb.getSource() != null
+      if (refa.getSource() != null && refb.getSource() != null
               && DBRefUtils.getCanonicalName(refb.getSource()).equals(
                       DBRefUtils.getCanonicalName(refa.getSource())))
       {
@@ -389,12 +402,13 @@ public class DBRefUtils
           {
             return true;
           }
-          if (refa.getMap() != null
-                  && refb.getMap() != null
-                  && ((refb.getMap().getMap() == null && refa.getMap()
-                          .getMap() == null) || (refb.getMap().getMap() != null
-                          && refa.getMap().getMap() != null && refb
-                          .getMap().getMap().equals(refa.getMap().getMap()))))
+          if (refa.getMap() != null && refb.getMap() != null
+                  && ((refb.getMap().getMap() == null
+                          && refa.getMap().getMap() == null)
+                          || (refb.getMap().getMap() != null
+                                  && refa.getMap().getMap() != null
+                                  && refb.getMap().getMap()
+                                          .equals(refa.getMap().getMap()))))
           {
             return true;
           }
@@ -414,8 +428,7 @@ public class DBRefUtils
     @Override
     public boolean matches(DBRefEntry refa, DBRefEntry refb)
     {
-      if (refa.getSource() != null
-              && refb.getSource() != null
+      if (refa.getSource() != null && refb.getSource() != null
               && DBRefUtils.getCanonicalName(refb.getSource()).equals(
                       DBRefUtils.getCanonicalName(refa.getSource())))
       {
@@ -429,11 +442,12 @@ public class DBRefUtils
             return true;
           }
           if ((refa.getMap() != null && refb.getMap() != null)
-                  && (refb.getMap().getMap() == null && refa.getMap()
-                          .getMap() == null)
+                  && (refb.getMap().getMap() == null
+                          && refa.getMap().getMap() == null)
                   || (refb.getMap().getMap() != null
-                          && refa.getMap().getMap() != null && (refb
-                          .getMap().getMap().equals(refa.getMap().getMap()))))
+                          && refa.getMap().getMap() != null
+                          && (refb.getMap().getMap()
+                                  .equals(refa.getMap().getMap()))))
           {
             return true;
           }
@@ -508,9 +522,7 @@ public class DBRefUtils
           PDBEntry pdbr = new PDBEntry();
           pdbr.setId(pdbid);
           pdbr.setType(PDBEntry.Type.PDB);
-          pdbr.setProperty(new Hashtable());
           pdbr.setChainCode(chaincode);
-          // pdbr.getProperty().put("CHAIN", chaincode);
           seq.addPDBId(pdbr);
         }
         else
@@ -564,8 +576,8 @@ public class DBRefUtils
     {
       return true;
     }
-    return DBRefUtils.getCanonicalName(o1).equals(
-            DBRefUtils.getCanonicalName(o2));
+    return DBRefUtils.getCanonicalName(o1)
+            .equals(DBRefUtils.getCanonicalName(o2));
   }
 
   /**
@@ -581,8 +593,8 @@ public class DBRefUtils
   public static DBRefEntry[] selectDbRefs(boolean selectDna,
           DBRefEntry[] refs)
   {
-    return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
-            : DBRefSource.PROTEINDBS);
+    return selectRefs(refs,
+            selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
     // could attempt to find other cross
     // refs here - ie PDB xrefs
     // (not dna, not protein seq)
@@ -604,7 +616,8 @@ public class DBRefUtils
     {
       for (DBRefEntry dbref : dbRefs)
       {
-        if (source.equalsIgnoreCase(dbref.getSource()))
+        if (source.equalsIgnoreCase(
+                DBRefUtils.getCanonicalName(dbref.getSource())))
         {
           matches.add(dbref);
         }
@@ -696,8 +709,7 @@ public class DBRefUtils
       }
 
       // collate candidates and promote them
-      DBRefEntry[] candidates = selectRefs(
-              selfs.toArray(new DBRefEntry[0]),
+      DBRefEntry[] candidates = selectRefs(selfs.toArray(new DBRefEntry[0]),
               promType.toArray(new String[0]));
       if (candidates != null)
       {
@@ -727,9 +739,9 @@ public class DBRefUtils
           toPromote.add(cand);
           if (!cand.isPrimaryCandidate())
           {
-            System.out.println("Warning: Couldn't promote dbref "
-                    + cand.toString() + " for sequence "
-                    + sequence.toString());
+            System.out.println(
+                    "Warning: Couldn't promote dbref " + cand.toString()
+                            + " for sequence " + sequence.toString());
           }
         }
       }