JAL-3253 ApplicationSingletonProvider replaces Instance
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis.scoremodels;
22
23 import jalview.api.AlignmentViewPanel;
24 import jalview.api.analysis.ScoreModelI;
25 import jalview.bin.ApplicationSingletonProvider;
26 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
27 import jalview.io.DataSourceType;
28 import jalview.io.FileParse;
29 import jalview.io.ScoreMatrixFile;
30
31 import java.io.IOException;
32 import java.util.LinkedHashMap;
33 import java.util.Map;
34
35 /**
36  * A class that can register and serve instances of ScoreModelI
37  */
38 public class ScoreModels implements ApplicationSingletonI
39 {
40   /**
41    * Answers the singleton instance of this class, with lazy initialisation
42    * (built-in score models are loaded on the first call to this method)
43    * 
44    * @return
45    */
46   public static ScoreModels getInstance()
47   {
48     return (ScoreModels) ApplicationSingletonProvider.getInstance(ScoreModels.class);
49   }
50
51   /**
52    * Private constructor to enforce use of singleton. Registers Jalview's
53    * "built-in" score models:
54    * <ul>
55    * <li>BLOSUM62</li>
56    * <li>PAM250</li>
57    * <li>PID</li>
58    * <li>DNA</li>
59    * <li>Sequence Feature Similarity</li>
60    * </ul>
61    */
62   private ScoreModels()
63   {
64     /*
65      * using LinkedHashMap keeps models ordered as added
66      */
67     models = new LinkedHashMap<>();
68     BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
69     PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
70     DNA = loadScoreMatrix("scoreModel/dna.scm");
71     registerScoreModel(new PIDModel());
72     registerScoreModel(new FeatureDistanceModel());
73   }
74
75   private final ScoreMatrix BLOSUM62;
76
77   private final ScoreMatrix PAM250;
78
79   private final ScoreMatrix DNA;
80
81   private Map<String, ScoreModelI> models;
82
83   /**
84    * Tries to load a score matrix from the given resource file, and if
85    * successful, registers it.
86    * 
87    * @param string
88    * @return
89    */
90   ScoreMatrix loadScoreMatrix(String resourcePath)
91   {
92     try
93     {
94       /*
95        * delegate parsing to ScoreMatrixFile
96        */
97       FileParse fp = new FileParse(resourcePath,
98               DataSourceType.CLASSLOADER);
99       ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
100       registerScoreModel(sm);
101       return sm;
102     } catch (IOException e)
103     {
104       System.err.println(
105               "Error reading " + resourcePath + ": " + e.getMessage());
106     }
107     return null;
108   }
109
110   /**
111    * Answers an iterable set of the registered score models. Currently these are
112    * returned in the order in which they were registered.
113    * 
114    * @return
115    */
116   public Iterable<ScoreModelI> getModels()
117   {
118     return models.values();
119   }
120
121   /**
122    * Returns an instance of a score model for the given name. If the model is of
123    * 'view dependent' type (e.g. feature similarity), instantiates a new
124    * instance configured for the given view. Otherwise returns a cached instance
125    * of the score model.
126    * 
127    * @param name
128    * @param avp
129    * @return
130    */
131   public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
132   {
133     ScoreModelI model = models.get(name);
134     return model == null ? null : model.getInstance(avp);
135   }
136
137   public void registerScoreModel(ScoreModelI sm)
138   {
139     ScoreModelI sm2 = models.get(sm.getName());
140     if (sm2 != null)
141     {
142       System.err.println("Warning: replacing score model " + sm2.getName());
143     }
144     models.put(sm.getName(), sm);
145   }
146
147   /**
148    * Resets to just the built-in score models
149    */
150   public void reset()
151   {
152     ApplicationSingletonProvider.removeInstance(this.getClass());
153   }
154
155   /**
156    * Returns the default peptide or nucleotide score model, currently BLOSUM62
157    * or DNA
158    * 
159    * @param forPeptide
160    * @return
161    */
162   public ScoreMatrix getDefaultModel(boolean forPeptide)
163   {
164     return forPeptide ? BLOSUM62 : DNA;
165   }
166
167   public ScoreMatrix getBlosum62()
168   {
169     return BLOSUM62;
170   }
171
172   public ScoreMatrix getPam250()
173   {
174     return PAM250;
175   }
176 }