JAL-2381 copy and paste sequence also copies contact matrix annotation
[jalview.git] / src / jalview / datamodel / Sequence.java
index ca2b6d4..32d295a 100755 (executable)
  */
 package jalview.datamodel;
 
-import jalview.analysis.AlignSeq;
-import jalview.datamodel.features.SequenceFeatures;
-import jalview.datamodel.features.SequenceFeaturesI;
-import jalview.util.Comparison;
-import jalview.util.DBRefUtils;
-import jalview.util.MapList;
-import jalview.util.StringUtils;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.Iterator;
@@ -39,6 +32,16 @@ import java.util.ListIterator;
 import java.util.Vector;
 
 import fr.orsay.lri.varna.models.rna.RNA;
+import jalview.analysis.AlignSeq;
+import jalview.analysis.AlignmentUtils;
+import jalview.analysis.SeqsetUtils;
+import jalview.datamodel.features.SequenceFeatures;
+import jalview.datamodel.features.SequenceFeaturesI;
+import jalview.util.Comparison;
+import jalview.util.DBRefUtils;
+import jalview.util.MapList;
+import jalview.util.StringUtils;
+import jalview.ws.datamodel.alphafold.MappableContactMatrix;
 
 /**
  * 
@@ -349,6 +352,11 @@ public class Sequence extends ASequence implements SequenceI
         {
           // only copy the given annotation
           AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
+          ContactMatrixI cm = seq.getContactMatrixFor(sqann[i]);
+          if (cm!=null)
+          {
+            addContactListFor(newann, cm);
+          }
           addAlignmentAnnotation(newann);
         }
       }
@@ -472,18 +480,19 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   /**
-   * DOCUMENT ME!
+   * Answers the sequence name, with '/start-end' appended if jvsuffix is true
    * 
-   * @return DOCUMENT ME!
+   * @return
    */
   @Override
   public String getDisplayId(boolean jvsuffix)
   {
-    StringBuffer result = new StringBuffer(name);
-    if (jvsuffix)
+    if (!jvsuffix)
     {
-      result.append("/" + start + "-" + end);
+      return name;
     }
+    StringBuilder result = new StringBuilder(name);
+    result.append("/").append(start).append("-").append(end);
 
     return result.toString();
   }
