Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.util

File ComparisonTest.java

 

Code metrics

0
74
8
1
215
131
8
0.11
9.25
8
1

Classes

Class Line # Actions
ComparisonTest 34 74 8 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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.util;
22   
23    import static org.testng.AssertJUnit.assertEquals;
24    import static org.testng.AssertJUnit.assertFalse;
25    import static org.testng.AssertJUnit.assertTrue;
26   
27    import jalview.datamodel.Sequence;
28    import jalview.datamodel.SequenceI;
29    import jalview.gui.JvOptionPane;
30   
31    import org.testng.annotations.BeforeClass;
32    import org.testng.annotations.Test;
33   
 
34    public class ComparisonTest
35    {
36   
 
37  1 toggle @BeforeClass(alwaysRun = true)
38    public void setUpJvOptionPane()
39    {
40  1 JvOptionPane.setInteractiveMode(false);
41  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
42    }
43   
 
44  1 toggle @Test(groups = { "Functional" })
45    public void testIsGap()
46    {
47  1 assertTrue(Comparison.isGap('-'));
48  1 assertTrue(Comparison.isGap('.'));
49  1 assertTrue(Comparison.isGap(' '));
50  1 assertFalse(Comparison.isGap('X'));
51  1 assertFalse(Comparison.isGap('x'));
52  1 assertFalse(Comparison.isGap('*'));
53  1 assertFalse(Comparison.isGap('G'));
54    }
55   
56    /**
57    * Test for isNucleotide is that sequences in a dataset are more than 85%
58    * AGCTU. Test is not case-sensitive and ignores gaps.
59    */
 
60  1 toggle @Test(groups = { "Functional" })
61    public void testIsNucleotide_sequences()
62    {
63  1 SequenceI seq = new Sequence("eightypercent", "agctuAGCPV");
64  1 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
65  1 assertFalse(Comparison.isNucleotide(new SequenceI[][] { new SequenceI[]
66    { seq } }));
67   
68  1 seq = new Sequence("eightyfivepercent", "agctuAGCPVagctuAGCUV");
69  1 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
70   
71  1 seq = new Sequence("nineypercent", "agctuAGCgVagctuAGCUV");
72  1 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq }));
73   
74  1 seq = new Sequence("eightyfivepercentgapped",
75    "--agc--tuA--GCPV-a---gct-uA-GC---UV");
76  1 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
77   
78  1 seq = new Sequence("nineypercentgapped",
79    "ag--ct-u-A---GC---g----Vag--c---tuAGCUV");
80  1 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq }));
81   
82  1 seq = new Sequence("allgap", "---------");
83  1 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq }));
84   
85  1 seq = new Sequence("DNA", "ACTugGCCAG");
86  1 SequenceI seq2 = new Sequence("Protein", "FLIMVSPTYW");
87    /*
88    * 90% DNA:
89    */
90  1 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq, seq, seq,
91    seq, seq, seq, seq, seq, seq, seq2 }));
92  1 assertTrue(Comparison.isNucleotide(new SequenceI[][] {
93    new SequenceI[] { seq }, new SequenceI[] { seq, seq, seq },
94    new SequenceI[] { seq, seq, seq, seq, seq, seq2 } }));
95    /*
96    * 80% DNA:
97    */
98  1 assertFalse(Comparison.isNucleotide(new SequenceI[] { seq, seq, seq,
99    seq, seq, seq, seq, seq, seq2, seq2 }));
100  1 assertFalse(Comparison.isNucleotide(new SequenceI[][] { new SequenceI[]
101    { seq }, new SequenceI[] { seq, seq, seq },
102    new SequenceI[] { seq, seq, seq, seq, seq2, seq2, null } }));
103   
104  1 seq = new Sequence("ProteinThatLooksLikeDNA", "WYATGCCTGAgtcgt");
105    // 12/14 = 85.7%
106  1 assertTrue(Comparison.isNucleotide(new SequenceI[] { seq }));
107   
108  1 assertFalse(Comparison.isNucleotide((SequenceI[]) null));
109  1 assertFalse(Comparison.isNucleotide((SequenceI[][]) null));
110    }
111   
112    /**
113    * Test the percentage identity calculation for two sequences
114    */
 
