JAL-2106 stricter check for 1:1 mapping (same numbering on both sides, and word size...
[jalview.git] / src / jalview / datamodel / DBRefEntry.java
index a641b1b..11e77d8 100755 (executable)
@@ -22,9 +22,12 @@ package jalview.datamodel;
 
 import jalview.api.DBRefEntryI;
 
+import java.util.Arrays;
+
 public class DBRefEntry implements DBRefEntryI
 {
   String source = "", version = "", accessionId = "";
+
   /**
    * maps from associated sequence to the database sequence's coordinate system
    */
@@ -35,7 +38,6 @@ public class DBRefEntry implements DBRefEntryI
 
   }
 
-
   public DBRefEntry(String source, String version, String accessionId)
   {
     this(source, version, accessionId, null);
@@ -138,7 +140,8 @@ public class DBRefEntry implements DBRefEntryI
     String otherAccession = other.getAccessionId();
     if ((accessionId == null && otherAccession != null)
             || (accessionId != null && otherAccession == null)
-            || (accessionId != null && !accessionId.equalsIgnoreCase(otherAccession)))
+            || (accessionId != null && !accessionId
+                    .equalsIgnoreCase(otherAccession)))
     {
       return false;
     }
@@ -148,7 +151,7 @@ public class DBRefEntry implements DBRefEntryI
      * otherwise the versions have to match
      */
     String otherVersion = other.getVersion();
-      
+
     if ((version == null || version.equals("0") || version.endsWith(":0"))
             && otherVersion != null)
     {
@@ -223,28 +226,24 @@ public class DBRefEntry implements DBRefEntryI
     return accessionId;
   }
 
-
   @Override
   public void setAccessionId(String accessionId)
   {
     this.accessionId = accessionId;
   }
 
-
   @Override
   public void setSource(String source)
   {
     this.source = source;
   }
 
-
   @Override
   public void setVersion(String version)
   {
     this.version = version;
   }
 
-
   @Override
   public Mapping getMap()
   {
@@ -280,4 +279,53 @@ public class DBRefEntry implements DBRefEntryI
   {
     return getSrcAccString();
   }
+
+  @Override
+  public boolean isPrimary()
+  {
+    /*
+     * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.  
+     */
+    if (map != null)
+    {
+      if (map.getTo() != null)
+      {
+        return false;
+      }
+      if (map.getMap().getFromRatio() != map.getMap().getToRatio()
+              || map.getMap().getFromRatio() != 1)
+      {
+        return false;
+      }
+      // check map is really 1:1, no shifts allowed.
+      if (map.getMap().getFromHighest() != map.getMap().getToHighest()
+              && map.getMap().getFromLowest() != map.getMap().getToLowest()
+              && !Arrays.equals(
+                      map.getMap().getFromRanges().toArray(new int[0][]),
+                      map.getMap().getToRanges().toArray(new int[0][])))
+      {
+        return false;
+      }
+    }
+    if (version == null)
+    {
+      // no version string implies the reference has not been verified at all.
+      return false;
+    }
+    // tricky - this test really needs to search the sequence's set of dbrefs to
+    // see if there is a primary reference that derived this reference.
+    String ucv = version.toUpperCase();
+    for (String primsrc : Arrays.asList(DBRefSource.allSources()))
+    {
+      if (ucv.startsWith(primsrc.toUpperCase()))
+      {
+        // by convention, many secondary references inherit the primary
+        // reference's
+        // source string as a prefix for any version information from the
+        // secondary reference.
+        return false;
+      }
+    }
+    return true;
+  }
 }