Clover icon

Coverage Report

  1. Project Clover database Wed Sep 17 2025 10:52:37 BST
  2. Package jalview.analysis

File TestAlignSeq.java

 

Code metrics

4
75
11
1
223
146
13
0.17
6.82
11
1.18

Classes

Class Line # Actions
TestAlignSeq 46 75 13
0.977777897.8%
 

Contributing tests

This file is covered by 6 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.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    import jalview.util.MapList;
31   
32    import java.io.PrintStream;
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   
40    /**
41    * Test the alignment -> Mapping routines
42    *
43    * @author jimp
44    *
45    */
 
46    public class TestAlignSeq
47    {
48   
 
49  1 toggle @BeforeClass(alwaysRun = true)
50    public void setUpJvOptionPane()
51    {
52  1 JvOptionPane.setInteractiveMode(false);
53  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54    }
55   
56    SequenceI s1, s2, s3;
57   
58    /**
59    * @throws java.lang.Exception
60    */
 
61  6 toggle @BeforeMethod(alwaysRun = true)
62    public void setUp() throws Exception
63    {
64  6 s1 = new Sequence("Seq1", "ASDFAQQQRRRSSS");
65  6 s1.setStart(3);
66  6 s1.setEnd(18);
67  6 s2 = new Sequence("Seq2", "ASDFA");
68  6 s2.setStart(5);
69  6 s2.setEnd(9);
70  6 s3 = new Sequence("Seq3", "SDFAQQQSSS");
71   
72    }
73   
 
74  1 toggle @Test(groups = { "Functional" })
75    /**
76    * test Mappings from alignments correspond to identical positions (for structure & sequence:sequence mappings)
77    */
78    public void testGetMappingForS1_1()
79    {
80  1 AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, s2, AlignSeq.PEP);
81  1 System.out.println("s1: " + as.getAStr1());
82  1 System.out.println("s2: " + as.getAStr2());
83   
84    // aligned results match
85  1 assertEquals("ASDFA", as.getAStr1());
86  1 assertEquals(as.getAStr1(), as.getAStr2());
87   
88  1 Mapping s1tos2 = as.getMappingFromS1(false);
89    // ASDFA = 5aa
90  1 assertEquals(s2.getStart(),s1tos2.getMappedPosition(s1.getStart()));
91  1 assertEquals(s2.getStart()+4,s1tos2.getMappedPosition(s1.getStart()+4));
92  1 checkMapping(s1tos2, s1, s2);
93    }
 
94  1 toggle @Test(groups = { "Functional" })
95    /**
96    * test Mappings from alignments correspond to identical positions (for structure & sequence:sequence mappings)
97    */
98    public void testGetMappingForS1_2()
99    {
100    // simple version of full pathological case
101  1 String s1str="MSNATGVVRSPFEYP";
102  1 String s2str="MSNKTGVPFEAP";
103  1 SequenceI ns1 = new Sequence("S1",s1str);SequenceI ns2 = new Sequence("S2",s2str);
104  1 AlignSeq as = AlignSeq.doGlobalNWAlignment(ns1, ns2, AlignSeq.PEP);
105  1 String astr1="MSNATGVVRSPFEYP";
106  1 String astr2="MSNKTGV---PFEAP";
107  1 int[] mstr1 = new int[] { 1,3, 5,7, 11,13,15,15};
108  1 int[] mstr2 = new int[] { 1,3, 5,7, 8,10, 12,12};
109  1 assertEquals(as.getAStr1(),astr1);
110  1 assertEquals(as.getAStr2(),astr2);
111  1 Mapping as1toas2 = as.getMappingFromS1(false);
112  1 checkMapping(as1toas2,ns1,ns2);
113  1 MapList maplist= new MapList(mstr1, mstr2, 1,1);
114  1 assertEquals(maplist,as.getMappingFromS1(false).getMap());
115    }
 
