JAL-722 redone additional derived alignment statistics
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 7 Mar 2019 09:11:24 +0000 (09:11 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 7 Mar 2019 09:11:24 +0000 (09:11 +0000)
src/jalview/io/AlignmentProperties.java
test/jalview/io/AlignmentPropertiesTest.java [new file with mode: 0644]

index a65b83d..69bd658 100644 (file)
@@ -83,9 +83,9 @@ public class AlignmentProperties
 
       /*
        * proportion of aligned sequence that is ungapped
-       * (note: not normalised by alignment width here)
+       * (note: normalised by alignment width here)
        */
-      float ungapped = (seqLength - gapCount) / (float) seqLength;
+      float ungapped = (seqWidth - gapCount) / (float) seqWidth;
       maxUngapped = Math.max(maxUngapped, ungapped);
       minUngapped = Math.min(minUngapped, ungapped);
     }
@@ -105,9 +105,9 @@ public class AlignmentProperties
     appendRow(sb, "Maximum (sequence length / width)",
             String.format(PCT_FORMAT, maxUngapped * 100f), html);
     appendRow(sb, "Residue density", String.format(PCT_FORMAT,
-            (totalLength - totalGaps) * 100f / totalLength), html);
+            (height * width - totalGaps) * 100f / (height * width)), html);
     appendRow(sb, "Gap density",
-            String.format(PCT_FORMAT, totalGaps * 100f / totalLength),
+            String.format(PCT_FORMAT, totalGaps * 100f / (height * width)),
             html);
     sb.append(html ? "</table>" : nl);
 
diff --git a/test/jalview/io/AlignmentPropertiesTest.java b/test/jalview/io/AlignmentPropertiesTest.java
new file mode 100644 (file)
index 0000000..a3f58ce
--- /dev/null
@@ -0,0 +1,36 @@
+package jalview.io;
+
+import static org.testng.Assert.assertEquals;
+
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+
+import org.testng.annotations.Test;
+
+public class AlignmentPropertiesTest
+{
+  @Test(groups="Functional")
+  public void testWriteProperties_asHtml()
+  {
+    SequenceI seq1 = new Sequence("Seq1", "--ABC-DEFGH-"); // 8 residues
+    SequenceI seq2 = new Sequence("Seq2", "---BC-DE-KH-"); // 6
+    SequenceI seq3 = new Sequence("Seq3", "-RABCFDE-KH-"); // 9
+    AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
+
+    AlignmentProperties ap = new AlignmentProperties(al);
+    StringBuilder sb = ap.writeProperties(true);
+    String expected = "<table border=\"1\">"
+            + "<tr><td>Sequences</td><td>3</td></tr>"
+            + "<tr><td>Alignment width</td><td>12</td></tr>"
+            + "<tr><td>Minimum Sequence Length</td><td>6</td></tr>"
+            + "<tr><td>Maximum Sequence Length</td><td>9</td></tr>"
+            + "<tr><td>Average Length</td><td>7</td></tr>"
+            + "<tr><td>Minimum (sequence length / width)</td><td>50.0%</td></tr>" // 6/12
+            + "<tr><td>Maximum (sequence length / width)</td><td>75.0%</td></tr>" // 9/12
+            + "<tr><td>Residue density</td><td>63.9%</td></tr>" // 23/36
+            + "<tr><td>Gap density</td><td>36.1%</td></tr>" + "</table>";
+    assertEquals(sb.toString(), expected);
+  }
+}