JAL-2403 ScoreModelI now DistanceModelI, ScoreMatrix delegate of
[jalview.git] / src / jalview / analysis / scoremodels / ScoreModels.java
index f1990c0..043e6fb 100644 (file)
@@ -1,21 +1,28 @@
 package jalview.analysis.scoremodels;
 
-import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.DistanceModelI;
+import jalview.io.DataSourceType;
+import jalview.io.FileParse;
+import jalview.io.ScoreMatrixFile;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
+import java.util.LinkedHashMap;
 import java.util.Map;
-import java.util.TreeMap;
 
 /**
  * A class that can register and serve instances of ScoreModelI
  */
 public class ScoreModels
 {
+  private final ScoreMatrix BLOSUM62;
+
+  private final ScoreMatrix PAM250;
+
+  private final ScoreMatrix DNA;
+
   private static ScoreModels instance = new ScoreModels();
 
-  private Map<String, ScoreModelI> models;
+  private Map<String, DistanceModelI> models;
 
   public static ScoreModels getInstance()
   {
@@ -28,6 +35,7 @@ public class ScoreModels
    * <ul>
    * <li>BLOSUM62</li>
    * <li>PAM250</li>
+   * <li>SeqSpace (identity matrix)</li>
    * <li>DNA</li>
    * <li>Sequence Feature Similarity</li>
    * <li>Percentage Identity</li>
@@ -36,82 +44,100 @@ public class ScoreModels
   private ScoreModels()
   {
     /*
-     * using TreeMap keeps models ordered alphabetically by name
+     * using LinkedHashMap keeps models ordered as added
      */
-    models = new TreeMap<String, ScoreModelI>(String.CASE_INSENSITIVE_ORDER);
-    loadScoreMatrix("/scoreModel/blosum62.scm");
-    loadScoreMatrix("/scoreModel/pam250.scm");
-    loadScoreMatrix("/scoreModel/dna.scm");
-    registerScoreModel(new FeatureScoreModel());
-    registerScoreModel(new PIDScoreModel());
+    models = new LinkedHashMap<String, DistanceModelI>();
+    BLOSUM62 = loadScoreMatrix("scoreModel/blosum62.scm");
+    PAM250 = loadScoreMatrix("scoreModel/pam250.scm");
+    loadScoreMatrix("scoreModel/seqspace.scm");
+    // drop seqspace.scm for IdentityScoreModel once JAL-2379 merged in?
+    // registerScoreModel(new IdentityScoreModel());
+    DNA = loadScoreMatrix("scoreModel/dna.scm");
+    registerScoreModel(new FeatureDistanceModel());
+    registerScoreModel(new PIDDistanceModel());
   }
 
   /**
-   * 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.
+   * Tries to load a score matrix from the given resource file, and if
+   * successful, registers it.
    * 
    * @param string
+   * @return
    */
-  boolean loadScoreMatrix(String resourcePath)
+  ScoreMatrix 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;
-      }
+      /*
+       * delegate parsing to ScoreMatrixFile
+       */
+      FileParse fp = new FileParse(resourcePath, DataSourceType.CLASSLOADER);
+      ScoreMatrix sm = new ScoreMatrixFile(fp).parseMatrix();
+      registerScoreModel(sm);
+      return sm;
     } catch (IOException e)
     {
-    } finally
-    {
-      if (is != null)
-      {
-        try
-        {
-          is.close();
-        } catch (IOException e)
-        {
-        }
-      }
+      System.err.println("Error reading " + resourcePath + ": "
+              + e.getMessage());
     }
-    return success;
+    return null;
+  }
+
+  /**
+   * Registers a pairwise score model
+   * 
+   * @param sm
+   */
+  public void registerScoreModel(PairwiseScoreModelI sm)
+  {
+    registerScoreModel(new PairwiseDistanceModel(sm));
   }
 
   /**
    * Answers an iterable set of the registered score models. Currently these are
-   * ordered by name (not case sensitive).
+   * returned in the order in which they were registered.
    * 
    * @return
    */
-  public Iterable<ScoreModelI> getModels()
+  public Iterable<DistanceModelI> getModels()
   {
     return models.values();
   }
 
-  public ScoreModelI forName(String s)
+  public DistanceModelI forName(String s)
   {
     return models.get(s);
   }
 
-  public void registerScoreModel(ScoreModelI sm)
+  public void registerScoreModel(DistanceModelI sm)
   {
-    ScoreModelI sm2 = models.get(sm.getName());
+    DistanceModelI sm2 = models.get(sm.getName());
     if (sm2 != null)
     {
       System.err.println("Warning: replacing score model " + sm2.getName());
     }
     models.put(sm.getName(), sm);
   }
+
+  /**
+   * Returns the default peptide or nucleotide score model, currently BLOSUM62
+   * or DNA
+   * 
+   * @param forPeptide
+   * @return
+   */
+  public ScoreMatrix getDefaultModel(boolean forPeptide)
+  {
+    return forPeptide ? BLOSUM62 : DNA;
+  }
+
+  public ScoreMatrix getBlosum62()
+  {
+    return BLOSUM62;
+  }
+
+  public ScoreMatrix getPam250()
+  {
+    return PAM250;
+  }
 }