+ /**
+ * trigger update of consensus annotation
+ */
+ public void updateConsensus(final AlignmentViewPanel ap)
+ {
+ // see note in mantis : issue number 8585
+ if (consensus == null || !autoCalculateConsensus)
+ {
+ return;
+ }
+ if (calculator.getRegisteredWorkersOfClass(ConsensusThread.class) == null)
+ {
+ calculator.registerWorker(new ConsensusThread(this, ap));
+ }
+
+ /*
+ * A separate thread to compute cDNA consensus for a protein alignment
+ * which has mapping to cDNA
+ */
+ final AlignmentI al = this.getAlignment();
+ if (!al.isNucleotide() && al.getCodonFrames() != null
+ && !al.getCodonFrames().isEmpty())
+ {
+ /*
+ * fudge - check first for protein-to-nucleotide mappings
+ * (we don't want to do this for protein-to-protein)
+ */
+ 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)
+ {
+ calculator
+ .registerWorker(new ComplementConsensusThread(this, ap));
+ }
+ }
+ }
+ }
+
+ // --------START Structure Conservation
+ public void updateStrucConsensus(final AlignmentViewPanel ap)
+ {
+ if (autoCalculateStrucConsensus && strucConsensus == null
+ && alignment.isNucleotide() && alignment.hasRNAStructure())
+ {
+ // secondary structure has been added - so init the consensus line
+ initRNAStructure();
+ }
+
+ // see note in mantis : issue number 8585
+ if (strucConsensus == null || !autoCalculateStrucConsensus)
+ {
+ return;
+ }
+ if (calculator.getRegisteredWorkersOfClass(StrucConsensusThread.class) == null)
+ {
+ calculator.registerWorker(new StrucConsensusThread(this, ap));
+ }
+ }
+
+ public boolean isCalcInProgress()
+ {
+ return calculator.isWorking();
+ }
+
+ @Override
+ public boolean isCalculationInProgress(
+ AlignmentAnnotation alignmentAnnotation)
+ {
+ if (!alignmentAnnotation.autoCalculated)
+ {
+ return false;
+ }
+ if (calculator.workingInvolvedWith(alignmentAnnotation))
+ {
+ // System.err.println("grey out ("+alignmentAnnotation.label+")");
+ return true;
+ }
+ return false;
+ }
+
+ public void setAlignment(AlignmentI align)
+ {
+ this.alignment = align;
+ }
+
+ /**
+ * Clean up references when this viewport is closed
+ */
+ @Override
+ public void dispose()
+ {
+ /*
+ * defensively null out references to large objects in case
+ * this object is not garbage collected (as if!)
+ */
+ consensus = null;
+ complementConsensus = null;
+ strucConsensus = null;
+ conservation = null;
+ quality = null;
+ groupConsensus = null;
+ groupConservation = null;
+ hconsensus = null;
+ hcomplementConsensus = null;
+ // TODO removed listeners from changeSupport?
+ changeSupport = null;
+ setAlignment(null);
+ }
+
+ @Override
+ public boolean isClosed()
+ {
+ // TODO: check that this isClosed is only true after panel is closed, not
+ // before it is fully constructed.
+ return alignment == null;
+ }
+
+ @Override
+ public AlignCalcManagerI getCalcManager()
+ {
+ return calculator;
+ }
+
+ /**
+ * should conservation rows be shown for groups
+ */
+ protected boolean showGroupConservation = false;
+
+ /**
+ * should consensus rows be shown for groups
+ */
+ protected boolean showGroupConsensus = false;
+
+ /**
+ * should consensus profile be rendered by default
+ */
+ protected boolean showSequenceLogo = false;
+
+ /**
+ * should consensus profile be rendered normalised to row height
+ */
+ protected boolean normaliseSequenceLogo = false;
+
+ /**
+ * should consensus histograms be rendered by default
+ */
+ protected boolean showConsensusHistogram = true;
+
+ /**
+ * @return the showConsensusProfile
+ */
+ @Override
+ public boolean isShowSequenceLogo()
+ {
+ return showSequenceLogo;
+ }
+
+ /**
+ * @param showSequenceLogo
+ * the new value
+ */
+ public void setShowSequenceLogo(boolean showSequenceLogo)
+ {
+ if (showSequenceLogo != this.showSequenceLogo)
+ {
+ // TODO: decouple settings setting from calculation when refactoring
+ // annotation update method from alignframe to viewport
+ this.showSequenceLogo = showSequenceLogo;
+ calculator.updateAnnotationFor(ConsensusThread.class);
+ calculator.updateAnnotationFor(ComplementConsensusThread.class);
+ calculator.updateAnnotationFor(StrucConsensusThread.class);
+ }
+ this.showSequenceLogo = showSequenceLogo;
+ }
+
+ /**
+ * @param showConsensusHistogram
+ * the showConsensusHistogram to set
+ */
+ public void setShowConsensusHistogram(boolean showConsensusHistogram)
+ {
+ this.showConsensusHistogram = showConsensusHistogram;
+ }
+
+ /**
+ * @return the showGroupConservation
+ */
+ public boolean isShowGroupConservation()
+ {
+ return showGroupConservation;
+ }
+
+ /**
+ * @param showGroupConservation
+ * the showGroupConservation to set
+ */
+ public void setShowGroupConservation(boolean showGroupConservation)
+ {
+ this.showGroupConservation = showGroupConservation;
+ }
+
+ /**
+ * @return the showGroupConsensus
+ */
+ public boolean isShowGroupConsensus()
+ {
+ return showGroupConsensus;
+ }
+
+ /**
+ * @param showGroupConsensus
+ * the showGroupConsensus to set
+ */
+ public void setShowGroupConsensus(boolean showGroupConsensus)
+ {
+ this.showGroupConsensus = showGroupConsensus;
+ }
+
+ /**
+ *
+ * @return flag to indicate if the consensus histogram should be rendered by
+ * default
+ */
+ @Override
+ public boolean isShowConsensusHistogram()
+ {
+ return this.showConsensusHistogram;
+ }
+
+ /**
+ * when set, updateAlignment will always ensure sequences are of equal length
+ */
+ private boolean padGaps = false;
+
+ /**
+ * when set, alignment should be reordered according to a newly opened tree
+ */
+ public boolean sortByTree = false;
+
+ /**
+ *
+ *
+ * @return null or the currently selected sequence region
+ */
+ @Override
+ public SequenceGroup getSelectionGroup()
+ {
+ return selectionGroup;
+ }
+
+ /**
+ * Set the selection group for this window.
+ *
+ * @param sg
+ * - group holding references to sequences in this alignment view
+ *
+ */
+ @Override
+ public void setSelectionGroup(SequenceGroup sg)
+ {
+ selectionGroup = sg;
+ }
+
+ public void setHiddenColumns(ColumnSelection colsel)
+ {
+ this.colSel = colsel;
+ }
+
+ @Override
+ public ColumnSelection getColumnSelection()
+ {
+ return colSel;
+ }
+
+ @Override
+ public void setColumnSelection(ColumnSelection colSel)
+ {
+ this.colSel = colSel;
+ if (colSel != null)
+ {
+ updateHiddenColumns();
+ }
+ isColSelChanged(true);
+ }
+
+ /**
+ *
+ * @return
+ */
+ @Override
+ public Map<SequenceI, SequenceCollectionI> getHiddenRepSequences()
+ {
+ return hiddenRepSequences;
+ }
+
+ @Override
+ public void setHiddenRepSequences(
+ Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+ {
+ this.hiddenRepSequences = hiddenRepSequences;
+ }
+
+ @Override
+ public boolean hasSelectedColumns()
+ {
+ ColumnSelection columnSelection = getColumnSelection();
+ return columnSelection != null && columnSelection.hasSelectedColumns();
+ }
+
+ @Override
+ public boolean hasHiddenColumns()
+ {
+ return colSel != null && colSel.hasHiddenColumns();
+ }
+
+ public void updateHiddenColumns()
+ {
+ // this method doesn't really do anything now. But - it could, since a
+ // column Selection could be in the process of modification
+ // hasHiddenColumns = colSel.hasHiddenColumns();
+ }
+
+ @Override
+ public boolean hasHiddenRows()
+ {
+ return alignment.getHiddenSequences().getSize() > 0;
+ }
+
+ protected SequenceGroup selectionGroup;
+
+ public void setSequenceSetId(String newid)
+ {
+ if (sequenceSetID != null)
+ {
+ System.err
+ .println("Warning - overwriting a sequenceSetId for a viewport!");
+ }
+ sequenceSetID = new String(newid);
+ }
+
+ @Override
+ public String getSequenceSetId()
+ {
+ if (sequenceSetID == null)
+ {
+ sequenceSetID = alignment.hashCode() + "";
+ }
+
+ return sequenceSetID;
+ }
+
+ /**
+ * unique viewId for synchronizing state (e.g. with stored Jalview Project)
+ *
+ */
+ protected String viewId = null;
+
+ @Override
+ public String getViewId()
+ {
+ if (viewId == null)
+ {
+ viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
+ }
+ return viewId;
+ }
+
+ public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
+ {
+ ignoreGapsInConsensusCalculation = b;
+ if (ap != null)
+ {
+ updateConsensus(ap);
+ if (globalColourScheme != null)
+ {
+ globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
+ ignoreGapsInConsensusCalculation);
+ }
+ }
+
+ }
+
+ private long sgrouphash = -1, colselhash = -1;
+
+ /**
+ * checks current SelectionGroup against record of last hash value, and
+ * updates record.
+ *
+ * @param b
+ * update the record of last hash value
+ *
+ * @return true if SelectionGroup changed since last call (when b is true)
+ */
+ public boolean isSelectionGroupChanged(boolean b)
+ {
+ int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
+ : selectionGroup.hashCode();
+ if (hc != -1 && hc != sgrouphash)
+ {
+ if (b)
+ {
+ sgrouphash = hc;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * checks current colsel against record of last hash value, and optionally
+ * updates record.
+ *
+ * @param b
+ * update the record of last hash value
+ * @return true if colsel changed since last call (when b is true)
+ */
+ public boolean isColSelChanged(boolean b)
+ {
+ int hc = (colSel == null || colSel.isEmpty()) ? -1 : colSel.hashCode();
+ if (hc != -1 && hc != colselhash)
+ {
+ if (b)
+ {
+ colselhash = hc;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean isIgnoreGapsConsensus()
+ {
+ return ignoreGapsInConsensusCalculation;
+ }
+
+ // property change stuff
+ // JBPNote Prolly only need this in the applet version.
+ private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
+ this);
+
+ protected boolean showConservation = true;
+
+ protected boolean showQuality = true;
+
+ protected boolean showConsensus = true;
+
+ private Map<SequenceI, Color> sequenceColours = new HashMap<SequenceI, Color>();
+
+ protected SequenceAnnotationOrder sortAnnotationsBy = null;
+
+ protected boolean showAutocalculatedAbove;
+
+ /**
+ * when set, view will scroll to show the highlighted position
+ */
+ private boolean followHighlight = true;
+
+ // TODO private with getters and setters?
+ public int startRes;
+
+ public int endRes;
+
+ public int startSeq;
+
+ public int endSeq;
+
+ /**
+ * Property change listener for changes in alignment
+ *
+ * @param listener
+ * DOCUMENT ME!
+ */
+ public void addPropertyChangeListener(
+ java.beans.PropertyChangeListener listener)
+ {
+ changeSupport.addPropertyChangeListener(listener);
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @param listener
+ * DOCUMENT ME!
+ */
+ public void removePropertyChangeListener(
+ java.beans.PropertyChangeListener listener)
+ {
+ changeSupport.removePropertyChangeListener(listener);
+ }
+
+ /**
+ * Property change listener for changes in alignment
+ *
+ * @param prop
+ * DOCUMENT ME!
+ * @param oldvalue
+ * DOCUMENT ME!
+ * @param newvalue
+ * DOCUMENT ME!
+ */
+ public void firePropertyChange(String prop, Object oldvalue,
+ Object newvalue)
+ {
+ changeSupport.firePropertyChange(prop, oldvalue, newvalue);
+ }
+
+ // common hide/show column stuff
+
+ public void hideSelectedColumns()
+ {
+ if (colSel.isEmpty())
+ {
+ return;
+ }
+
+ colSel.hideSelectedColumns();
+ setSelectionGroup(null);
+ isColSelChanged(true);
+ }
+
+ public void hideColumns(int start, int end)
+ {
+ if (start == end)
+ {
+ colSel.hideColumns(start);
+ }
+ else
+ {
+ colSel.hideColumns(start, end);
+ }
+ isColSelChanged(true);
+ }
+
+ public void showColumn(int col)
+ {
+ colSel.revealHiddenColumns(col);
+ isColSelChanged(true);
+ }
+
+ public void showAllHiddenColumns()
+ {
+ colSel.revealAllHiddenColumns();
+ isColSelChanged(true);
+ }
+
+ // common hide/show seq stuff
+ public void showAllHiddenSeqs()
+ {
+ if (alignment.getHiddenSequences().getSize() > 0)
+ {
+ if (selectionGroup == null)
+ {
+ selectionGroup = new SequenceGroup();
+ selectionGroup.setEndRes(alignment.getWidth() - 1);
+ }
+ List<SequenceI> tmp = alignment.getHiddenSequences().showAll(
+ hiddenRepSequences);
+ for (SequenceI seq : tmp)
+ {
+ selectionGroup.addSequence(seq, false);
+ setSequenceAnnotationsVisible(seq, true);
+ }
+
+ hiddenRepSequences = null;
+
+ firePropertyChange("alignment", null, alignment.getSequences());
+ // used to set hasHiddenRows/hiddenRepSequences here, after the property
+ // changed event
+ sendSelection();
+ }
+ }
+
+ public void showSequence(int index)
+ {
+ List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(
+ index, hiddenRepSequences);
+ if (tmp.size() > 0)
+ {
+ if (selectionGroup == null)
+ {
+ selectionGroup = new SequenceGroup();
+ selectionGroup.setEndRes(alignment.getWidth() - 1);
+ }
+
+ for (SequenceI seq : tmp)
+ {
+ selectionGroup.addSequence(seq, false);
+ setSequenceAnnotationsVisible(seq, true);
+ }
+ firePropertyChange("alignment", null, alignment.getSequences());
+ sendSelection();
+ }
+ }
+
+ public void hideAllSelectedSeqs()
+ {
+ if (selectionGroup == null || selectionGroup.getSize() < 1)
+ {
+ return;
+ }
+
+ SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
+
+ hideSequence(seqs);
+
+ setSelectionGroup(null);
+ }
+
+ public void hideSequence(SequenceI[] seq)
+ {
+ if (seq != null)
+ {
+ for (int i = 0; i < seq.length; i++)
+ {
+ alignment.getHiddenSequences().hideSequence(seq[i]);
+ setSequenceAnnotationsVisible(seq[i], false);
+ }
+ firePropertyChange("alignment", null, alignment.getSequences());
+ }
+ }
+
+ /**
+ * 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
+ */
+ protected void setSequenceAnnotationsVisible(SequenceI sequenceI,
+ boolean visible)
+ {
+ AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
+ if (anns != null)
+ {
+ for (AlignmentAnnotation ann : anns)
+ {
+ if (ann.sequenceRef == sequenceI)
+ {
+ ann.visible = visible;
+ }
+ }
+ }
+ }
+
+ public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
+ {
+ int sSize = sg.getSize();
+ if (sSize < 2)
+ {
+ return;
+ }
+
+ if (hiddenRepSequences == null)
+ {
+ hiddenRepSequences = new Hashtable<SequenceI, SequenceCollectionI>();
+ }
+
+ hiddenRepSequences.put(repSequence, sg);
+
+ // Hide all sequences except the repSequence