ea7b30eea7798d238bcf2ad9b80616e256605fb2
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
1 package jalview.analysis.scoremodels;
2
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;
8
9 import java.io.IOException;
10 import java.util.LinkedHashMap;
11 import java.util.Map;
12
13 /**
14  * A class that can register and serve instances of ScoreModelI
15  */
16 public class ScoreModels
17 {
18   private final ScoreMatrix BLOSUM62;
19
20   private final ScoreMatrix PAM250;
21
22   private final ScoreMatrix DNA;
23
24   private static ScoreModels instance = new ScoreModels();
25
26   private Map<String, ScoreModelI> models;
27
28   public static ScoreModels getInstance()
29   {
30     return instance;
31   }
32
33   /**
34    * Private constructor to enforce use of singleton. Registers Jalview's
35    * "built-in" score models:
36    * <ul>
37    * <li>BLOSUM62</li>
38    * <li>PAM250</li>
39    * <li>PID</li>
40    * <li>DNA</li>
41    * <li>Sequence Feature Similarity</li>
42    * </ul>
43    */
44   private ScoreModels()
45   {
46     /*
47      * using LinkedHashMap keeps models ordered as added
48      */
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());
55   }
56
57   /**
58    * Tries to load a score matrix from the given resource file, and if
59    * successful, registers it.
60    * 
61    * @param string
62    * @return
63    */
64   ScoreMatrix loadScoreMatrix(String resourcePath)
65   {
66     try
67     {
68       /*
69        * delegate parsing to ScoreMatrixFile
70        */
71       FileParse fp = new FileParse(resourcePath,
72               DataSourceType.CLASSLOADER);
73       ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
74       registerScoreModel(sm);
75       return sm;
76     } catch (IOException e)
77     {
78       System.err.println(
79               "Error reading " + resourcePath + ": " + e.getMessage());
80     }
81     return null;
82   }
83
84   /**
85    * Answers an iterable set of the registered score models. Currently these are
86    * returned in the order in which they were registered.
87    * 
88    * @return
89    */
90   public Iterable<ScoreModelI> getModels()
91   {
92     return models.values();
93   }
94
95   /**
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
99    * of the score model.
100    * 
101    * @param name
102    * @param avp
103    * @return
104    */
105   public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
106   {
107     ScoreModelI model = models.get(name);
108     return model == null ? null : model.getInstance(avp);
109   }
110
111   public void registerScoreModel(ScoreModelI sm)
112   {
113     ScoreModelI sm2 = models.get(sm.getName());
114     if (sm2 != null)
115     {
116       System.err.println("Warning: replacing score model " + sm2.getName());
117     }
118     models.put(sm.getName(), sm);
119   }
120
121   /**
122    * Returns the default peptide or nucleotide score model, currently BLOSUM62
123    * or DNA
124    * 
125    * @param forPeptide
126    * @return
127    */
128   public ScoreMatrix getDefaultModel(boolean forPeptide)
129   {
130     return forPeptide ? BLOSUM62 : DNA;
131   }
132
133   public ScoreMatrix getBlosum62()
134   {
135     return BLOSUM62;
136   }
137
138   public ScoreMatrix getPam250()
139   {
140     return PAM250;
141   }
142 }