7431b864674c93fedbf781297386038e4a332816
[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.Instance;
26 import jalview.io.DataSourceType;
27 import jalview.io.FileParse;
28 import jalview.io.ScoreMatrixFile;
29
30 import java.io.IOException;
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33
34 /**
35  * A class that can register and serve instances of ScoreModelI
36  */
37 public class ScoreModels
38 {
39   /**
40    * Answers the singleton instance of this class, with lazy initialisation
41    * (built-in score models are loaded on the first call to this method)
42    * 
43    * @return
44    */
45   public static ScoreModels getInstance()
46   {
47     Instance j = Instance.getInstance();
48     return (j.scoreModels == null ? j.scoreModels = new ScoreModels()
49             : j.scoreModels);
50   }
51
52   /**
53    * Private constructor to enforce use of singleton. Registers Jalview's
54    * "built-in" score models:
55    * <ul>
56    * <li>BLOSUM62</li>
57    * <li>PAM250</li>
58    * <li>PID</li>
59    * <li>DNA</li>
60    * <li>Sequence Feature Similarity</li>
61    * </ul>
62    */
63   private ScoreModels()
64   {
65     /*
66      * using LinkedHashMap keeps models ordered as added
67      */
68     models = new LinkedHashMap<>();
69     BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
70     PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
71     DNA = loadScoreMatrix("scoreModel/dna.scm");
72     registerScoreModel(new PIDModel());
73     registerScoreModel(new FeatureDistanceModel());
74   }
75
76   private final ScoreMatrix BLOSUM62;
77
78   private final ScoreMatrix PAM250;
79
80   private final ScoreMatrix DNA;
81
82   private Map<String, ScoreModelI> models;
83
84   /**
85    * Tries to load a score matrix from the given resource file, and if
86    * successful, registers it.
87    * 
88    * @param string
89    * @return
90    */
91   ScoreMatrix loadScoreMatrix(String resourcePath)
92   {
93     try
94     {
95       /*
96        * delegate parsing to ScoreMatrixFile
97        */
98       FileParse fp = new FileParse(resourcePath,
99               DataSourceType.CLASSLOADER);
100       ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
101       registerScoreModel(sm);
102       return sm;
103     } catch (IOException e)
104     {
105       System.err.println(
106               "Error reading " + resourcePath + ": " + e.getMessage());
107     }
108     return null;
109   }
110
111   /**
112    * Answers an iterable set of the registered score models. Currently these are
113    * returned in the order in which they were registered.
114    * 
115    * @return
116    */
117   public Iterable<ScoreModelI> getModels()
118   {
119     return models.values();
120   }
121
122   /**
123    * Returns an instance of a score model for the given name. If the model is of
124    * 'view dependent' type (e.g. feature similarity), instantiates a new
125    * instance configured for the given view. Otherwise returns a cached instance
126    * of the score model.
127    * 
128    * @param name
129    * @param avp
130    * @return
131    */
132   public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
133   {
134     ScoreModelI model = models.get(name);
135     return model == null ? null : model.getInstance(avp);
136   }
137
138   public void registerScoreModel(ScoreModelI sm)
139   {
140     ScoreModelI sm2 = models.get(sm.getName());
141     if (sm2 != null)
142     {
143       System.err.println("Warning: replacing score model " + sm2.getName());
144     }
145     models.put(sm.getName(), sm);
146   }
147
148   /**
149    * Resets to just the built-in score models
150    */
151   public void reset()
152   {
153     Instance.getInstance().scoreModels = new ScoreModels();
154   }
155
156   /**
157    * Returns the default peptide or nucleotide score model, currently BLOSUM62
158    * or DNA
159    * 
160    * @param forPeptide
161    * @return
162    */
163   public ScoreMatrix getDefaultModel(boolean forPeptide)
164   {
165     return forPeptide ? BLOSUM62 : DNA;
166   }
167
168   public ScoreMatrix getBlosum62()
169   {
170     return BLOSUM62;
171   }
172
173   public ScoreMatrix getPam250()
174   {
175     return PAM250;
176   }
177 }