X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fworkers%2FConsensusThread.java;h=b97c4c58ecfb6b787b040140b71a74a375cc1758;hb=747167089ecf8d6afc70d417f5a20352e029bd95;hp=8cde493b8fa91d50218929ca394b8d345142f2f9;hpb=ad15cff29620f960119f80176f1fd443da9f6763;p=jalview.git diff --git a/src/jalview/workers/ConsensusThread.java b/src/jalview/workers/ConsensusThread.java index 8cde493..b97c4c5 100644 --- a/src/jalview/workers/ConsensusThread.java +++ b/src/jalview/workers/ConsensusThread.java @@ -21,22 +21,17 @@ package jalview.workers; import jalview.analysis.AAFrequency; -import jalview.api.AlignCalcWorkerI; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; +import jalview.datamodel.ProfilesI; import jalview.datamodel.SequenceI; -import jalview.schemes.ColourSchemeI; +import jalview.renderer.ResidueShaderI; -import java.util.Hashtable; - -public class ConsensusThread extends AlignCalcWorker implements - AlignCalcWorkerI +public class ConsensusThread extends AlignCalcWorker { - private long nseq = -1; - public ConsensusThread(AlignViewportI alignViewport, AlignmentViewPanel alignPanel) { @@ -46,100 +41,112 @@ public class ConsensusThread extends AlignCalcWorker implements @Override public void run() { - if (calcMan.isPending(this)) + AlignmentAnnotation consensus = getConsensusAnnotation(); + AlignmentAnnotation gap = getGapAnnotation(); + if ((consensus == null && gap == null)) { return; } - calcMan.notifyStart(this); - long started = System.currentTimeMillis(); - try + if (alignViewport.isClosed()) { - AlignmentAnnotation consensus = alignViewport - .getAlignmentConsensusAnnotation(); - if (consensus == null || calcMan.isPending(this)) - { - calcMan.workerComplete(this); - return; - } - 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(); - } - } - if (alignViewport.isClosed()) - { - abortAndDestroy(); - return; - } - AlignmentI alignment = alignViewport.getAlignment(); - - int aWidth = -1; + abortAndDestroy(); + return; + } + AlignmentI alignment = alignViewport.getAlignment(); - if (alignment == null || (aWidth = alignment.getWidth()) < 0) - { - calcMan.workerComplete(this); - // .updatingConservation = false; - // AlignViewport.UPDATING_CONSERVATION = false; + int aWidth = -1; - return; - } - consensus = alignViewport.getAlignmentConsensusAnnotation(); + if (alignment == null || (aWidth = alignment.getWidth()) < 0) + { + return; + } - consensus.annotations = null; - consensus.annotations = new Annotation[aWidth]; - Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash(); - hconsensus = new Hashtable[aWidth]; - try - { - SequenceI aseqs[] = alignment.getSequencesArray(); - nseq = aseqs.length; - AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus, - true); - } catch (ArrayIndexOutOfBoundsException x) - { - // this happens due to a race condition - - // alignment was edited at same time as calculation was running - // - // calcMan.workerCannotRun(this); - calcMan.workerComplete(this); - return; - } - alignViewport.setSequenceConsensusHash(hconsensus); - updateResultAnnotation(true); - ColourSchemeI globalColourScheme = alignViewport - .getGlobalColourScheme(); - if (globalColourScheme != null) - { - globalColourScheme.setConsensus(hconsensus); - } + eraseConsensus(aWidth); + computeConsensus(alignment); + updateResultAnnotation(true); - } catch (OutOfMemoryError error) + if (ap != null) { - calcMan.workerCannotRun(this); + ap.paintAlignment(true, true); + } + } - // consensus = null; - // hconsensus = null; - ap.raiseOOMWarning("calculating consensus", error); + /** + * 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(); + if (consensus != null) + { + consensus.annotations = new Annotation[aWidth]; + } + AlignmentAnnotation gap = getGapAnnotation(); + if (gap != null) + { + gap.annotations = new Annotation[aWidth]; } + } - calcMan.workerComplete(this); - if (ap != null) + /** + * @param alignment + */ + protected void computeConsensus(AlignmentI alignment) + { + + SequenceI[] aseqs = getSequences(); + int width = alignment.getWidth(); + ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width, + true); + + alignViewport.setSequenceConsensusHash(hconsensus); + setColourSchemeConsensus(hconsensus); + } + + /** + * @return + */ + protected SequenceI[] getSequences() + { + return alignViewport.getAlignment().getSequencesArray(); + } + + /** + * @param hconsensus + */ + protected void setColourSchemeConsensus(ProfilesI hconsensus) + { + ResidueShaderI cs = alignViewport.getResidueShading(); + if (cs != null) { - ap.paintAlignment(true); + cs.setConsensus(hconsensus); } } /** + * Get the Consensus annotation for the alignment + * + * @return + */ + protected AlignmentAnnotation getConsensusAnnotation() + { + return alignViewport.getAlignmentConsensusAnnotation(); + } + + /** + * Get the Gap annotation for the alignment + * + * @return + */ + protected AlignmentAnnotation getGapAnnotation() + { + return alignViewport.getAlignmentGapAnnotation(); + } + + /** * update the consensus annotation from the sequence profile data using * current visualization settings. */ @@ -151,15 +158,64 @@ public class ConsensusThread extends AlignCalcWorker implements public void updateResultAnnotation(boolean immediate) { - AlignmentAnnotation consensus = alignViewport - .getAlignmentConsensusAnnotation(); - Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash(); + AlignmentAnnotation consensus = getConsensusAnnotation(); + ProfilesI hconsensus = (ProfilesI) getViewportConsensus(); if (immediate || !calcMan.isWorking(this) && consensus != null && hconsensus != null) { - AAFrequency.completeConsensus(consensus, hconsensus, 0, - hconsensus.length, alignViewport.getIgnoreGapsConsensus(), - alignViewport.isShowSequenceLogo(), nseq); + deriveConsensus(consensus, hconsensus); + AlignmentAnnotation gap = getGapAnnotation(); + if (gap != null) + { + deriveGap(gap, hconsensus); + } } } + + /** + * Convert the computed consensus data into the desired annotation for + * display. + * + * @param consensusAnnotation + * the annotation to be populated + * @param hconsensus + * the computed consensus data + */ + protected void deriveConsensus(AlignmentAnnotation consensusAnnotation, + ProfilesI hconsensus) + { + long nseq = getSequences().length; + AAFrequency.completeConsensus(consensusAnnotation, hconsensus, + hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1, + alignViewport.isIgnoreGapsConsensus(), + alignViewport.isShowSequenceLogo(), nseq); + } + + /** + * Convert the computed consensus data into a gap annotation row for display. + * + * @param gapAnnotation + * the annotation to be populated + * @param hconsensus + * the computed consensus data + */ + protected void deriveGap(AlignmentAnnotation gapAnnotation, + ProfilesI hconsensus) + { + long nseq = getSequences().length; + AAFrequency.completeGapAnnot(gapAnnotation, hconsensus, + hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1, + nseq); + } + + /** + * Get the consensus data stored on the viewport. + * + * @return + */ + protected Object getViewportConsensus() + { + // TODO convert ComplementConsensusThread to use Profile + return alignViewport.getSequenceConsensusHash(); + } }