JAL-2157 JAL-1803 refactored Sequence.updatePDBIds, parse DBRefs with
[jalview.git] / src / jalview / datamodel / Sequence.java
index 4f626a4..44522a8 100755 (executable)
@@ -412,72 +412,24 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   @Override
-  public void addPDBId(PDBEntry entry)
+  public boolean addPDBId(PDBEntry entry)
   {
     if (pdbIds == null)
     {
       pdbIds = new Vector<PDBEntry>();
+      pdbIds.add(entry);
+      return true;
     }
-    if (!updatedPDBEntry(pdbIds, entry))
-    {
-      pdbIds.addElement(entry);
-    }
-  }
 
-  private static boolean updatedPDBEntry(List<PDBEntry> entries,
-          PDBEntry newEntry)
-  {
-    for (PDBEntry xtant : entries)
+    for (PDBEntry pdbe : pdbIds)
     {
-      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()))
+      if (pdbe.updateFrom(entry))
       {
-        // don't overwrite - multiple chain mappings for a sequence yield
-        // multiple PDBEntries
-        // each with different chaincode
-        continue;
+        return false;
       }
-
-      xtant.updateFrom(newEntry);
-
-      return true;
     }
-    // if we got to the end of the loop, nothing was updated.
-    return false;
+    pdbIds.addElement(entry);
+    return true;
   }
 
   /**
@@ -1269,46 +1221,22 @@ public class Sequence extends ASequence implements SequenceI
     {
       return false;
     }
-    Vector newpdb = new Vector();
-    for (int i = 0; i < dbrefs.length; i++)
+    boolean added = false;
+    for (DBRefEntry dbr : dbrefs)
     {
-      if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
+      if (DBRefSource.PDB.equals(dbr.getSource()))
       {
-        PDBEntry pdbe = new PDBEntry();
-        pdbe.setId(dbrefs[i].getAccessionId());
-        if (pdbIds == null || pdbIds.size() == 0)
-        {
-          newpdb.addElement(pdbe);
-        }
-        else
-        {
-          Enumeration en = pdbIds.elements();
-          boolean matched = false;
-          while (!matched && en.hasMoreElements())
-          {
-            PDBEntry anentry = (PDBEntry) en.nextElement();
-            if (anentry.getId().equals(pdbe.getId()))
-            {
-              matched = true;
-            }
-          }
-          if (!matched)
-          {
-            newpdb.addElement(pdbe);
-          }
-        }
-      }
-    }
-    if (newpdb.size() > 0)
-    {
-      Enumeration en = newpdb.elements();
-      while (en.hasMoreElements())
-      {
-        addPDBId((PDBEntry) en.nextElement());
+        /*
+         * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
+         * PDB id is not already present in a 'matching' PDBEntry
+         * Constructor parses out a chain code if appended to the accession id
+         * (a fudge used to 'store' the chain code in the DBRef)
+         */
+        PDBEntry pdbe = new PDBEntry(dbr);
+        added |= addPDBId(pdbe);
       }
-      return true;
     }
-    return false;
+    return added;
   }
 
   @Override