JAL-2933 pass viewport in Finder constructor, find acts on viewport selection if any
[jalview.git] / src / jalview / analysis / Dna.java
index a6fe541..2ad8487 100644 (file)
@@ -71,6 +71,10 @@ public class Dna
 
   private final AlignmentI dataset;
 
+  private ShiftList vismapping;
+
+  private int[] startcontigs;
+
   /*
    * Working variables for the translation.
    * 
@@ -101,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;
   }
 
   /**
@@ -393,38 +436,13 @@ 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[] skipint = null;
 
     int npos = 0;
-    int[] lastregion = null;
-    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;
-      vc++;
-    }
+    int vc = 0;
 
-    int[] scontigs = new int[vc];
-    vc = 0;
-    while (contigs.hasNext())
-    {
-      int[] region = contigs.next();
-      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);
@@ -557,7 +575,7 @@ public class Dna
             skip.add(skipint);
             skipint = null;
           }
-          if (aa.equals("STOP"))
+          if (aa.equals(ResidueProperties.STOP))
           {
             aa = STOP_ASTERIX;
           }
@@ -864,6 +882,23 @@ public class Dna
   }
 
   /**
+   * Answers the reverse complement of the input string
+   * 
+   * @see #getComplement(char)
+   * @param s
+   * @return
+   */
+  public static String reverseComplement(String s)
+  {
+    StringBuilder sb = new StringBuilder(s.length());
+    for (int i = s.length() - 1; i >= 0; i--)
+    {
+      sb.append(Dna.getComplement(s.charAt(i)));
+    }
+    return sb.toString();
+  }
+
+  /**
    * Returns dna complement (preserving case) for aAcCgGtTuU. Ambiguity codes
    * are treated as on http://reverse-complement.com/. Anything else is left
    * unchanged.