JAL-3423 more System.lineSeparator() in place of \n
[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
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 /**
38  * Test the alignment -> Mapping routines
39  * 
40  * @author jimp
41  * 
42  */
43 public class TestAlignSeq
44 {
45   private static final String NL = System.lineSeparator();
46
47   @BeforeClass(alwaysRun = true)
48   public void setUpJvOptionPane()
49   {
50     JvOptionPane.setInteractiveMode(false);
51     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
52   }
53
54   SequenceI s1, s2, s3;
55
56   /**
57    * @throws java.lang.Exception
58    */
59   @BeforeMethod(alwaysRun = true)
60   public void setUp() throws Exception
61   {
62     s1 = new Sequence("Seq1", "ASDFAQQQRRRSSS");
63     s1.setStart(3);
64     s1.setEnd(18);
65     s2 = new Sequence("Seq2", "ASDFA");
66     s2.setStart(5);
67     s2.setEnd(9);
68     s3 = new Sequence("Seq3", "SDFAQQQSSS");
69
70   }
71
72   @Test(groups = { "Functional" })
73   /**
74    * simple test that mapping from alignment corresponds identical positions.
75    */
76   public void testGetMappingForS1()
77   {
78     AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, s2, AlignSeq.PEP);
79     System.out.println("s1: " + as.getAStr1());
80     System.out.println("s2: " + as.getAStr2());
81
82     // aligned results match
83     assertEquals("ASDFA", as.getAStr1());
84     assertEquals(as.getAStr1(), as.getAStr2());
85
86     Mapping s1tos2 = as.getMappingFromS1(false);
87     System.out.println(s1tos2.getMap().toString());
88     for (int i = s2.getStart(); i < s2.getEnd(); i++)
89     {
90       System.out.println("Position in s2: " + i
91               + " maps to position in s1: " + s1tos2.getPosition(i));
92       // TODO fails: getCharAt doesn't allow for the start position??
93       // assertEquals(String.valueOf(s2.getCharAt(i)),
94       // String.valueOf(s1.getCharAt(s1tos2.getPosition(i))));
95     }
96   }
97
98   @Test(groups = { "Functional" })
99   public void testExtractGaps()
100   {
101     assertNull(AlignSeq.extractGaps(null, null));
102     assertNull(AlignSeq.extractGaps(". -", null));
103     assertNull(AlignSeq.extractGaps(null, "AB-C"));
104
105     assertEquals("ABCD", AlignSeq.extractGaps(" .-", ". -A-B.C D."));
106   }
107
108   @Test(groups = { "Functional" })
109   public void testPrintAlignment()
110   {
111     AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, s3, AlignSeq.PEP);
112
113     // s1 = new Sequence("Seq1", "ASDFAQQQRRRSSS");
114     // s1.setStart(3);
115     // s1.setEnd(18);
116     // s3 = new Sequence("Seq3", "SDFAQQQSSS");
117     //
118     final StringBuilder baos = new StringBuilder();
119     PrintStream ps = new PrintStream(System.out)
120     {
121       @Override
122       public void print(String x)
123       {
124         baos.append(x);
125       }
126
127       @Override
128       public void println()
129       {
130         baos.append(NL);
131       }
132     };
133
134     as.printAlignment(ps);
135     String expected = "Score = 320.0" + NL + "Length of alignment = 10" + NL
136             + "Sequence Seq1/4-13 (Sequence length = 14)" + NL
137             + "Sequence Seq3/1-10 (Sequence length = 10)" + NL + NL
138             + "Seq1/4-13 SDFAQQQRRR" + NL + "          |||||||   " + NL
139             + "Seq3/1-10 SDFAQQQSSS" + NL + NL + "Percentage ID = 70.00"
140             + NL + NL;
141
142     String s = baos.toString();
143     System.out.println(s);
144
145     assertEquals(expected, s);
146   }
147 }