Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.analysis.scoremodels

File ScoreModels.java

 

Coverage histogram

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

Code metrics

6
24
9
1
162
76
13
0.54
2.67
9
1.44

Classes

Class Line # Actions
ScoreModels 36 24 13 4
0.897435989.7%
 

Contributing tests

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