Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

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

Code metrics

8
29
11
1
194
92
16
0.55
2.64
11
1.45

Classes

Class Line # Actions
ScoreModels 38 29 16
0.916666791.7%
 

Contributing tests

This file is covered by 198 tests. .

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  16316 toggle public static ScoreModels getInstance()
56    {
57  16316 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  73 toggle private ScoreModels()
73    {
74    /*
75    * using LinkedHashMap keeps models ordered as added
76    */
77  73 models = new LinkedHashMap<>();
78  73 BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm",true);
79  73 PAM250 = loadScoreMatrix("scoreModel/pam250.scm",true);
80  73 DNA = loadScoreMatrix("scoreModel/dna.scm",true);
81  73 registerScoreModel(new PIDModel());
82  73 registerScoreModel(new FeatureDistanceModel());
83  72 SECONDARYSTRUCTURE = loadScoreMatrix(
84    "scoreModel/secondarystructure.scm",false);
85  72 registerScoreModel(new SecondaryStructureDistanceModel());
86    }
87   
88    /**
89    * Tries to load a score matrix from the given resource file, and if
90    * successful, optionally registers it.
91    *
92    * @param resourcePath
93    * @param register (if true)
94    * @return loaded matrix
95    */
 
96  291 toggle ScoreMatrix loadScoreMatrix(String resourcePath, boolean register)
97    {
98  291 try
99    {
100    /*
101    * delegate parsing to ScoreMatrixFile
102    */
103  291 FileParse fp = new FileParse(resourcePath,
104    DataSourceType.CLASSLOADER);
105  291 ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
106  291 if (register) {
107  219 registerScoreModel(sm);
108    }
109  291 return sm;
110    } catch (IOException e)
111    {
112  0 jalview.bin.Console.errPrintln(
113    "Error reading " + resourcePath + ": " + e.getMessage());
114    }
115  0 return null;
116    }
117   
118    /**
119    * Answers an iterable set of the registered score models. Currently these are
120    * returned in the order in which they were registered.
121    *
122    * @return
123    */
 
124  17 toggle public Iterable<ScoreModelI> getModels()
125    {
126  17 return models.values();
127    }
128   
129    /**
130    * Returns an instance of a score model for the given name. If the model is of
131    * 'view dependent' type (e.g. feature similarity), instantiates a new
132    * instance configured for the given view. Otherwise returns a cached instance
133    * of the score model.
134    *
135    * @param name
136    * @param avp
137    * @return
138    */
 
139  18 toggle public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
140    {
141  18 ScoreModelI model = models.get(name);
142  18 return model == null ? null : model.getInstance(avp);
143    }
144   
 
145  437 toggle public void registerScoreModel(ScoreModelI sm)
146    {
147  437 ScoreModelI sm2 = models.get(sm.getName());
148  437 if (sm2 != null)
149    {
150  0 jalview.bin.Console.errPrintln(
151    "Warning: replacing score model " + sm2.getName());
152    }
153  437 models.put(sm.getName(), sm);
154    }
155   
156    /**
157    * Resets to just the built-in score models
158    */
 
159  20 toggle public void reset()
160    {
161  20 ApplicationSingletonProvider.removeInstance(this.getClass());
162    }
163   
164    /**
165    * Returns the default peptide or nucleotide score model, currently BLOSUM62
166    * or DNA
167    *
168    * @param forPeptide
169    * @return
170    */
 
171  302 toggle public ScoreMatrix getDefaultModel(boolean forPeptide)
172    {
173  302 return forPeptide ? BLOSUM62 : DNA;
174    }
175   
 
176  15707 toggle public ScoreMatrix getBlosum62()
177    {
178  15707 return BLOSUM62;
179    }
180   
 
181  242 toggle public ScoreMatrix getPam250()
182    {
183  242 return PAM250;
184    }
185   
186    /**
187    * get currently configured matrix for comparing secondary structure
188    * @return
189    */
 
190  11 toggle public ScoreMatrix getSecondaryStructureMatrix()
191    {
192  11 return SECONDARYSTRUCTURE;
193    }
194    }