JAL-1632 JAL-2416 load score matrices from file, as float[][]
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
index 4fa6396..f1990c0 100644 (file)
@@ -1,8 +1,10 @@
 package jalview.analysis.scoremodels;
 
 import jalview.api.analysis.ScoreModelI;
-import jalview.schemes.ResidueProperties;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -37,18 +39,65 @@ public class ScoreModels
      * using TreeMap keeps models ordered alphabetically by name
      */
     models = new TreeMap<String, ScoreModelI>(String.CASE_INSENSITIVE_ORDER);
-    registerScoreModel(new ScoreMatrix("BLOSUM62",
-            ResidueProperties.BLOSUM62, 0));
-    registerScoreModel(new ScoreMatrix("PAM250", ResidueProperties.PAM250,
-            0));
-    registerScoreModel(new ScoreMatrix("DNA", ResidueProperties.DNA, 1));
+    loadScoreMatrix("/scoreModel/blosum62.scm");
+    loadScoreMatrix("/scoreModel/pam250.scm");
+    loadScoreMatrix("/scoreModel/dna.scm");
     registerScoreModel(new FeatureScoreModel());
     registerScoreModel(new PIDScoreModel());
   }
 
-  public Iterable<String> getModelNames()
+  /**
+   * Try to load a score matrix from the given resource file, and if successful,
+   * register it. Answers true if successful, else false. Any errors are
+   * reported on syserr but not thrown.
+   * 
+   * @param string
+   */
+  boolean loadScoreMatrix(String resourcePath)
+  {
+    URL url = this.getClass().getResource(resourcePath);
+    if (url == null)
+    {
+      System.err.println("Failed to locate " + resourcePath);
+      return false;
+    }
+    boolean success = false;
+    InputStream is = null;
+    try
+    {
+      is = url.openStream();
+      ScoreMatrix sm = ScoreMatrix.parse(is);
+      if (sm != null)
+      {
+        registerScoreModel(sm);
+        success = true;
+      }
+    } catch (IOException e)
+    {
+    } finally
+    {
+      if (is != null)
+      {
+        try
+        {
+          is.close();
+        } catch (IOException e)
+        {
+        }
+      }
+    }
+    return success;
+  }
+
+  /**
+   * Answers an iterable set of the registered score models. Currently these are
+   * ordered by name (not case sensitive).
+   * 
+   * @return
+   */
+  public Iterable<ScoreModelI> getModels()
   {
-    return models.keySet();
+    return models.values();
   }
 
   public ScoreModelI forName(String s)