JAL-4159 JAL-4257 simple test for default gap parameters, and new methods/constructor...
[jalview.git] / src / jalview / analysis / AlignSeq.java
index 1b2578e..1a83188 100755 (executable)
@@ -20,6 +20,8 @@
  */
 package jalview.analysis;
 
+import java.util.Locale;
+
 import jalview.analysis.scoremodels.PIDModel;
 import jalview.analysis.scoremodels.ScoreMatrix;
 import jalview.analysis.scoremodels.ScoreModels;
@@ -52,9 +54,13 @@ public class AlignSeq
 {
   private static final int MAX_NAME_LENGTH = 30;
 
-  private static final int GAP_OPEN_COST = 120;
+  private static final int DEFAULT_OPENCOST = 120;
+
+  private static final int DEFAULT_EXTENDCOST = 20;
+  
+  private int GAP_OPEN_COST=DEFAULT_OPENCOST;
 
-  private static final int GAP_EXTEND_COST = 20;
+  private int GAP_EXTEND_COST=DEFAULT_EXTENDCOST;
 
   private static final int GAP_INDEX = -1;
 
@@ -138,17 +144,35 @@ public class AlignSeq
   /**
    * Creates a new AlignSeq object.
    * 
-   * @param s1
-   *          DOCUMENT ME!
-   * @param s2
-   *          DOCUMENT ME!
+   * @param s1,string1
+   *          s1 reference sequence for string1
+   * @param s2,string2
+   *          s2 reference sequence for string2
    * @param type
-   *          DOCUMENT ME!
+   *          molecule type, either AlignSeq.PEP or AlignSeq.DNA
    */
   public AlignSeq(SequenceI s1, String string1, SequenceI s2,
           String string2, String type)
   {
-    seqInit(s1, string1.toUpperCase(), s2, string2.toUpperCase(), type);
+    seqInit(s1, string1.toUpperCase(Locale.ROOT), s2,
+            string2.toUpperCase(Locale.ROOT), type);
+  }
+
+  public AlignSeq(SequenceI s1, SequenceI s2, String type, int opencost,
+          int extcost)
+  {
+    this(s1,s2,type);
+    GAP_OPEN_COST=opencost;
+    GAP_EXTEND_COST=extcost;
+  }
+
+  public AlignSeq(SequenceI s12, String string1, SequenceI s22,
+          String string2, String type2, int defaultOpencost,
+          int defaultExtendcost)
+  {
+    this(s12,string1,s22,string2,type2);
+    GAP_OPEN_COST=defaultOpencost;
+    GAP_EXTEND_COST=defaultExtendcost;
   }
 
   /**
@@ -296,6 +320,13 @@ public class AlignSeq
   public void seqInit(SequenceI s1, String string1, SequenceI s2,
           String string2, String type)
   {
+    seqInit(s1,string1,s2,string2,type,GAP_OPEN_COST,GAP_EXTEND_COST);
+  }
+  public void seqInit(SequenceI s1, String string1, SequenceI s2,
+          String string2, String type, int opening,int extension)
+  {
+    GAP_OPEN_COST=opening;
+    GAP_EXTEND_COST=extension;
     this.s1 = s1;
     this.s2 = s2;
     setDefaultParams(type);
@@ -632,23 +663,26 @@ public class AlignSeq
   {
     int n = seq1.length;
     int m = seq2.length;
-
+    final int GAP_EX_COST=GAP_EXTEND_COST;
+    final int GAP_OP_COST = GAP_OPEN_COST;
     // top left hand element
     score[0][0] = scoreMatrix.getPairwiseScore(s1str.charAt(0),
             s2str.charAt(0)) * 10;
-    E[0][0] = -GAP_EXTEND_COST;
+    E[0][0] = -GAP_EX_COST;
     F[0][0] = 0;
 
     // Calculate the top row first
     for (int j = 1; j < m; j++)
     {
       // What should these values be? 0 maybe
-      E[0][j] = max(score[0][j - 1] - GAP_OPEN_COST, E[0][j - 1] - GAP_EXTEND_COST);
-      F[0][j] = -GAP_EXTEND_COST;
+      E[0][j] = max(score[0][j - 1] - GAP_OP_COST,
+              E[0][j - 1] - GAP_EX_COST);
+      F[0][j] = -GAP_EX_COST;
 
       float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(0),
               s2str.charAt(j));
-      score[0][j] = max(pairwiseScore * 10, -GAP_OPEN_COST, -GAP_EXTEND_COST);
+      score[0][j] = max(pairwiseScore * 10, -GAP_OP_COST,
+              -GAP_EX_COST);
 
       traceback[0][j] = 1;
     }
@@ -656,8 +690,9 @@ public class AlignSeq
     // Now do the left hand column
     for (int i = 1; i < n; i++)
     {
-      E[i][0] = -GAP_OPEN_COST;
-      F[i][0] = max(score[i - 1][0] - GAP_OPEN_COST, F[i - 1][0] - GAP_EXTEND_COST);
+      E[i][0] = -GAP_OP_COST;
+      F[i][0] = max(score[i - 1][0] - GAP_OP_COST,
+              F[i - 1][0] - GAP_EX_COST);
 
       float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
               s2str.charAt(0));
@@ -670,8 +705,10 @@ public class AlignSeq
     {
       for (int j = 1; j < m; j++)
       {
-        E[i][j] = max(score[i][j - 1] - GAP_OPEN_COST, E[i][j - 1] - GAP_EXTEND_COST);
-        F[i][j] = max(score[i - 1][j] - GAP_OPEN_COST, F[i - 1][j] - GAP_EXTEND_COST);
+        E[i][j] = max(score[i][j - 1] - GAP_OP_COST,
+                E[i][j - 1] - GAP_EX_COST);
+        F[i][j] = max(score[i - 1][j] - GAP_OP_COST,
+                F[i - 1][j] - GAP_EX_COST);
 
         float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
                 s2str.charAt(j));
@@ -849,7 +886,13 @@ public class AlignSeq
   public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
           String type)
   {
-    AlignSeq as = new AlignSeq(s1, s2, type);
+    return doGlobalNWAlignment(s1, s2, type, DEFAULT_OPENCOST,DEFAULT_EXTENDCOST);
+  }
+  public static AlignSeq doGlobalNWAlignment(SequenceI s1, SequenceI s2,
+          String type, int opencost,int extcost)
+  {
+  
+    AlignSeq as = new AlignSeq(s1, s2, type,opencost,extcost);
 
     as.calcScoreMatrix();
     as.traceAlignment();
@@ -882,7 +925,8 @@ public class AlignSeq
         pdbpos++;
       }
 
-      if (allowmismatch || c1 == c2)
+      // ignore case differences
+      if (allowmismatch || (c1 == c2) || (Math.abs(c2-c1)==('a'-'A')))
       {
         // extend mapping interval
         if (lp1 + 1 != alignpos || lp2 + 1 != pdbpos)