116  1 toggle @Test(groups = { "Functional" })
117    /**
118    * test Mappings from alignments correspond to identical positions (for structure & sequence:sequence mappings)
119    */
120    public void testGetMappingForS1_complex()
121    {
122    // chance encountered a pathological case from JAL-4566
123    // OPSD_MACFA to 7zbc
124  1 String s1str="MNGTEGPNFYVPFSNATGVVRSPFEYPQYYLAEPWQFSMLAAYMFLLIVLGFPINFLTLYVTVQHKKLRTPLNYILLNLAVADLFMVFGGFTTTLYTSLHGYFVFGPTGCNAEGFFATLGGEIALWSLVVLAIERYVVVCKPMSNFRFGENHAIMGVAFTWVMALACAAPPLFGWSRYIPEGLQCSCGIDYYTLKPEVNNESFVIYMFVVHFTIPMIVIFFCYGQLVFTVKEARAQQQESATTQKAEKEVTRMVIIMVIAFLICWVPYASVAFYIFTHQGSNFGPIFMTIPAFFAKSASIYNPVIYIMMNKQFRNCMLTTICCGKNPLGDDEASATVSKTETSQVAPA";
125  1 String s2str="MNGTEGPNFYVPFSNKTGVVRSPFEAPQYYLAEPWQFSMLAAYMFLLIMLGFPINFLTLYVTVQHKKLRTPLNYILLNLAVADLFMVFGGFTTTLYTSLHGYFVFGPTGCNLEGFFATLGGEIALWSLVVLAIERYVVVCKPRFGENHAIMGVAFTWVMALACAAPPLVGWSRYIPEGMQCSCGIDYYTPHEETNNESFVIYMFVVHFIIPLIVIFFCYGQLVFTQKAEKEVTRMVIIMVIAFLICWLPYAGVAFYIFTHQGSDFGPIFMTIPAFFAKTSAVYNPVIYIMMNKQFRNCMVTTLC";
126  1 SequenceI ns1 = new Sequence("S1",s1str);
127  1 SequenceI ns2 = new Sequence("S2",s2str);
128  1 AlignSeq as = AlignSeq.doGlobalNWAlignment(ns1, ns2, AlignSeq.PEP);
129  1 String astr1="MNGTEGPNFYVPFSNATGVVRSPFEYPQYYLAEPWQFSMLAAYMFLLIVLGFPINFLTLYVTVQHKKLRTPLNYILLNLAVADLFMVFGGFTTTLYTSLHGYFVFGPTGCNAEGFFATLGGEIALWSLVVLAIERYVVVCKPMSNFRFGENHAIMGVAFTWVMALACAAPPLFGWSRYIPEGLQCSCGIDYYTLKPEVNNESFVIYMFVVHFTIPMIVIFFCYGQLVFTVKEARAQQQESATTQKAEKEVTRMVIIMVIAFLICWVPYASVAFYIFTHQGSNFGPIFMTIPAFFAKSASIYNPVIYIMMNKQFRNCMLTTIC";
130  1 String astr2="MNGTEGPNFYVPFSNKTGVVRSPFEAPQYYLAEPWQFSMLAAYMFLLIMLGFPINFLTLYVTVQHKKLRTPLNYILLNLAVADLFMVFGGFTTTLYTSLHGYFVFGPTGCNLEGFFATLGGEIALWSLVVLAIERYVVVCKP----RFGENHAIMGVAFTWVMALACAAPPLVGWSRYIPEGMQCSCGIDYYTPHEETNNESFVIYMFVVHFIIPLIVIFFCYGQLVF--------------TQKAEKEVTRMVIIMVIAFLICWLPYAGVAFYIFTHQGSDFGPIFMTIPAFFAKTSAVYNPVIYIMMNKQFRNCMVTTLC";
131  1 assertEquals(as.getAStr1(),astr1);
132  1 assertEquals(as.getAStr2(),astr2);
133  1 Mapping as1toas2 = as.getMappingFromS1(false);
134  1 checkMapping(as1toas2,ns1,ns2);
135    }
136   
137    /**
138    * verifies positions in _s2 are either not mapped, or mapped to identical residues in _s1
139    * @param s1tos2
140    * @param _s1
141    * @param _s2
142    */
 
143  4 toggle public void checkMapping(Mapping s1tos2, SequenceI _s1, SequenceI _s2)
144    {
145  4 System.out.println(s1tos2.getMap().toString());
146  326 for (int i = _s2.getStart(); i < _s2.getEnd(); i++)
147    {
148  322 int[] mp = s1tos2.locateRange(i, i);
149  322 char s2c = _s2.getCharAt(i - _s2.getStart());
150  322 if (mp == null)
151    {
152  23 System.out.println("Position in s2: " + i + s2c + " is NOT MAPPED");
153    }
154    else
155    {
156  299 char s1c = _s1.getCharAt(mp[0] - _s1.getStart());
157  299 System.out.println("Position in s2: " + i + s2c
158    + " maps to position in s1: " + mp[0] + s1c);
159  299 assertEquals(s1c, s2c);
160    }
161    }
162    }
163   
 
164  1 toggle @Test(groups = { "Functional" })
165    /**
166    * simple test that mapping from alignment corresponds identical positions.
167    */
168    public void testGetMappingForS1_withLowerCase()
169    {
170    // make one of the sequences lower case
171  1 SequenceI ns2 = new Sequence(s2);
172  1 ns2.replace('D', 'd');
173  1 AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, ns2, AlignSeq.PEP);
174  1 System.out.println("s1: " + as.getAStr1());
175  1 System.out.println("s2: " + as.getAStr2());
176   
177    // aligned results match
178  1 assertEquals("ASDFA", as.getAStr1());
179  1 assertEquals(as.getAStr1(), as.getAStr2().toUpperCase(Locale.ROOT));
180   
181  1 Mapping s1tos2 = as.getMappingFromS1(false);
182  1 assertEquals("ASdFA", as.getAStr2());
183    // verify mapping is consistent between original all-caps sequences
184  1 checkMapping(s1tos2, s1, s2);
185    }
186   
 
187  1 toggle @Test(groups = { "Functional" })
188    public void testExtractGaps()
189    {
190  1 assertNull(AlignSeq.extractGaps(null, null));
191  1 assertNull(AlignSeq.extractGaps(". -", null));
192  1 assertNull(AlignSeq.extractGaps(null, "AB-C"));
193   
194  1 assertEquals("ABCD", AlignSeq.extractGaps(" .-", ". -A-B.C D."));
195    }
196   
 
197  1 toggle @Test(groups = { "Functional" })
198    public void testPrintAlignment()
199    {
200  1 AlignSeq as = AlignSeq.doGlobalNWAlignment(s1, s3, AlignSeq.PEP);
201  1 final StringBuilder baos = new StringBuilder();
202  1 PrintStream ps = new PrintStream(System.out)
203    {
 
204  1 toggle @Override
205    public void print(String x)
206    {
207  1 baos.append(x);
208    }
209   
 
210  0 toggle @Override
211    public void println()
212    {
213  0 baos.append("\n");
214    }
215    };
216   
217  1 as.printAlignment(ps);
218  1 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"
219    + "Seq1/4-13 SDFAQQQRRR\n" + " ||||||| \n"
220    + "Seq3/1-10 SDFAQQQSSS\n\n" + "Percentage ID = 70.00\n\n";
221  1 assertEquals(expected, baos.toString());
222    }
223    }