JAL-1619 further refactoring
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Sat, 17 Jan 2015 06:51:37 +0000 (06:51 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Sat, 17 Jan 2015 06:51:37 +0000 (06:51 +0000)
src/jalview/analysis/Dna.java

index cf6f83e..0e7d736 100644 (file)
@@ -27,6 +27,7 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.DBRefSource;
 import jalview.datamodel.FeatureProperties;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.Mapping;
@@ -152,7 +153,8 @@ public class Dna
     for (s = 0; s < sSize; s++)
     {
       SequenceI newseq = translateCodingRegion(cdnaseqs.get(s),
-              seqstring[s], viscontigs, acf, alignedCodons, gapCharacter,
+              seqstring[s], viscontigs, acf, alignedCodons, pepseqs,
+              gapCharacter,
               false);
 
       if (newseq != null)
@@ -378,6 +380,7 @@ public class Dna
    * @param acf
    *          Definition of global ORF alignment reference frame
    * @param alignedCodons
+   * @param proteinSeqs
    * @param gapCharacter
    * @param starForStop
    *          when true stop codons will translate as '*', otherwise as 'X'
@@ -385,7 +388,8 @@ public class Dna
    */
   protected static SequenceI translateCodingRegion(SequenceI selection,
           String seqstring, int[] viscontigs, AlignedCodonFrame acf,
-          int[][] alignedCodons, char gapCharacter,
+          int[][] alignedCodons, List<SequenceI> proteinSeqs,
+          char gapCharacter,
           final boolean starForStop)
   {
     List<int[]> skip = new ArrayList<int[]>();
@@ -552,13 +556,12 @@ public class Dna
           final int compareCodonPos = Dna.compareCodonPos(cdp,
                   alignedCodons[aspos]);
           // debug
-          // System.out.println(seq + "/" + aa + " codons: "
-          // + Arrays.deepToString(alignedCodons));
-          // System.out
-          // .println(("Compare " + Arrays.toString(cdp) + " at pos "
-          // + aspos + " with "
-          // + Arrays.toString(alignedCodons[aspos]) + " got " +
-          // compareCodonPos));
+          System.out.println(seq + "/" + aa + " codons: "
+                  + Arrays.deepToString(alignedCodons));
+          System.out
+                  .println(("Compare " + Arrays.toString(cdp) + " at pos "
+                          + aspos + " with "
+                          + Arrays.toString(alignedCodons[aspos]) + " got " + compareCodonPos));
           // end debug
           switch (compareCodonPos)
           {
@@ -568,7 +571,7 @@ public class Dna
              * This codon should precede the mapped positions - need to insert a
              * gap in all prior sequences.
              */
-            acf.insertAAGap(aspos, gapCharacter);
+            insertAAGap(aspos, gapCharacter, alignedCodons, proteinSeqs);
             findpos = false;
             break;
 
@@ -608,6 +611,7 @@ public class Dna
           // update maximum alignment width
           // (we can do this without calling checkCodonFrameWidth because it was
           // already done above)
+          System.out.println("aspos " + aspos + " >= " + acf.aaWidth);
           acf.setAaWidth(aspos);
         }
         // ready for next translated reading frame alignment position (if any)
@@ -699,11 +703,17 @@ public class Dna
         { 1, resSize }, 3, 1);
 
         transferCodedFeatures(selection, newseq, map, null, null);
-        SequenceI rseq = newseq.deriveSequence(); // construct a dataset
-        // sequence for our new
-        // peptide, regardless.
-        // store a mapping (this actually stores a mapping between the dataset
-        // sequences for the two sequences
+
+        /*
+         * Construct a dataset sequence for our new peptide.
+         */
+        SequenceI rseq = newseq.deriveSequence();
+
+        /*
+         * Store a mapping (between the dataset sequences for the two
+         * sequences).
+         */
+        // SIDE-EFFECT: acf stores the aligned sequence reseq; to remove!
         acf.addMap(selection, rseq, map);
         return rseq;
       }
@@ -714,6 +724,33 @@ public class Dna
   }
 
   /**
+   * Insert a gap into the aligned proteins and the codon mapping array.
+   * 
+   * @param pos
+   * @param gapCharacter
+   * @param alignedCodons
+   * @param proteinSeqs
+   */
+  protected static void insertAAGap(int pos, char gapCharacter,
+          int[][] alignedCodons, List<SequenceI> proteinSeqs)
+  {
+    System.out.println("insertAAGap " + pos + "/" + proteinSeqs.size());
+    // aaWidth++;
+    for (SequenceI seq : proteinSeqs)
+    {
+      seq.insertCharAt(pos, gapCharacter);
+    }
+
+    // if (pos < aaWidth)
+    // {
+    // aaWidth++;
+    System.arraycopy(alignedCodons, pos, alignedCodons, pos + 1,
+            alignedCodons.length - pos - 1);
+    alignedCodons[pos] = null; // clear so new codon position can be marked.
+    // }
+  }
+
+  /**
    * Check the codons array is big enough to accommodate the given position, if
    * not resize it.
    * 
@@ -760,9 +797,8 @@ public class Dna
     SequenceFeature[] sf = (dna.getDatasetSequence() != null ? dna
             .getDatasetSequence() : dna).getSequenceFeatures();
     Boolean fgstate;
-    jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
-            .selectRefs(dna.getDBRef(),
-                    jalview.datamodel.DBRefSource.DNACODINGDBS);
+    DBRefEntry[] dnarefs = DBRefUtils.selectRefs(dna.getDBRef(),
+            DBRefSource.DNACODINGDBS);
     if (dnarefs != null)
     {
       // intersect with pep