JAL-3285 add unmerged code to SequenceGroup
[jalview.git] / src / jalview / datamodel / SequenceGroup.java
index b7ec9f9..59cb4bb 100755 (executable)
@@ -48,69 +48,82 @@ public class SequenceGroup implements AnnotatedCollectionI
   // a subclass of ViewportProperties similarly to ViewportRanges.
   // Done here as a quick fix for JAL-2665
   public static final String SEQ_GROUP_CHANGED = "Sequence group changed";
+  
+  protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
+          this);
+
+  public void addPropertyChangeListener(PropertyChangeListener listener)
+  {
+    changeSupport.addPropertyChangeListener(listener);
+  }
 
-  private String groupName;
+  public void removePropertyChangeListener(PropertyChangeListener listener)
+  {
+    changeSupport.removePropertyChangeListener(listener);
+  }
+  // end of event notification functionality initialisation
 
-  private String description;
+  String groupName;
 
-  private AnnotatedCollectionI context;
+  String description;
+  
+  Conservation conserve;
 
-  private Conservation conservationData;
+  Conservation conservationData;
 
-  private ProfilesI consensusProfiles;
+  ProfilesI consensusProfiles;
 
-  private ProfilesI hmmProfiles;
+  ProfilesI hmmProfiles;
 
-  private boolean displayBoxes = true;
+  boolean displayBoxes = true;
 
-  private boolean displayText = true;
+  boolean displayText = true;
 
-  private boolean colourText = false;
+  boolean colourText = false;
 
   /*
    * true if the group is defined as a group on the alignment, false if it is
    * just a selection
    */
-  private boolean isDefined;
+  boolean isDefined = false;
 
   /*
    * after Olivier's non-conserved only character display
    */
-  private boolean showNonconserved;
+  boolean showNonconserved = false;
 
   /*
    * sequences in the group
    */
-  private List<SequenceI> sequences = new ArrayList<>();
+  private List<SequenceI> sequences;
 
   /*
    * representative sequence for this group (if any)
    */
-  private SequenceI seqrep;
+  private SequenceI seqrep = null;
 
-  /*
-   * HMM consensus sequence for group (if any)
-   */
-  private SequenceI hmmConsensus;
-
-  private int width = -1;
+  int width = -1;
 
   /*
    * colour scheme applied to group if any
    */
   public ResidueShaderI cs;
 
-  // start column (base 0)
-  private int startRes;
+  /**
+   * start column (base 0)
+   */
+  private int startRes = 0;
 
-  // end column (base 0)
-  private int endRes;
+  /**
+   *  end column (base 0)
+   */
+  private int endRes = 0;
 
   public Color outlineColour = Color.black;
 
-  public Color idColour;
+  public Color idColour = null;
 
-  public int thresholdTextColour;
+  public int thresholdTextColour = 0;
 
   public Color textColour = Color.black;
 
@@ -121,12 +134,10 @@ public class SequenceGroup implements AnnotatedCollectionI
    */
   private boolean ignoreGapsInConsensus = true;
 
-  private boolean showSequenceLogo;
+  private boolean showSequenceLogo = false;
 
   private boolean normaliseSequenceLogo;
 
-  private boolean showConsensusHistogram;
-
   /*
    * properties for HMM information annotation
    */
@@ -134,7 +145,7 @@ public class SequenceGroup implements AnnotatedCollectionI
 
   private boolean hmmUseInfoLetterHeight;
 
-  private boolean showHMMSequenceLogo;
+  private boolean hmmShowSequenceLogo;
 
   private boolean hmmNormaliseSequenceLogo;
 
@@ -143,18 +154,23 @@ public class SequenceGroup implements AnnotatedCollectionI
   /*
    * visibility of rows or represented rows covered by group
    */
-  private boolean hidereps;
+  private boolean hidereps = false;
 
   /*
    * visibility of columns intersecting this group
    */
   private boolean hidecols;
 
-  private AlignmentAnnotation consensus;
+  AlignmentAnnotation consensus = null;
 
-  private AlignmentAnnotation conservation;
+  AlignmentAnnotation conservation = null;
 
   private AlignmentAnnotation hmmInformation;
