2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis.scoremodels;
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;
30 import java.io.IOException;
31 import java.util.LinkedHashMap;
35 * A class that can register and serve instances of ScoreModelI
37 public class ScoreModels
39 private final ScoreMatrix BLOSUM62;
41 private final ScoreMatrix PAM250;
43 private final ScoreMatrix DNA;
45 private static ScoreModels instance;
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)
53 public static ScoreModels getInstance()
55 ScoreModels m = instance;
56 @SuppressWarnings("unused")
60 g = Thread.currentThread().getThreadGroup();
62 * @j2sNative m = g._jalviewScoreModelsInstance;
68 m = new ScoreModels();
72 * @j2sNative g._jalviewScoreModelsInstance = m;
84 private Map<String, ScoreModelI> models;
87 * Private constructor to enforce use of singleton. Registers Jalview's
88 * "built-in" score models:
94 * <li>Sequence Feature Similarity</li>
100 * using LinkedHashMap keeps models ordered as added
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());
111 * Tries to load a score matrix from the given resource file, and if
112 * successful, registers it.
117 ScoreMatrix loadScoreMatrix(String resourcePath)
122 * delegate parsing to ScoreMatrixFile
124 FileParse fp = new FileParse(resourcePath,
125 DataSourceType.CLASSLOADER);
126 ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
127 registerScoreModel(sm);
129 } catch (IOException e)
132 "Error reading " + resourcePath + ": " + e.getMessage());
138 * Answers an iterable set of the registered score models. Currently these are
139 * returned in the order in which they were registered.
143 public Iterable<ScoreModelI> getModels()
145 return models.values();
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.
158 public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
160 ScoreModelI model = models.get(name);
161 return model == null ? null : model.getInstance(avp);
164 public void registerScoreModel(ScoreModelI sm)
166 ScoreModelI sm2 = models.get(sm.getName());
169 System.err.println("Warning: replacing score model " + sm2.getName());
171 models.put(sm.getName(), sm);
175 * Resets to just the built-in score models
179 instance = new ScoreModels();
183 * Returns the default peptide or nucleotide score model, currently BLOSUM62
189 public ScoreMatrix getDefaultModel(boolean forPeptide)
191 return forPeptide ? BLOSUM62 : DNA;
194 public ScoreMatrix getBlosum62()
199 public ScoreMatrix getPam250()