package jalview.workers; import jalview.analysis.AAFrequency; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; import jalview.datamodel.HiddenMarkovModel; import jalview.datamodel.ProfilesI; import jalview.datamodel.SequenceI; import jalview.renderer.ResidueShaderI; import java.util.List; public class InformationThread extends AlignCalcWorker { /** * Constructor for information thread. * * @param alignViewport * @param alignPanel */ public InformationThread(AlignViewportI alignViewport, AlignmentViewPanel alignPanel) { super(alignViewport, alignPanel); } @Override public void run() { if (calcMan.isPending(this)) { return; } calcMan.notifyStart(this); long started = System.currentTimeMillis(); List information = getInformationAnnotations(); try { if ((information == null) || calcMan.isPending(this)) { calcMan.workerComplete(this); return; } while (!calcMan.notifyWorking(this)) { // System.err.println("Thread // (Information"+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; if (alignment == null || (aWidth = alignment.getWidth()) < 0) { calcMan.workerComplete(this); return; } eraseInformation(aWidth); computeInformation(alignment); updateResultAnnotation(true); if (ap != null) { ap.paintAlignment(true); } } catch (OutOfMemoryError error) { calcMan.disableWorker(this); ap.raiseOOMWarning("calculating information", error); } finally { calcMan.workerComplete(this); } } /** * Clear out any existing information annotations * * @param aWidth * the width (number of columns) of the annotated alignment */ protected void eraseInformation(int aWidth) { List information = getInformationAnnotations(); for (AlignmentAnnotation info : information) { info.annotations = new Annotation[aWidth]; } } /** * Computes the profiles from a HMM for an alignment. * * @param alignment */ protected void computeInformation(AlignmentI alignment) { int width = alignment.getWidth(); List hmmSeqs = alignment.getHMMConsensusSequences(false); int index = 0; for (SequenceI seq : hmmSeqs) { HiddenMarkovModel hmm = seq.getHMM(); ProfilesI hinformation = AAFrequency.calculateHMMProfiles(hmm, width, 0, width, true, alignViewport.isIgnoreBelowBackground()); alignViewport.setSequenceInformationHash(hinformation, index); // setColourSchemeInformation(hinformation); index++; } } /** * gets the sequences on the alignment on the viewport. * * @return */ protected SequenceI[] getSequences() { return alignViewport.getAlignment().getSequencesArray(); } protected void setColourSchemeInformation(ProfilesI information) { ResidueShaderI cs = alignViewport.getResidueShading(); if (cs != null) { cs.setInformation(information); } } /** * Get the Information annotation for the alignment * * @return */ protected List getInformationAnnotations() { return alignViewport.getInformationAnnotations(); } /** * Get the Gap annotation for the alignment * * @return */ protected AlignmentAnnotation getGapAnnotation() { return alignViewport.getAlignmentGapAnnotation(); } /** * update the information annotation from the sequence profile data using * current visualization settings. */ @Override public void updateAnnotation() { updateResultAnnotation(false); } /** * Derives the information content for an information annotation. * * @param immediate */ public void updateResultAnnotation(boolean immediate) { List annots = getInformationAnnotations(); int index = 0; for (AlignmentAnnotation information : annots) { ProfilesI hinformation = (ProfilesI) getSequenceInformation(index); if (immediate || !calcMan.isWorking(this) && information != null && hinformation != null) { deriveInformation(information, hinformation); } index++; } } /** * Convert the computed information data into the desired annotation for * display. * * @param informationAnnotation * the annotation to be populated * @param hinformation * the computed information data */ protected void deriveInformation( AlignmentAnnotation informationAnnotation, ProfilesI hinformation) { long nseq = getSequences().length; AAFrequency.completeInformation(informationAnnotation, hinformation, hinformation.getStartColumn(), hinformation.getEndColumn() + 1, alignViewport.isIgnoreBelowBackground(), alignViewport.isShowHMMSequenceLogo(), nseq); } /** * Get the information data stored on the viewport. * * @return */ protected Object getSequenceInformation(int index) { // TODO convert ComplementInformationThread to use Profile return alignViewport.getSequenceInformationHash(index); } }