Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

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

Code metrics

10
31
11
1
200
95
17
0.55
2.82
11
1.55

Classes

Class Line # Actions
ScoreModels 36 31 17
0.923076992.3%
 

Contributing tests

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