Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

../../../img/srcFileCovDistChart9.png
7% of files have more coverage

Code metrics

10
33
12
1
209
101
18
0.55
2.75
12
1.5

Classes

Class Line # Actions
ScoreModels 36 33 18
0.854545585.5%
 

Contributing tests

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