JAL-2629 uniquify all hmmer command sequence outputs
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index abca110..f12e55e 100644 (file)
@@ -98,47 +98,22 @@ public abstract class AlignmentViewport
 
   protected Deque<CommandI> redoList = new ArrayDeque<>();
 
-  protected String sequenceSetID;
-
+  /**
+   * alignment displayed in the viewport. Please use get/setter
+   */
+  protected AlignmentI alignment;
+  
   /*
    * probably unused indicator that view is of a dataset rather than an
    * alignment
    */
-  protected boolean isDataset = false;
-
-  private Map<SequenceI, SequenceCollectionI> hiddenRepSequences;
-
-  protected ColumnSelection colSel = new ColumnSelection();
-
-  public boolean autoCalculateConsensus = true;
-
-  protected boolean autoCalculateStrucConsensus = true;
-
-  protected boolean ignoreGapsInConsensusCalculation = false;
 
   protected boolean ignoreBelowBackGroundFrequencyCalculation = false;
 
   protected boolean infoLetterHeight = false;
 
-  protected ResidueShaderI residueShading = new ResidueShader();
-
-  protected AlignmentAnnotation consensus;
-
-  protected AlignmentAnnotation complementConsensus;
-
   protected AlignmentAnnotation occupancy;
-
-  protected AlignmentAnnotation strucConsensus;
-
-  protected AlignmentAnnotation conservation;
-
-  protected AlignmentAnnotation quality;
-
-  /**
-   * alignment displayed in the viewport
-   */
-  private AlignmentI alignment;
-
+  
   /**
    * results of alignment consensus analysis for visible portion of view
    */
@@ -149,19 +124,6 @@ public abstract class AlignmentViewport
    */
   protected ProfilesI hmmProfiles;
 
-  /**
-   * results of cDNA complement consensus visible portion of view
-   */
-  protected Hashtable[] hcomplementConsensus;
-
-  /**
-   * results of secondary structure base pair consensus for visible portion of
-   * view
-   */
-  protected Hashtable[] hStrucConsensus;
-
-  protected Conservation hconservation;
-
   public AlignmentViewport(AlignmentI al)
   {
     setAlignment(al);
@@ -641,6 +603,14 @@ public abstract class AlignmentViewport
     return alignment.getGapCharacter();
   }
 
+  protected String sequenceSetID;
+
+  /**
+   * probably unused indicator that view is of a dataset rather than an
+   * alignment
+   */
+  protected boolean isDataset = false;
+  
   public void setDataset(boolean b)
   {
     isDataset = b;
@@ -651,6 +621,18 @@ public abstract class AlignmentViewport
     return isDataset;
   }
 
