package jalview.analysis.scoremodels;
import jalview.api.analysis.ScoreModelI;
+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;
* <ul>
* <li>BLOSUM62</li>
* <li>PAM250</li>
+ * <li>SeqSpace (identity matrix)</li>
* <li>DNA</li>
* <li>Sequence Feature Similarity</li>
* <li>Percentage Identity</li>
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, ScoreModelI>();
+ 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;
}
/**
* 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
*/
}
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;
+ }
}