2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.JvOptionPane;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
34 public class ComparisonTest
37 @BeforeClass(alwaysRun = true)
38 public void setUpJvOptionPane()
40 JvOptionPane.setInteractiveMode(false);
41 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
44 @Test(groups = { "Functional" })
45 public void testIsGap()
47 assertTrue(Comparison.isGap('-'));
48 assertTrue(Comparison.isGap('.'));
49 assertTrue(Comparison.isGap(' '));
50 assertFalse(Comparison.isGap('X'));
51 assertFalse(Comparison.isGap('x'));
52 assertFalse(Comparison.isGap('*'));
53 assertFalse(Comparison.isGap('G'));
57 * Test for isNucleotide is that sequences in a dataset are more than 85%
58 * AGCTU. Test is not case-sensitive and ignores gaps.
60 @Test(groups = { "Functional" })
61 public void testIsNucleotide_sequences()
63 SequenceI seq = new Sequence("eightypercent", "agctuAGCPV");
64 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
65 assertFalse(Comparison.isNucleotide(new SequenceI[][] { new SequenceI[]
68 seq = new Sequence("eightyfivepercent", "agctuAGCPVagctuAGCUV");
69 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
71 seq = new Sequence("nineypercent", "agctuAGCgVagctuAGCUV");
72 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq }));
74 seq = new Sequence("eightyfivepercentgapped",
75 "--agc--tuA--GCPV-a---gct-uA-GC---UV");
76 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
78 seq = new Sequence("nineypercentgapped",
79 "ag--ct-u-A---GC---g----Vag--c---tuAGCUV");
80 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq }));
82 seq = new Sequence("allgap", "---------");
83 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
85 seq = new Sequence("DNA", "ACTugGCCAG");
86 SequenceI seq2 = new Sequence("Protein", "FLIMVSPTYW");
90 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq, seq, seq,
91 seq, seq, seq, seq, seq, seq, seq2 }));
92 assertTrue(Comparison.isNucleotide(new SequenceI[][] {
93 new SequenceI[] { seq }, new SequenceI[] { seq, seq, seq },
94 new SequenceI[] { seq, seq, seq, seq, seq, seq2 } }));
98 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq, seq, seq,
99 seq, seq, seq, seq, seq, seq2, seq2 }));
100 assertFalse(Comparison.isNucleotide(new SequenceI[][] { new SequenceI[]
101 { seq }, new SequenceI[] { seq, seq, seq },
102 new SequenceI[] { seq, seq, seq, seq, seq2, seq2, null } }));
104 seq = new Sequence("ProteinThatLooksLikeDNA", "WYATGCCTGAgtcgt");
106 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq }));
108 assertFalse(Comparison.isNucleotide((SequenceI[]) null));
109 assertFalse(Comparison.isNucleotide((SequenceI[][]) null));
113 * Test the percentage identity calculation for two sequences
115 @Test(groups = { "Functional" })
116 public void testPID_includingGaps()
118 String seq1 = "ABCDEFG"; // extra length here is ignored
119 String seq2 = "abcdef";
120 assertEquals("identical", 100f, Comparison.PID(seq1, seq2), 0.001f);
122 // comparison range defaults to length of first sequence
123 seq2 = "abcdefghijklmnopqrstuvwxyz";
124 assertEquals("identical", 100f, Comparison.PID(seq1, seq2), 0.001f);
126 // 5 identical, 2 gap-gap, 2 gap-residue, 1 mismatch
129 int length = seq1.length();
131 // match gap-residue, match gap-gap: 9/10 identical
132 // TODO should gap-gap be included in a PID score? JAL-791
133 assertEquals(90f, Comparison.PID(seq1, seq2, 0, length, true, false),
135 // overloaded version of the method signature above:
136 assertEquals(90f, Comparison.PID(seq1, seq2), 0.001f);
138 // don't match gap-residue, match gap-gap: 7/10 identical
139 // TODO should gap-gap be included in a PID score?
140 assertEquals(70f, Comparison.PID(seq1, seq2, 0, length, false, false),
144 @Test(groups = { "Functional" })
145 public void testIsNucleotide()
147 assertTrue(Comparison.isNucleotide('a'));
148 assertTrue(Comparison.isNucleotide('A'));
149 assertTrue(Comparison.isNucleotide('c'));
150 assertTrue(Comparison.isNucleotide('C'));
151 assertTrue(Comparison.isNucleotide('g'));
152 assertTrue(Comparison.isNucleotide('G'));
153 assertTrue(Comparison.isNucleotide('t'));
154 assertTrue(Comparison.isNucleotide('T'));
155 assertTrue(Comparison.isNucleotide('u'));
156 assertTrue(Comparison.isNucleotide('U'));
157 assertFalse(Comparison.isNucleotide('-'));
158 assertFalse(Comparison.isNucleotide('P'));
162 * Test the percentage identity calculation for two sequences
164 @Test(groups = { "Functional" })
165 public void testPID_ungappedOnly()
167 // 5 identical, 2 gap-gap, 2 gap-residue, 1 mismatch
168 // the extra length of seq1 is ignored
169 String seq1 = "a--b-cdefhr";
170 String seq2 = "a---bcdefg";
171 int length = seq1.length();
174 * As currently coded, 'ungappedOnly' ignores gap-residue but counts
175 * gap-gap. Is this a bug - should gap-gap also be ignored, giving a PID of
178 * Note also there is no variant of the calculation that penalises
179 * gap-residue i.e. counts it as a mismatch. This would give a score of 5/8
180 * (if we ignore gap-gap) or 5/10 (if we count gap-gap as a match).
182 // match gap-residue, match gap-gap: 7/8 identical
183 assertEquals(87.5f, Comparison.PID(seq1, seq2, 0, length, true, true),
186 // don't match gap-residue with 'ungapped only' - same as above
187 assertEquals(87.5f, Comparison.PID(seq1, seq2, 0, length, false, true),
191 @Test(groups = { "Functional" })
192 public void testIsNucleotideSequence()
194 assertFalse(Comparison.isNucleotideSequence(null, true));
195 assertTrue(Comparison.isNucleotideSequence("", true));
196 assertTrue(Comparison.isNucleotideSequence("aAgGcCtTuU", true));
197 assertTrue(Comparison.isNucleotideSequence("aAgGcCtTuU", false));
198 assertFalse(Comparison.isNucleotideSequence("xAgGcCtTuU", false));
199 assertFalse(Comparison.isNucleotideSequence("aAgGcCtTuUx", false));
200 assertTrue(Comparison.isNucleotideSequence("a A-g.GcCtTuU", true));
201 assertFalse(Comparison.isNucleotideSequence("a A-g.GcCtTuU", false));
204 @Test(groups = { "Functional" })
205 public void testIsSameResidue()
207 assertTrue(Comparison.isSameResidue('a', 'a', false));
208 assertTrue(Comparison.isSameResidue('a', 'a', true));
209 assertTrue(Comparison.isSameResidue('A', 'a', false));
210 assertTrue(Comparison.isSameResidue('a', 'A', false));
212 assertFalse(Comparison.isSameResidue('a', 'A', true));
213 assertFalse(Comparison.isSameResidue('A', 'a', true));