JAL-1601 Create alignment copying method in AlignmentUtils
[jalview.git] / src / jalview / analysis / AlignmentUtils.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;
+  }
 }