Merge branch 'patch/VAqua_saveload_update_JAL-2988' into releases/Release_2_10_5_Branch
[jalview.git] / src / jalview / analysis / Dna.java
index c3408bd..d534c8f 100644 (file)
@@ -44,6 +44,7 @@ import jalview.util.ShiftList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.Iterator;
 import java.util.List;
 
 public class Dna
@@ -60,7 +61,7 @@ public class Dna
 
   private final String[] seqstring;
 
-  private final List<int[]> contigs;
+  private final Iterator<int[]> contigs;
 
   private final char gapChar;
 
@@ -70,6 +71,10 @@ public class Dna
 
   private final AlignmentI dataset;
 
+  private ShiftList vismapping;
+
+  private int[] startcontigs;
+
   /*
    * Working variables for the translation.
    * 
@@ -91,7 +96,7 @@ public class Dna
    * @param viewport
    * @param visibleContigs
    */
-  public Dna(AlignViewportI viewport, List<int[]> visibleContigs)
+  public Dna(AlignViewportI viewport, Iterator<int[]> visibleContigs)
   {
     this.selection = Arrays.asList(viewport.getSequenceSelection());
     this.seqstring = viewport.getViewAsString(true);
@@ -100,6 +105,45 @@ public class Dna
     this.annotations = viewport.getAlignment().getAlignmentAnnotation();
     this.dnaWidth = viewport.getAlignment().getWidth();
     this.dataset = viewport.getAlignment().getDataset();
+    initContigs();
+  }
+
+  /**
+   * Initialise contigs used as starting point for translateCodingRegion
+   */
+  private void initContigs()
+  {
+    vismapping = new ShiftList(); // map from viscontigs to seqstring
+    // intervals
+
+    int npos = 0;
+    int[] lastregion = null;
+    ArrayList<Integer> tempcontigs = new ArrayList<>();
+    while (contigs.hasNext())
+    {
+      int[] region = contigs.next();
+      if (lastregion == null)
+      {
+        vismapping.addShift(npos, region[0]);
+      }
+      else
+      {
+        // hidden region
+        vismapping.addShift(npos, region[0] - lastregion[1] + 1);
+      }
+      lastregion = region;
+      tempcontigs.add(region[0]);
+      tempcontigs.add(region[1]);
+    }
+
+    startcontigs = new int[tempcontigs.size()];
+    int i = 0;
+    for (Integer val : tempcontigs)
+    {
+      startcontigs[i] = val;
+      i++;
+    }
+    tempcontigs = null;
   }
 
   /**
@@ -392,30 +436,12 @@ public class Dna
           List<SequenceI> proteinSeqs)
   {
     List<int[]> skip = new ArrayList<>();
-    int skipint[] = null;
-    ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
-    // intervals
-    int vc = 0;
-    int[] scontigs = new int[contigs.size() * 2];
+    int[] skipint = null;
     int npos = 0;
-    int[] lastregion = null;
-    for (int[] region : contigs)
-    {
-      if (lastregion == null)
-      {
-        vismapping.addShift(npos, region[0]);
-      }
-      else
-      {
-        // hidden region
-        vismapping.addShift(npos, region[0] - lastregion[1] + 1);
-      }
-      lastregion = region;
+    int vc = 0;
 
-      scontigs[vc] = region[0];
-      scontigs[vc + 1] = region[1];
-      vc++;
-    }
+    int[] scontigs = new int[startcontigs.length];
+    System.arraycopy(startcontigs, 0, scontigs, 0, startcontigs.length);
 
     // allocate a roughly sized buffer for the protein sequence
     StringBuilder protein = new StringBuilder(seqstring.length() / 2);