JAL-34 - quick hack to see how we can do alignment comparison for splitframes linking...
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.analysis.scoremodels;
22
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
29 import java.io.IOException;
30 import java.util.LinkedHashMap;
31 import java.util.Map;
32
33 /**
34  * A class that can register and serve instances of ScoreModelI
35  */
36 public class ScoreModels
37 {
38   private final ScoreMatrix BLOSUM62;
39
40   private final ScoreMatrix PAM250;
41
42   private final ScoreMatrix DNA;
43
44   private final ScoreMatrix FOLDSEEK3DI;
45
46   private static ScoreModels instance;
47
48   private Map<String, ScoreModelI> models;
49
50   /**
51    * Answers the singleton instance of this class, with lazy initialisation
52    * (built-in score models are loaded on the first call to this method)
53    * 
54    * @return
55    */
56   public static ScoreModels getInstance()
57   {
58     if (instance == null)
59     {
60       instance = new ScoreModels();
61     }
62     return instance;
63   }
64
65   /**
66    * Private constructor to enforce use of singleton. Registers Jalview's
67    * "built-in" score models:
68    * <ul>
69    * <li>BLOSUM62</li>
70    * <li>PAM250</li>
71    * <li>PID</li>
72    * <li>DNA</li>
73    * <li>Sequence Feature Similarity</li>
74    * </ul>
75    */
76   private ScoreModels()
77   {
78     /*
79      * using LinkedHashMap keeps models ordered as added
80      */
81     models = new LinkedHashMap<>();
82     BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
83     PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
84     DNA = loadScoreMatrix("scoreModel/dna.scm");
85     FOLDSEEK3DI = loadScoreMatrix("scoreModel/foldseek_mat3di.scm");
86     registerScoreModel(new PIDModel());
87     registerScoreModel(new FeatureDistanceModel());
88   }
89
90   /**
91    * Tries to load a score matrix from the given resource file, and if
92    * successful, registers it.
93    * 
94    * @param string
95    * @return
96    */
97   ScoreMatrix loadScoreMatrix(String resourcePath)
98   {
99     try
100     {
101       /*
102        * delegate parsing to ScoreMatrixFile
103        */
104       FileParse fp = new FileParse(resourcePath,
105               DataSourceType.CLASSLOADER);
106       ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
107       registerScoreModel(sm);
108       return sm;
109     } catch (IOException e)
110     {
111       jalview.bin.Console.errPrintln(
112               "Error reading " + resourcePath + ": " + e.getMessage());
113     }
114     return null;
115   }
116
117   /**
118    * Answers an iterable set of the registered score models. Currently these are
119    * returned in the order in which they were registered.
120    * 
121    * @return
122    */
123   public Iterable<ScoreModelI> getModels()
124   {
125     return models.values();
126   }
127
128   /**
129    * Returns an instance of a score model for the given name. If the model is of
130    * 'view dependent' type (e.g. feature similarity), instantiates a new
131    * instance configured for the given view. Otherwise returns a cached instance
132    * of the score model.
133    * 
134    * @param name
135    * @param avp
136    * @return
137    */
138   public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
139   {
140     ScoreModelI model = models.get(name);
141     return model == null ? null : model.getInstance(avp);
142   }
143
144   public void registerScoreModel(ScoreModelI sm)
145   {
146     ScoreModelI sm2 = models.get(sm.getName());
147     if (sm2 != null)
148     {
149       jalview.bin.Console.errPrintln(
150               "Warning: replacing score model " + sm2.getName());
151     }
152     models.put(sm.getName(), sm);
153   }
154
155   /**
156    * Resets to just the built-in score models
157    */
158   public void reset()
159   {
160     instance = new ScoreModels();
161   }
162
163   /**
164    * Returns the default peptide or nucleotide score model, currently BLOSUM62
165    * or DNA
166    * 
167    * @param forPeptide
168    * @return
169    */
170   public ScoreMatrix getDefaultModel(boolean forPeptide)
171   {
172     return forPeptide ? BLOSUM62 : DNA;
173   }
174
175   public ScoreMatrix getBlosum62()
176   {
177     return BLOSUM62;
178   }
179
180   public ScoreMatrix getPam250()
181   {
182     return PAM250;
183   }
184   public ScoreMatrix getFOLDSEEK3DI()
185   {
186     return FOLDSEEK3DI;
187   }
188   
189 }