Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

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

Code metrics

8
27
10
1
180
84
15
0.56
2.7
10
1.5

Classes

Class Line # Actions
ScoreModels 36 27 15
0.911111191.1%
 

Contributing tests

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