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;
29 import java.io.IOException;
30 import java.util.LinkedHashMap;
34 * A class that can register and serve instances of ScoreModelI
36 public class ScoreModels
38 private final ScoreMatrix BLOSUM62;
40 private final ScoreMatrix PAM250;
42 private final ScoreMatrix DNA;
44 private static ScoreModels instance = new ScoreModels();
46 private Map<String, ScoreModelI> models;
48 public static ScoreModels getInstance()
54 * Private constructor to enforce use of singleton. Registers Jalview's
55 * "built-in" score models:
61 * <li>Sequence Feature Similarity</li>
67 * using LinkedHashMap keeps models ordered as added
69 models = new LinkedHashMap<String, ScoreModelI>();
70 BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
71 PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
72 registerScoreModel(new PIDModel());
73 DNA = loadScoreMatrix("scoreModel/dna.scm");
74 registerScoreModel(new FeatureDistanceModel());
78 * Tries to load a score matrix from the given resource file, and if
79 * successful, registers it.
84 ScoreMatrix loadScoreMatrix(String resourcePath)
89 * delegate parsing to ScoreMatrixFile
91 FileParse fp = new FileParse(resourcePath,
92 DataSourceType.CLASSLOADER);
93 ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
94 registerScoreModel(sm);
96 } catch (IOException e)
99 "Error reading " + resourcePath + ": " + e.getMessage());
105 * Answers an iterable set of the registered score models. Currently these are
106 * returned in the order in which they were registered.
110 public Iterable<ScoreModelI> getModels()
112 return models.values();
116 * Returns an instance of a score model for the given name. If the model is of
117 * 'view dependent' type (e.g. feature similarity), instantiates a new
118 * instance configured for the given view. Otherwise returns a cached instance
119 * of the score model.
125 public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
127 ScoreModelI model = models.get(name);
128 return model == null ? null : model.getInstance(avp);
131 public void registerScoreModel(ScoreModelI sm)
133 ScoreModelI sm2 = models.get(sm.getName());
136 System.err.println("Warning: replacing score model " + sm2.getName());
138 models.put(sm.getName(), sm);
142 * Returns the default peptide or nucleotide score model, currently BLOSUM62
148 public ScoreMatrix getDefaultModel(boolean forPeptide)
150 return forPeptide ? BLOSUM62 : DNA;
153 public ScoreMatrix getBlosum62()
158 public ScoreMatrix getPam250()