JAL-2403 extract method refactoring of FeatureScoreModel + test
[jalview.git] / src / jalview / analysis / scoremodels / FeatureScoreModel.java
index 7d8e1fe..6da55c3 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
- * Copyright (C) 2015 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -26,9 +26,11 @@ import jalview.datamodel.AlignmentView;
 import jalview.datamodel.SeqCigar;
 import jalview.datamodel.SequenceFeature;
 
-import java.util.ArrayList;
-import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class FeatureScoreModel implements ScoreModelI, ViewBasedAnalysisI
 {
@@ -42,88 +44,66 @@ public class FeatureScoreModel implements ScoreModelI, ViewBasedAnalysisI
     return true;
   }
 
+  /**
+   * Calculates a distance measure [i][j] between each pair of sequences as the
+   * average number of features they have but do not share. That is, find the
+   * 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.
+   */
   @Override
   public float[][] findDistances(AlignmentView seqData)
   {
-    int nofeats = 0;
     List<String> dft = fr.getDisplayedFeatureTypes();
-    nofeats = dft.size();
     SeqCigar[] seqs = seqData.getSequences();
     int noseqs = seqs.length;
     int cpwidth = 0;// = seqData.getWidth();
     float[][] distance = new float[noseqs][noseqs];
-    if (nofeats == 0)
+    if (dft.isEmpty())
     {
-      for (float[] d : distance)
-      {
-        for (int i = 0; i < d.length; d[i++] = 0f)
-        {
-          ;
-        }
-      }
       return distance;
     }
+
     // need to get real position for view position
     int[] viscont = seqData.getVisibleContigs();
+
+    /*
+     * scan each column, compute and add to each distance[i, j]
+     * the number of feature types that seqi and seqj do not share
+     */
     for (int vc = 0; vc < viscont.length; vc += 2)
     {
-
       for (int cpos = viscont[vc]; cpos <= viscont[vc + 1]; cpos++)
       {
         cpwidth++;
-        // get visible features at cpos under view's display settings and
-        // compare them
-        List<Hashtable<String, SequenceFeature>> sfap = new ArrayList<Hashtable<String, SequenceFeature>>();
-        for (int i = 0; i < noseqs; i++)
-        {
-          Hashtable<String, SequenceFeature> types = new Hashtable<String, SequenceFeature>();
-          int spos = seqs[i].findPosition(cpos);
-          if (spos != -1)
-          {
-            List<SequenceFeature> sfs = fr.findFeaturesAtRes(
-                    seqs[i].getRefSeq(), spos);
-            for (SequenceFeature sf : sfs)
-            {
-              types.put(sf.getType(), sf);
-            }
-          }
-          sfap.add(types);
-        }
+
+        /*
+         * first pass: record features types in column for each sequence
+         */
+        Map<SeqCigar, Set<String>> sfap = findFeatureTypesAtColumn(
+                seqs, cpos);
+
+        /*
+         * count feature types on either i'th or j'th sequence but not both
+         * and add this 'distance' measure to the total for [i, j] for j > i
+         */
         for (int i = 0; i < (noseqs - 1); i++)
         {
-          if (cpos == 0)
-          {
-            distance[i][i] = 0f;
-          }
           for (int j = i + 1; j < noseqs; j++)
           {
-            int sfcommon = 0;
-            // compare the two lists of features...
-            Hashtable<String, SequenceFeature> fi = sfap.get(i), fk, fj = sfap
-                    .get(j);
-            if (fi.size() > fj.size())
-            {
-              fk = fj;
-            }
-            else
-            {
-              fk = fi;
-              fi = fj;
-            }
-            for (String k : fi.keySet())
-            {
-              SequenceFeature sfj = fk.get(k);
-              if (sfj != null)
-              {
-                sfcommon++;
-              }
-            }
-            distance[i][j] += (fi.size() + fk.size() - 2f * sfcommon);
-            distance[j][i] += distance[i][j];
+            int seqDistance = countUnsharedFeatureTypes(sfap.get(seqs[i]),
+                    sfap.get(seqs[j]));
+            distance[i][j] += seqDistance;
+            // distance[j][i] += distance[i][j];
           }
         }
       }
     }
+
+    /*
+     * normalise the distance scores (summed over columns) by the
+     * number of visible columns used in the calculation
+     */
     for (int i = 0; i < noseqs; i++)
     {
       for (int j = i + 1; j < noseqs; j++)
@@ -135,6 +115,62 @@ public class FeatureScoreModel implements ScoreModelI, ViewBasedAnalysisI
     return distance;
   }
 
+  /**
+   * Returns the count of values that are set1 or set2 but not in both
+   * 
+   * @param set1
+   * @param set2
+   * @return
+   */
+  protected int countUnsharedFeatureTypes(Set<String> set1, Set<String> set2)
+  {
+    int size1 = set1.size();
+    int size2 = set2.size();
+    Set<String> smallerSet = size1 < size2 ? set1 : set2;
+    Set<String> largerSet = (smallerSet == set1 ? set2 : set1);
+    int inCommon = 0;
+    for (String k : smallerSet)
+    {
+      if (largerSet.contains(k))
+      {
+        inCommon++;
+      }
+    }
+
+    int notInCommon = (size1 - inCommon) + (size2 - inCommon);
+    return notInCommon;
+  }
+
+  /**
+   * Builds and returns a list (one per SeqCigar) of visible feature types at
+   * the given column position
+   * 
+   * @param seqs
+   * @param columnPosition
+   * @return
+   */
+  protected Map<SeqCigar, Set<String>> findFeatureTypesAtColumn(
+          SeqCigar[] seqs, int columnPosition)
+  {
+    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)
+      {
+        List<SequenceFeature> sfs = fr.findFeaturesAtRes(seq.getRefSeq(),
+                spos);
+        for (SequenceFeature sf : sfs)
+        {
+          types.add(sf.getType());
+        }
+      }
+      sfap.put(seq, types);
+    }
+    return sfap;
+  }
+
   @Override
   public String getName()
   {