JAL-2499 JAL-2403 push 'configure for view' inside ScoreModels, defer
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
1 package jalview.analysis.scoremodels;
2
3 import jalview.api.AlignmentViewPanel;
4 import jalview.api.analysis.ScoreModelI;
5 import jalview.api.analysis.ViewBasedAnalysisI;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileParse;
8 import jalview.io.ScoreMatrixFile;
9
10 import java.io.IOException;
11 import java.util.LinkedHashMap;
12 import java.util.Map;
13
14 /**
15  * A class that can register and serve instances of ScoreModelI
16  */
17 public class ScoreModels
18 {
19   private final ScoreMatrix BLOSUM62;
20
21   private final ScoreMatrix PAM250;
22
23   private final ScoreMatrix DNA;
24
25   private static ScoreModels instance = new ScoreModels();
26
27   private Map<String, ScoreModelI> models;
28
29   public static ScoreModels getInstance()
30   {
31     return instance;
32   }
33
34   /**
35    * Private constructor to enforce use of singleton. Registers Jalview's
36    * "built-in" score models:
37    * <ul>
38    * <li>BLOSUM62</li>
39    * <li>PAM250</li>
40    * <li>PID</li>
41    * <li>DNA</li>
42    * <li>Sequence Feature Similarity</li>
43    * </ul>
44    */
45   private ScoreModels()
46   {
47     /*
48      * using LinkedHashMap keeps models ordered as added
49      */
50     models = new LinkedHashMap<String, ScoreModelI>();
51     BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
52     PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
53     registerScoreModel(new PIDModel());
54     DNA = loadScoreMatrix("scoreModel/dna.scm");
55     registerScoreModel(new FeatureDistanceModel());
56   }
57
58   /**
59    * Tries to load a score matrix from the given resource file, and if
60    * successful, registers it.
61    * 
62    * @param string
63    * @return
64    */
65   ScoreMatrix loadScoreMatrix(String resourcePath)
66   {
67     try
68     {
69       /*
70        * delegate parsing to ScoreMatrixFile
71        */
72       FileParse fp = new FileParse(resourcePath, DataSourceType.CLASSLOADER);
73       ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
74       registerScoreModel(sm);
75       return sm;
76     } catch (IOException e)
77     {
78       System.err.println("Error reading " + resourcePath + ": "
79               + e.getMessage());
80     }
81     return null;
82   }
83
84   /**
85    * Answers an iterable set of the registered score models. Currently these are
86    * returned in the order in which they were registered.
87    * 
88    * @return
89    */
90   public Iterable<ScoreModelI> getModels()
91   {
92     return models.values();
93   }
94
95   /**
96    * Returns an instance of a score model for the given name. If the model is of
97    * 'view dependent' type (e.g. feature similarity), instantiates a new
98    * instance configured for the given view. Otherwise returns a cached instance
99    * of the score model.
100    * 
101    * @param name
102    * @param avp
103    * @return
104    */
105   public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
106   {
107     ScoreModelI model = models.get(name);
108     if (model instanceof ViewBasedAnalysisI)
109     {
110       try
111       {
112         model = model.getClass().newInstance();
113         ((ViewBasedAnalysisI) model).configureFromAlignmentView(avp);
114       } catch (IllegalAccessException | InstantiationException e)
115       {
116         System.err.println("Error creating score model " + name + ": "
117                 + e.getMessage());
118         return null;
119       }
120     }
121     return model;
122   }
123
124   public void registerScoreModel(ScoreModelI sm)
125   {
126     ScoreModelI sm2 = models.get(sm.getName());
127     if (sm2 != null)
128     {
129       System.err.println("Warning: replacing score model " + sm2.getName());
130     }
131     models.put(sm.getName(), sm);
132   }
133
134   /**
135    * Returns the default peptide or nucleotide score model, currently BLOSUM62
136    * or DNA
137    * 
138    * @param forPeptide
139    * @return
140    */
141   public ScoreMatrix getDefaultModel(boolean forPeptide)
142   {
143     return forPeptide ? BLOSUM62 : DNA;
144   }
145
146   public ScoreMatrix getBlosum62()
147   {
148     return BLOSUM62;
149   }
150
151   public ScoreMatrix getPam250()
152   {
153     return PAM250;
154   }
155 }