import jalview.util.Comparison;
/**
- * A class to provide sequence pairwise similarity based on residue identity
+ * A class to provide sequence pairwise similarity based on residue identity.
+ * Instances of this class are immutable and thread-safe.
*/
public class PIDModel extends SimilarityScoreModel implements
PairwiseScoreModelI
{
private static final String NAME = "PID";
- private String description;
-
/**
* Constructor
*/
return NAME;
}
+ /**
+ * Answers null for description. If a display name is needed, use getName() or
+ * an internationalized string built from the name.
+ */
@Override
public String getDescription()
{
- return description;
+ return null;
}
@Override
* Computes similarity scores based on pairwise percentage identity of
* sequences. For consistency with Jalview 2.10.1's SeqSpace mode PCA
* calculation, the percentage scores are rescaled to the width of the
- * sequences (as if counts of identical residues).
+ * sequences (as if counts of identical residues). This method is thread-safe.
*/
@Override
public MatrixI findSimilarities(AlignmentView seqData,
/**
* A distance score is computed in the usual way (by reversing the range of
* the similarity score results), and then rescaled to percentage values
- * (reversing the rescaling to count values done in findSimilarities)
+ * (reversing the rescaling to count values done in findSimilarities). This
+ * method is thread-safe.
*/
@Override
public MatrixI findDistances(AlignmentView seqData,
/**
* A class that models a substitution score matrix for any given alphabet of
- * symbols
+ * symbols. Instances of this class are immutable and thread-safe.
*/
public class ScoreMatrix extends SimilarityScoreModel implements
PairwiseScoreModelI
private float minValue;
private float maxValue;
-
+
/**
* Constructor given a name, symbol alphabet, and matrix of scores for pairs
* of symbols. The matrix should be square and of the same size as the
*/
public ScoreMatrix(String theName, char[] alphabet, float[][] values)
{
+ this(theName, null, alphabet, values);
+ }
+
+ /**
+ * Constructor given a name, description, symbol alphabet, and matrix of
+ * scores for pairs of symbols. The matrix should be square and of the same
+ * size as the alphabet, for example 20x20 for a 20 symbol alphabet.
+ *
+ * @param theName
+ * Unique, human readable name for the matrix
+ * @param theDescription
+ * descriptive display name suitable for use in menus
+ * @param alphabet
+ * the symbols to which scores apply
+ * @param values
+ * Pairwise scores indexed according to the symbol alphabet
+ */
+ public ScoreMatrix(String theName, String theDescription,
+ char[] alphabet, float[][] values)
+ {
if (alphabet.length != values.length)
{
throw new IllegalArgumentException(
this.matrix = values;
this.name = theName;
+ this.description = theDescription;
this.symbols = alphabet;
symbolIndex = buildSymbolIndex(alphabet);
* <li>product[0, 1] = F.R + K.- + L.D = -3 + -1 + -3 = -8
* <li>and so on</li>
* </ul>
+ * This method is thread-safe.
*/
@Override
public MatrixI findSimilarities(AlignmentView seqstrings,
return new String(symbols);
}
- public void setDescription(String desc)
- {
- description = desc;
- }
-
public float getMinimumScore()
{
return minValue;
package jalview.analysis.scoremodels;
+import jalview.api.AlignmentViewPanel;
import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.ViewBasedAnalysisI;
import jalview.io.DataSourceType;
import jalview.io.FileParse;
import jalview.io.ScoreMatrixFile;
return models.values();
}
- public ScoreModelI forName(String s)
+ /**
+ * Returns an instance of a score model for the given name. If the model is of
+ * 'view dependent' type (e.g. feature similarity), instantiates a new
+ * instance configured for the given view. Otherwise returns a cached instance
+ * of the score model.
+ *
+ * @param name
+ * @param avp
+ * @return
+ */
+ public ScoreModelI getScoreModel(String name, AlignmentViewPanel avp)
{
- return models.get(s);
+ ScoreModelI model = models.get(name);
+ if (model instanceof ViewBasedAnalysisI)
+ {
+ try
+ {
+ model = model.getClass().newInstance();
+ ((ViewBasedAnalysisI) model).configureFromAlignmentView(avp);
+ } catch (IllegalAccessException | InstantiationException e)
+ {
+ System.err.println("Error creating score model " + name + ": "
+ + e.getMessage());
+ return null;
+ }
+ }
+ return model;
}
public void registerScoreModel(ScoreModelI sm)
import jalview.analysis.scoremodels.ScoreModels;
import jalview.analysis.scoremodels.SimilarityParams;
import jalview.api.analysis.ScoreModelI;
-import jalview.api.analysis.ViewBasedAnalysisI;
-import jalview.bin.Cache;
import jalview.datamodel.Alignment;
import jalview.datamodel.ColumnSelection;
import jalview.datamodel.SequenceI;
/**
* Creates a new TreePanel object.
- *
- * @param av
- * DOCUMENT ME!
- * @param seqVector
- * DOCUMENT ME!
- * @param type
- * DOCUMENT ME!
- * @param pwtype
- * DOCUMENT ME!
- * @param s
- * DOCUMENT ME!
- * @param e
- * DOCUMENT ME!
*/
- public TreePanel(AlignmentPanel ap, String type, String pwtype)
+ public TreePanel(AlignmentPanel alignPanel, String type, String pwtype)
{
try
{
ex.printStackTrace();
}
- initTreePanel(ap, type, pwtype, null);
+ initTreePanel(alignPanel, type, pwtype, null);
}
/**
* Creates a new TreePanel object.
*
- * @param av
- * DOCUMENT ME!
- * @param seqVector
- * DOCUMENT ME!
- * @param newtree
- * DOCUMENT ME!
- * @param type
- * DOCUMENT ME!
- * @param pwtype
- * DOCUMENT ME!
*/
public TreePanel(AlignmentPanel ap, String type, String pwtype,
NewickFile newtree)
}
/**
- * Gets the score model for the given name. If the score model is one that
- * requires to get state data from the current view, allow it to do so
+ * Gets an instantiated score model for the given name, configured for the
+ * current view if applicable
*
- * @param sm
+ * @param modelName
* @return
*/
protected ScoreModelI configureScoreModel(String modelName)
{
- ScoreModelI sm = ScoreModels.getInstance().forName(modelName);
- if (sm instanceof ViewBasedAnalysisI)
- {
- try
- {
- sm = sm.getClass().newInstance();
- ((ViewBasedAnalysisI) sm).configureFromAlignmentView(treeCanvas.ap);
- } catch (Exception q)
- {
- Cache.log.error("Couldn't create a scoremodel instance for "
- + sm.getName());
- }
- }
+ ScoreModelI sm = ScoreModels.getInstance().getScoreModel(modelName,
+ treeCanvas.ap);
return sm;
}
import jalview.api.FeatureSettingsControllerI;
import jalview.api.SplitContainerI;
import jalview.api.ViewStyleI;
-import jalview.api.analysis.ScoreModelI;
import jalview.api.analysis.SimilarityParamsI;
import jalview.bin.Cache;
import jalview.bin.Jalview;
}
}
- /**
- * DOCUMENT ME!
- *
- * @param e
- * DOCUMENT ME!
- */
- @Override
- public void PCAMenuItem_actionPerformed(ActionEvent e)
- {
- if (((viewport.getSelectionGroup() != null)
- && (viewport.getSelectionGroup().getSize() < 4) && (viewport
- .getSelectionGroup().getSize() > 0))
- || (viewport.getAlignment().getHeight() < 4))
- {
- JvOptionPane
- .showInternalMessageDialog(
- this,
- MessageManager
- .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
- MessageManager
- .getString("label.sequence_selection_insufficient"),
- JvOptionPane.WARNING_MESSAGE);
-
- return;
- }
-
- new PCAPanel(alignPanel);
- }
-
@Override
public void autoCalculate_actionPerformed(ActionEvent e)
{
*
* @param type
* tree type (NJ or AV)
- * @param sm
- * distance or similarity score model used to compute the tree
+ * @param modelName
+ * name of score model used to compute the tree
* @param options
* parameters for the distance or similarity calculation
*/
- void newTreePanel(String type, ScoreModelI sm, SimilarityParamsI options)
+ void newTreePanel(String type, String modelName, SimilarityParamsI options)
{
String frameTitle = "";
TreePanel tp;
+ boolean onSelection = false;
if (viewport.getSelectionGroup() != null
&& viewport.getSelectionGroup().getSize() > 0)
{
return;
}
}
-
- tp = new TreePanel(alignPanel, type, sm, options);
- frameTitle = tp.getPanelTitle() + " on region";
+ onSelection = true;
}
else
{
{
return;
}
-
- tp = new TreePanel(alignPanel, type, sm, options);
- frameTitle = tp.getPanelTitle();
}
+ tp = new TreePanel(alignPanel, type, modelName, options);
+ frameTitle = tp.getPanelTitle() + (onSelection ? " on region" : "");
+
frameTitle += " from ";
if (viewport.viewName != null)
}
/**
- * Open and calculate the selected tree on 'OK'
+ * Open and calculate the selected tree or PCA on 'OK'
*/
protected void ok_actionPerformed()
{
boolean doPCA = pca.isSelected();
- ScoreModelI sm = ScoreModels.getInstance().forName(
- modelNames.getSelectedItem().toString());
+ String modelName = modelNames.getSelectedItem().toString();
SimilarityParamsI params = getSimilarityParameters(doPCA);
if (doPCA)
{
- openPcaPanel(sm, params);
+ openPcaPanel(modelName, params);
}
else
{
- openTreePanel(sm, params);
+ openTreePanel(modelName, params);
}
// closeFrame();
/**
* Open a new Tree panel on the desktop
*
- * @param sm
+ * @param modelName
* @param params
*/
- protected void openTreePanel(ScoreModelI sm, SimilarityParamsI params)
+ protected void openTreePanel(String modelName, SimilarityParamsI params)
{
String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
: TreeBuilder.AVERAGE_DISTANCE;
- af.newTreePanel(treeType, sm, params);
+ af.newTreePanel(treeType, modelName, params);
}
/**
* Open a new PCA panel on the desktop
*
- * @param sm
+ * @param modelName
* @param params
*/
- protected void openPcaPanel(ScoreModelI sm, SimilarityParamsI params)
+ protected void openPcaPanel(String modelName, SimilarityParamsI params)
{
AlignViewport viewport = af.getViewport();
if (((viewport.getSelectionGroup() != null)
JvOptionPane.WARNING_MESSAGE);
return;
}
- new PCAPanel(af.alignPanel, sm, params);
+ new PCAPanel(af.alignPanel, modelName, params);
}
/**
import jalview.analysis.scoremodels.SimilarityParams;
import jalview.api.analysis.ScoreModelI;
import jalview.api.analysis.SimilarityParamsI;
-import jalview.api.analysis.ViewBasedAnalysisI;
-import jalview.bin.Cache;
import jalview.datamodel.Alignment;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.AlignmentView;
*/
public PCAPanel(AlignmentPanel alignPanel)
{
- this(alignPanel, ScoreModels.getInstance().getDefaultModel(
- !alignPanel.av.getAlignment().isNucleotide()),
- SimilarityParams.SeqSpace);
+ this(alignPanel, ScoreModels.getInstance()
+ .getDefaultModel(!alignPanel.av.getAlignment().isNucleotide())
+ .getName(), SimilarityParams.SeqSpace);
}
/**
- * Constructor given sequence data, a similarity (or distance) score model,
- * and score calculation parameters
+ * Constructor given sequence data, a similarity (or distance) score model
+ * name, and score calculation parameters
*
* @param alignPanel
- * @param scoreModel
+ * @param modelName
* @param params
*/
- public PCAPanel(AlignmentPanel alignPanel, ScoreModelI scoreModel,
+ public PCAPanel(AlignmentPanel alignPanel, String modelName,
SimilarityParamsI params)
{
super();
seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
}
+ ScoreModelI scoreModel = ScoreModels.getInstance().getScoreModel(
+ modelName, ap);
pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
params);
PaintRefresher.Register(this, av.getSequenceSetId());
{
if (!pcaModel.getScoreModelName().equals(name))
{
- ScoreModelI sm2 = configureScoreModel(sm);
+ ScoreModelI sm2 = ScoreModels.getInstance().getScoreModel(
+ name, ap);
pcaModel.setScoreModel(sm2);
Thread worker = new Thread(PCAPanel.this);
worker.start();
top = t;
zCombobox.setSelectedIndex(2);
}
-
- /**
- * If the score model is one that requires to get state data from the current
- * view, allow it to do so
- *
- * @param sm
- * @return
- */
- protected ScoreModelI configureScoreModel(ScoreModelI sm)
- {
- if (sm instanceof ViewBasedAnalysisI)
- {
- try
- {
- sm = sm.getClass().newInstance();
- ((ViewBasedAnalysisI) sm).configureFromAlignmentView(ap);
- } catch (Exception q)
- {
- Cache.log.error("Couldn't create a scoremodel instance for "
- + sm.getName());
- }
- }
- return sm;
- }
}
import jalview.analysis.NJTree;
import jalview.analysis.TreeBuilder;
import jalview.analysis.TreeModel;
+import jalview.analysis.scoremodels.ScoreModels;
import jalview.api.analysis.ScoreModelI;
import jalview.api.analysis.SimilarityParamsI;
-import jalview.api.analysis.ViewBasedAnalysisI;
import jalview.bin.Cache;
import jalview.commands.CommandI;
import jalview.commands.OrderCommand;
{
String treeType;
- ScoreModelI scoreModel; // if tree computed
+ String scoreModelName; // if tree computed
String treeTitle; // if tree loaded
*
* @param ap
* @param type
- * @param sm
+ * @param modelName
* @param options
*/
- public TreePanel(AlignmentPanel ap, String type, ScoreModelI sm,
+ public TreePanel(AlignmentPanel ap, String type, String modelName,
SimilarityParamsI options)
{
super();
this.similarityParams = options;
- initTreePanel(ap, type, sm, null, null);
+ initTreePanel(ap, type, modelName, null, null);
// We know this tree has distances. JBPNote TODO: prolly should add this as
// a userdefined default
return treeCanvas.av;
}
- void initTreePanel(AlignmentPanel ap, String type, ScoreModelI sm,
+ void initTreePanel(AlignmentPanel ap, String type, String modelName,
NewickFile newTree, AlignmentView inputData)
{
av = ap.av;
this.treeType = type;
- this.scoreModel = sm;
+ this.scoreModelName = modelName;
treeCanvas = new TreeCanvas(this, ap, scrollPane);
scrollPane.setViewportView(treeCanvas);
/*
* short score model name (long description can be too long)
*/
- String smn = scoreModel.getName();
+ String smn = scoreModelName;
/*
* put them together as <method> Using <model>
}
/**
- * If the score model is one that requires to get state data from the current
- * view, create and configure a new instance of it
+ * Instantiate an instance of the score model, configured for the current view
+ * if applicable
*
* @return
*/
protected ScoreModelI configureScoreModel()
{
- if (scoreModel instanceof ViewBasedAnalysisI)
- {
- try
- {
- scoreModel = scoreModel.getClass().newInstance();
- ((ViewBasedAnalysisI) scoreModel)
- .configureFromAlignmentView(treeCanvas.ap);
- } catch (Exception q)
- {
- Cache.log.error("Couldn't create a scoremodel instance for "
- + scoreModel.getName());
- }
- }
- return scoreModel;
+ return ScoreModels.getInstance().getScoreModel(scoreModelName,
+ treeCanvas.ap);
}
}
}
}
- ScoreMatrix sm = new ScoreMatrix(name, alphabet, scores);
- sm.setDescription(description);
+ ScoreMatrix sm = new ScoreMatrix(name, description, alphabet, scores);
matrixName = name;
return sm;
pairwiseAlignmentMenuItem_actionPerformed(e);
}
});
- JMenuItem PCAMenuItem = new JMenuItem(
- MessageManager.getString("label.principal_component_analysis"));
- PCAMenuItem.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- PCAMenuItem_actionPerformed(e);
- }
- });
this.getContentPane().setLayout(new BorderLayout());
alignFrameMenuBar.setFont(new java.awt.Font("Verdana", 0, 11));
calculateMenu.add(calculateTree);
calculateMenu.addSeparator();
calculateMenu.add(pairwiseAlignmentMenuItem);
- // calculateMenu.add(PCAMenuItem);
calculateMenu.addSeparator();
calculateMenu.add(showTranslation);
calculateMenu.add(showReverse);
{
}
- protected void PCAMenuItem_actionPerformed(ActionEvent e)
- {
- }
-
protected void neighbourTreeMenuItem_actionPerformed(ActionEvent e)
{
}
SequenceI[] seqs;
/*
- * Score model used to calculate PCA
+ * Name of score model used to calculate PCA
*/
ScoreModelI scoreModel;
private Vector<SequencePoint> points;
- private boolean jvCalcMode = true;
-
private SimilarityParamsI similarityParams;
/**
* @param seqData
* @param sqs
* @param nuc
- * @param sm
+ * @param modelName
* @param params
*/
- public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc, ScoreModelI sm,
+ public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc,
+ ScoreModelI modelName,
SimilarityParamsI params)
{
seqstrings = seqData;
seqs = sqs;
nucleotide = nuc;
- scoreModel = sm;
+ scoreModel = modelName;
similarityParams = params;
}
return pts;
}
- public void setJvCalcMode(boolean state)
- {
- jvCalcMode = state;
- }
-
public String getScoreModelName()
{
return scoreModel == null ? "" : scoreModel.getName();
assertTrue(sm.isProtein());
assertFalse(sm.isDNA());
assertNull(sm.getDescription());
- sm.setDescription("BLOSUM62");
- assertEquals(sm.getDescription(), "BLOSUM62");
/*
* verify expected scores against ARNDCQEGHILKMFPSTWYVBZX
public void testParse_ncbiFormat() throws MalformedURLException,
IOException
{
- assertNull(ScoreModels.getInstance().forName("MyNewTest"));
+ assertNull(ScoreModels.getInstance().getScoreModel("MyNewTest", null));
String data = "ScoreMatrix MyNewTest\n" + "\tA\tB\tC\n"
+ "A\t1.0\t2.0\t3.0\n" + "B\t4.0\t5.0\t6.0\n"
parser.parse();
- ScoreMatrix sm = (ScoreMatrix) ScoreModels.getInstance().forName(
- "MyNewTest");
+ ScoreMatrix sm = (ScoreMatrix) ScoreModels.getInstance().getScoreModel(
+ "MyNewTest", null);
assertNotNull(sm);
assertEquals(sm.getName(), "MyNewTest");
assertEquals(parser.getMatrixName(), "MyNewTest");