89169d6dbb10cc387f372442133b575e8fa5d580
[jalview.git] / test / jalview / datamodel / SeqCigarTest.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.datamodel;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25
26 import jalview.gui.JvOptionPane;
27 import jalview.util.Comparison;
28
29 import org.testng.annotations.BeforeClass;
30 import org.testng.annotations.Test;
31
32 /**
33  * Unit tests for SeqCigar
34  */
35 public class SeqCigarTest
36 {
37
38   @BeforeClass(alwaysRun = true)
39   public void setUpJvOptionPane()
40   {
41     JvOptionPane.setInteractiveMode(false);
42     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
43   }
44
45   @Test(groups = { "Functional" })
46   public void testFindPosition()
47   {
48     SequenceI oseq = new Sequence("MySeq", "ASD---ASD---ASD", 37, 45);
49     oseq.createDatasetSequence();
50     SeqCigar cs = new SeqCigar(oseq);
51     assertEquals(oseq.getSequenceAsString(), cs.getSequenceString('-'));
52     for (int c = 0, cLen = oseq.getLength(); c < cLen; c++)
53     {
54       int os_p = oseq.findPosition(c);
55       int cigar_p = cs.findPosition(c);
56       if (Comparison.isGap(oseq.getCharAt(c)))
57       {
58         assertEquals("Expected gap at position " + os_p + " column " + c,
59                 -1, cigar_p);
60       }
61       else
62       {
63         assertEquals("Positions don't match for at column " + c, os_p,
64                 cigar_p);
65       }
66     }
67   }
68
69   /*
70    * refactored 'as is' from main method
71    * 
72    * TODO: split into separate tests
73    */
74   @Test(groups = { "Functional" })
75   public void testSomething() throws Exception
76   {
77     String o_seq = "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt";
78     Sequence s = new Sequence("MySeq", o_seq, 39, 80);
79     String orig_gapped = "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt";
80     Sequence s_gapped = new Sequence("MySeq", orig_gapped, 39, 80);
81     String ex_cs_gapped = "4I4M6I6M3I11M4I12M4I9M";
82     s_gapped.setDatasetSequence(s);
83     String sub_gapped_s = "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh";
84     Sequence s_subsequence_gapped = new Sequence("MySeq", sub_gapped_s, 43,
85             77);
86     s_subsequence_gapped.setDatasetSequence(s);
87
88     SeqCigar c_null = new SeqCigar(s);
89     String cs_null = c_null.getCigarstring();
90     assertEquals("Failed to recover ungapped sequence cigar operations",
91             "42M", cs_null);
92     testCigar_string(s_gapped, ex_cs_gapped);
93     SeqCigar gen_sgapped = SeqCigar.parseCigar(s, ex_cs_gapped);
94     assertEquals("Failed parseCigar", ex_cs_gapped,
95             gen_sgapped.getCigarstring());
96
97     testSeqRecovery(gen_sgapped, s_gapped);
98
99     /*
100      * Test dataset resolution
101      */
102     SeqCigar sub_gapped = new SeqCigar(s_subsequence_gapped);
103     testSeqRecovery(sub_gapped, s_subsequence_gapped);
104
105     /*
106      * Test width functions
107      */
108     assertEquals("Failed getWidth", sub_gapped_s.length(),
109             sub_gapped.getWidth());
110
111     sub_gapped.getFullWidth();
112     assertFalse("hasDeletedRegions is incorrect",
113             sub_gapped.hasDeletedRegions());
114
115     // Test start-end region SeqCigar
116     SeqCigar sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
117     assertEquals(
118             "SeqCigar(seq, start, end) not properly clipped alignsequence",
119             41, sub_se_gp.getWidth());
120
121     /*
122      * TODO: can we add assertions to the sysouts that follow?
123      */
124     System.out.println("\nOriginal sequence align:\n" + sub_gapped_s
125             + "\nReconstructed window from 8 to 48\n" + "XXXXXXXX"
126             + sub_se_gp.getSequenceString('-') + "..." + "\nCigar String:"
127             + sub_se_gp.getCigarstring() + "\n");
128     SequenceI ssgp = sub_se_gp.getSeq('-');
129     System.out.println("\t " + ssgp.getSequenceAsString());
130     for (int r = 0; r < 10; r++)
131     {
132       sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
133       int sl = sub_se_gp.getWidth();
134       int st = sl - 1 - r;
135       for (int rs = 0; rs < 10; rs++)
136       {
137         int e = st + rs;
138         sub_se_gp.deleteRange(st, e);
139         String ssgapedseq = sub_se_gp.getSeq('-').getSequenceAsString();
140         System.out.println(st + "," + e + "\t:" + ssgapedseq);
141         st -= 3;
142       }
143     }
144
145     SeqCigar[] set = new SeqCigar[] { new SeqCigar(s),
146         new SeqCigar(s_subsequence_gapped, 8, 48), new SeqCigar(s_gapped) };
147     Alignment al = new Alignment(set);
148     for (int i = 0; i < al.getHeight(); i++)
149     {
150       System.out.println("" + al.getSequenceAt(i).getName() + "\t"
151               + al.getSequenceAt(i).getStart() + "\t"
152               + al.getSequenceAt(i).getEnd() + "\t"
153               + al.getSequenceAt(i).getSequenceAsString());
154     }
155
156     System.out.println("Gapped.");
157     set = new SeqCigar[] { new SeqCigar(s),
158         new SeqCigar(s_subsequence_gapped, 8, 48), new SeqCigar(s_gapped) };
159     set[0].deleteRange(20, 25);
160     al = new Alignment(set);
161     for (int i = 0; i < al.getHeight(); i++)
162     {
163       System.out.println("" + al.getSequenceAt(i).getName() + "\t"
164               + al.getSequenceAt(i).getStart() + "\t"
165               + al.getSequenceAt(i).getEnd() + "\t"
166               + al.getSequenceAt(i).getSequenceAsString());
167     }
168
169     // if (!ssgapedseq.equals("ryas---dtqqwa----slchvh"))
170     // System.err.println("Subseqgaped\n------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhryas---dtqwrtsasll--qwa----slchvh\n"+ssgapedseq+"\n"+sub_se_gp.getCigarstring());
171   }
172
173   /**
174    * non rigorous testing
175    * 
176    * @param seq
177    *          Sequence
178    * @param ex_cs_gapped
179    *          String
180    * @return String
181    */
182
183   protected void testCigar_string(Sequence seq, String ex_cs_gapped)
184   {
185     SeqCigar c_sgapped = new SeqCigar(seq);
186     String cs_gapped = c_sgapped.getCigarstring();
187     assertEquals("Failed getCigarstring", ex_cs_gapped, cs_gapped);
188   }
189
190   protected void testSeqRecovery(SeqCigar gen_sgapped, SequenceI s_gapped)
191   {
192     // this is non-rigorous - start and end recovery is not tested.
193     SequenceI gen_sgapped_s = gen_sgapped.getSeq('-');
194     // assertEquals("Couldn't reconstruct sequence", s_gapped.getSequence(),
195     // gen_sgapped_s);
196     if (!gen_sgapped_s.getSequenceAsString().equals(
197             s_gapped.getSequenceAsString()))
198     {
199       // TODO: investigate errors reported here, to allow full conversion to
200       // passing JUnit assertion form
201       System.err.println("Couldn't reconstruct sequence.\n"
202               + gen_sgapped_s.getSequenceAsString() + "\n"
203               + s_gapped.getSequenceAsString());
204     }
205   }
206
207 }