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