JAL-1601 Create alignment copying method in AlignmentUtils
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 17 Oct 2023 12:24:33 +0000 (14:24 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 17 Oct 2023 12:24:33 +0000 (14:24 +0200)
src/jalview/analysis/AlignmentUtils.java
src/jalview/ws2/gui/SearchServiceGuiHandler.java

index 6ac59ba..efe5228 100644 (file)
@@ -48,6 +48,7 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GeneLociI;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.IncompleteCodonException;
 import jalview.datamodel.Mapping;
 import jalview.datamodel.Sequence;
@@ -2768,4 +2769,26 @@ public class AlignmentUtils
     }
     return true;
   }
+
+  /**
+   * Creates a deep copy of an alignment along with a dataset alignment,
+   * alignment annotations, representative sequence and hidden columns.
+   */
+  public static AlignmentI deepCopyAlignment(AlignmentI alignment)
+  {
+    var alnCpy = new Alignment(alignment);
+    alnCpy.setGapCharacter(alignment.getGapCharacter());
+    alnCpy.setDataset(alignment.getDataset());
+    for (AlignmentAnnotation annotation : alignment.getAlignmentAnnotation())
+      alnCpy.addAnnotation(new AlignmentAnnotation(annotation));
+    if (alignment.hasSeqrep())
+    {
+      int idx = alignment.findIndex(alignment.getSeqrep());
+      if (idx >= 0)
+        alnCpy.setSeqrep(alnCpy.getSequenceAt(idx));
+    }
+    if (alignment.getHiddenColumns() != null)
+      alnCpy.setHiddenColumns(new HiddenColumns(alignment.getHiddenColumns()));
+    return alnCpy;
+  }
 }
index e66315f..6c7ee13 100644 (file)
@@ -4,6 +4,7 @@ import java.util.List;
 
 import javax.swing.SwingUtilities;
 
+import jalview.analysis.AlignmentUtils;
 import jalview.bin.Console;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -138,19 +139,7 @@ class SearchServiceGuiHandler implements TaskEventListener<AlignmentI>
     }
     infoPanel.showResultsNewFrame.addActionListener(evt -> {
       // copy alignment for each frame to have its own instance
-      var alnCpy = new Alignment(result);
-      alnCpy.setGapCharacter(result.getGapCharacter());
-      alnCpy.setDataset(result.getDataset());
-      for (AlignmentAnnotation annotation : result.getAlignmentAnnotation())
-        alnCpy.addAnnotation(new AlignmentAnnotation(annotation));
-      if (result.hasSeqrep())
-      {
-        int idx = result.findIndex(result.getSeqrep());
-        if (idx >= 0)
-          alnCpy.setSeqrep(alnCpy.getSequenceAt(idx));
-      }
-      if (result.getHiddenColumns() != null)
-        alnCpy.setHiddenColumns(new HiddenColumns(result.getHiddenColumns()));
+      var alnCpy = AlignmentUtils.deepCopyAlignment(result);
       displayResultsNewFrame(alnCpy);
     });
     SwingUtilities.invokeLater(infoPanel::setResultsReady);