1 package jalview.analysis.scoremodels;
3 import jalview.api.AlignmentViewPanel;
4 import jalview.api.analysis.ScoreModelI;
5 import jalview.io.DataSourceType;
6 import jalview.io.FileParse;
7 import jalview.io.ScoreMatrixFile;
9 import java.io.IOException;
10 import java.util.LinkedHashMap;
14 * A class that can register and serve instances of ScoreModelI
16 public class ScoreModels
18 private final ScoreMatrix BLOSUM62;
20 private final ScoreMatrix PAM250;
22 private final ScoreMatrix DNA;
24 private static ScoreModels instance = new ScoreModels();
26 private Map<String, ScoreModelI> models;
28 public static ScoreModels getInstance()
34 * Private constructor to enforce use of singleton. Registers Jalview's
35 * "built-in" score models:
41 * <li>Sequence Feature Similarity</li>
47 * using LinkedHashMap keeps models ordered as added
49 models = new LinkedHashMap<String, ScoreModelI>();
50 BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
51 PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
52 registerScoreModel(new PIDModel());
53 DNA = loadScoreMatrix("scoreModel/dna.scm");
54 registerScoreModel(new FeatureDistanceModel());
58 * Tries to load a score matrix from the given resource file, and if
59 * successful, registers it.
64 ScoreMatrix loadScoreMatrix(String resourcePath)
69 * delegate parsing to ScoreMatrixFile
71 FileParse fp = new FileParse(resourcePath,
72 DataSourceType.CLASSLOADER);
73 ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
74 registerScoreModel(sm);
76 } catch (IOException e)
79 "Error reading " + resourcePath + ": " + e.getMessage());
85 * Answers an iterable set of the registered score models. Currently these are
86 * returned in the order in which they were registered.
90 public Iterable<ScoreModelI> getModels()
92 return models.values();
96 * Returns an instance of a score model for the given name. If the model is of
97 * 'view dependent' type (e.g. feature similarity), instantiates a new
98 * instance configured for the given view. Otherwise returns a cached instance
105 public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
107 ScoreModelI model = models.get(name);
108 return model == null ? null : model.getInstance(avp);
111 public void registerScoreModel(ScoreModelI sm)
113 ScoreModelI sm2 = models.get(sm.getName());
116 System.err.println("Warning: replacing score model " + sm2.getName());
118 models.put(sm.getName(), sm);
122 * Returns the default peptide or nucleotide score model, currently BLOSUM62
128 public ScoreMatrix getDefaultModel(boolean forPeptide)
130 return forPeptide ? BLOSUM62 : DNA;
133 public ScoreMatrix getBlosum62()
138 public ScoreMatrix getPam250()