+  private Map<SequenceI, SequenceCollectionI> hiddenRepSequences;
+
+  protected ColumnSelection colSel = new ColumnSelection();
+
+  public boolean autoCalculateConsensus = true;
+
+  protected boolean autoCalculateStrucConsensus = true;
+
+  protected boolean ignoreGapsInConsensusCalculation = false;
+
+  protected ResidueShaderI residueShading = new ResidueShader();
+  
   @Override
   public void setGlobalColourScheme(ColourSchemeI cs)
   {
@@ -702,7 +684,8 @@ public abstract class AlignmentViewport
          * retain any colour thresholds per group while
          * changing choice of colour scheme (JAL-2386)
          */
-        sg.setColourScheme(cs);
+        sg.setColourScheme(
+                cs == null ? null : cs.getInstance(this, sg));
         if (cs != null)
         {
           sg.getGroupColourScheme().alignmentChanged(sg,
@@ -723,6 +706,41 @@ public abstract class AlignmentViewport
   {
     return residueShading;
   }
+  
+  protected AlignmentAnnotation consensus;
+
+  protected AlignmentAnnotation complementConsensus;
+
+  protected AlignmentAnnotation gapcounts;
+
+  protected AlignmentAnnotation strucConsensus;
+
+  protected AlignmentAnnotation conservation;
+
+  protected AlignmentAnnotation quality;
+
+  protected AlignmentAnnotation[] groupConsensus;
+
+  protected AlignmentAnnotation[] groupConservation;
+
+  /**
+   * results of alignment consensus analysis for visible portion of view
+   */
+  protected ProfilesI hconsensus = null;
+
+  /**
+   * results of cDNA complement consensus visible portion of view
+   */
+  protected Hashtable[] hcomplementConsensus = null;
+
+  /**
+   * results of secondary structure base pair consensus for visible portion of
+   * view
+   */
+  protected Hashtable[] hStrucConsensus = null;
+
+  protected Conservation hconservation = null;
+  
   @Override
   public void setConservation(Conservation cons)
   {
@@ -742,9 +760,9 @@ public abstract class AlignmentViewport
   }
 
   @Override
-  public void setConsensusProfiles(ProfilesI hconsensus)
+  public void setSequenceConsensusHash(ProfilesI hconsensus)
   {
-    this.consensusProfiles = hconsensus;
+    this.hconsensus = hconsensus;
   }
 
   @Override
@@ -754,9 +772,9 @@ public abstract class AlignmentViewport
   }
 
   @Override
-  public ProfilesI getConsensusProfiles()
+  public ProfilesI getSequenceConsensusHash()
   {
-    return consensusProfiles;
+    return hconsensus;
   }
 
   @Override
@@ -809,9 +827,9 @@ public abstract class AlignmentViewport
   }
 
   @Override
-  public AlignmentAnnotation getOccupancyAnnotation()
+  public AlignmentAnnotation getAlignmentGapAnnotation()
   {
-    return occupancy;
+    return gapcounts;
   }
 
   @Override
@@ -974,9 +992,12 @@ public abstract class AlignmentViewport
     conservation = null;
     quality = null;
     consensusProfiles = null;
+    groupConsensus = null;
+    groupConservation = null;
+    hconsensus = null;
     hconservation = null;
     hcomplementConsensus = null;
-    occupancy = null;
+    gapcounts = null;
     calculator = null;
     residueShading = null; // may hold a reference to Consensus
     changeSupport = null;
@@ -1310,6 +1331,7 @@ public abstract class AlignmentViewport
     ignoreGapsInConsensusCalculation = b;
     if (ap != null)
     {
+      updateConsensus(ap);
       if (residueShading != null)
       {
         residueShading.setThreshold(residueShading.getThreshold(),
@@ -1736,6 +1758,7 @@ public abstract class AlignmentViewport
   public void invertColumnSelection()
   {
     colSel.invertColumnSelection(0, alignment.getWidth(), alignment);
+    isColSelChanged(true);
   }
 
   @Override
@@ -1759,8 +1782,7 @@ public abstract class AlignmentViewport
     }
     else
     {
-      sequences = selectionGroup.getSelectionAsNewSequences(alignment,
-              true);
+      sequences = selectionGroup.getSelectionAsNewSequences(alignment);
     }
 
     return sequences;
@@ -1954,6 +1976,18 @@ public abstract class AlignmentViewport
     {
       alignment.padGaps();
     }
+    if (autoCalculateConsensus)
+    {
+      updateConsensus(ap);
+    }
+    if (hconsensus != null && autoCalculateConsensus)
+    {
+      updateConservation(ap);
+    }
+    if (autoCalculateStrucConsensus)
+    {
+      updateStrucConsensus(ap);
+    }
 
     // Reset endRes of groups if beyond alignment width
     int alWidth = alignment.getWidth();
@@ -1988,7 +2022,7 @@ public abstract class AlignmentViewport
     {
       rs.alignmentChanged(alignment, hiddenRepSequences);
 
-      rs.setConsensus(consensusProfiles);
+      rs.setConsensus(hconsensus);
       if (rs.conservationApplied())
       {
         rs.setConservation(Conservation.calculateConservation("All",
@@ -2013,7 +2047,7 @@ public abstract class AlignmentViewport
     // depending on if the user wants to see the annotation or not in a
     // specific alignment
 
-    if (consensusProfiles == null && !isDataset)
+    if (hconsensus == null && !isDataset)
     {
       if (!alignment.isNucleotide())
       {
@@ -2029,7 +2063,7 @@ public abstract class AlignmentViewport
               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
       initConsensus(consensus);
 
-      initOccupancy();
+      initGapCounts();
 
       initComplementConsensus();
     }
@@ -2088,20 +2122,20 @@ public abstract class AlignmentViewport
 
   // these should be extracted from the view model - style and settings for
   // derived annotation
-  private void initOccupancy()
+  private void initGapCounts()
   {
     if (showOccupancy)
     {
-      occupancy = new AlignmentAnnotation("Occupancy",
+      gapcounts = new AlignmentAnnotation("Occupancy",
               MessageManager.getString("label.occupancy_descr"),
               new Annotation[1], 0f, alignment.getHeight(),
               AlignmentAnnotation.BAR_GRAPH);
-      occupancy.hasText = true;
-      occupancy.autoCalculated = true;
-      occupancy.scaleColLabel = true;
-      occupancy.graph = AlignmentAnnotation.BAR_GRAPH;
+      gapcounts.hasText = true;
+      gapcounts.autoCalculated = true;
+      gapcounts.scaleColLabel = true;
+      gapcounts.graph = AlignmentAnnotation.BAR_GRAPH;
 
-      alignment.addAnnotation(occupancy);
+      alignment.addAnnotation(gapcounts);
     }
   }
 
@@ -3084,4 +3118,110 @@ public abstract class AlignmentViewport
   {
     hmmNormaliseSequenceLogo = state;
   }
+
+  /**
+   * flag set to indicate if structure views might be out of sync with sequences
+   * in the alignment
+   */
+
+  private boolean needToUpdateStructureViews = false;
+
+  @Override
+  public boolean isUpdateStructures()
+  {
+    return needToUpdateStructureViews;
+  }
+
+  @Override
+  public void setUpdateStructures(boolean update)
+  {
+    needToUpdateStructureViews = update;
+  }
+
+  @Override
+  public boolean needToUpdateStructureViews()
+  {
+    boolean update = needToUpdateStructureViews;
+    needToUpdateStructureViews = false;
+    return update;
+  }
+
+  @Override
+  public void addSequenceGroup(SequenceGroup sequenceGroup)
+  {
+    alignment.addGroup(sequenceGroup);
+
+    Color col = sequenceGroup.idColour;
+    if (col != null)
+    {
+      col = col.brighter();
+
+      for (SequenceI sq : sequenceGroup.getSequences())
+      {
+        setSequenceColour(sq, col);
+      }
+    }
+
+    if (codingComplement != null)
+    {
+      SequenceGroup mappedGroup = MappingUtils
+              .mapSequenceGroup(sequenceGroup, this, codingComplement);
+      if (mappedGroup.getSequences().size() > 0)
+      {
+        codingComplement.getAlignment().addGroup(mappedGroup);
+
+        if (col != null)
+        {
+          for (SequenceI seq : mappedGroup.getSequences())
+          {
+            codingComplement.setSequenceColour(seq, col);
+          }
+        }
+      }
+      // propagate the structure view update flag according to our own setting
+      codingComplement.setUpdateStructures(needToUpdateStructureViews);
+    }
+  }
+
+  /**
+   * Filters out sequences with an eValue higher than the specified value. The
+   * filtered sequences are hidden or deleted. Sequences with no eValues are also
+   * filtered out.
+   * 
+   * @param eValue
+   * @param delete
+   */
+  public void filterByEvalue(double eValue)
+  {
+    for (SequenceI seq : alignment.getSequencesArray())
+    {
+      if ((seq.getAnnotation("Search Scores") == null
+              || seq.getAnnotation("Search Scores")[0].getEValue() > eValue)
+              && seq.getHMM() == null)
+      {
+        hideSequence(new SequenceI[] { seq });
+      }
+    }
+  }
+
+  /**
+   * Filters out sequences with an score lower than the specified value. The
+   * filtered sequences are hidden or deleted.
+   * 
+   * @param score
+   * @param delete
+   */
+  public void filterByScore(double score)
+  {
+    for (SequenceI seq : alignment.getSequencesArray())
+    {
+      if ((seq.getAnnotation("Search Scores") == null
+              || seq.getAnnotation("Search Scores")[0]
+                      .getBitScore() < score)
+              && seq.getHMM() == null)
+      {
+        hideSequence(new SequenceI[] { seq });
+      }
+    }
+  }
 }