Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.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.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.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  0 toggle public static ScoreModels getInstance()
57    {
58  0 if (instance == null)
59    {
60  0 instance = new ScoreModels();
61    }
62  0 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  0 toggle private ScoreModels()
78    {
79    /*
80    * using LinkedHashMap keeps models ordered as added
81    */
82  0 models = new LinkedHashMap<>();
83  0 BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm",true);
84  0 PAM250 = loadScoreMatrix("scoreModel/pam250.scm",true);
85  0 DNA = loadScoreMatrix("scoreModel/dna.scm",true);
86  0 registerScoreModel(new PIDModel());
87  0 registerScoreModel(new FeatureDistanceModel());
88  0 SECONDARYSTRUCTURE = loadScoreMatrix(
89    "scoreModel/secondarystructure.scm",false);
90  0 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  0 toggle ScoreMatrix loadScoreMatrix(String resourcePath, boolean register)
103    {
104  0 try
105    {
106    /*
107    * delegate parsing to ScoreMatrixFile
108    */
109  0 FileParse fp = new FileParse(resourcePath,
110    DataSourceType.CLASSLOADER);
111  0 ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
112  0 if (register) {
113  0 registerScoreModel(sm);
114    }
115  0 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  0 toggle public Iterable<ScoreModelI> getModels()
131    {
132  0 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  0 toggle public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
146    {
147  0 ScoreModelI model = models.get(name);
148  0 return model == null ? null : model.getInstance(avp);
149    }
150   
 
151  0 toggle public void registerScoreModel(ScoreModelI sm)
152    {
153  0 ScoreModelI sm2 = models.get(sm.getName());
154  0 if (sm2 != null)
155    {
156  0 jalview.bin.Console.errPrintln(
157    "Warning: replacing score model " + sm2.getName());
158    }
159  0 models.put(sm.getName(), sm);
160    }
161   
162    /**
163    * Resets to just the built-in score models
164    */
 
165  0 toggle public void reset()
166    {
167  0 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  0 toggle public ScoreMatrix getDefaultModel(boolean forPeptide)
178    {
179  0 return forPeptide ? BLOSUM62 : DNA;
180    }
181   
 
182  0 toggle public ScoreMatrix getBlosum62()
183    {
184  0 return BLOSUM62;
185    }
186   
 
187  0 toggle public ScoreMatrix getPam250()
188    {
189  0 return PAM250;
190    }
191   
192    /**
193    * get currently configured matrix for comparing secondary structure
194    * @return
195    */
 
196  0 toggle public ScoreMatrix getSecondaryStructureMatrix()
197    {
198  0 return SECONDARYSTRUCTURE;
199    }
200    }