From 840c90a082ffc6f29a512501337c4e4397ad604f Mon Sep 17 00:00:00 2001 From: James Procter Date: Mon, 4 Sep 2023 12:11:24 +0100 Subject: [PATCH] JAL-4159 JAL-4257 simple test for default gap parameters, and new methods/constructor params to provide alternative gap parameters --- src/jalview/analysis/AlignSeq.java | 79 ++++++++++++++++++++++--------- test/jalview/analysis/AlignSeqTest.java | 12 +++++ 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/jalview/analysis/AlignSeq.java b/src/jalview/analysis/AlignSeq.java index 02b3f41..1a83188 100755 --- a/src/jalview/analysis/AlignSeq.java +++ b/src/jalview/analysis/AlignSeq.java @@ -54,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 GAP_EXTEND_COST = 20; + private static final int DEFAULT_EXTENDCOST = 20; + + private int GAP_OPEN_COST=DEFAULT_OPENCOST; + + private int GAP_EXTEND_COST=DEFAULT_EXTENDCOST; private static final int GAP_INDEX = -1; @@ -140,12 +144,12 @@ 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) @@ -154,6 +158,23 @@ public class AlignSeq 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; + } + /** * DOCUMENT ME! * @@ -299,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); @@ -635,25 +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; } @@ -661,9 +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)); @@ -676,10 +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)); @@ -857,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(); diff --git a/test/jalview/analysis/AlignSeqTest.java b/test/jalview/analysis/AlignSeqTest.java index a9a9730..03bdd0b 100644 --- a/test/jalview/analysis/AlignSeqTest.java +++ b/test/jalview/analysis/AlignSeqTest.java @@ -76,4 +76,16 @@ public class AlignSeqTest String s = "aArRnNzZxX *.-?"; assertArrayEquals(expected, as.indexEncode(s)); } + @Test(groups= {"Functional"}) + public void testGlobalAlignment() + { + String seq1="CAGCTAGCG",seq2="CCATACGA"; + Sequence sq1=new Sequence("s1",seq1),sq2=new Sequence("s2",seq2); + // AlignSeq doesn't report the unaligned regions at either end of sequences + //String alseq1="-CAGCTAGCG-",alseq2="CCA--TA-CGA"; + // so we check we have the aligned segment correct only + String alseq1="CAGCTAGCG",alseq2="CA--TA-CG"; + AlignSeq as = AlignSeq.doGlobalNWAlignment(sq1,sq2,AlignSeq.DNA); + assertEquals(as.getAStr1()+"\n"+as.getAStr2(),alseq1+"\n"+alseq2); + } } -- 1.7.10.2