JAL-2423 tweak tree panel title, add Spanish text
[jalview.git] / src / jalview / analysis / scoremodels / PIDModel.java
index 50c4a71..d537e33 100644 (file)
@@ -2,7 +2,6 @@ package jalview.analysis.scoremodels;
 
 import jalview.api.analysis.PairwiseScoreModelI;
 import jalview.api.analysis.SimilarityParamsI;
-import jalview.api.analysis.SimilarityScoreModelI;
 import jalview.datamodel.AlignmentView;
 import jalview.math.Matrix;
 import jalview.math.MatrixI;
@@ -11,14 +10,30 @@ import jalview.util.Comparison;
 /**
  * A class to provide sequence pairwise similarity based on residue identity
  */
-public class PIDModel implements SimilarityScoreModelI,
+public class PIDModel extends SimilarityScoreModel implements
         PairwiseScoreModelI
 {
+  private static final String NAME = "PID";
+
+  private String description;
+
+  /**
+   * Constructor
+   */
+  public PIDModel()
+  {
+  }
 
   @Override
   public String getName()
   {
-    return "% Identity (PID)";
+    return NAME;
+  }
+
+  @Override
+  public String getDescription()
+  {
+    return description;
   }
 
   @Override
@@ -61,12 +76,42 @@ public class PIDModel implements SimilarityScoreModelI,
     return c;
   }
 
+  /**
+   * 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).
+   */
   @Override
   public MatrixI findSimilarities(AlignmentView seqData,
           SimilarityParamsI options)
   {
-    String[] seqs = seqData.getSequenceStrings(' ');
-    return findSimilarities(seqs, options);
+    String[] seqs = seqData.getSequenceStrings(Comparison.GAP_DASH);
+
+    MatrixI result = findSimilarities(seqs, options);
+
+    result.multiply(seqData.getWidth() / 100d);
+
+    return result;
+  }
+
+  /**
+   * 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)
+   */
+  @Override
+  public MatrixI findDistances(AlignmentView seqData,
+          SimilarityParamsI options)
+  {
+    MatrixI result = super.findDistances(seqData, options);
+
+    if (seqData.getWidth() != 0)
+    {
+      result.multiply(100d / seqData.getWidth());
+    }
+
+    return result;
   }
 
   /**
@@ -103,7 +148,7 @@ public class PIDModel implements SimilarityScoreModelI,
    * @param options
    * @return
    */
-  protected static double computePID(String seq1, String seq2,
+  public static double computePID(String seq1, String seq2,
           SimilarityParamsI options)
   {
     int len1 = seq1.length();
@@ -124,7 +169,7 @@ public class PIDModel implements SimilarityScoreModelI,
         {
           break;
         }
-        if (options.includesGaps())
+        if (options.includeGaps())
         {
           divideBy++;
         }
@@ -159,7 +204,7 @@ public class PIDModel implements SimilarityScoreModelI,
          * gap-residue: include if options say so, 
          * count as match if options say so
          */
-        if (options.includesGaps())
+        if (options.includeGaps())
         {
           divideBy++;
         }