JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / analysis / Dna.java
index f3088ea..5dcf212 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
@@ -56,19 +57,23 @@ public class Dna
    * 'final' variables describe the inputs to the translation, which should not
    * be modified.
    */
-  final private List<SequenceI> selection;
+  private final List<SequenceI> selection;
 
-  final private String[] seqstring;
+  private final String[] seqstring;
 
-  final private int[] contigs;
+  private final Iterator<int[]> contigs;
 
-  final private char gapChar;
+  private final char gapChar;
 
-  final private AlignmentAnnotation[] annotations;
+  private final AlignmentAnnotation[] annotations;
 
-  final private int dnaWidth;
+  private final int dnaWidth;
 
-  final private AlignmentI dataset;
+  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, 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;
   }
 
   /**
@@ -119,7 +163,8 @@ public class Dna
    * @param ac2
    * @return
    */
-  public static final int compareCodonPos(AlignedCodon ac1, AlignedCodon ac2)
+  public static final int compareCodonPos(AlignedCodon ac1,
+          AlignedCodon ac2)
   {
     return comparator.compare(ac1, ac2);
     // return jalview_2_8_2compare(ac1, ac2);
@@ -150,10 +195,11 @@ public class Dna
   }
 
   /**
+   * Translates cDNA using the specified code table
    * 
    * @return
    */
