JAL-518 better test coverage and support for case preservation features/JAL-518_justify_seqs_in_region
authorJim Procter <jprocter@dundee.ac.uk>
Mon, 13 May 2024 16:05:11 +0000 (17:05 +0100)
committerJim Procter <jprocter@dundee.ac.uk>
Mon, 13 May 2024 16:05:11 +0000 (17:05 +0100)
src/jalview/commands/JustifyLeftOrRightCommand.java
test/jalview/commands/EditCommandTest.java

index f83f754..2559662 100644 (file)
@@ -25,6 +25,7 @@ import java.util.List;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ContiguousI;
 import jalview.datamodel.SequenceI;
+import jalview.util.Comparison;
 
 public class JustifyLeftOrRightCommand extends EditCommand
 {
@@ -57,6 +58,11 @@ public class JustifyLeftOrRightCommand extends EditCommand
       {
         continue;
       }
+      char[] range = seq.getSequence(from, to+1);
+      if (range==null || range.length==0)
+      {
+        continue;
+      }
       int dsstart = seq.getDatasetSequence().getStart();
       char[] sqchar = seq.getDatasetSequence().getSequence(
               -dsstart + cont.getBegin(), -dsstart + cont.getEnd() + 1);
@@ -72,9 +78,12 @@ public class JustifyLeftOrRightCommand extends EditCommand
         alseq[gapstart + gp] = gc;
       }
 
-      for (int sqp = 0; sqp < sqchar.length; sqp++)
+      for (int sqp = 0,insp=0; sqp<alseq.length; sqp++)
       {
-        alseq[sqp + sqstart] = sqchar[sqp];
+        if (sqp < range.length && !Comparison.isGap(range[sqp]))
+        {
+          alseq[insp++ + sqstart] = range[sqp];
+        }
       }
       SequenceI[] sqa = new SequenceI[1];
       sqa[0] = seq;
@@ -82,9 +91,6 @@ public class JustifyLeftOrRightCommand extends EditCommand
       addEdit(new jalview.commands.EditCommand.Edit(
               jalview.commands.EditCommand.Action.REPLACE, sqa, from,
               to + 1, al, new String(alseq)));
-      // addEdit(new jalview.commands.EditCommand("justify",
-      // jalview.commands.EditCommand.Action.REPLACE,
-      // new String(alseq), sqa, from, to + 1, al).getEdit(0));
     }
 
     performEdit(0, null);
index fa52c79..1255c58 100644 (file)
@@ -1219,8 +1219,26 @@ public class EditCommandTest
     Alignment al = new Alignment(sqs.toArray(new SequenceI[0]));
     EditCommand lefj = new JustifyLeftOrRightCommand("Left J", true, sqs, 1, 7, al);
     String exp = "-ABcD---EF";
-    assertEquals(exp.toUpperCase(Locale.ROOT),seq1.getSequenceAsString());
+    // check without case conservation
+    assertEquals(exp.toUpperCase(Locale.ROOT),seq1.getSequenceAsString().toUpperCase(Locale.ROOT));
     // check case
     assertEquals(exp,seq1.getSequenceAsString());
+    // and other seqs
+    assertEquals("-GHiJ---",seq2.getSequenceAsString());
+    assertEquals("-MNoP---Q",seq3.getSequenceAsString());
+    lefj.undoCommand(new AlignmentI[] { al});
+    assertEquals(original3,seq3.getSequenceAsString());
+    assertEquals(original1,seq1.getSequenceAsString());
+    assertEquals(original2,seq2.getSequenceAsString());
+    
+    EditCommand righj = new JustifyLeftOrRightCommand("Right J", false, sqs, 2, 7, al);
+    assertEquals("----ABcDEF",seq1.getSequenceAsString());
+    assertEquals("-G---HiJ",seq2.getSequenceAsString());
+    assertEquals("-M---NoPQ",seq3.getSequenceAsString());
+    righj.undoCommand(new AlignmentI[] { al});
+    assertEquals(original3,seq3.getSequenceAsString());
+    assertEquals(original1,seq1.getSequenceAsString());
+    assertEquals(original2,seq2.getSequenceAsString());
+        
   }
 }