+  
+  private boolean showConsensusHistogram;
+  
+  private AnnotatedCollectionI context;
+
 
   /**
    * Constructor, assigning a generated default name of "JGroup:" with object
@@ -164,6 +180,7 @@ public class SequenceGroup implements AnnotatedCollectionI
   {
     groupName = "JGroup:" + this.hashCode();
     cs = new ResidueShader();
+    sequences = new ArrayList<>();
   }
 
   /**
@@ -200,10 +217,13 @@ public class SequenceGroup implements AnnotatedCollectionI
    * copy constructor
    * 
    * @param seqsel
+   * @param keepsequences
+   *          if false do not add sequences from seqsel to new instance
    */
   public SequenceGroup(SequenceGroup seqsel)
   {
     this();
+
     if (seqsel != null)
     {
       sequences = new ArrayList<>();
@@ -215,6 +235,7 @@ public class SequenceGroup implements AnnotatedCollectionI
       displayBoxes = seqsel.displayBoxes;
       displayText = seqsel.displayText;
       colourText = seqsel.colourText;
+      
       startRes = seqsel.startRes;
       endRes = seqsel.endRes;
       cs = new ResidueShader((ResidueShader) seqsel.cs);
@@ -228,7 +249,7 @@ public class SequenceGroup implements AnnotatedCollectionI
       showSequenceLogo = seqsel.showSequenceLogo;
       normaliseSequenceLogo = seqsel.normaliseSequenceLogo;
       showConsensusHistogram = seqsel.showConsensusHistogram;
-      showHMMSequenceLogo = seqsel.showHMMSequenceLogo;
+      hmmShowSequenceLogo = seqsel.hmmShowSequenceLogo;
       hmmNormaliseSequenceLogo = seqsel.hmmNormaliseSequenceLogo;
       hmmShowHistogram = seqsel.hmmShowHistogram;
       idColour = seqsel.idColour;
@@ -241,25 +262,25 @@ public class SequenceGroup implements AnnotatedCollectionI
       ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
       hmmIgnoreBelowBackground = seqsel.hmmIgnoreBelowBackground;
       hmmUseInfoLetterHeight = seqsel.hmmUseInfoLetterHeight;
-      if (seqsel.conservationData != null)
+      if (seqsel.conserve != null)
       {
+        // todo avoid doing this if we don't actually want derived calculations
+        // !
         recalcConservation(); // safer than
         // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
       }
     }
   }
 
-  protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
-          this);
-
-  public void addPropertyChangeListener(PropertyChangeListener listener)
-  {
-    changeSupport.addPropertyChangeListener(listener);
-  }
-
-  public void removePropertyChangeListener(PropertyChangeListener listener)
+  /**
+   * Constructor that copies the given list of sequences
+   * 
+   * @param seqs
+   */
+  public SequenceGroup(List<SequenceI> seqs)
   {
-    changeSupport.removePropertyChangeListener(listener);
+    this();
+    this.sequences.addAll(seqs);
   }
 
   public boolean isShowSequenceLogo()
@@ -283,7 +304,6 @@ public class SequenceGroup implements AnnotatedCollectionI
         seqs[ipos].setDescription(seq.getDescription());
         seqs[ipos].setDBRefs(seq.getDBRefs());
         seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
-        seqs[ipos].setIsHMMConsensusSequence(seq.isHMMConsensusSequence());
         if (seq.getDatasetSequence() != null)
         {
           seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
@@ -513,7 +533,7 @@ public class SequenceGroup implements AnnotatedCollectionI
    */
   public Conservation getConservation()
   {
-    return conservationData;
+    return conserve;
   }
 
   /**
@@ -524,7 +544,7 @@ public class SequenceGroup implements AnnotatedCollectionI
    */
   public void setConservation(Conservation c)
   {
-    conservationData = c;
+    conserve = c;
   }
 
   /**
@@ -585,7 +605,7 @@ public class SequenceGroup implements AnnotatedCollectionI
    */
   public boolean recalcConservation()
   {
-    return recalcAnnotations(false);
+    return recalcConservation(false);
   }
 
   /**
@@ -597,7 +617,7 @@ public class SequenceGroup implements AnnotatedCollectionI
    *          when set, colourschemes for this group are not refreshed after
    *          recalculation
    */
-  public boolean recalcAnnotations(boolean defer)
+  public boolean recalcConservation(boolean defer)
   {
     if (cs == null && consensus == null && conservation == null
             && hmmInformation == null)
@@ -617,9 +637,8 @@ public class SequenceGroup implements AnnotatedCollectionI
 
         ProfilesI info = AAFrequency.calculateHMMProfiles(hmm,
                 (endRes + 1) - startRes, startRes, endRes + 1,
-                showHMMSequenceLogo, hmmIgnoreBelowBackground,
-                hmmUseInfoLetterHeight);
-        _updateInformationRow(info, sequences.size());
+                hmmIgnoreBelowBackground, hmmUseInfoLetterHeight);
+        _updateInformationRow(info);
         upd = true;
       }
       if (consensus != null)
