JAL-1803 first pass at updatePDBEntry
[jalview.git] / src / jalview / datamodel / Sequence.java
index 5886ae0..4f626a4 100755 (executable)
@@ -418,22 +418,66 @@ public class Sequence extends ASequence implements SequenceI
     {
       pdbIds = new Vector<PDBEntry>();
     }
-    if (pdbIds.contains(entry))
-    {
-      updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
-    }
-    else
+    if (!updatedPDBEntry(pdbIds, entry))
     {
       pdbIds.addElement(entry);
     }
   }
 
-  private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
+  private static boolean updatedPDBEntry(List<PDBEntry> entries,
+          PDBEntry newEntry)
   {
-    if (newEntry.getFile() != null)
+    for (PDBEntry xtant : entries)
     {
-      oldEntry.setFile(newEntry.getFile());
+      if (xtant.getFile() != null && newEntry.getFile() != null
+              && !xtant.getFile().equals(newEntry.getFile()))
+      {
+        // different structure data, so leave alone.
+        continue;
+      }
+      // loop through to check whether we can find a matching ID
+
+      // either exact
+      if (!xtant.getId().equals(newEntry.getId()))
+      {
+        /* TODO: support stemming to group PDB IDs.
+        // or stemming, with exactly one alphanumeric character difference
+        if (xtant.getId().length() < newEntry.getId().length())
+        {
+          if (!newEntry.getId().startsWith(xtant.getId()))
+          {
+            continue;
+          }
+          // newEntry may be chain specific PDBEntry
+          // TODO: copy/update details from newEntry to xtant
+        }
+        else
+        {
+          if (!xtant.getId().startsWith(newEntry.getId()))
+          {
+            continue;
+          }
+          // xtant may be chain specific PDBEntry
+          // TODO: copy/update missing details from newEntry
+        }*/
+        continue;
+      }
+      if (xtant.getChainCode() != null && xtant.getChainCode().length() > 0
+              && newEntry.getChainCode() != null
+              && !newEntry.getChainCode().equals(xtant.getChainCode()))
+      {
+        // don't overwrite - multiple chain mappings for a sequence yield
+        // multiple PDBEntries
+        // each with different chaincode
+        continue;
+      }
+
+      xtant.updateFrom(newEntry);
+
+      return true;
     }
+    // if we got to the end of the loop, nothing was updated.
+    return false;
   }
 
   /**