44bea811761f89441dd095ff44945226bf985eb5
[jalview.git] / test / jalview / analysis / TestAlignSeq.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25
26 import jalview.datamodel.Mapping;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.JvOptionPane;
30
31 import java.io.PrintStream;
32 import java.nio.charset.Charset;
33 import java.util.Locale;
34
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
38
39 import com.google.common.base.Charsets;
40
41 /**
42  * Test the alignment -> Mapping routines
43  * 
44  * @author jimp
45  * 
46  */
47 public class TestAlignSeq
48 {
49
50   @BeforeClass(alwaysRun = true)
51   public void setUpJvOptionPane()
52   {
53     JvOptionPane.setInteractiveMode(false);
54     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55   }
56
57   SequenceI s1, s2, s3;
58
59   /**
60    * @throws java.lang.Exception
61    */
62   @BeforeMethod(alwaysRun = true)
63   public void setUp() throws Exception
64   {
65     s1 = new Sequence("Seq1", "ASDFAQQQRRRSSS");
66     s1.setStart(3);
67     s1.setEnd(18);
68     s2 = new Sequence("Seq2", "ASDFA");
69     s2.setStart(5);
70     s2.setEnd(9);
71     s3 = new Sequence("Seq3", "SDFAQQQSSS");
72
73   }
74
75   @Test(groups = { "Functional" })
76   /**
77    * simple test that mapping from alignment corresponds identical positions.
78    */
79   public void testGetMappingForS1()
80   {
81     AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, s2, AlignSeq.PEP);
82     System.out.println("s1: " + as.getAStr1());
83     System.out.println("s2: " + as.getAStr2());
84
85     // aligned results match
86     assertEquals("ASDFA", as.getAStr1());
87     assertEquals(as.getAStr1(), as.getAStr2());
88
89     Mapping s1tos2 = as.getMappingFromS1(false);
90     checkMapping(s1tos2,s1,s2);
91   }
92
93   public void checkMapping(Mapping s1tos2,SequenceI _s1,SequenceI _s2)
94   {
95     System.out.println(s1tos2.getMap().toString());
96     for (int i = _s2.getStart(); i < _s2.getEnd(); i++)
97     {
98       int p=s1tos2.getPosition(i);
99       char s2c=_s2.getCharAt(i-_s2.getStart());
100       char s1c=_s1.getCharAt(p-_s1.getStart());
101       System.out.println("Position in s2: " + i +s2c 
102       + " maps to position in s1: " +p+s1c);
103       assertEquals(s1c,s2c);
104     }
105   }
106   @Test(groups = { "Functional" })
107   /**
108    * simple test that mapping from alignment corresponds identical positions.
109    */
110   public void testGetMappingForS1_withLowerCase()
111   {
112     // make one of the sequences lower case
113     SequenceI ns2 = new Sequence(s2);
114     ns2.replace('D', 'd');
115     AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, ns2, AlignSeq.PEP);
116     System.out.println("s1: " + as.getAStr1());
117     System.out.println("s2: " + as.getAStr2());
118
119     // aligned results match
120     assertEquals("ASDFA", as.getAStr1());
121     assertEquals(as.getAStr1(), as.getAStr2().toUpperCase(Locale.ROOT));
122
123     Mapping s1tos2 = as.getMappingFromS1(false);
124     assertEquals("ASdFA",as.getAStr2());
125     // verify mapping is consistent between original all-caps sequences
126     checkMapping(s1tos2,s1,s2);
127   }
128
129   @Test(groups = { "Functional" })
130   public void testExtractGaps()
131   {
132     assertNull(AlignSeq.extractGaps(null, null));
133     assertNull(AlignSeq.extractGaps(". -", null));
134     assertNull(AlignSeq.extractGaps(null, "AB-C"));
135
136     assertEquals("ABCD", AlignSeq.extractGaps(" .-", ". -A-B.C D."));
137   }
138
139   @Test(groups = { "Functional" })
140   public void testPrintAlignment()
141   {
142     AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, s3, AlignSeq.PEP);
143     final StringBuilder baos = new StringBuilder();
144     PrintStream ps = new PrintStream(System.out)
145     {
146       @Override
147       public void print(String x)
148       {
149         baos.append(x);
150       }
151
152       @Override
153       public void println()
154       {
155         baos.append("\n");
156       }
157     };
158
159     as.printAlignment(ps);
160     String expected = "Score = 320.0\nLength of alignment = 10\nSequence Seq1/4-13 (Sequence length = 14)\nSequence Seq3/1-10 (Sequence length = 10)\n\n"
161             + "Seq1/4-13 SDFAQQQRRR\n" + "          |||||||   \n"
162             + "Seq3/1-10 SDFAQQQSSS\n\n" + "Percentage ID = 70.00\n\n";
163     assertEquals(expected, baos.toString());
164   }
165 }