07840fcff6f5038499ba5734cd0f724d60697f25
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
1 package jalview.analysis.scoremodels;
2
3 import jalview.api.analysis.ScoreModelI;
4 import jalview.io.DataSourceType;
5 import jalview.io.FileParse;
6 import jalview.io.ScoreMatrixFile;
7
8 import java.io.IOException;
9 import java.util.LinkedHashMap;
10 import java.util.Map;
11
12 /**
13  * A class that can register and serve instances of ScoreModelI
14  */
15 public class ScoreModels
16 {
17   /*
18    * constants for built-in score model names
19    * NB! these have to match names in loaded score matrix files
20    */
21   public static final String BLOSUM62 = "BLOSUM62";
22
23   public static final String PAM250 = "PAM250";
24
25   public static final String DNA = "DNA";
26
27   private static ScoreModels instance = new ScoreModels();
28   private Map<String, ScoreModelI> models;
29
30   public static ScoreModels getInstance()
31   {
32     return instance;
33   }
34
35   /**
36    * Private constructor to enforce use of singleton. Registers Jalview's
37    * "built-in" score models:
38    * <ul>
39    * <li>BLOSUM62</li>
40    * <li>PAM250</li>
41    * <li>SeqSpace (identity matrix)</li>
42    * <li>DNA</li>
43    * <li>Sequence Feature Similarity</li>
44    * <li>Percentage Identity</li>
45    * </ul>
46    */
47   private ScoreModels()
48   {
49     /*
50      * using LinkedHashMap keeps models ordered as added
51      */
52     models = new LinkedHashMap<String, ScoreModelI>();
53     loadScoreMatrix("scoreModel/blosum62.scm");
54     loadScoreMatrix("scoreModel/pam250.scm");
55     loadScoreMatrix("scoreModel/seqspace.scm");
56     loadScoreMatrix("scoreModel/dna.scm");
57     registerScoreModel(new FeatureScoreModel());
58     registerScoreModel(new PIDScoreModel());
59   }
60
61   /**
62    * Try to load a score matrix from the given resource file, and if successful,
63    * register it. Answers true if successful, else false.
64    * 
65    * @param string
66    */
67   boolean loadScoreMatrix(String resourcePath)
68   {
69     try
70     {
71       /*
72        * delegate parsing to ScoreMatrixFile
73        */
74       FileParse fp = new FileParse(resourcePath, DataSourceType.CLASSLOADER);
75       ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
76       registerScoreModel(sm);
77       return true;
78     } catch (IOException e)
79     {
80       System.err.println("Error reading " + resourcePath + ": "
81               + e.getMessage());
82     }
83     return false;
84   }
85
86   /**
87    * Answers an iterable set of the registered score models. Currently these are
88    * returned in the order in which they were registered.
89    * 
90    * @return
91    */
92   public Iterable<ScoreModelI> getModels()
93   {
94     return models.values();
95   }
96
97   public ScoreModelI forName(String s)
98   {
99     return models.get(s);
100   }
101
102   public void registerScoreModel(ScoreModelI sm)
103   {
104     ScoreModelI sm2 = models.get(sm.getName());
105     if (sm2 != null)
106     {
107       System.err.println("Warning: replacing score model " + sm2.getName());
108     }
109     models.put(sm.getName(), sm);
110   }
111
112   /**
113    * Returns the default peptide or nucleotide score model, currently BLOSUM62
114    * or DNA
115    * 
116    * @param forPeptide
117    * @return
118    */
119   public PairwiseSeqScoreModel getDefaultModel(boolean forPeptide)
120   {
121     return (PairwiseSeqScoreModel) (forPeptide ? forName("BLOSUM62")
122             : forName("DNA"));
123   }
124 }