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 (calcMan.isPending(this))
51 calcMan.notifyStart(this);
52 // long started = System.currentTimeMillis();
56 if (calcMan.isPending(this))
58 // another instance of this is waiting to run
59 calcMan.workerComplete(this);
62 while (!calcMan.notifyWorking(this))
64 // another thread in progress, wait my turn
69 ap.paintAlignment(false, false);
72 } catch (Exception ex)
77 if (alignViewport.isClosed())
83 AlignmentI alignment = alignViewport.getAlignment();
84 int aWidth = alignment == null ? -1 : alignment.getWidth();
87 calcMan.workerComplete(this);
92 * compute information profiles for any HMM consensus sequences
93 * for the alignment or sub-groups
95 computeProfiles(alignment);
98 * construct the corresponding annotations
104 ap.adjustAnnotationHeight();
105 ap.paintAlignment(true, true);
107 } catch (OutOfMemoryError error)
109 calcMan.disableWorker(this);
110 ap.raiseOOMWarning("calculating information", error);
113 calcMan.workerComplete(this);
118 * Computes HMM profiles for any HMM consensus sequences (for alignment or
119 * subgroups). Any alignment profile computed is stored on the viewport, any
120 * group profile computed is stored on the respective sequence group.
123 * @see AlignViewportI#setHmmProfiles(ProfilesI)
125 protected void computeProfiles(AlignmentI alignment)
127 int width = alignment.getWidth();
130 * alignment HMM profile
132 List<SequenceI> seqs = alignment.getHmmSequences();
135 HiddenMarkovModel hmm = seqs.get(0).getHMM();
136 ProfilesI hmmProfiles = AAFrequency.calculateHMMProfiles(hmm, width,
137 0, width, alignViewport.isIgnoreBelowBackground(),
138 alignViewport.isInfoLetterHeight());
139 alignViewport.setHmmProfiles(hmmProfiles);
145 List<SequenceGroup> groups = alignment.getGroups();
146 for (SequenceGroup group : groups)
148 seqs = group.getHmmSequences();
151 HiddenMarkovModel hmm = seqs.get(0).getHMM();
152 ProfilesI hmmProfiles = AAFrequency.calculateHMMProfiles(hmm, width,
153 0, width, group.isIgnoreBelowBackground(),
154 group.isUseInfoLetterHeight());
155 group.setHmmProfiles(hmmProfiles);
161 * gets the sequences on the alignment on the viewport.
165 protected SequenceI[] getSequences()
167 return alignViewport.getAlignment().getSequencesArray();
171 * Get the Gap annotation for the alignment
175 protected AlignmentAnnotation getGapAnnotation()
177 return alignViewport.getOccupancyAnnotation();
181 * Computes Information Content annotation for any HMM consensus sequences
182 * (for alignment or groups), and updates (or adds) the annotation to the
183 * sequence and the alignment
186 public void updateAnnotation()
188 AlignmentI alignment = alignViewport.getAlignment();
190 float maxInformation = 0f;
191 List<AlignmentAnnotation> infos = new ArrayList<>();
194 * annotation for alignment HMM consensus if present
196 List<SequenceI> hmmSeqs = alignment.getHmmSequences();
197 if (!hmmSeqs.isEmpty())
199 ProfilesI profile = alignViewport.getHmmProfiles();
200 float m = updateInformationAnnotation(hmmSeqs.get(0), profile, null,
202 maxInformation = Math.max(maxInformation, m);
206 * annotation for group HMM consensus if present
208 for (SequenceGroup group : alignment.getGroups())
210 hmmSeqs = group.getHmmSequences();
211 ProfilesI profiles = group.getHmmProfiles();
212 float m = updateInformationAnnotation(hmmSeqs.get(0), profiles, group,
214 maxInformation = Math.max(maxInformation, m);
218 * maxInformation holds the maximum value of information score;
219 * set this as graphMax in all annotations to scale them all the same
221 for (AlignmentAnnotation ann : infos)
223 ann.graphMax = maxInformation;
228 * Updates (and first constructs if necessary) an HMM Profile information
229 * content annotation for a sequence. The <code>group</code> argument is null
230 * for the whole alignment annotation, not null for a subgroup annotation. The
231 * updated annotation is added to the <code>infos</code> list. Answers the
232 * maximum information content value of any annotation (for use as a scaling
233 * factor for display).
241 protected float updateInformationAnnotation(SequenceI seq,
242 ProfilesI profile, SequenceGroup group,
243 List<AlignmentAnnotation> infos)
245 if (seq == null || profile == null)
250 AlignmentAnnotation ann = findOrCreateAnnotation(seq, group);
252 seq.addAlignmentAnnotation(ann);
255 float max = AAFrequency.completeInformation(ann, profile,
256 profile.getStartColumn(), profile.getEndColumn() + 1);
262 * A helper method that first searches for the HMM annotation that matches the
263 * group reference (null for the whole alignment annotation). If found, its
264 * sequence reference is updated to the given sequence (the recomputed HMM
265 * consensus sequence). If not found, it is created. This supports both
266 * creating the annotation the first time hmmbuild is run, and updating it if
267 * hmmbuild is re-run.
273 AlignmentAnnotation findOrCreateAnnotation(SequenceI seq,
277 * can't use Alignment.findOrCreateAnnotation here because we
278 * want to update, rather than match on, the sequence ref
280 AlignmentAnnotation info = null;
282 AlignmentI alignment = alignViewport.getAlignment();
283 AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
286 for (AlignmentAnnotation ann : anns)
288 if (HMM_CALC_ID.equals(ann.getCalcId()) && group == ann.groupRef)
291 info.setSequenceRef(seq);
292 info.label = seq.getName(); // in case group name changed!
300 int aWidth = alignment.getWidth();
301 String desc = MessageManager
302 .getString("label.information_description");
303 float graphMax = 6.52f; // todo where does this value derive from?
304 info = new AlignmentAnnotation(seq.getName(), desc,
305 new Annotation[aWidth], 0f, graphMax,
306 AlignmentAnnotation.BAR_GRAPH);
307 info.setCalcId(HMM_CALC_ID);
308 info.setSequenceRef(seq);
309 info.groupRef = group;
311 alignment.addAnnotation(info);