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