JAL-2403 improved ScoreModelI hierarchy as per Kira's review suggestions
[jalview.git] / src / jalview / analysis / scoremodels / FeatureDistanceModel.java
index 9245898..f88180a 100644 (file)
@@ -22,7 +22,7 @@ package jalview.analysis.scoremodels;
 
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
-import jalview.api.analysis.DistanceScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
 import jalview.api.analysis.ViewBasedAnalysisI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.SeqCigar;
@@ -37,11 +37,22 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-public class FeatureDistanceModel implements DistanceScoreModelI,
+public class FeatureDistanceModel extends DistanceScoreModel implements
         ViewBasedAnalysisI
 {
+  private static final String NAME = "Sequence Feature Similarity";
+
+  private String description;
+
   FeatureRenderer fr;
 
+  /**
+   * Constructor
+   */
+  public FeatureDistanceModel()
+  {
+  }
+
   @Override
   public boolean configureFromAlignmentView(AlignmentViewPanel view)
 
@@ -56,16 +67,28 @@ public class FeatureDistanceModel implements DistanceScoreModelI,
    * features each sequence pair has at each column, ignore feature types they
    * have in common, and count the rest. The totals are normalised by the number
    * of columns processed.
+   * <p>
+   * The parameters argument provides settings for treatment of gap-residue
+   * aligned positions, and whether the score is over the longer or shorter of
+   * each pair of sequences
+   * 
+   * @param seqData
+   * @param params
    */
   @Override
-  public MatrixI findDistances(AlignmentView seqData)
+  public MatrixI findDistances(AlignmentView seqData,
+          SimilarityParamsI params)
   {
-    List<String> dft = fr.getDisplayedFeatureTypes();
     SeqCigar[] seqs = seqData.getSequences();
     int noseqs = seqs.length;
     int cpwidth = 0;// = seqData.getWidth();
     double[][] distances = new double[noseqs][noseqs];
-    if (dft.isEmpty())
+    List<String> dft = null;
+    if (fr != null)
+    {
+      dft = fr.getDisplayedFeatureTypes();
+    }
+    if (dft == null || dft.isEmpty())
     {
       return new Matrix(distances);
     }
@@ -84,7 +107,7 @@ public class FeatureDistanceModel implements DistanceScoreModelI,
         cpwidth++;
 
         /*
-         * first pass: record features types in column for each sequence
+         * first record feature types in this column for each sequence
          */
         Map<SeqCigar, Set<String>> sfap = findFeatureTypesAtColumn(
                 seqs, cpos);
@@ -97,9 +120,23 @@ public class FeatureDistanceModel implements DistanceScoreModelI,
         {
           for (int j = i + 1; j < noseqs; j++)
           {
-            int seqDistance = SetUtils.countDisjunction(sfap.get(seqs[i]),
-                    sfap.get(seqs[j]));
-            distances[i][j] += seqDistance;
+            SeqCigar sc1 = seqs[i];
+            SeqCigar sc2 = seqs[j];
+            Set<String> set1 = sfap.get(sc1);
+            Set<String> set2 = sfap.get(sc2);
+            boolean gap1 = set1 == null;
+            boolean gap2 = set2 == null;
+
+            /*
+             * gap-gap always scores zero
+             * residue-residue is always scored
+             * include gap-residue score if params say to do so
+             */
+            if ((!gap1 && !gap2) || params.includeGaps())
+            {
+              int seqDistance = SetUtils.countDisjunction(set1, set2);
+              distances[i][j] += seqDistance;
+            }
           }
         }
       }
@@ -123,8 +160,9 @@ public class FeatureDistanceModel implements DistanceScoreModelI,
   }
 
   /**
-   * Builds and returns a list (one per SeqCigar) of visible feature types at
-   * the given column position
+   * Builds and returns a map containing a (possibly empty) list (one per
+   * SeqCigar) of visible feature types at the given column position. The map
+   * has no entry for sequences which are gapped at the column position.
    * 
    * @param seqs
    * @param columnPosition
@@ -136,18 +174,18 @@ public class FeatureDistanceModel implements DistanceScoreModelI,
     Map<SeqCigar, Set<String>> sfap = new HashMap<SeqCigar, Set<String>>();
     for (SeqCigar seq : seqs)
     {
-      Set<String> types = new HashSet<String>();
       int spos = seq.findPosition(columnPosition);
       if (spos != -1)
       {
+        Set<String> types = new HashSet<String>();
         List<SequenceFeature> sfs = fr.findFeaturesAtRes(seq.getRefSeq(),
                 spos);
         for (SequenceFeature sf : sfs)
         {
           types.add(sf.getType());
         }
+        sfap.put(seq, types);
       }
-      sfap.put(seq, types);
     }
     return sfap;
   }
@@ -155,7 +193,13 @@ public class FeatureDistanceModel implements DistanceScoreModelI,
   @Override
   public String getName()
   {
-    return "Sequence Feature Similarity";
+    return NAME;
+  }
+
+  @Override
+  public String getDescription()
+  {
+    return description;
   }
 
   @Override