JAL-2034 moved ‘isDefinedGroup’ flag to a test method on AlignViewportI so controller...
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index c16fdce..457f2c5 100644 (file)
@@ -844,14 +844,22 @@ public abstract class AlignmentViewport implements AlignViewportI,
             && !al.getCodonFrames().isEmpty())
     {
       /*
-       * fudge - check first mapping is protein-to-nucleotide
+       * fudge - check first for protein-to-nucleotide mappings
        * (we don't want to do this for protein-to-protein)
        */
-      AlignedCodonFrame mapping = al.getCodonFrames().iterator().next();
-      // TODO hold mapping type e.g. dna-to-protein in AlignedCodonFrame?
-      MapList[] mapLists = mapping.getdnaToProt();
-      // mapLists can be empty if project load has not finished resolving seqs
-      if (mapLists.length > 0 && mapLists[0].getFromRatio() == 3)
+      boolean doConsensus = false;
+      for (AlignedCodonFrame mapping : al.getCodonFrames())
+      {
+        // TODO hold mapping type e.g. dna-to-protein in AlignedCodonFrame?
+        MapList[] mapLists = mapping.getdnaToProt();
+        // mapLists can be empty if project load has not finished resolving seqs
+        if (mapLists.length > 0 && mapLists[0].getFromRatio() == 3)
+        {
+          doConsensus = true;
+          break;
+        }
+      }
+      if (doConsensus)
       {
         if (calculator
                 .getRegisteredWorkersOfClass(ComplementConsensusThread.class) == null)
@@ -1413,6 +1421,39 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   /**
+   * Hides the specified sequence, or the sequences it represents
+   * 
+   * @param sequence
+   *          the sequence to hide, or keep as representative
+   * @param representGroup
+   *          if true, hide the current selection group except for the
+   *          representative sequence
+   */
+  public void hideSequences(SequenceI sequence, boolean representGroup)
+  {
+    if (selectionGroup == null || selectionGroup.getSize() < 1)
+    {
+      hideSequence(new SequenceI[] { sequence });
+      return;
+    }
+
+    if (representGroup)
+    {
+      hideRepSequences(sequence, selectionGroup);
+      setSelectionGroup(null);
+      return;
+    }
+
+    int gsize = selectionGroup.getSize();
+    SequenceI[] hseqs = selectionGroup.getSequences().toArray(
+            new SequenceI[gsize]);
+
+    hideSequence(hseqs);
+    setSelectionGroup(null);
+    sendSelection();
+  }
+
+  /**
    * Set visibility for any annotations for the given sequence.
    * 
    * @param sequenceI
@@ -1420,11 +1461,15 @@ public abstract class AlignmentViewport implements AlignViewportI,
   protected void setSequenceAnnotationsVisible(SequenceI sequenceI,
           boolean visible)
   {
-    for (AlignmentAnnotation ann : alignment.getAlignmentAnnotation())
+    AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
+    if (anns != null)
     {
-      if (ann.sequenceRef == sequenceI)
+      for (AlignmentAnnotation ann : anns)
       {
-        ann.visible = visible;
+        if (ann.sequenceRef == sequenceI)
+        {
+          ann.visible = visible;
+        }
       }
     }
   }
@@ -1588,6 +1633,13 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public String[] getViewAsString(boolean selectedRegionOnly)
   {
+    return getViewAsString(selectedRegionOnly, true);
+  }
+
+  @Override
+  public String[] getViewAsString(boolean selectedRegionOnly,
+          boolean exportHiddenSeqs)
+  {
     String[] selection = null;
     SequenceI[] seqs = null;
     int i, iSize;
@@ -1601,13 +1653,13 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
     else
     {
-      if (hasHiddenRows())
+      if (hasHiddenRows() && exportHiddenSeqs)
       {
-        iSize = alignment.getHiddenSequences().getFullAlignment()
-                .getHeight();
-        seqs = alignment.getHiddenSequences().getFullAlignment()
-                .getSequencesArray();
-        end = alignment.getHiddenSequences().getFullAlignment().getWidth();
+        AlignmentI fullAlignment = alignment.getHiddenSequences()
+                .getFullAlignment();
+        iSize = fullAlignment.getHeight();
+        seqs = fullAlignment.getSequencesArray();
+        end = fullAlignment.getWidth();
       }
       else
       {
@@ -1828,12 +1880,20 @@ public abstract class AlignmentViewport implements AlignViewportI,
               .getCodonFrames();
       if (codonMappings != null && !codonMappings.isEmpty())
       {
-        // fudge: check mappings are not protein-to-protein
-        // TODO: nicer
-        AlignedCodonFrame mapping = codonMappings.iterator().next();
-        MapList[] mapLists = mapping.getdnaToProt();
-        // mapLists can be empty if project load has not finished resolving seqs
-        if (mapLists.length > 0 && mapLists[0].getFromRatio() == 3)
+        boolean doConsensus = false;
+        for (AlignedCodonFrame mapping : codonMappings)
+        {
+          // TODO hold mapping type e.g. dna-to-protein in AlignedCodonFrame?
+          MapList[] mapLists = mapping.getdnaToProt();
+          // mapLists can be empty if project load has not finished resolving
+          // seqs
+          if (mapLists.length > 0 && mapLists[0].getFromRatio() == 3)
+          {
+            doConsensus = true;
+            break;
+          }
+        }
+        if (doConsensus)
         {
           complementConsensus = new AlignmentAnnotation("cDNA Consensus",
                   "PID for cDNA", new Annotation[1], 0f, 100f,
@@ -2656,6 +2716,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
      * all gapped visible regions
      */
     int lastSeq = alignment.getHeight() - 1;
+    List<AlignedCodonFrame> seqMappings = null;
     for (int seqNo = getStartSeq(); seqNo < lastSeq; seqNo++, seqOffset++)
     {
       sequence = getAlignment().getSequenceAt(seqNo);
@@ -2667,15 +2728,16 @@ public abstract class AlignmentViewport implements AlignViewportI,
       {
         continue;
       }
-      List<AlignedCodonFrame> seqMappings = MappingUtils
-              .findMappingsForSequence(sequence, mappings);
+      seqMappings = MappingUtils
+              .findMappingsForSequenceAndOthers(sequence, mappings,
+                      getCodingComplement().getAlignment());
       if (!seqMappings.isEmpty())
       {
         break;
       }
     }
 
-    if (sequence == null)
+    if (sequence == null || seqMappings == null || seqMappings.isEmpty())
     {
       /*
        * No ungapped mapped sequence in middle column - do nothing
@@ -2683,7 +2745,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
       return 0;
     }
     MappingUtils.addSearchResults(sr, sequence,
-            sequence.findPosition(middleColumn), mappings);
+            sequence.findPosition(middleColumn), seqMappings);
     return seqOffset;
   }
 
@@ -2719,4 +2781,32 @@ public abstract class AlignmentViewport implements AlignViewportI,
       }
     }
   }
+
+  /**
+   * hold status of current selection group - defined on alignment or not.
+   */
+  private boolean selectionIsDefinedGroup = false;
+
+  @Override
+  public boolean isSelectionDefinedGroup()
+  {
+    if (selectionGroup == null)
+    {
+      return false;
+    }
+    if (isSelectionGroupChanged(true))
+    {
+      selectionIsDefinedGroup = false;
+      List<SequenceGroup> gps = alignment.getGroups();
+      if (gps == null || gps.size() == 0)
+      {
+        selectionIsDefinedGroup = false;
+      }
+      else
+      {
+        selectionIsDefinedGroup = gps.contains(selectionGroup);
+      }
+    }
+    return selectionIsDefinedGroup;
+  }
 }