@@ -522,6 +531,7 @@ public class Sequence extends ASequence implements SequenceI
   public void setStart(int start)
   {
     this.start = start;
+    sequenceChanged();
   }
 
   /**
@@ -696,8 +706,8 @@ public class Sequence extends ASequence implements SequenceI
   public void setGeneLoci(String speciesId, String assemblyId,
           String chromosomeId, MapList map)
   {
-    addDBRef(new DBRefEntry(speciesId, assemblyId,
-            DBRefEntry.CHROMOSOME + ":" + chromosomeId, new Mapping(map)));
+    addDBRef(new GeneLocus(speciesId, assemblyId, chromosomeId,
+            new Mapping(map)));
   }
 
   /**
@@ -713,41 +723,9 @@ public class Sequence extends ASequence implements SequenceI
     {
       for (final DBRefEntry ref : refs)
       {
-        if (ref.isChromosome())
+        if (ref instanceof GeneLociI)
         {
-          return new GeneLociI()
-          {
-            @Override
-            public String getSpeciesId()
-            {
-              return ref.getSource();
-            }
-
-            @Override
-            public String getAssemblyId()
-            {
-              // DEV NOTE: DBRefEntry is reused here to hold chromosomal locus
-              // of a gene sequence.
-              // source=species, version=assemblyId, accession=chromosome, map =
-              // positions.
-
-              return ref.getVersion();
-            }
-
-            @Override
-            public String getChromosomeId()
-            {
-              // strip off "chromosome:" prefix to chrId
-              return ref.getAccessionId()
-                      .substring(DBRefEntry.CHROMOSOME.length() + 1);
-            }
-
-            @Override
-            public MapList getMap()
-            {
-              return ref.getMap().getMap();
-            }
-          };
+          return (GeneLociI) ref;
         }
       }
     }
@@ -1445,6 +1423,7 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public void addDBRef(DBRefEntry entry)
   {
+    // TODO JAL-3980 maintain as sorted list
     if (datasetSequence != null)
     {
       datasetSequence.addDBRef(entry);
@@ -1455,6 +1434,7 @@ public class Sequence extends ASequence implements SequenceI
     {
       dbrefs = new DBModList<>();
     }
+    // TODO JAL-3979 LOOK UP RATHER THAN SWEEP FOR EFFICIENCY
 
     for (int ib = 0, nb = dbrefs.size(); ib < nb; ib++)
     {
@@ -1612,7 +1592,7 @@ public class Sequence extends ASequence implements SequenceI
       _isNa = Comparison.isNucleotide(this);
     }
     return !_isNa;
-  };
+  }
 
   /*
    * (non-Javadoc)
@@ -1653,8 +1633,20 @@ public class Sequence extends ASequence implements SequenceI
           _aa.adjustForAlignment(); // uses annotation's own record of
                                     // sequence-column mapping
           datasetSequence.addAlignmentAnnotation(_aa);
+
+          if (_cmholder != null)
+          { // transfer contact matrices
+            ContactMatrixI cm = _cmholder.getContactMatrixFor(aa);
+            if (cm != null)
+            {
+              datasetSequence.addContactListFor(_aa, cm);
+              datasetSequence.addContactListFor(aa, cm);
+            }
+          }
         }
       }
+      // all matrices should have been transferred. so we clear the local holder
+      _cmholder=null;
     }
     return datasetSequence;
   }
@@ -1829,13 +1821,30 @@ public class Sequence extends ASequence implements SequenceI
   public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
           String label)
   {
+    return getAlignmentAnnotations(calcId, label, null, true);
+  }
+
+  @Override
+  public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
+          String label, String description)
+  {
+    return getAlignmentAnnotations(calcId, label, description, false);
+  }
+
+  private List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
+          String label, String description, boolean ignoreDescription)
+  {
     List<AlignmentAnnotation> result = new ArrayList<>();
     if (this.annotation != null)
     {
       for (AlignmentAnnotation ann : annotation)
       {
-        if (ann.calcId != null && ann.calcId.equals(calcId)
-                && ann.label != null && ann.label.equals(label))
+        if ((ann.calcId != null && ann.calcId.equals(calcId))
+                && (ann.label != null && ann.label.equals(label))
+                && ((ignoreDescription && description == null)
+                        || (ann.description != null
+                                && ann.description.equals(description))))
+
         {
           result.add(ann);
         }
@@ -1967,15 +1976,6 @@ public class Sequence extends ASequence implements SequenceI
 
     List<SequenceFeature> result = getFeatures().findFeatures(startPos,
             endPos, types);
-    if (datasetSequence != null)
-    {
-      result = datasetSequence.getFeatures().findFeatures(startPos, endPos,
-              types);
-    }
-    else
-    {
-      result = sequenceFeatureStore.findFeatures(startPos, endPos, types);
-    }
 
     /*
      * if end column is gapped, endPos may be to the right, 
@@ -2127,4 +2127,83 @@ public class Sequence extends ASequence implements SequenceI
     // otherwise, sequence was completely hidden
     return 0;
   }
+
+  ////
+  //// Contact Matrix Holder Boilerplate
+  ////
+  ContactMapHolderI _cmholder = null;
+
+  private ContactMapHolderI getContactMapHolder()
+  {
+    if (datasetSequence!=null) {
+      return ((Sequence)datasetSequence).getContactMapHolder();
+    }
+    if (_cmholder==null)
+    {
+      _cmholder=new ContactMapHolder();
+    }
+    return _cmholder;
+  }
+  @Override
+  public Collection<ContactMatrixI> getContactMaps()
+  {
+    return getContactMapHolder().getContactMaps();
+  }
+
+  @Override
+  public ContactMatrixI getContactMatrixFor(AlignmentAnnotation ann)
+  {
+    return getContactMapHolder().getContactMatrixFor(ann);
+  }
+
+  @Override
+  public ContactListI getContactListFor(AlignmentAnnotation _aa, int column)
+  {
+    return getContactMapHolder().getContactListFor(_aa, column);
+  }
+
+  @Override
+  public AlignmentAnnotation addContactList(ContactMatrixI cm)
+  {
+    AlignmentAnnotation aa;
+    
+    if (datasetSequence != null)
+    {
+      aa = datasetSequence.addContactList(cm);
+      // clone the annotation for the local sequence
+      aa = new AlignmentAnnotation(aa);
+      aa.restrict(start, end);
+      aa.adjustForAlignment();
+      getContactMapHolder().addContactListFor(aa,cm);
+      addAlignmentAnnotation(aa);
+      return aa;
+    }
+    
+    // construct new annotation for matrix on dataset sequence
+    aa = getContactMapHolder().addContactList(cm);
+
+    Annotation _aa[] = new Annotation[getLength()];
+
+    for (int i = 0; i < _aa.length; _aa[i++] = new Annotation(0.0f))
+    {
+      ;
+    }
+    aa.annotations = _aa;
+    aa.setSequenceRef(this);
+    if (cm instanceof MappableContactMatrix
+            && !((MappableContactMatrix) cm).hasReferenceSeq())
+    {
+      ((MappableContactMatrix) cm).setRefSeq(this);
+    }
+    aa.createSequenceMapping(this, getStart(), false);
+    addAlignmentAnnotation(aa);
+    return aa;
+  }
+
+  @Override
+  public void addContactListFor(AlignmentAnnotation annotation,
+          ContactMatrixI cm)
+  {
+    getContactMapHolder().addContactListFor(annotation, cm);
+  }
 }