Clover icon

Coverage Report

  1. Project Clover database Tue Mar 10 2026 14:58:44 GMT
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

8
30
11
1
195
93
16
0.53
2.73
11
1.45

Classes

Class Line # Actions
ScoreModels 38 30 16
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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