JAL-1632 JAL-2416 load score matrices from file, as float[][]
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
1 package jalview.analysis.scoremodels;
2
3 import jalview.api.analysis.ScoreModelI;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.Map;
9 import java.util.TreeMap;
10
11 /**
12  * A class that can register and serve instances of ScoreModelI
13  */
14 public class ScoreModels
15 {
16   private static ScoreModels instance = new ScoreModels();
17
18   private Map<String, ScoreModelI> models;
19
20   public static ScoreModels getInstance()
21   {
22     return instance;
23   }
24
25   /**
26    * Private constructor to enforce use of singleton. Registers Jalview's
27    * "built-in" score models:
28    * <ul>
29    * <li>BLOSUM62</li>
30    * <li>PAM250</li>
31    * <li>DNA</li>
32    * <li>Sequence Feature Similarity</li>
33    * <li>Percentage Identity</li>
34    * </ul>
35    */
36   private ScoreModels()
37   {
38     /*
39      * using TreeMap keeps models ordered alphabetically by name
40      */
41     models = new TreeMap<String, ScoreModelI>(String.CASE_INSENSITIVE_ORDER);
42     loadScoreMatrix("/scoreModel/blosum62.scm");
43     loadScoreMatrix("/scoreModel/pam250.scm");
44     loadScoreMatrix("/scoreModel/dna.scm");
45     registerScoreModel(new FeatureScoreModel());
46     registerScoreModel(new PIDScoreModel());
47   }
48
49   /**
50    * Try to load a score matrix from the given resource file, and if successful,
51    * register it. Answers true if successful, else false. Any errors are
52    * reported on syserr but not thrown.
53    * 
54    * @param string
55    */
56   boolean loadScoreMatrix(String resourcePath)
57   {
58     URL url = this.getClass().getResource(resourcePath);
59     if (url == null)
60     {
61       System.err.println("Failed to locate " + resourcePath);
62       return false;
63     }
64     boolean success = false;
65     InputStream is = null;
66     try
67     {
68       is = url.openStream();
69       ScoreMatrix sm = ScoreMatrix.parse(is);
70       if (sm != null)
71       {
72         registerScoreModel(sm);
73         success = true;
74       }
75     } catch (IOException e)
76     {
77     } finally
78     {
79       if (is != null)
80       {
81         try
82         {
83           is.close();
84         } catch (IOException e)
85         {
86         }
87       }
88     }
89     return success;
90   }
91
92   /**
93    * Answers an iterable set of the registered score models. Currently these are
94    * ordered by name (not case sensitive).
95    * 
96    * @return
97    */
98   public Iterable<ScoreModelI> getModels()
99   {
100     return models.values();
101   }
102
103   public ScoreModelI forName(String s)
104   {
105     return models.get(s);
106   }
107
108   public void registerScoreModel(ScoreModelI sm)
109   {
110     ScoreModelI sm2 = models.get(sm.getName());
111     if (sm2 != null)
112     {
113       System.err.println("Warning: replacing score model " + sm2.getName());
114     }
115     models.put(sm.getName(), sm);
116   }
117 }