JAL-1483 refactor NJTree constructor to allow preconfigured scoremodel to be passed...
authorJim Procter <jprocter@dundee.ac.uk>
Wed, 7 Jan 2015 17:56:40 +0000 (17:56 +0000)
committerJim Procter <jprocter@dundee.ac.uk>
Wed, 7 Jan 2015 17:57:00 +0000 (17:57 +0000)
src/jalview/analysis/NJTree.java
src/jalview/appletgui/TreePanel.java
src/jalview/gui/TreePanel.java

index ec5b79b..f08a436 100644 (file)
@@ -218,7 +218,7 @@ public class NJTree
    *          DOCUMENT ME!
    */
   public NJTree(SequenceI[] sequence, AlignmentView seqData, String type,
-          String pwtype, int start, int end)
+          String pwtype, ScoreModelI sm, int start, int end)
   {
     this.sequence = sequence;
     this.node = new Vector();
@@ -245,7 +245,7 @@ public class NJTree
       type = "AV";
     }
 
-    if (!(pwtype.equals("PID")))
+    if (sm == null && !(pwtype.equals("PID")))
     {
       if (ResidueProperties.getScoreMatrix(pwtype) == null)
       {
@@ -265,7 +265,7 @@ public class NJTree
 
     noseqs = i++;
 
-    distance = findDistances();
+    distance = findDistances(sm);
     // System.err.println("Made distances");// dbg
     makeLeaves();
     // System.err.println("Made leaves");// dbg
@@ -323,7 +323,7 @@ public class NJTree
 
           for (int j = 0; j < seqs.length; j++)
           {
-            seqs[j] = (SequenceI) list.get(j);
+            seqs[j] = list.get(j);
           }
 
           seqmatcher = new SequenceIdMatcher(seqs);
@@ -730,16 +730,18 @@ public class NJTree
    * 
    * @return similarity matrix used to compute tree
    */
-  public float[][] findDistances()
+  public float[][] findDistances(ScoreModelI _pwmatrix)
   {
 
     float[][] distance = new float[noseqs][noseqs];
-
-    // Pairwise substitution score (with no gap penalties)
-    ScoreModelI _pwmatrix = ResidueProperties.getScoreModel(pwtype);
     if (_pwmatrix == null)
     {
-      _pwmatrix = ResidueProperties.getScoreMatrix("BLOSUM62");
+      // Resolve substitution model
+      _pwmatrix = ResidueProperties.getScoreModel(pwtype);
+      if (_pwmatrix == null)
+      {
+        _pwmatrix = ResidueProperties.getScoreMatrix("BLOSUM62");
+      }
     }
     distance = _pwmatrix.findDistances(seqData);
     return distance;
index 2fb34b0..9530dd1 100644 (file)
 package jalview.appletgui;
 
 import jalview.analysis.NJTree;
+import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.ViewBasedAnalysisI;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.SequenceI;
 import jalview.io.NewickFile;
+import jalview.schemes.ResidueProperties;
 import jalview.util.MessageManager;
 
 import java.awt.BorderLayout;
@@ -232,8 +235,27 @@ public class TreePanel extends EmbmenuFrame implements ActionListener,
           seqs = av.getSelectionGroup().getSequencesInOrder(
                   av.getAlignment());
         }
-
-        tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
+        ScoreModelI sm = ResidueProperties.getScoreModel(pwtype);
+        if (sm instanceof ViewBasedAnalysisI)
+        {
+          try
+          {
+            sm = sm.getClass().newInstance();
+            ((ViewBasedAnalysisI) sm)
+                    .configureFromAlignmentView(treeCanvas.ap);
+          } catch (Exception q)
+          {
+            System.err.println("Couldn't create a scoremodel instance for "
+                    + sm.getName());
+            q.printStackTrace();
+          }
+          tree = new NJTree(seqs, seqStrings, type, pwtype, sm, start, end);
+        }
+        else
+        {
+          tree = new NJTree(seqs, seqStrings, type, pwtype, null, start,
+                  end);
+        }
       }
 
       tree.reCount(tree.getTopNode());
index 5bedf36..6ce68b3 100755 (executable)
@@ -313,8 +313,26 @@ public class TreePanel extends GTreePanel
           seqs = av.getSelectionGroup().getSequencesInOrder(
                   av.getAlignment());
         }
-
-        tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
+        ScoreModelI sm = ResidueProperties.getScoreModel(pwtype);
+        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());
+          }
+          tree = new NJTree(seqs, seqStrings, type, pwtype, sm, start, end);
+        }
+        else
+        {
+          tree = new NJTree(seqs, seqStrings, type, pwtype, null, start,
+                  end);
+        }
         showDistances(true);
       }