JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / datamodel / Sequence.java
index 4f626a4..c8b94ce 100755 (executable)
@@ -22,6 +22,7 @@ package jalview.datamodel;
 
 import jalview.analysis.AlignSeq;
 import jalview.api.DBRefEntryI;
+import jalview.util.Comparison;
 import jalview.util.DBRefUtils;
 import jalview.util.MapList;
 import jalview.util.StringUtils;
@@ -232,8 +233,7 @@ public class Sequence extends ASequence implements SequenceI
     {
       char[] oseq = seq.getSequence();
       initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length),
-              seq.getStart(),
-            seq.getEnd());
+              seq.getStart(), seq.getEnd());
     }
     description = seq.getDescription();
     if (seq != datasetSequence)
@@ -292,7 +292,6 @@ public class Sequence extends ASequence implements SequenceI
     }
   }
 
-
   @Override
   public void setSequenceFeatures(SequenceFeature[] features)
   {
@@ -317,7 +316,7 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public synchronized void addSequenceFeature(SequenceFeature sf)
   {
-    if (sequenceFeatures==null && datasetSequence != null)
+    if (sequenceFeatures == null && datasetSequence != null)
     {
       datasetSequence.addSequenceFeature(sf);
       return;
@@ -347,8 +346,9 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (sequenceFeatures == null)
     {
-      if (datasetSequence!=null) {
-         datasetSequence.deleteFeature(sf);
+      if (datasetSequence != null)
+      {
+        datasetSequence.deleteFeature(sf);
       }
       return;
     }
@@ -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;
   }
 
   /**
@@ -992,7 +944,17 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public void setDBRefs(DBRefEntry[] dbref)
   {
+    if (dbrefs == null && datasetSequence != null
+            && this != datasetSequence)
+    {
+      datasetSequence.setDBRefs(dbref);
+      return;
+    }
     dbrefs = dbref;
+    if (dbrefs != null)
+    {
+      DBRefUtils.ensurePrimaries(this);
+    }
   }
 
   @Override
@@ -1009,7 +971,12 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public void addDBRef(DBRefEntry entry)
   {
-    // TODO add to dataset sequence instead if there is one?
+    if (datasetSequence != null)
+    {
+      datasetSequence.addDBRef(entry);
+      return;
+    }
+
     if (dbrefs == null)
     {
       dbrefs = new DBRefEntry[0];
@@ -1037,12 +1004,23 @@ public class Sequence extends ASequence implements SequenceI
     temp[temp.length - 1] = entry;
 
     dbrefs = temp;
+
+    DBRefUtils.ensurePrimaries(this);
   }
 
   @Override
   public void setDatasetSequence(SequenceI seq)
   {
-    // TODO check for circular reference before setting?
+    if (seq == this)
+    {
+      throw new IllegalArgumentException(
+              "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
+    }
+    if (seq != null && seq.getDatasetSequence() != null)
+    {
+      throw new IllegalArgumentException(
+              "Implementation error: cascading dataset sequences are not allowed.");
+    }
     datasetSequence = seq;
   }
 
@@ -1115,7 +1093,7 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public SequenceI deriveSequence()
   {
-    Sequence seq=null;
+    Sequence seq = null;
     if (datasetSequence == null)
     {
       if (isValidDatasetSequence())
@@ -1129,7 +1107,7 @@ public class Sequence extends ASequence implements SequenceI
       else
       {
         // Create a new, valid dataset sequence
-       createDatasetSequence();
+        createDatasetSequence();
       }
     }
     return new Sequence(this);
@@ -1139,6 +1117,10 @@ public class Sequence extends ASequence implements SequenceI
 
   private long _seqhash = 0;
 
+  /**
+   * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
+   * true
+   */
   @Override
   public boolean isProtein()
   {
@@ -1149,7 +1131,7 @@ public class Sequence extends ASequence implements SequenceI
     if (_seqhash != sequence.hashCode())
     {
       _seqhash = sequence.hashCode();
-      _isNa=jalview.util.Comparison.isNucleotide(new SequenceI[] { this });
+      _isNa = Comparison.isNucleotide(this);
     }
     return !_isNa;
   };
@@ -1173,9 +1155,9 @@ public class Sequence extends ASequence implements SequenceI
       dsseq.setDescription(description);
       // move features and database references onto dataset sequence
       dsseq.sequenceFeatures = sequenceFeatures;
-      sequenceFeatures=null;
+      sequenceFeatures = null;
       dsseq.dbrefs = dbrefs;
-      dbrefs=null;
+      dbrefs = null;
       // TODO: search and replace any references to this sequence with
       // references to the dataset sequence in Mappings on dbref
       dsseq.pdbIds = pdbIds;
@@ -1269,46 +1251,22 @@ public class Sequence extends ASequence implements SequenceI
     {
       return false;
     }
-    Vector newpdb = new Vector();
-    for (int i = 0; i < dbrefs.length; i++)
-    {
-      if (DBRefSource.PDB.equals(dbrefs[i].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)
+    boolean added = false;
+    for (DBRefEntry dbr : dbrefs)
     {
-      Enumeration en = newpdb.elements();
-      while (en.hasMoreElements())
+      if (DBRefSource.PDB.equals(dbr.getSource()))
       {
-        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
@@ -1457,15 +1415,14 @@ public class Sequence extends ASequence implements SequenceI
     return null;
   }
 
-
   @Override
   public List<DBRefEntry> getPrimaryDBRefs()
   {
-    if (datasetSequence!=null)
+    if (datasetSequence != null)
     {
       return datasetSequence.getPrimaryDBRefs();
     }
-    if (dbrefs==null || dbrefs.length==0)
+    if (dbrefs == null || dbrefs.length == 0)
     {
       return Collections.emptyList();
     }