/*
* 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 jalview.analysis.Conservation;
import jalview.api.AlignCalcWorkerI;
import jalview.api.AlignViewportI;
import jalview.api.AlignmentViewPanel;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.AlignmentI;
import jalview.schemes.ResidueProperties;
import java.util.ArrayList;
import java.util.List;
public class ConservationThread extends AlignCalcWorker implements
AlignCalcWorkerI
{
private int ConsPercGaps = 25; // JBPNote : This should be a configurable
// property!
@Override
public AlignCalcWorkerI getNewWorker() {
return new ConservationThread(alignViewport, ap);
}
public ConservationThread(AlignViewportI alignViewport,
AlignmentViewPanel alignPanel)
{
super(alignViewport, alignPanel);
ConsPercGaps = alignViewport.getConsPercGaps();
}
private Conservation cons;
AlignmentAnnotation conservation, quality;
@Override
protected void run1(int state) {
while (!interrupted()) {
try {
switch (state) {
case INIT:
if (calcMan.isPending(this))
return;
calcMan.notifyStart(this);
state = LOOP_STANDBY;
break;
case LOOP_STANDBY:
while (!calcMan.notifyWorking(this)) {
if (ap != null) {
ap.paintAlignment(false);
}
try {
if (sleepAndReturn(200, state))
return;
} catch (InterruptedException e) {
state = DONE;
break;
}
}
if (alignViewport.isClosed()) {
abortAndDestroy();
state = DONE;
break;
}
List ourAnnot = new ArrayList();
alignment = alignViewport.getAlignment();
conservation = alignViewport.getAlignmentConservationAnnotation();
quality = alignViewport.getAlignmentQualityAnnot();
ourAnnot.add(conservation);
ourAnnot.add(quality);
ourAnnots = ourAnnot;
ConsPercGaps = alignViewport.getConsPercGaps();
// AlignViewport.UPDATING_CONSERVATION = true;
if (alignment == null || (aWidth = alignment.getWidth()) < 0)
{
calcMan.workerComplete(this);
// .updatingConservation = false;
// AlignViewport.UPDATING_CONSERVATION = false;
return;
}
state = LOOP_CALCULATE;
break;
case LOOP_CALCULATE:
iFirst = iLast;
nPer = aWidth + 1; // for now - one big chunk
iLast = Math.min(iLast + nPer, aWidth);
if (iLast == iFirst) {
state = DONE;
} else {
computeConsensus();
if (sleepAndReturn(0, state))
return;
}
break;
case DONE:
updateResultAnnotation(true);
notifyDone();
return;
}
} catch (OutOfMemoryError error) {
calcMan.workerCannotRun(this);
ap.raiseOOMWarning("calculating conservation", error);
} catch (Throwable e) {
System.out.println("Error in ConsensusThread: " + e);
e.printStackTrace();
calcMan.workerComplete(this);
}
}
}
private void computeConsensus() {
cons = Conservation.calculateConservation("All",
ResidueProperties.propHash, 3,
alignment.getSequences(), 0, aWidth - 1, false,
ConsPercGaps, quality != null);
}
private void updateResultAnnotation(boolean b)
{
if (b || !calcMan.isWorking(this) && cons != null
&& conservation != null && quality != null)
{
alignViewport.setConservation(cons);
cons.completeAnnotations(conservation, quality, 0, aWidth);
}
}
@Override
public void updateAnnotation()
{
updateResultAnnotation(false);
}
}