Can edit annotation name/description
[jalview.git] / src / jalview / analysis / NJTree.java
index 4cd123f..b882ba7 100755 (executable)
@@ -23,6 +23,7 @@ import jalview.datamodel.*;
 import jalview.io.NewickFile;
 
 import jalview.schemes.ResidueProperties;
+import jalview.schemes.ScoreMatrix;
 
 import jalview.util.*;
 
@@ -150,8 +151,8 @@ public class NJTree
                 j.setElement(nam);
                 if (one2many.contains(nam)) {
                   countOne2Many++;
-                  if (jalview.bin.Cache.log.isDebugEnabled())
-                    jalview.bin.Cache.log.debug("One 2 many relationship for "+nam.getName());
+                //  if (jalview.bin.Cache.log.isDebugEnabled())
+                //    jalview.bin.Cache.log.debug("One 2 many relationship for "+nam.getName());
                 } else {
                   one2many.addElement(nam);
                   namesleft--;
@@ -163,10 +164,10 @@ public class NJTree
                 j.setPlaceholder(true);
             }
         }
-        if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
-          jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment sequence ids (out of "+one2many.size()+" unique ids) linked to two or more leaves.");
-        }
-        one2many.clear();
+      //  if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
+      //    jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment sequence ids (out of "+one2many.size()+" unique ids) linked to two or more leaves.");
+      //  }
+      //  one2many.clear();
     }
 
     /**
@@ -208,7 +209,9 @@ public class NJTree
 
         if (!(pwtype.equals("PID")))
         {
-            type = "BL";
+          if (ResidueProperties.getScoreMatrix(pwtype)==null) {
+            type = "BLOSUM62";
+          }
         }
 
         int i = 0;
@@ -284,12 +287,21 @@ public class NJTree
 
                 if (nam != null)
                 {
+                    if (!leaf.isPlaceholder()) {
+                      // remapping the node to a new sequenceI - should remove any refs to old one.
+                      // TODO - make many sequenceI to one leaf mappings possible! (JBPNote)
+                    }
                     leaf.setPlaceholder(false);
                     leaf.setElement(nam);
                 }
                 else
                 {
+                    if (!leaf.isPlaceholder()) {
+                        // Construct a new placeholder sequence object for this leaf
+                        leaf.setElement(new Sequence(leaf.getName(), "THISISAPLACEHLDER"));
+                    }
                     leaf.setPlaceholder(true);
+                    
                 }
             }
         }
@@ -656,49 +668,56 @@ public class NJTree
                 }
             }
         }
-        else if (pwtype.equals("BL"))
-        {
-            int maxscore = 0;
-            int end = sequenceString[0].length();
-            for (int i = 0; i < (noseqs - 1); i++)
+        else {
+          // Pairwise substitution score (with no gap penalties)
+          ScoreMatrix pwmatrix=ResidueProperties.getScoreMatrix(pwtype);
+          if (pwmatrix==null) {
+            pwmatrix=ResidueProperties.getScoreMatrix("BLOSUM62");
+          }
+          int maxscore = 0;
+          int end = sequenceString[0].length();
+          for (int i = 0; i < (noseqs - 1); i++)
+          {
+            for (int j = i; j < noseqs; j++)
             {
-                for (int j = i; j < noseqs; j++)
+              int score = 0;
+              
+              for (int k = 0; k < end; k++)
+              {
+                try
                 {
-                    int score = 0;
-
-                    for (int k = 0; k < end; k++)
-                    {
-                        try
-                        {
-                            score += ResidueProperties.getBLOSUM62(
-                              sequenceString[i].substring(k, k + 1),
-                              sequenceString[j].substring(k, k + 1));
-                        }
-                        catch (Exception ex)
-                        {
-                            System.err.println("err creating BLOSUM62 tree");
-                            ex.printStackTrace();
-                        }
-                    }
-
-                    distance[i][j] = (float) score;
-
-                    if (score > maxscore)
-                    {
-                        maxscore = score;
-                    }
+                  score += pwmatrix.getPairwiseScore(sequenceString[i].charAt(k),
+                      sequenceString[j].charAt(k));
                 }
-            }
-
-            for (int i = 0; i < (noseqs - 1); i++)
-            {
-                for (int j = i; j < noseqs; j++)
+                catch (Exception ex)
                 {
-                    distance[i][j] = (float) maxscore - distance[i][j];
-                    distance[j][i] = distance[i][j];
+                  System.err.println("err creating BLOSUM62 tree");
+                  ex.printStackTrace();
                 }
+              }
+              
+              distance[i][j] = (float) score;
+              
+              if (score > maxscore)
+              {
+                maxscore = score;
+              }
             }
+          }
+          
+          for (int i = 0; i < (noseqs - 1); i++)
+          {
+            for (int j = i; j < noseqs; j++)
+            {
+              distance[i][j] = (float) maxscore - distance[i][j];
+              distance[j][i] = distance[i][j];
+            }
+          }
+          
         }
+        return distance;
+    
+          //   else
       /*  else if (pwtype.equals("SW"))
         {
             float max = -1;
@@ -729,8 +748,6 @@ public class NJTree
                 }
             }
         }/*/
-
-        return distance;
     }
 
     /**