JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / datamodel / SeqCigarTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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 org.testng.annotations.Test;
27
28 /**
29  * Unit tests for SeqCigar
30  */
31 public class SeqCigarTest
32 {
33   /*
34    * refactored 'as is' from main method
35    * 
36    * TODO: split into separate tests
37    */
38   @Test(groups = { "Functional" })
39   public void testSomething() throws Exception
40   {
41     String o_seq = "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt";
42     Sequence s = new Sequence("MySeq", o_seq, 39, 80);
43     String orig_gapped = "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt";
44     Sequence s_gapped = new Sequence("MySeq", orig_gapped, 39, 80);
45     String ex_cs_gapped = "4I4M6I6M3I11M4I12M4I9M";
46     s_gapped.setDatasetSequence(s);
47     String sub_gapped_s = "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh";
48     Sequence s_subsequence_gapped = new Sequence("MySeq", sub_gapped_s, 43,
49             77);
50     s_subsequence_gapped.setDatasetSequence(s);
51
52     SeqCigar c_null = new SeqCigar(s);
53     String cs_null = c_null.getCigarstring();
54     assertEquals("Failed to recover ungapped sequence cigar operations",
55             "42M", cs_null);
56     testCigar_string(s_gapped, ex_cs_gapped);
57     SeqCigar gen_sgapped = SeqCigar.parseCigar(s, ex_cs_gapped);
58     assertEquals("Failed parseCigar", ex_cs_gapped,
59             gen_sgapped.getCigarstring());
60
61     testSeqRecovery(gen_sgapped, s_gapped);
62
63     /*
64      * Test dataset resolution
65      */
66     SeqCigar sub_gapped = new SeqCigar(s_subsequence_gapped);
67     testSeqRecovery(sub_gapped, s_subsequence_gapped);
68
69     /*
70      * Test width functions
71      */
72     assertEquals("Failed getWidth", sub_gapped_s.length(),
73             sub_gapped.getWidth());
74
75     sub_gapped.getFullWidth();
76     assertFalse("hasDeletedRegions is incorrect",
77             sub_gapped.hasDeletedRegions());
78
79     // Test start-end region SeqCigar
80     SeqCigar sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
81     assertEquals(
82             "SeqCigar(seq, start, end) not properly clipped alignsequence",
83             41, sub_se_gp.getWidth());
84
85     /*
86      * TODO: can we add assertions to the sysouts that follow?
87      */
88     System.out.println("Original sequence align:\n" + sub_gapped_s
89             + "\nReconstructed window from 8 to 48\n" + "XXXXXXXX"
90             + sub_se_gp.getSequenceString('-') + "..." + "\nCigar String:"
91             + sub_se_gp.getCigarstring() + "\n");
92     SequenceI ssgp = sub_se_gp.getSeq('-');
93     System.out.println("\t " + ssgp.getSequenceAsString());
94     for (int r = 0; r < 10; r++)
95     {
96       sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
97       int sl = sub_se_gp.getWidth();
98       int st = sl - 1 - r;
99       for (int rs = 0; rs < 10; rs++)
100       {
101         int e = st + rs;
102         sub_se_gp.deleteRange(st, e);
103         String ssgapedseq = sub_se_gp.getSeq('-').getSequenceAsString();
104         System.out.println(st + "," + e + "\t:" + ssgapedseq);
105         st -= 3;
106       }
107     }
108
109     SeqCigar[] set = new SeqCigar[] { new SeqCigar(s),
110         new SeqCigar(s_subsequence_gapped, 8, 48), new SeqCigar(s_gapped) };
111     Alignment al = new Alignment(set);
112     for (int i = 0; i < al.getHeight(); i++)
113     {
114       System.out.println("" + al.getSequenceAt(i).getName() + "\t"
115               + al.getSequenceAt(i).getStart() + "\t"
116               + al.getSequenceAt(i).getEnd() + "\t"
117               + al.getSequenceAt(i).getSequenceAsString());
118     }
119
120     System.out.println("Gapped.");
121     set = new SeqCigar[] { new SeqCigar(s),
122         new SeqCigar(s_subsequence_gapped, 8, 48), new SeqCigar(s_gapped) };
123     set[0].deleteRange(20, 25);
124     al = new Alignment(set);
125     for (int i = 0; i < al.getHeight(); i++)
126     {
127       System.out.println("" + al.getSequenceAt(i).getName() + "\t"
128               + al.getSequenceAt(i).getStart() + "\t"
129               + al.getSequenceAt(i).getEnd() + "\t"
130               + al.getSequenceAt(i).getSequenceAsString());
131     }
132
133     // if (!ssgapedseq.equals("ryas---dtqqwa----slchvh"))
134     // System.err.println("Subseqgaped\n------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhryas---dtqwrtsasll--qwa----slchvh\n"+ssgapedseq+"\n"+sub_se_gp.getCigarstring());
135   }
136
137   /**
138    * non rigorous testing
139    * 
140    * @param seq
141    *          Sequence
142    * @param ex_cs_gapped
143    *          String
144    * @return String
145    */
146
147   protected void testCigar_string(Sequence seq, String ex_cs_gapped)
148   {
149     SeqCigar c_sgapped = new SeqCigar(seq);
150     String cs_gapped = c_sgapped.getCigarstring();
151     assertEquals("Failed getCigarstring", ex_cs_gapped, cs_gapped);
152   }
153
154   protected void testSeqRecovery(SeqCigar gen_sgapped, SequenceI s_gapped)
155   {
156     // this is non-rigorous - start and end recovery is not tested.
157     SequenceI gen_sgapped_s = gen_sgapped.getSeq('-');
158     // assertEquals("Couldn't reconstruct sequence", s_gapped.getSequence(),
159     // gen_sgapped_s);
160     if (!gen_sgapped_s.getSequence().equals(s_gapped.getSequence()))
161     {
162       // TODO: investigate errors reported here, to allow full conversion to
163       // passing JUnit assertion form
164       System.err.println("Couldn't reconstruct sequence.\n"
165               + gen_sgapped_s.getSequenceAsString() + "\n"
166               + s_gapped.getSequenceAsString());
167     }
168   }
169
170 }