add HMMFile class to read and write HMM files
[jalview.git] / src / jalview / datamodel / HiddenMarkovModel.java
1 package jalview.datamodel;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Scanner;
8
9 /**
10  * Data structure which stores a hidden Markov model. Currently contains file properties as well, not sure whether these should be transferred to the HMMFile class
11  * 
12  * @author TZVanaalten
13  * 
14  */
15 public class HiddenMarkovModel
16 {
17   // Stores file properties. Do not directly access this field as it contains
18   // only string value - use the getter methods. For example, to find the length
19   // of theHMM, use getModelLength()to return an int value
20   Map<String, String> fileProperties = new HashMap<>();
21
22   // contains the average emission probabilities for each symbol
23   List<Double> averageMatchStateEmissionProbabilities = new ArrayList<>();
24
25   // contains the probabilities of insert 0 emissions for each symbol
26   List<Double> insertZeroEmissions = new ArrayList<>();
27
28   // contains the probabilities of transitions from the begin state and insert
29   // state 0. These are bm, bi, bd, im, ii, dm and dd in order (0th position in
30   // the array indicates the probability of a bm transition)
31
32   List<Double> beginStateTransitions = new ArrayList<>();
33
34   // contains the alignment column index for each node
35   List<Integer> alignmentColumnIndexes = new ArrayList<>();
36
37   // contains all other annotations for each node. These can be the
38   // consensus(CONS), reference annotation(RF), mask value(MM) or consensus
39   // structure(CS)
40   List<HashMap<String, Character>> annotations = new ArrayList<>();
41
42   // contains the match emission for each symbol at each node
43   List<List<Double>> matchEmissions = new ArrayList<>();
44
45   // contains the insert emission for each symbol at each node
46   List<List<Double>> insertEmissions = new ArrayList<>();
47
48   // contains the state transition for each state transition. See
49   // beginStateTransitions field for transition possibilities.
50   List<List<Double>> stateTransitions = new ArrayList<>();
51
52   // contains cutoffs and thresholds from PFAM
53   Map<String, Double[]> pfamData = new HashMap<>();
54
55   // contains e-value statistic objects which contain the alignment mode
56   // configuration, and the slope and location of each distribution
57   Map<String, EValueStatistic> eValueStatistics = new HashMap<>();
58
59   final String yes = "yes";
60
61   final String no = "no";
62
63   List<Character> symbols = new ArrayList<>();
64
65   public List<Double> getBeginStateTransitions()
66   {
67     return beginStateTransitions;
68   }
69
70   public void setBeginStateTransitions(List<Double> beginStateTransitionsL)
71   {
72     this.beginStateTransitions = beginStateTransitionsL;
73   }
74
75   public List<List<Double>> getStateTransitions()
76   {
77     return stateTransitions;
78   }
79
80   public void setStateTransitions(List<List<Double>> stateTransitionsL)
81   {
82     this.stateTransitions = stateTransitionsL;
83   }
84
85   public List<Character> getSymbols()
86   {
87     return symbols;
88   }
89
90   public void setSymbols(List<Character> symbolsL)
91   {
92     this.symbols = symbolsL;
93   }
94
95   public List<Double> getAverageMatchStateEmissionProbabilities()
96   {
97     return averageMatchStateEmissionProbabilities;
98   }
99
100   public void setAverageMatchStateEmissionProbabilities(
101           List<Double> averageMatchStateEmissionProbabilitiesL)
102   {
103     this.averageMatchStateEmissionProbabilities = averageMatchStateEmissionProbabilitiesL;
104   }
105
106
107   public List<Double> getInsertZeroEmissions()
108   {
109     return insertZeroEmissions;
110   }
111
112   public void setInsertZeroEmissions(List<Double> insertZeroEmissionsL)
113   {
114     this.insertZeroEmissions = insertZeroEmissionsL;
115   }
116
117   public List<List<Double>> getMatchEmissions()
118   {
119     return matchEmissions;
120   }
121
122   public void setMatchEmissions(List<List<Double>> matchEmissionsL)
123   {
124     this.matchEmissions = matchEmissionsL;
125   }
126
127   public List<List<Double>> getInsertEmissions()
128   {
129     return insertEmissions;
130   }
131
132   public void setInsertEmissions(List<List<Double>> insertEmissionsL)
133   {
134     this.insertEmissions = insertEmissionsL;
135   }
136   public void fillSymbols(String line)
137   {
138     Scanner scanner = new Scanner(line);
139     scanner.next();
140     while (scanner.hasNext())
141     {
142       symbols.add(scanner.next().charAt(0));
143     }
144     scanner.close();
145   }
146
147   public String getName()
148   {
149     return fileProperties.get("NAME");
150   }
151   public String getAccessionNumber()
152   {
153     return fileProperties.get("ACC");
154   }
155
156   public void setAccessionNumber(String value)
157   {
158     fileProperties.put("ACC", value);
159   }
160
161   public String getDescription()
162   {
163     return fileProperties.get("DESC");
164   }
165
166   public void setDescription(String value)
167   {
168     fileProperties.put("DESC", value);
169   }
170
171   public Integer getLength()
172   {
173     if (fileProperties.get("LENG") == null)
174     {
175       return null;
176     }
177     return Integer.parseInt(fileProperties.get("LENG"));
178   }
179
180   public void setLength(int value)
181   {
182     fileProperties.put("LENG", String.valueOf(value));
183   }
184
185   public Integer getMaxInstanceLength()
186   {
187     if (fileProperties.get("MAXL") == null)
188     {
189       return null;
190     }
191     return Integer.parseInt(fileProperties.get("MAXL"));
192   }
193
194   public void setMaxInstanceLength(int value)
195   {
196     fileProperties.put("MAXL", String.valueOf(value));
197   }
198
199   // gets type of symbol alphabet - "amino", "DNA", "RNA"
200   public String getAlphabetType()
201   {
202     return fileProperties.get("ALPH");
203   }
204
205   public void setAlphabetType(String value)
206   {
207     fileProperties.put("ALPH", value);
208   }
209
210   // returns boolean indicating whether the reference annotation character field
211   // for each match state is valid or ignored
212   public boolean getReferenceAnnotationFlag()
213   {
214     if (fileProperties.get("RF") != null)
215     {
216       if (fileProperties.get("RF").equals(yes))
217       {
218         return true;
219       }
220     }
221     return false;
222   }
223
224   public void setReferenceAnnotationFlag(boolean value)
225   {
226     if (value)
227     {
228       fileProperties.put("RF", yes);
229     }
230     else
231     {
232       fileProperties.put("RF", no);
233     }
234
235   }
236
237   // returns boolean indicating whether the model mask annotation character
238   // field
239   // for each match state is valid or ignored
240   public boolean getModelMaskedFlag()
241   {
242     if (fileProperties.get("MM") != null)
243     {
244       if (fileProperties.get("MM").equals(yes))
245       {
246         return true;
247       }
248     }
249     return false;
250   }
251
252   public void setModelMaskedFlag(boolean value)
253   {
254     if (value)
255     {
256       fileProperties.put("MM", yes);
257     }
258     else
259     {
260       fileProperties.put("MM", no);
261     }
262   }
263
264   // returns boolean indicating whether the consensus residue field
265   // for each match state is valid or ignored
266   public boolean getConsensusResidueAnnotationFlag()
267   {
268     if (fileProperties.get("CONS") != null)
269     {
270       if (fileProperties.get("CONS").equals(yes))
271       {
272         return true;
273       }
274     }
275     return false;
276   }
277
278   public void setConsensusResidueeAnnotationFlag(boolean value)
279   {
280     if (value)
281     {
282       fileProperties.put("CONS", yes);
283     }
284     else
285     {
286       fileProperties.put("CONS", no);
287     }
288   }
289
290   // returns boolean indicating whether the consensus structure character field
291   // for each match state is valid or ignored
292   public boolean getConsensusStructureAnnotationFlag()
293   {
294     if (fileProperties.get("CS") != null)
295     {
296       if (fileProperties.get("CS").equals(yes))
297       {
298         return true;
299       }
300     }
301     return false;
302   }
303
304   public void setConsensusStructureAnnotationFlag(boolean value)
305   {
306     if (value)
307     {
308       fileProperties.put("CS", yes);
309     }
310     else
311     {
312       fileProperties.put("CS", no);
313     }
314   }
315
316   // returns boolean indicating whether the model mask annotation character
317   // field
318   // for each match state is valid or ignored
319   public boolean getMapAnnotationFlag()
320   {
321     if (fileProperties.get("MAP") != null)
322     {
323       if (fileProperties.get("MAP").equals(yes))
324       {
325         return true;
326       }
327     }
328     return false;
329   }
330
331   public void setMapAnnotationFlag(boolean value)
332   {
333     if (value)
334     {
335       fileProperties.put("MAP", yes);
336     }
337     else
338     {
339       fileProperties.put("MAP", no);
340     }
341   }
342
343   // not sure whether to implement this with Date object
344   public String getDate()
345   {
346     return fileProperties.get("DATE");
347   }
348
349   public void setDate(String value)
350   {
351     fileProperties.put("DATE", value);
352   }
353
354   // not sure whether to implement this
355   public String getCommandLineLog()
356   {
357     return fileProperties.get("COM");
358   }
359
360   public void setCommandLineLog(String value)
361   {
362     fileProperties.put("COM", value);
363   }
364
365   // gets the number of sequences that the HMM was trained on
366   public Integer getSequenceNumber()
367   {
368     if (fileProperties.get("NSEQ") == null)
369     {
370       return null;
371     }
372     return Integer.parseInt(fileProperties.get("NSEQ"));
373   }
374
375   public void setSequenceNumber(int value)
376   {
377     fileProperties.put("NSEQ", String.valueOf(value));
378   }
379
380   // gets the effective number determined during sequence weighting
381   public Double getEffectiveSequenceNumber()
382   {
383     if (fileProperties.get("LENG") == null)
384     {
385       return null;
386     }
387     return Double.parseDouble(fileProperties.get("EFFN"));
388   }
389
390   public void setEffectiveSequenceNumber(double value)
391   {
392     fileProperties.put("EFFN", String.valueOf(value));
393   }
394
395   public Long getCheckSum()
396   {
397     if (fileProperties.get("LENG") == null)
398     {
399       return null;
400     }
401     return Long.parseLong(fileProperties.get("CKSUM"));
402   }
403
404   public void setCheckSum(long value)
405   {
406     fileProperties.put("CKSUM", String.valueOf(value));
407   }
408
409   public Double getGatheringThreshold1()
410   {
411     try
412     {
413     return pfamData.get("GA")[0];
414     } catch (NullPointerException e)
415     {
416       return null;
417     }
418   }
419
420   public void setPFAMData(String key, Double[] data)
421   {
422     pfamData.put(key, data);
423   }
424
425   public Double getGatheringThreshold2()
426   {
427     try
428     {
429       return pfamData.get("GA")[1];
430     } catch (NullPointerException e)
431     {
432       return null;
433     }
434
435   }
436
437   public Double getTrustedCutoff1()
438   {
439     try
440     {
441       return pfamData.get("TC")[0];
442     } catch (NullPointerException e)
443     {
444       return null;
445     }
446
447   }
448
449   public Double getTrustedCutoff2()
450   {
451     try
452     {
453       return pfamData.get("TC")[1];
454     } catch (NullPointerException e)
455     {
456       return null;
457     }
458
459   }
460
461   public Double getNoiseCutoff1()
462   {
463     try
464     {
465       return pfamData.get("NC")[0];
466     } catch (NullPointerException e)
467     {
468       return null;
469     }
470
471   }
472
473   public Double getNoiseCutoff2()
474   {
475     try
476     {
477       return pfamData.get("NC")[1];
478     } catch (NullPointerException e)
479     {
480       return null;
481     }
482
483   }
484
485   public String getAlignmentModeConfiguration(String key)
486   {
487     return eValueStatistics.get(key).alignmentModeConfiguration;
488   }
489
490   public Double getSlopeOfDistribution(String scoreDistribution)
491   {
492     try
493     {
494     return eValueStatistics.get(scoreDistribution).slopeOfDistribution;
495     } catch (NullPointerException e)
496     {
497       return null;
498     }
499   }
500
501   public Double getLocationOfDistribution(String scoreDistribution)
502   {
503     try
504     {
505       return eValueStatistics.get(scoreDistribution).locationOfDistribution;
506     } catch (NullPointerException e)
507     {
508       return null;
509     }
510   }
511
512   public void addStatistic(String name, EValueStatistic stats)
513   {
514     eValueStatistics.put(name, stats);
515   }
516
517   /**
518    * public double getBeginStateTransitions(Character symbol) { return
519    * beginStateTransitions.get(symbol); }
520    **/
521
522   public void put(String key, String value)
523   {
524     fileProperties.put(key, value);
525   }
526
527   public Map<String, EValueStatistic> getEValueStatistics()
528   {
529     return eValueStatistics;
530   }
531
532   public void setEValueStatistics(
533           Map<String, EValueStatistic> eValueStatisticsM)
534   {
535     this.eValueStatistics = eValueStatisticsM;
536   }
537
538   public List<Integer> getAlignmentColumnIndexes()
539   {
540     return alignmentColumnIndexes;
541   }
542
543   public void setAlignmentColumnIndexes(
544           List<Integer> alignmentColumnIndexesL)
545   {
546     this.alignmentColumnIndexes = alignmentColumnIndexesL;
547   }
548
549   public List<HashMap<String, Character>> getAnnotations()
550   {
551     return annotations;
552   }
553
554   public void setAnnotations(List<HashMap<String, Character>> annotationsL)
555   {
556     this.annotations = annotationsL;
557   }
558
559   public Map<String, String> getFileProperties()
560   {
561     return fileProperties;
562   }
563
564   public void setFileProperties(Map<String, String> fileProperties)
565   {
566     this.fileProperties = fileProperties;
567   }
568 }
569