X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fworkers%2FConsensusThread.java;h=e12c5b8d87765b52779d4ba9dccff4e140615cf7;hb=refs%2Fheads%2Fj2s%2Fdevelop-bh;hp=7dd25f847f0188e223d4174f378dc5fa57885738;hpb=34286266321abbbab6d462c39f500650f9c860c9;p=jalview.git diff --git a/src/jalview/workers/ConsensusThread.java b/src/jalview/workers/ConsensusThread.java index 7dd25f8..e12c5b8 100644 --- a/src/jalview/workers/ConsensusThread.java +++ b/src/jalview/workers/ConsensusThread.java @@ -1,5 +1,27 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.workers; +import java.util.Hashtable; + import jalview.analysis.AAFrequency; import jalview.api.AlignCalcWorkerI; import jalview.api.AlignViewportI; @@ -7,11 +29,11 @@ import jalview.api.AlignmentViewPanel; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; +import jalview.datamodel.SequenceI; import jalview.schemes.ColourSchemeI; -import java.util.Hashtable; - -public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI +public class ConsensusThread extends AlignCalcWorker implements + AlignCalcWorkerI { public ConsensusThread(AlignViewportI alignViewport, AlignmentViewPanel alignPanel) @@ -19,33 +41,43 @@ public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI super(alignViewport, alignPanel); } + @Override public void run() { + + if (calcMan.isPending(this)) + { + return; + } + calcMan.notifyStart(this); + long started = System.currentTimeMillis(); try { - AlignmentAnnotation consensus = alignViewport.getAlignmentConsensusAnnotation(); - if (consensus==null) { return; + AlignmentAnnotation consensus = getConsensusAnnotation(); + if (consensus == null || calcMan.isPending(this)) + { + calcMan.workerComplete(this); + return; } - calcMan.notifyStart(this); - while (!calcMan.notifyWorking(this)) + while (!calcMan.notifyWorking(this)) { + // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around."); try { if (ap != null) { ap.paintAlignment(false); } - Thread.sleep(200); } catch (Exception ex) { ex.printStackTrace(); } } - calcMan.notifyWorking(this); if (alignViewport.isClosed()) { abortAndDestroy(); + return; } AlignmentI alignment = alignViewport.getAlignment(); @@ -54,49 +86,94 @@ public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI if (alignment == null || (aWidth = alignment.getWidth()) < 0) { calcMan.workerComplete(this); - // .updatingConservation = false; - // AlignViewport.UPDATING_CONSERVATION = false; - return; } - consensus = alignViewport - .getAlignmentConsensusAnnotation(); - - consensus.annotations = null; - consensus.annotations = new Annotation[aWidth]; - Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash(); - hconsensus = new Hashtable[aWidth]; - AAFrequency.calculate(alignment.getSequencesArray(), 0, - alignment.getWidth(), hconsensus, true); - alignViewport.setSequenceConsensusHash(hconsensus); + + eraseConsensus(aWidth); + computeConsensus(alignment); updateResultAnnotation(true); - ColourSchemeI globalColourScheme = alignViewport - .getGlobalColourScheme(); - if (globalColourScheme != null) + + if (ap != null) { - globalColourScheme.setConsensus(hconsensus); + ap.paintAlignment(true); } - } catch (OutOfMemoryError error) { calcMan.workerCannotRun(this); - - // consensus = null; - // hconsensus = null; ap.raiseOOMWarning("calculating consensus", error); + } finally + { + /* + * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition + * - alignment was edited at same time as calculation was running + */ + calcMan.workerComplete(this); } + } + + /** + * Clear out any existing consensus annotations + * + * @param aWidth + * the width (number of columns) of the annotated alignment + */ + protected void eraseConsensus(int aWidth) + { + AlignmentAnnotation consensus = getConsensusAnnotation(); + consensus.annotations = new Annotation[aWidth]; + } + + /** + * @param alignment + */ + protected void computeConsensus(AlignmentI alignment) + { + Hashtable[] hconsensus = new Hashtable[alignment.getWidth()]; - calcMan.workerComplete(this); - if (ap != null) + SequenceI[] aseqs = getSequences(); + AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus, + true); + + alignViewport.setSequenceConsensusHash(hconsensus); + setColourSchemeConsensus(hconsensus); + } + + /** + * @return + */ + protected SequenceI[] getSequences() + { + return alignViewport.getAlignment().getSequencesArray(); + } + + /** + * @param hconsensus + */ + protected void setColourSchemeConsensus(Hashtable[] hconsensus) + { + ColourSchemeI globalColourScheme = alignViewport + .getGlobalColourScheme(); + if (globalColourScheme != null) { - ap.paintAlignment(true); + globalColourScheme.setConsensus(hconsensus); } } /** + * Get the Consensus annotation for the alignment + * + * @return + */ + protected AlignmentAnnotation getConsensusAnnotation() + { + return alignViewport.getAlignmentConsensusAnnotation(); + } + + /** * update the consensus annotation from the sequence profile data using * current visualization settings. */ + @Override public void updateAnnotation() { updateResultAnnotation(false); @@ -104,14 +181,40 @@ public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI public void updateResultAnnotation(boolean immediate) { - AlignmentAnnotation consensus = alignViewport - .getAlignmentConsensusAnnotation(); - Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash(); - if (immediate || !calcMan.isWorking(this) && consensus!=null && hconsensus!=null) + AlignmentAnnotation consensus = getConsensusAnnotation(); + Hashtable[] hconsensus = getViewportConsensus(); + if (immediate || !calcMan.isWorking(this) && consensus != null + && hconsensus != null) { - AAFrequency.completeConsensus(consensus, hconsensus, 0, - hconsensus.length, alignViewport.getIgnoreGapsConsensus(), - alignViewport.isShowSequenceLogo()); + deriveConsensus(consensus, hconsensus); } } + + /** + * Convert the computed consensus data into the desired annotation for + * display. + * + * @param consensusAnnotation + * the annotation to be populated + * @param consensusData + * the computed consensus data + */ + protected void deriveConsensus(AlignmentAnnotation consensusAnnotation, + Hashtable[] consensusData) + { + long nseq = getSequences().length; + AAFrequency.completeConsensus(consensusAnnotation, consensusData, 0, + consensusData.length, alignViewport.isIgnoreGapsConsensus(), + alignViewport.isShowSequenceLogo(), nseq); + } + + /** + * Get the consensus data stored on the viewport. + * + * @return + */ + protected Hashtable[] getViewportConsensus() + { + return alignViewport.getSequenceConsensusHash(); + } }