1 package jalview.workers;
3 import jalview.analysis.AAFrequency;
4 import jalview.api.AlignViewportI;
5 import jalview.api.AlignmentViewPanel;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.HiddenMarkovModel;
10 import jalview.datamodel.ProfilesI;
11 import jalview.datamodel.SequenceGroup;
12 import jalview.datamodel.SequenceI;
13 import jalview.util.MessageManager;
15 import java.util.ArrayList;
16 import java.util.List;
19 * This class calculates HMM Information Content annotations, based on any HMM
20 * consensus sequences and their HMM models. HMM consensus sequences may be
21 * present for the whole alignment, or subgroups of it.
24 public class InformationThread extends AlignCalcWorker
26 public static final String HMM_CALC_ID = "HMM";
31 * @param alignViewport
34 public InformationThread(AlignViewportI alignViewport,
35 AlignmentViewPanel alignPanel)
37 super(alignViewport, alignPanel);
41 * Recomputes Information annotations for any HMM consensus sequences (for
42 * alignment and/or groups)
47 if (alignViewport.getAlignment().getHmmSequences().isEmpty())
51 if (calcMan.isPending(this))
55 calcMan.notifyStart(this);
56 // long started = System.currentTimeMillis();
60 if (calcMan.isPending(this))
62 // another instance of this is waiting to run
63 calcMan.workerComplete(this);
66 while (!calcMan.notifyWorking(this))
68 // another thread in progress, wait my turn
73 ap.paintAlignment(false, false);
76 } catch (Exception ex)
81 if (alignViewport.isClosed())
87 AlignmentI alignment = alignViewport.getAlignment();
88 int aWidth = alignment == null ? -1 : alignment.getWidth();
91 calcMan.workerComplete(this);
96 * compute information profiles for any HMM consensus sequences
97 * for the alignment or sub-groups
99 computeProfiles(alignment);
102 * construct the corresponding annotations
108 ap.adjustAnnotationHeight();
109 ap.paintAlignment(true, true);
111 } catch (OutOfMemoryError error)
113 calcMan.disableWorker(this);
114 ap.raiseOOMWarning("calculating information", error);
117 calcMan.workerComplete(this);
122 * Computes HMM profiles for any HMM consensus sequences (for alignment or
123 * subgroups). Any alignment profile computed is stored on the viewport, any
124 * group profile computed is stored on the respective sequence group.
127 * @see AlignViewportI#setHmmProfiles(ProfilesI)
129 protected void computeProfiles(AlignmentI alignment)
131 int width = alignment.getWidth();
134 * alignment HMM profile
136 List<SequenceI> seqs = alignment.getHmmSequences();
139 HiddenMarkovModel hmm = seqs.get(0).getHMM();
140 ProfilesI hmmProfiles = AAFrequency.calculateHMMProfiles(hmm, width,
141 0, width, alignViewport.isIgnoreBelowBackground(),
142 alignViewport.isInfoLetterHeight());
143 alignViewport.setHmmProfiles(hmmProfiles);
149 List<SequenceGroup> groups = alignment.getGroups();
150 for (SequenceGroup group : groups)
152 seqs = group.getHmmSequences();
155 HiddenMarkovModel hmm = seqs.get(0).getHMM();
156 ProfilesI hmmProfiles = AAFrequency.calculateHMMProfiles(hmm, width,
157 0, width, group.isIgnoreBelowBackground(),
158 group.isUseInfoLetterHeight());
159 group.setHmmProfiles(hmmProfiles);
165 * gets the sequences on the alignment on the viewport.
169 protected SequenceI[] getSequences()
171 return alignViewport.getAlignment().getSequencesArray();
175 * Get the Gap annotation for the alignment
179 protected AlignmentAnnotation getGapAnnotation()
181 return alignViewport.getAlignmentGapAnnotation();
185 * Computes Information Content annotation for any HMM consensus sequences
186 * (for alignment or groups), and updates (or adds) the annotation to the
187 * sequence and the alignment
190 public void updateAnnotation()
192 AlignmentI alignment = alignViewport.getAlignment();
194 float maxInformation = 0f;
195 List<AlignmentAnnotation> infos = new ArrayList<>();
198 * annotation for alignment HMM consensus if present
200 List<SequenceI> hmmSeqs = alignment.getHmmSequences();
201 if (!hmmSeqs.isEmpty())
203 ProfilesI profile = alignViewport.getHmmProfiles();
204 float m = updateInformationAnnotation(hmmSeqs.get(0), profile, null,
206 maxInformation = Math.max(maxInformation, m);
210 * annotation for group HMM consensus if present
212 for (SequenceGroup group : alignment.getGroups())
214 hmmSeqs = group.getHmmSequences();
215 if (!hmmSeqs.isEmpty())
217 ProfilesI profiles = group.getHmmProfiles();
218 float m = updateInformationAnnotation(hmmSeqs.get(0), profiles,
220 maxInformation = Math.max(maxInformation, m);
225 * maxInformation holds the maximum value of information score;
226 * set this as graphMax in all annotations to scale them all the same
228 for (AlignmentAnnotation ann : infos)
230 ann.graphMax = maxInformation;
235 * Updates (and first constructs if necessary) an HMM Profile information
236 * content annotation for a sequence. The <code>group</code> argument is null
237 * for the whole alignment annotation, not null for a subgroup annotation. The
238 * updated annotation is added to the <code>infos</code> list. Answers the
239 * maximum information content value of any annotation (for use as a scaling
240 * factor for display).
248 protected float updateInformationAnnotation(SequenceI seq,
249 ProfilesI profile, SequenceGroup group,
250 List<AlignmentAnnotation> infos)
252 if (seq == null || profile == null)
257 AlignmentAnnotation ann = findOrCreateAnnotation(seq, group);
259 seq.addAlignmentAnnotation(ann);
262 float max = AAFrequency.completeInformation(ann, profile,
263 profile.getStartColumn(), profile.getEndColumn() + 1);
269 * A helper method that first searches for the HMM annotation that matches the
270 * group reference (null for the whole alignment annotation). If found, its
271 * sequence reference is updated to the given sequence (the recomputed HMM
272 * consensus sequence). If not found, it is created. This supports both
273 * creating the annotation the first time hmmbuild is run, and updating it if
274 * hmmbuild is re-run.
280 AlignmentAnnotation findOrCreateAnnotation(SequenceI seq,
284 * can't use Alignment.findOrCreateAnnotation here because we
285 * want to update, rather than match on, the sequence ref
287 AlignmentAnnotation info = null;
289 AlignmentI alignment = alignViewport.getAlignment();
290 AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
293 for (AlignmentAnnotation ann : anns)
295 if (HMM_CALC_ID.equals(ann.getCalcId()) && group == ann.groupRef)
298 info.setSequenceRef(seq);
299 info.label = seq.getName(); // in case group name changed!
307 int aWidth = alignment.getWidth();
308 String desc = MessageManager
309 .getString("label.information_description");
310 float graphMax = 6.52f; // todo where does this value derive from?
311 info = new AlignmentAnnotation(seq.getName(), desc,
312 new Annotation[aWidth], 0f, graphMax,
313 AlignmentAnnotation.BAR_GRAPH);
314 info.setCalcId(HMM_CALC_ID);
315 info.setSequenceRef(seq);
316 info.groupRef = group;
318 alignment.addAnnotation(info);