X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fworkers%2FConsensusThread.java;h=2b11477e19b1f131d845f9238d27e75d8db091c2;hb=494a321d41897be1492b973fdb37fa4ccb0ec94e;hp=a8c33ce7874f81bc9539ef5d050c3a319fc050dc;hpb=ab43013b7e357b84b4abade0dba949668dfb2a0e;p=jalview.git diff --git a/src/jalview/workers/ConsensusThread.java b/src/jalview/workers/ConsensusThread.java index a8c33ce..2b11477 100644 --- a/src/jalview/workers/ConsensusThread.java +++ b/src/jalview/workers/ConsensusThread.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1) - * Copyright (C) 2014 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -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.ProfileI; import jalview.datamodel.SequenceI; import jalview.schemes.ColourSchemeI; -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) { @@ -54,8 +49,7 @@ public class ConsensusThread extends AlignCalcWorker implements long started = System.currentTimeMillis(); try { - AlignmentAnnotation consensus = alignViewport - .getAlignmentConsensusAnnotation(); + AlignmentAnnotation consensus = getConsensusAnnotation(); if (consensus == null || calcMan.isPending(this)) { calcMan.workerComplete(this); @@ -88,58 +82,89 @@ public class ConsensusThread extends AlignCalcWorker implements 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]; - 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); + 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; + calcMan.disableWorker(this); 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) + { + ProfileI[] hconsensus = new ProfileI[alignment.getWidth()]; + + SequenceI[] aseqs = getSequences(); + AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus, true); + + alignViewport.setSequenceConsensusHash(hconsensus); + setColourSchemeConsensus(hconsensus); + } + + /** + * @return + */ + protected SequenceI[] getSequences() + { + return alignViewport.getAlignment().getSequencesArray(); + } - calcMan.workerComplete(this); - if (ap != null) + /** + * @param hconsensus + */ + protected void setColourSchemeConsensus(ProfileI[] 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. */ @@ -151,15 +176,41 @@ public class ConsensusThread extends AlignCalcWorker implements public void updateResultAnnotation(boolean immediate) { - AlignmentAnnotation consensus = alignViewport - .getAlignmentConsensusAnnotation(); - Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash(); + AlignmentAnnotation consensus = getConsensusAnnotation(); + ProfileI[] hconsensus = (ProfileI[]) 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); } } + + /** + * 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, + ProfileI[] hconsensus) + { + long nseq = getSequences().length; + AAFrequency.completeConsensus(consensusAnnotation, hconsensus, 0, + hconsensus.length, alignViewport.isIgnoreGapsConsensus(), + alignViewport.isShowSequenceLogo(), nseq); + } + + /** + * Get the consensus data stored on the viewport. + * + * @return + */ + protected Object[] getViewportConsensus() + { + // TODO convert ComplementConsensusThread to use Profile + return alignViewport.getSequenceConsensusHash(); + } }