1 package jalview.analysis.scoremodels;
3 import jalview.api.AlignmentViewPanel;
4 import jalview.api.analysis.ScoreModelI;
5 import jalview.api.analysis.ViewBasedAnalysisI;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileParse;
8 import jalview.io.ScoreMatrixFile;
10 import java.io.IOException;
11 import java.util.LinkedHashMap;
15 * A class that can register and serve instances of ScoreModelI
17 public class ScoreModels
19 private final ScoreMatrix BLOSUM62;
21 private final ScoreMatrix PAM250;
23 private final ScoreMatrix DNA;
25 private static ScoreModels instance = new ScoreModels();
27 private Map<String, ScoreModelI> models;
29 public static ScoreModels getInstance()
35 * Private constructor to enforce use of singleton. Registers Jalview's
36 * "built-in" score models:
42 * <li>Sequence Feature Similarity</li>
48 * using LinkedHashMap keeps models ordered as added
50 models = new LinkedHashMap<String, ScoreModelI>();
51 BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
52 PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
53 registerScoreModel(new PIDModel());
54 DNA = loadScoreMatrix("scoreModel/dna.scm");
55 registerScoreModel(new FeatureDistanceModel());
59 * Tries to load a score matrix from the given resource file, and if
60 * successful, registers it.
65 ScoreMatrix loadScoreMatrix(String resourcePath)
70 * delegate parsing to ScoreMatrixFile
72 FileParse fp = new FileParse(resourcePath, DataSourceType.CLASSLOADER);
73 ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
74 registerScoreModel(sm);
76 } catch (IOException e)
78 System.err.println("Error reading " + resourcePath + ": "
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 if (model instanceof ViewBasedAnalysisI)
112 model = model.getClass().newInstance();
113 ((ViewBasedAnalysisI) model).configureFromAlignmentView(avp);
114 } catch (IllegalAccessException | InstantiationException e)
116 System.err.println("Error creating score model " + name + ": "
124 public void registerScoreModel(ScoreModelI sm)
126 ScoreModelI sm2 = models.get(sm.getName());
129 System.err.println("Warning: replacing score model " + sm2.getName());
131 models.put(sm.getName(), sm);
135 * Returns the default peptide or nucleotide score model, currently BLOSUM62
141 public ScoreMatrix getDefaultModel(boolean forPeptide)
143 return forPeptide ? BLOSUM62 : DNA;
146 public ScoreMatrix getBlosum62()
151 public ScoreMatrix getPam250()