-  public AlignmentI translateCdna()
+  public AlignmentI translateCdna(GeneticCodeI codeTable)
   {
     AlignedCodonFrame acf = new AlignedCodonFrame();
 
@@ -161,11 +207,11 @@ public class Dna
 
     int s;
     int sSize = selection.size();
-    List<SequenceI> pepseqs = new ArrayList<SequenceI>();
+    List<SequenceI> pepseqs = new ArrayList<>();
     for (s = 0; s < sSize; s++)
     {
       SequenceI newseq = translateCodingRegion(selection.get(s),
-              seqstring[s], acf, pepseqs);
+              seqstring[s], acf, pepseqs, codeTable);
 
       if (newseq != null)
       {
@@ -208,29 +254,30 @@ public class Dna
     for (int gd = 0; gd < selection.length; gd++)
     {
       SequenceI dna = selection[gd];
-      DBRefEntry[] dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
+      List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
               jalview.datamodel.DBRefSource.DNACODINGDBS);
       if (dnarefs != null)
       {
         // intersect with pep
-        List<DBRefEntry> mappedrefs = new ArrayList<DBRefEntry>();
-        DBRefEntry[] refs = dna.getDBRefs();
-        for (int d = 0; d < refs.length; d++)
+        List<DBRefEntry> mappedrefs = new ArrayList<>();
+        List<DBRefEntry> refs = dna.getDBRefs();
+        for (int d = 0, nd = refs.size(); d < nd; d++)
         {
-          if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
-                  && refs[d].getMap().getMap().getFromRatio() == 3
-                  && refs[d].getMap().getMap().getToRatio() == 1)
+          DBRefEntry ref = refs.get(d);
+          if (ref.getMap() != null && ref.getMap().getMap() != null
+                  && ref.getMap().getMap().getFromRatio() == 3
+                  && ref.getMap().getMap().getToRatio() == 1)
           {
-            mappedrefs.add(refs[d]); // add translated protein maps
+            mappedrefs.add(ref); // add translated protein maps
           }
         }
-        dnarefs = mappedrefs.toArray(new DBRefEntry[mappedrefs.size()]);
-        for (int d = 0; d < dnarefs.length; d++)
+        dnarefs = mappedrefs;// .toArray(new DBRefEntry[mappedrefs.size()]);
+        for (int d = 0, nd = dnarefs.size(); d < nd; d++)
         {
-          Mapping mp = dnarefs[d].getMap();
+          Mapping mp = dnarefs.get(d).getMap();
           if (mp != null)
           {
-            for (int vc = 0; vc < viscontigs.length; vc += 2)
+            for (int vc = 0, nv = viscontigs.length; vc < nv; vc += 2)
             {
               int[] mpr = mp.locateMappedRange(viscontigs[vc],
                       viscontigs[vc + 1]);
@@ -385,33 +432,21 @@ public class Dna
    * @param acf
    *          Definition of global ORF alignment reference frame
    * @param proteinSeqs
+   * @param codeTable
    * @return sequence ready to be added to alignment.
    */
   protected SequenceI translateCodingRegion(SequenceI selection,
           String seqstring, AlignedCodonFrame acf,
-          List<SequenceI> proteinSeqs)
+          List<SequenceI> proteinSeqs, GeneticCodeI codeTable)
   {
-    List<int[]> skip = new ArrayList<int[]>();
-    int skipint[] = null;
-    ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
-    // intervals
-    int vc;
-    int[] scontigs = new int[contigs.length];
+    List<int[]> skip = new ArrayList<>();
+    int[] skipint = null;
+
     int npos = 0;
-    for (vc = 0; vc < contigs.length; vc += 2)
-    {
-      if (vc == 0)
-      {
-        vismapping.addShift(npos, contigs[vc]);
-      }
-      else
-      {
-        // hidden region
-        vismapping.addShift(npos, contigs[vc] - contigs[vc - 1] + 1);
-      }
-      scontigs[vc] = contigs[vc];
-      scontigs[vc + 1] = contigs[vc + 1];
-    }
+    int vc = 0;
+
+    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);
@@ -437,7 +472,7 @@ public class Dna
          */
         AlignedCodon alignedCodon = new AlignedCodon(cdp[0], cdp[1],
                 cdp[2]);
-        String aa = ResidueProperties.codonTranslate(new String(codon));
+        String aa = codeTable.translate(new String(codon));
         rf = 0;
         final String gapString = String.valueOf(gapChar);
         if (aa == null)
@@ -544,7 +579,7 @@ public class Dna
             skip.add(skipint);
             skipint = null;
           }
-          if (aa.equals("STOP"))
+          if (aa.equals(ResidueProperties.STOP))
           {
             aa = STOP_ASTERIX;
           }
@@ -765,28 +800,29 @@ public class Dna
   private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
           MapList map)
   {
-    DBRefEntry[] dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
-            DBRefSource.DNACODINGDBS);
-    if (dnarefs != null)
-    {
-      // intersect with pep
-      for (int d = 0; d < dnarefs.length; d++)
-      {
-        Mapping mp = dnarefs[d].getMap();
-        if (mp != null)
-        {
-        }
-      }
-    }
+    // BH 2019.01.25 nop?
+    // List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
+    // DBRefSource.DNACODINGDBS);
+    // if (dnarefs != null)
+    // {
+    // // intersect with pep
+    // for (int d = 0, nd = dnarefs.size(); d < nd; d++)
+    // {
+    // Mapping mp = dnarefs.get(d).getMap();
+    // if (mp != null)
+    // {
+    // }
+    // }
+    // }
     for (SequenceFeature sf : dna.getFeatures().getAllFeatures())
     {
-        if (FeatureProperties.isCodingFeature(null, sf.getType()))
+      if (FeatureProperties.isCodingFeature(null, sf.getType()))
+      {
+        // if (map.intersectsFrom(sf[f].begin, sf[f].end))
         {
-          // if (map.intersectsFrom(sf[f].begin, sf[f].end))
-          {
 
-          }
         }
+      }
     }
   }
 
@@ -800,7 +836,7 @@ public class Dna
   public AlignmentI reverseCdna(boolean complement)
   {
     int sSize = selection.size();
-    List<SequenceI> reversed = new ArrayList<SequenceI>();
+    List<SequenceI> reversed = new ArrayList<>();
     for (int s = 0; s < sSize; s++)
     {
       SequenceI newseq = reverseSequence(selection.get(s).getName(),