@@ -694,6 +713,8 @@ public class SequenceGroup implements AnnotatedCollectionI
     c.completeAnnotations(conservation, null, startRes, endRes + 1);
   }
 
+  public ProfilesI consensusData = null;
+
   private void _updateConsensusRow(ProfilesI cnsns, long nseq)
   {
     if (consensus == null)
@@ -702,7 +723,7 @@ public class SequenceGroup implements AnnotatedCollectionI
     }
     consensus.label = "Consensus for " + getName();
     consensus.description = "Percent Identity";
-    consensusProfiles = cnsns;
+    consensusData = cnsns;
     // preserve width if already set
     int aWidth = (consensus.annotations != null)
             ? (endRes < consensus.annotations.length
@@ -720,12 +741,11 @@ public class SequenceGroup implements AnnotatedCollectionI
   }
 
   /**
-   * Recalculates the information content on the HMM annotation.
+   * Recalculates the information content on the HMM annotation
    * 
    * @param cnsns
-   * @param nseq
    */
-  private void _updateInformationRow(ProfilesI cnsns, long nseq)
+  private void _updateInformationRow(ProfilesI cnsns)
   {
     if (hmmInformation == null)
     {
@@ -744,7 +764,7 @@ public class SequenceGroup implements AnnotatedCollectionI
                                                       // width
     hmmInformation.setCalcId(InformationThread.HMM_CALC_ID);
     AAFrequency.completeInformation(hmmInformation, cnsns, startRes,
-            endRes + 1, nseq, 0f);
+            endRes + 1);
   }
 
   /**
@@ -816,13 +836,16 @@ public class SequenceGroup implements AnnotatedCollectionI
   /**
    * Set the first column selected by this group. Runs from 0<=i<N_cols
    * 
-   * @param i
+   * @param newStart
    */
-  public void setStartRes(int i)
+  public void setStartRes(int newStart)
   {
     int before = startRes;
-    startRes = i;
-    changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, startRes);
+   startRes= Math.max(0,newStart); // sanity check for negative start column positions
+   changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, startRes);
+    
+
+
   }
 
   /**
@@ -1298,7 +1321,7 @@ public class SequenceGroup implements AnnotatedCollectionI
     ignoreGapsInConsensus = state;
   }
 
-  public boolean isIgnoreGapsConsensus()
+  public boolean getIgnoreGapsConsensus()
   {
     return ignoreGapsInConsensus;
   }
@@ -1580,18 +1603,16 @@ public class SequenceGroup implements AnnotatedCollectionI
       // recalcConservation(); TODO don't know what to do here next
     }
     this.hmmShowHistogram = state;
-
   }
 
   public boolean isShowHMMSequenceLogo()
   {
-    return showHMMSequenceLogo;
+    return hmmShowSequenceLogo;
   }
 
-  public void setshowHMMSequenceLogo(boolean state)
+  public void setShowHMMSequenceLogo(boolean state)
   {
-    showHMMSequenceLogo = state;
-
+    hmmShowSequenceLogo = state;
   }
 
   public boolean isNormaliseHMMSequenceLogo()
@@ -1601,19 +1622,7 @@ public class SequenceGroup implements AnnotatedCollectionI
 
   public void setNormaliseHMMSequenceLogo(boolean state)
   {
-    normaliseSequenceLogo = state;
-  }
-
-  @Override
-  public SequenceI getHmmConsensus()
-  {
-    return hmmConsensus;
-  }
-
-  @Override
-  public void setHmmConsensus(SequenceI hmmSeq)
-  {
-    this.hmmConsensus = hmmSeq;
+    hmmNormaliseSequenceLogo = state;
   }
 
   public ProfilesI getConsensusData()
@@ -1631,4 +1640,19 @@ public class SequenceGroup implements AnnotatedCollectionI
     this.hmmProfiles = hmmProfiles;
   }
 
+  @Override
+  public List<SequenceI> getHmmSequences()
+  {
+    List<SequenceI> result = new ArrayList<>();
+    for (int i = 0; i < sequences.size(); i++)
+    {
+      SequenceI seq = sequences.get(i);
+      if (seq.hasHMMProfile())
+      {
+        result.add(seq);
+      }
+    }
+    return result;
+  }
+
 }