115  1 toggle @Test(groups = { "Functional" })
116    public void testPID_includingGaps()
117    {
118  1 String seq1 = "ABCDEFG"; // extra length here is ignored
119  1 String seq2 = "abcdef";
120  1 assertEquals("identical", 100f, Comparison.PID(seq1, seq2), 0.001f);
121   
122    // comparison range defaults to length of first sequence
123  1 seq2 = "abcdefghijklmnopqrstuvwxyz";
124  1 assertEquals("identical", 100f, Comparison.PID(seq1, seq2), 0.001f);
125   
126    // 5 identical, 2 gap-gap, 2 gap-residue, 1 mismatch
127  1 seq1 = "a--b-cdefh";
128  1 seq2 = "a---bcdefg";
129  1 int length = seq1.length();
130   
131    // match gap-residue, match gap-gap: 9/10 identical
132    // TODO should gap-gap be included in a PID score? JAL-791
133  1 assertEquals(90f, Comparison.PID(seq1, seq2, 0, length, true, false),
134    0.001f);
135    // overloaded version of the method signature above:
136  1 assertEquals(90f, Comparison.PID(seq1, seq2), 0.001f);
137   
138    // don't match gap-residue, match gap-gap: 7/10 identical
139    // TODO should gap-gap be included in a PID score?
140  1 assertEquals(70f, Comparison.PID(seq1, seq2, 0, length, false, false),
141    0.001f);
142    }
143   
 
144  1 toggle @Test(groups = { "Functional" })
145    public void testIsNucleotide()
146    {
147  1 assertTrue(Comparison.isNucleotide('a'));
148  1 assertTrue(Comparison.isNucleotide('A'));
149  1 assertTrue(Comparison.isNucleotide('c'));
150  1 assertTrue(Comparison.isNucleotide('C'));
151  1 assertTrue(Comparison.isNucleotide('g'));
152  1 assertTrue(Comparison.isNucleotide('G'));
153  1 assertTrue(Comparison.isNucleotide('t'));
154  1 assertTrue(Comparison.isNucleotide('T'));
155  1 assertTrue(Comparison.isNucleotide('u'));
156  1 assertTrue(Comparison.isNucleotide('U'));
157  1 assertFalse(Comparison.isNucleotide('-'));
158  1 assertFalse(Comparison.isNucleotide('P'));
159    }
160   
161    /**
162    * Test the percentage identity calculation for two sequences
163    */
 
164  1 toggle @Test(groups = { "Functional" })
165    public void testPID_ungappedOnly()
166    {
167    // 5 identical, 2 gap-gap, 2 gap-residue, 1 mismatch
168    // the extra length of seq1 is ignored
169  1 String seq1 = "a--b-cdefhr";
170  1 String seq2 = "a---bcdefg";
171  1 int length = seq1.length();
172   
173    /*
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
176    * 5/6?
177    *
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).
181    */
182    // match gap-residue, match gap-gap: 7/8 identical
183  1 assertEquals(87.5f, Comparison.PID(seq1, seq2, 0, length, true, true),
184    0.001f);
185   
186    // don't match gap-residue with 'ungapped only' - same as above
187  1 assertEquals(87.5f, Comparison.PID(seq1, seq2, 0, length, false, true),
188    0.001f);
189    }
190   
 
191  1 toggle @Test(groups = { "Functional" })
192    public void testIsNucleotideSequence()
193    {
194  1 assertFalse(Comparison.isNucleotideSequence(null, true));
195  1 assertTrue(Comparison.isNucleotideSequence("", true));
196  1 assertTrue(Comparison.isNucleotideSequence("aAgGcCtTuU", true));
197  1 assertTrue(Comparison.isNucleotideSequence("aAgGcCtTuU", false));
198  1 assertFalse(Comparison.isNucleotideSequence("xAgGcCtTuU", false));
199  1 assertFalse(Comparison.isNucleotideSequence("aAgGcCtTuUx", false));
200  1 assertTrue(Comparison.isNucleotideSequence("a A-g.GcCtTuU", true));
201  1 assertFalse(Comparison.isNucleotideSequence("a A-g.GcCtTuU", false));
202    }
203   
 
204  1 toggle @Test(groups = { "Functional" })
205    public void testIsSameResidue()
206    {
207  1 assertTrue(Comparison.isSameResidue('a', 'a', false));
208  1 assertTrue(Comparison.isSameResidue('a', 'a', true));
209  1 assertTrue(Comparison.isSameResidue('A', 'a', false));
210  1 assertTrue(Comparison.isSameResidue('a', 'A', false));
211   
212  1 assertFalse(Comparison.isSameResidue('a', 'A', true));
213  1 assertFalse(Comparison.isSameResidue('A', 'a', true));
214    }
215    }