merge from 2_4_Release branch
[jalview.git] / src / jalview / analysis / Dna.java
index c6bcfda..8bf8962 100644 (file)
@@ -1,3 +1,21 @@
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
+ * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * \r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * \r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ */\r
 package jalview.analysis;\r
 \r
 import java.util.Enumeration;\r
@@ -66,7 +84,8 @@ public class Dna
    * @param gapCharacter\r
    * @param annotations\r
    * @param aWidth\r
-   * @param dataset destination dataset for translated sequences and mappings\r
+   * @param dataset\r
+   *                destination dataset for translated sequences and mappings\r
    * @return\r
    */\r
   public static AlignmentI CdnaTranslate(SequenceI[] selection,\r
@@ -81,7 +100,9 @@ public class Dna
    * \r
    * @param selection\r
    * @param seqstring\r
-   * @param product - array of DbRefEntry objects from which exon map in seqstring is derived\r
+   * @param product -\r
+   *                array of DbRefEntry objects from which exon map in seqstring\r
+   *                is derived\r
    * @param viscontigs\r
    * @param gapCharacter\r
    * @param annotations\r
@@ -91,7 +112,8 @@ public class Dna
    */\r
   public static AlignmentI CdnaTranslate(SequenceI[] selection,\r
           String[] seqstring, DBRefEntry[] product, int viscontigs[],\r
-          char gapCharacter, AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)\r
+          char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,\r
+          Alignment dataset)\r
   {\r
     AlignedCodonFrame codons = new AlignedCodonFrame(aWidth); // stores hash of\r
     // subsequent\r
@@ -104,12 +126,14 @@ public class Dna
     for (s = 0; s < sSize; s++)\r
     {\r
       SequenceI newseq = translateCodingRegion(selection[s], seqstring[s],\r
-              viscontigs, codons, gapCharacter, (product!=null) ? product[s] : null); // possibly anonymous product\r
+              viscontigs, codons, gapCharacter,\r
+              (product != null) ? product[s] : null); // possibly anonymous\r
+                                                      // product\r
       if (newseq != null)\r
       {\r
         pepseqs.addElement(newseq);\r
         SequenceI ds = newseq;\r
-        while (ds.getDatasetSequence()!=null)\r
+        while (ds.getDatasetSequence() != null)\r
         {\r
           ds = ds.getDatasetSequence();\r
         }\r
@@ -127,31 +151,36 @@ public class Dna
     al.addCodonFrame(codons);\r
     return al;\r
   }\r
+\r
   /**\r
-   * fake the collection of DbRefs with associated exon mappings to identify\r
-   * if a translation would generate distinct product in the currently selected region.\r
+   * fake the collection of DbRefs with associated exon mappings to identify if\r
+   * a translation would generate distinct product in the currently selected\r
+   * region.\r
+   * \r
    * @param selection\r
    * @param viscontigs\r
    * @return\r
    */\r
-  public static boolean canTranslate(SequenceI[] selection, int viscontigs[])\r
+  public static boolean canTranslate(SequenceI[] selection,\r
+          int viscontigs[])\r
   {\r
-    for (int gd=0; gd<selection.length; gd++)\r
+    for (int gd = 0; gd < selection.length; gd++)\r
     {\r
       SequenceI dna = selection[gd];\r
       jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils\r
-      .selectRefs(dna.getDBRef(),\r
-              jalview.datamodel.DBRefSource.DNACODINGDBS);\r
+              .selectRefs(dna.getDBRef(),\r
+                      jalview.datamodel.DBRefSource.DNACODINGDBS);\r
       if (dnarefs != null)\r
       {\r
         // intersect with pep\r
         // intersect with pep\r
         Vector mappedrefs = new Vector();\r
-        DBRefEntry[] refs=dna.getDBRef();\r
-        for (int d=0; d<refs.length; d++)\r
+        DBRefEntry[] refs = dna.getDBRef();\r
+        for (int d = 0; d < refs.length; d++)\r
         {\r
-          if (refs[d].getMap()!=null && refs[d].getMap().getMap()!=null && refs[d].getMap().getMap().getFromRatio()==3\r
-                  && refs[d].getMap().getMap().getToRatio()==1)\r
+          if (refs[d].getMap() != null && refs[d].getMap().getMap() != null\r
+                  && refs[d].getMap().getMap().getFromRatio() == 3\r
+                  && refs[d].getMap().getMap().getToRatio() == 1)\r
           {\r
             mappedrefs.addElement(refs[d]); // add translated protein maps\r
           }\r
@@ -163,10 +192,12 @@ public class Dna
           Mapping mp = dnarefs[d].getMap();\r
           if (mp != null)\r
           {\r
-            for (int vc=0; vc<viscontigs.length; vc+=2)\r
+            for (int vc = 0; vc < viscontigs.length; vc += 2)\r
             {\r
-              int[] mpr=mp.locateMappedRange(viscontigs[vc], viscontigs[vc+1]);\r
-              if (mpr!=null) {\r
+              int[] mpr = mp.locateMappedRange(viscontigs[vc],\r
+                      viscontigs[vc + 1]);\r
+              if (mpr != null)\r
+              {\r
                 return true;\r
               }\r
             }\r
@@ -176,12 +207,15 @@ public class Dna
     }\r
     return false;\r
   }\r
+\r
   /**\r
    * generate a set of translated protein products from annotated sequenceI\r
+   * \r
    * @param selection\r
    * @param viscontigs\r
    * @param gapCharacter\r
-   * @param dataset destination dataset for translated sequences\r
+   * @param dataset\r
+   *                destination dataset for translated sequences\r
    * @param annotations\r
    * @param aWidth\r
    * @return\r
@@ -189,25 +223,26 @@ public class Dna
   public static AlignmentI CdnaTranslate(SequenceI[] selection,\r
           int viscontigs[], char gapCharacter, Alignment dataset)\r
   {\r
-    int alwidth=0;\r
-    Vector cdnasqs=new Vector();\r
-    Vector cdnasqi=new Vector();\r
-    Vector cdnaprod=new Vector();\r
-    for (int gd=0; gd<selection.length; gd++)\r
+    int alwidth = 0;\r
+    Vector cdnasqs = new Vector();\r
+    Vector cdnasqi = new Vector();\r
+    Vector cdnaprod = new Vector();\r
+    for (int gd = 0; gd < selection.length; gd++)\r
     {\r
       SequenceI dna = selection[gd];\r
       jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils\r
-      .selectRefs(dna.getDBRef(),\r
-              jalview.datamodel.DBRefSource.DNACODINGDBS);\r
+              .selectRefs(dna.getDBRef(),\r
+                      jalview.datamodel.DBRefSource.DNACODINGDBS);\r
       if (dnarefs != null)\r
       {\r
         // intersect with pep\r
         Vector mappedrefs = new Vector();\r
-        DBRefEntry[] refs=dna.getDBRef();\r
-        for (int d=0; d<refs.length; d++)\r
+        DBRefEntry[] refs = dna.getDBRef();\r
+        for (int d = 0; d < refs.length; d++)\r
         {\r
-          if (refs[d].getMap()!=null && refs[d].getMap().getMap()!=null && refs[d].getMap().getMap().getFromRatio()==3\r
-                  && refs[d].getMap().getMap().getToRatio()==1)\r
+          if (refs[d].getMap() != null && refs[d].getMap().getMap() != null\r
+                  && refs[d].getMap().getMap().getFromRatio() == 3\r
+                  && refs[d].getMap().getMap().getToRatio() == 1)\r
           {\r
             mappedrefs.addElement(refs[d]); // add translated protein maps\r
           }\r
@@ -217,13 +252,13 @@ public class Dna
         for (int d = 0; d < dnarefs.length; d++)\r
         {\r
           Mapping mp = dnarefs[d].getMap();\r
-          StringBuffer sqstr=new StringBuffer();\r
+          StringBuffer sqstr = new StringBuffer();\r
           if (mp != null)\r
           {\r
             Mapping intersect = mp.intersectVisContigs(viscontigs);\r
             // generate seqstring for this sequence based on mapping\r
-            \r
-            if (sqstr.length()>alwidth)\r
+\r
+            if (sqstr.length() > alwidth)\r
               alwidth = sqstr.length();\r
             cdnasqs.addElement(sqstr.toString());\r
             cdnasqi.addElement(dna);\r
@@ -237,7 +272,8 @@ public class Dna
       cdnasqs.copyInto(xons);\r
       cdnaprod.copyInto(prods);\r
       cdnasqi.copyInto(cdna);\r
-      return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter, null, alwidth, dataset);\r
+      return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,\r
+              null, alwidth, dataset);\r
     }\r
     return null;\r
   }\r
@@ -282,7 +318,8 @@ public class Dna
             if (codons.codons[a] != null\r
                     && codons.codons[a][0] == (codons.codons[a][2] - 2))\r
             {\r
-              anots[a] = getCodonAnnotation(codons.codons[a], annotations[i].annotations);\r
+              anots[a] = getCodonAnnotation(codons.codons[a],\r
+                      annotations[i].annotations);\r
             }\r
           }\r
         }\r
@@ -292,9 +329,10 @@ public class Dna
         aa.graph = annotations[i].graph;\r
         aa.graphGroup = annotations[i].graphGroup;\r
         aa.graphHeight = annotations[i].graphHeight;\r
-        if (annotations[i].getThreshold()!=null)\r
+        if (annotations[i].getThreshold() != null)\r
         {\r
-          aa.setThreshold(new jalview.datamodel.GraphLine(annotations[i].getThreshold()));        \r
+          aa.setThreshold(new jalview.datamodel.GraphLine(annotations[i]\r
+                  .getThreshold()));\r
         }\r
         if (annotations[i].hasScore)\r
         {\r
@@ -321,13 +359,14 @@ public class Dna
     }\r
   }\r
 \r
-  private static Annotation getCodonAnnotation(int[] is, Annotation[] annotations)\r
-  { \r
+  private static Annotation getCodonAnnotation(int[] is,\r
+          Annotation[] annotations)\r
+  {\r
     // Have a look at all the codon positions for annotation and put the first\r
     // one found into the translated annotation pos.\r
-    for (int p=0; p<3; p++)\r
+    for (int p = 0; p < 3; p++)\r
     {\r
-      if (annotations[is[p]]!=null)\r
+      if (annotations[is[p]] != null)\r
       {\r
         return new Annotation(annotations[is[p]]);\r
       }\r
@@ -338,10 +377,15 @@ public class Dna
   /**\r
    * Translate a na sequence\r
    * \r
-   * @param selection sequence displayed under viscontigs visible columns\r
-   * @param seqstring ORF read in some global alignment reference frame\r
-   * @param viscontigs mapping from global reference frame to visible seqstring ORF read\r
-   * @param codons Definition of global ORF alignment reference frame\r
+   * @param selection\r
+   *                sequence displayed under viscontigs visible columns\r
+   * @param seqstring\r
+   *                ORF read in some global alignment reference frame\r
+   * @param viscontigs\r
+   *                mapping from global reference frame to visible seqstring ORF\r
+   *                read\r
+   * @param codons\r
+   *                Definition of global ORF alignment reference frame\r
    * @param gapCharacter\r
    * @param newSeq\r
    * @return sequence ready to be added to alignment.\r
@@ -350,22 +394,25 @@ public class Dna
           String seqstring, int[] viscontigs, AlignedCodonFrame codons,\r
           char gapCharacter, DBRefEntry product)\r
   {\r
-    Vector skip=new Vector();\r
-    int skipint[]=null;\r
+    Vector skip = new Vector();\r
+    int skipint[] = null;\r
     ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring\r
     // intervals\r
     int vc, scontigs[] = new int[viscontigs.length];\r
     int npos = 0;\r
     for (vc = 0; vc < viscontigs.length; vc += 2)\r
     {\r
-      if (vc==0) {\r
-      vismapping.addShift(npos, viscontigs[vc]);\r
-      } else {\r
+      if (vc == 0)\r
+      {\r
+        vismapping.addShift(npos, viscontigs[vc]);\r
+      }\r
+      else\r
+      {\r
         // hidden region\r
-        vismapping.addShift(npos, viscontigs[vc]-viscontigs[vc-1]+1);\r
+        vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);\r
       }\r
       scontigs[vc] = viscontigs[vc];\r
-      scontigs[vc + 1] = viscontigs[vc+1];\r
+      scontigs[vc + 1] = viscontigs[vc + 1];\r
     }\r
 \r
     StringBuffer protein = new StringBuffer();\r
@@ -389,37 +436,42 @@ public class Dna
         if (aa == null)\r
         {\r
           aa = String.valueOf(gapCharacter);\r
-          if (skipint==null)\r
+          if (skipint == null)\r
           {\r
-            skipint = new int[] { cdp[0],cdp[2] };\r
+            skipint = new int[]\r
+            { cdp[0], cdp[2] };\r
           }\r
           skipint[1] = cdp[2];\r
-        } else {\r
-          if (skipint!=null)\r
+        }\r
+        else\r
+        {\r
+          if (skipint != null)\r
           {\r
             // edit scontigs\r
             skipint[0] = vismapping.shift(skipint[0]);\r
             skipint[1] = vismapping.shift(skipint[1]);\r
-            for (vc=0; vc<scontigs.length; vc+=2)\r
+            for (vc = 0; vc < scontigs.length; vc += 2)\r
             {\r
-              if (scontigs[vc+1]<skipint[0])\r
+              if (scontigs[vc + 1] < skipint[0])\r
               {\r
                 continue;\r
               }\r
-              if (scontigs[vc]<=skipint[0])\r
+              if (scontigs[vc] <= skipint[0])\r
               {\r
-               if (skipint[0]==scontigs[vc])\r
-               {\r
-                 \r
-               } else {\r
-                 int[] t = new int[scontigs.length+2];\r
-                 System.arraycopy(scontigs, 0, t, 0, vc-1);\r
-                 // scontigs[vc]; //\r
-               }\r
+                if (skipint[0] == scontigs[vc])\r
+                {\r
+\r
+                }\r
+                else\r
+                {\r
+                  int[] t = new int[scontigs.length + 2];\r
+                  System.arraycopy(scontigs, 0, t, 0, vc - 1);\r
+                  // scontigs[vc]; //\r
+                }\r
               }\r
             }\r
             skip.addElement(skipint);\r
-            skipint=null;\r
+            skipint = null;\r
           }\r
           if (aa.equals("STOP"))\r
           {\r
@@ -446,8 +498,8 @@ public class Dna
             // with a gap\r
             aa = "" + gapCharacter + aa;\r
             aspos++;\r
-            //if (aspos >= codons.aaWidth)\r
-            //  codons.aaWidth = aspos + 1;\r
+            // if (aspos >= codons.aaWidth)\r
+            // codons.aaWidth = aspos + 1;\r
             break; // check the next position for alignment\r
           case 0:\r
             // codon aligns at aspos position.\r
@@ -515,8 +567,7 @@ public class Dna
         for (vc = 0; vc < scontigs.length; vc += 2)\r
         {\r
           scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!\r
-          scontigs[vc + 1] = selection\r
-                  .findPosition(scontigs[vc + 1]); // exclusive\r
+          scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive\r
           if (scontigs[vc + 1] == selection.getEnd())\r
             break;\r
         }\r
@@ -528,62 +579,35 @@ public class Dna
           scontigs = t;\r
         }\r
         /*\r
-         * delete intervals in scontigs which are not translated.\r
-         * 1. map skip into sequence position intervals\r
-         * 2. truncate existing ranges and add new ranges to exclude untranslated regions.\r
-        if (skip.size()>0)\r
-        {\r
-          Vector narange = new Vector();\r
-          for (vc=0; vc<scontigs.length; vc++)\r
-          {\r
-            narange.addElement(new int[] {scontigs[vc]});\r
-          }\r
-          int sint=0,iv[];\r
-          vc = 0;\r
-          while (sint<skip.size())\r
-          {\r
-            skipint = (int[]) skip.elementAt(sint);\r
-            do {\r
-              iv = (int[]) narange.elementAt(vc);\r
-              if (iv[0]>=skipint[0] && iv[0]<=skipint[1])\r
-              {\r
-                if (iv[0]==skipint[0])\r
-                {\r
-                  // delete beginning of range\r
-                } else {\r
-                  // truncate range and create new one if necessary\r
-                  iv = (int[]) narange.elementAt(vc+1);\r
-                  if (iv[0]<=skipint[1])\r
-                  {\r
-                    // truncate range\r
-                    iv[0] = skipint[1];\r
-                  } else {\r
-                    \r
-                  }\r
-                }\r
-              } else\r
-                if (iv[0]<skipint[0])\r
-                {\r
-                  iv = (int[]) narange.elementAt(vc+1);\r
-                }\r
-            } while (iv[0])\r
-          }\r
-        }*/\r
+         * delete intervals in scontigs which are not translated. 1. map skip\r
+         * into sequence position intervals 2. truncate existing ranges and add\r
+         * new ranges to exclude untranslated regions. if (skip.size()>0) {\r
+         * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {\r
+         * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =\r
+         * 0; while (sint<skip.size()) { skipint = (int[])\r
+         * skip.elementAt(sint); do { iv = (int[]) narange.elementAt(vc); if\r
+         * (iv[0]>=skipint[0] && iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { //\r
+         * delete beginning of range } else { // truncate range and create new\r
+         * one if necessary iv = (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { //\r
+         * truncate range iv[0] = skipint[1]; } else {\r
+         *  } } } else if (iv[0]<skipint[0]) { iv = (int[])\r
+         * narange.elementAt(vc+1); } } while (iv[0]) } }\r
+         */\r
         MapList map = new MapList(scontigs, new int[]\r
-                                                    { 1, resSize }, 3, 1);\r
-        \r
+        { 1, resSize }, 3, 1);\r
+\r
         // update newseq as if it was generated as mapping from product\r
-        \r
+\r
         if (product != null)\r
         {\r
           newseq.setName(product.getSource() + "|"\r
                   + product.getAccessionId());\r
           if (product.getMap() != null)\r
           {\r
-            //Mapping mp = product.getMap();\r
-            //newseq.setStart(mp.getPosition(scontigs[0]));\r
-            //newseq.setEnd(mp\r
-            //        .getPosition(scontigs[scontigs.length - 1]));\r
+            // Mapping mp = product.getMap();\r
+            // newseq.setStart(mp.getPosition(scontigs[0]));\r
+            // newseq.setEnd(mp\r
+            // .getPosition(scontigs[scontigs.length - 1]));\r
           }\r
         }\r
         transferCodedFeatures(selection, newseq, map, null, null);\r
@@ -611,10 +635,10 @@ public class Dna
    * @param pep\r
    * @param map\r
    * @param featureTypes\r
-   *          hash who's keys are the displayed feature type strings\r
+   *                hash who's keys are the displayed feature type strings\r
    * @param featureGroups\r
-   *          hash where keys are feature groups and values are Boolean objects\r
-   *          indicating if they are displayed.\r
+   *                hash where keys are feature groups and values are Boolean\r
+   *                objects indicating if they are displayed.\r
    */\r
   private static void transferCodedFeatures(SequenceI dna, SequenceI pep,\r
           MapList map, Hashtable featureTypes, Hashtable featureGroups)\r