Clover icon

Coverage Report

  1. Project Clover database Mon Sep 2 2024 17:57:51 BST
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

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

Code metrics

10
32
11
1
197
97
17
0.53
2.91
11
1.55

Classes

Class Line # Actions
ScoreModels 36 32 17
0.924528392.5%
 

Contributing tests

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