Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.datamodel

File SequenceTest.java

 

Code metrics

2
1,175
54
1
2,347
1,578
57
0.05
21.76
54
1.06

Classes

Class Line # Actions
SequenceTest 54 1,175 57
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.datamodel;
22   
23    import static org.testng.AssertJUnit.assertEquals;
24    import static org.testng.AssertJUnit.assertFalse;
25    import static org.testng.AssertJUnit.assertNotNull;
26    import static org.testng.AssertJUnit.assertNotSame;
27    import static org.testng.AssertJUnit.assertNull;
28    import static org.testng.AssertJUnit.assertSame;
29    import static org.testng.AssertJUnit.assertTrue;
30   
31    import java.io.File;
32    import java.util.ArrayList;
33    import java.util.Arrays;
34    import java.util.BitSet;
35    import java.util.Iterator;
36    import java.util.List;
37    import java.util.Locale;
38    import java.util.Vector;
39   
40    import org.testng.Assert;
41    import org.testng.annotations.BeforeClass;
42    import org.testng.annotations.BeforeMethod;
43    import org.testng.annotations.Test;
44   
45    import jalview.analysis.AlignmentGenerator;
46    import jalview.bin.Cache;
47    import jalview.commands.EditCommand;
48    import jalview.commands.EditCommand.Action;
49    import jalview.datamodel.PDBEntry.Type;
50    import jalview.gui.JvOptionPane;
51    import jalview.util.MapList;
52    import junit.extensions.PA;
53   
 
54    public class SequenceTest
55    {
 
56  0 toggle @BeforeClass(alwaysRun = true)
57    public void setUpJvOptionPane()
58    {
59  0 JvOptionPane.setInteractiveMode(false);
60  0 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
61    }
62   
 
63  0 toggle @BeforeMethod(alwaysRun = true)
64    public void loadProperties()
65    {
66  0 Cache.loadProperties("test/jalview/util/comparisonTestProps.jvprops");
67    }
68   
69    Sequence seq;
70   
 
71  0 toggle @BeforeMethod(alwaysRun = true)
72    public void setUp()
73    {
74  0 seq = new Sequence("FER1", "AKPNGVL");
75    }
76   
 
77  0 toggle @Test(groups = { "Functional" })
78    public void testInsertGapsAndGapmaps()
79    {
80  0 SequenceI aseq = seq.deriveSequence();
81  0 aseq.insertCharAt(2, 3, '-');
82  0 aseq.insertCharAt(6, 3, '-');
83  0 assertEquals("Gap insertions not correct", "AK---P---NGVL",
84    aseq.getSequenceAsString());
85  0 List<int[]> gapInt = aseq.getInsertions();
86  0 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
87  0 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
88  0 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
89  0 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
90   
91  0 BitSet gapfield = aseq.getInsertionsAsBits();
92  0 BitSet expectedgaps = new BitSet();
93  0 expectedgaps.set(2, 5);
94  0 expectedgaps.set(6, 9);
95   
96  0 assertEquals(6, expectedgaps.cardinality());
97   
98  0 assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
99    6, gapfield.cardinality());
100   
101  0 assertEquals("getInsertionsAsBits not correct.", expectedgaps,
102    gapfield);
103    }
104   
 
105  0 toggle @Test(groups = ("Functional"))
106    public void testIsProtein()
107    {
108    // test Protein
109  0 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
110    // test DNA
111  0 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
112    // test RNA
113  0 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
114  0 assertFalse(sq.isProtein());
115    // change sequence, should trigger an update of cached result
116  0 sq.setSequence("ASDFASDFADSF");
117  0 assertTrue(sq.isProtein());
118    }
119   
 
120  0 toggle @Test(groups = ("Functional"))
121    public void testIsProteinWithXorNAmbiguityCodes()
122    {
123    // test Protein with N - poly asparagine
124  0 assertTrue(new Sequence("prot", "ASDFASDFASDFNNNNNNNNN").isProtein());
125  0 assertTrue(new Sequence("prot", "NNNNNNNNNNNNNNNNNNNNN").isProtein());
126    // test Protein with X
127  0 assertTrue(new Sequence("prot", "ASDFASDFASDFXXXXXXXXX").isProtein());
128    // test DNA with X
129  0 assertFalse(new Sequence("prot", "ACGTACGTACGTXXXXXXXX").isProtein());
130    // short sequence is nucleotide only if 50% is nucleotide and remaining N/X
131    // is either N or X only
132  0 assertTrue(new Sequence("prot", "ACGTACGTACGTXN").isProtein());
133    // test DNA with N
134  0 assertFalse(new Sequence("prot", "ACGTACGTACGTNNNNNNNN").isProtein());
135    // test RNA with X
136  0 assertFalse(new Sequence("prot", "ACGUACGUACGUACTGACAXX").isProtein());
137  0 assertFalse(new Sequence("prot", "ACGUACGUACGUXXXXXXXXX").isProtein());
138  0 assertFalse(new Sequence("prot", "ACGUACGUACGUNNNNNNNNN").isProtein());
139    }
140   
 
141  0 toggle @Test(groups = { "Functional" })
142    public void testGetAnnotation()
143    {
144    // initial state returns null not an empty array
145  0 assertNull(seq.getAnnotation());
146  0 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
147    1f);
148  0 AlignmentAnnotation[] anns = seq.getAnnotation();
149  0 assertEquals(1, anns.length);
150  0 assertSame(ann, anns[0]);
151   
152    // removing all annotations reverts array to null
153  0 seq.removeAlignmentAnnotation(ann);
154  0 assertNull(seq.getAnnotation());
155    }
156   
 
157  0 toggle @Test(groups = { "Functional" })
158    public void testGetAnnotation_forLabel()
159    {
160  0 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
161    1f);
162  0 addAnnotation("label2", "desc2", "calcId2", 1f);
163  0 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
164    1f);
165  0 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
166  0 assertEquals(2, anns.length);
167  0 assertSame(ann1, anns[0]);
168  0 assertSame(ann3, anns[1]);
169    }
170   
 
171  0 toggle private AlignmentAnnotation addAnnotation(String label,
172    String description, String calcId, float value)
173    {
174  0 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
175    description, value);
176  0 annotation.setCalcId(calcId);
177  0 seq.addAlignmentAnnotation(annotation);
178  0 return annotation;
179    }
180   
 
181  0 toggle @Test(groups = { "Functional" })
182    public void testGetAlignmentAnnotations_forCalcIdAndLabel()
183    {
184  0 addAnnotation("label1", "desc1", "calcId1", 1f);
185  0 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
186    1f);
187  0 addAnnotation("label2", "desc3", "calcId3", 1f);
188  0 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
189    1f);
190  0 addAnnotation("label5", "desc3", null, 1f);
191  0 addAnnotation(null, "desc3", "calcId3", 1f);
192   
193  0 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
194    "label2");
195  0 assertEquals(2, anns.size());
196  0 assertSame(ann2, anns.get(0));
197  0 assertSame(ann4, anns.get(1));
198   
199  0 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
200  0 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
201  0 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
202  0 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
203  0 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
204    }
205   
 
206  0 toggle @Test(groups = { "Functional" })
207    public void testGetAlignmentAnnotations_forCalcIdLabelAndDescription()
208    {
209  0 addAnnotation("label1", "desc1", "calcId1", 1f);
210  0 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
211    1f);
212  0 addAnnotation("label2", "desc3", "calcId3", 1f);
213  0 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
214    1f);
215  0 addAnnotation("label5", "desc3", null, 1f);
216  0 addAnnotation(null, "desc3", "calcId3", 1f);
217   
218  0 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
219    "label2", "desc3");
220  0 assertEquals(1, anns.size());
221  0 assertSame(ann4, anns.get(0));
222    /**
223    * null matching should fail
224    */
225  0 assertTrue(seq.getAlignmentAnnotations("calcId3", "label2", null)
226    .isEmpty());
227   
228  0 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3", null)
229    .isEmpty());
230  0 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5", null)
231    .isEmpty());
232  0 assertTrue(
233    seq.getAlignmentAnnotations("calcId2", null, null).isEmpty());
234  0 assertTrue(seq.getAlignmentAnnotations(null, "label3", null).isEmpty());
235  0 assertTrue(seq.getAlignmentAnnotations(null, null, null).isEmpty());
236    }
237   
238    /**
239    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
240    * setting the sequenceRef on the annotation. Adding the same annotation twice
241    * should be ignored.
242    */
 
243  0 toggle @Test(groups = { "Functional" })
244    public void testAddAlignmentAnnotation()
245    {
246  0 assertNull(seq.getAnnotation());
247  0 final AlignmentAnnotation annotation = new AlignmentAnnotation("a", "b",
248    2d);
249  0 assertNull(annotation.sequenceRef);
250  0 seq.addAlignmentAnnotation(annotation);
251  0 assertSame(seq, annotation.sequenceRef);
252  0 AlignmentAnnotation[] anns = seq.getAnnotation();
253  0 assertEquals(1, anns.length);
254  0 assertSame(annotation, anns[0]);
255   
256    // re-adding does nothing
257  0 seq.addAlignmentAnnotation(annotation);
258  0 anns = seq.getAnnotation();
259  0 assertEquals(1, anns.length);
260  0 assertSame(annotation, anns[0]);
261   
262    // an identical but different annotation can be added
263  0 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
264    "b", 2d);
265  0 seq.addAlignmentAnnotation(annotation2);
266  0 anns = seq.getAnnotation();
267  0 assertEquals(2, anns.length);
268  0 assertSame(annotation, anns[0]);
269  0 assertSame(annotation2, anns[1]);
270    }
271   
 
272  0 toggle @Test(groups = { "Functional" })
273    public void testGetStartGetEnd()
274    {
275  0 SequenceI sq = new Sequence("test", "ABCDEF");
276  0 assertEquals(1, sq.getStart());
277  0 assertEquals(6, sq.getEnd());
278   
279  0 sq = new Sequence("test", "--AB-C-DEF--");
280  0 assertEquals(1, sq.getStart());
281  0 assertEquals(6, sq.getEnd());
282   
283  0 sq = new Sequence("test", "----");
284  0 assertEquals(1, sq.getStart());
285  0 assertEquals(0, sq.getEnd()); // ??
286    }
287   
288    /**
289    * Tests for the method that returns an alignment column position (base 1) for
290    * a given sequence position (base 1).
291    */
 
292  0 toggle @Test(groups = { "Functional" })
293    public void testFindIndex()
294    {
295    /*
296    * call sequenceChanged() after each test to invalidate any cursor,
297    * forcing the 1-arg findIndex to be executed
298    */
299  0 SequenceI sq = new Sequence("test", "ABCDEF");
300  0 assertEquals(0, sq.findIndex(0));
301  0 sq.sequenceChanged();
302  0 assertEquals(1, sq.findIndex(1));
303  0 sq.sequenceChanged();
304  0 assertEquals(5, sq.findIndex(5));
305  0 sq.sequenceChanged();
306  0 assertEquals(6, sq.findIndex(6));
307  0 sq.sequenceChanged();
308  0 assertEquals(6, sq.findIndex(9));
309   
310  0 final String aligned = "-A--B-C-D-E-F--";
311  0 assertEquals(15, aligned.length());
312  0 sq = new Sequence("test/8-13", aligned);
313  0 assertEquals(2, sq.findIndex(8));
314  0 sq.sequenceChanged();
315  0 assertEquals(5, sq.findIndex(9));
316  0 sq.sequenceChanged();
317  0 assertEquals(7, sq.findIndex(10));
318   
319    // before start returns 0
320  0 sq.sequenceChanged();
321  0 assertEquals(0, sq.findIndex(0));
322  0 sq.sequenceChanged();
323  0 assertEquals(0, sq.findIndex(-1));
324   
325    // beyond end returns last residue column
326  0 sq.sequenceChanged();
327  0 assertEquals(13, sq.findIndex(99));
328   
329    /*
330    * residue before sequence 'end' but beyond end of sequence returns
331    * length of sequence (last column) (rightly or wrongly!)
332    */
333  0 sq = new Sequence("test/8-15", "A-B-C-"); // trailing gap case
334  0 assertEquals(6, sq.getLength());
335  0 sq.sequenceChanged();
336  0 assertEquals(sq.getLength(), sq.findIndex(14));
337  0 sq = new Sequence("test/8-99", "-A--B-C-D"); // trailing residue case
338  0 sq.sequenceChanged();
339  0 assertEquals(sq.getLength(), sq.findIndex(65));
340   
341    /*
342    * residue after sequence 'start' but before first residue returns
343    * zero (before first column) (rightly or wrongly!)
344    */
345  0 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
346  0 sq.sequenceChanged();
347  0 assertEquals(0, sq.findIndex(3));
348  0 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
349  0 sq.sequenceChanged();
350  0 assertEquals(0, sq.findIndex(2));
351    }
352   
 
353  0 toggle @Test(groups = { "Functional" })
354    public void testFindPositions()
355    {
356  0 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
357   
358    /*
359    * invalid inputs
360    */
361  0 assertNull(sq.findPositions(6, 5));
362  0 assertNull(sq.findPositions(0, 5));
363  0 assertNull(sq.findPositions(-1, 5));
364   
365    /*
366    * all gapped ranges
367    */
368  0 assertNull(sq.findPositions(1, 1)); // 1-based columns
369  0 assertNull(sq.findPositions(5, 5));
370  0 assertNull(sq.findPositions(5, 6));
371  0 assertNull(sq.findPositions(5, 7));
372   
373    /*
374    * all ungapped ranges
375    */
376  0 assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
377  0 assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
378  0 assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
379  0 assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
380   
381    /*
382    * gap to ungapped range
383    */
384  0 assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
385  0 assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
386   
387    /*
388    * ungapped to gapped range
389    */
390  0 assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
391  0 assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
392   
393    /*
394    * ungapped to ungapped enclosing gaps
395    */
396  0 assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
397  0 assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
398   
399    /*
400    * gapped to gapped enclosing ungapped
401    */
402  0 assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
403  0 assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
404  0 assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
405  0 assertEquals(new Range(8, 13), sq.findPositions(1, 99));
406    }
407   
408    /**
409    * Tests for the method that returns a dataset sequence position (start..) for
410    * an aligned column position (base 0).
411    */
 
412  0 toggle @Test(groups = { "Functional" })
413    public void testFindPosition()
414    {
415    /*
416    * call sequenceChanged() after each test to invalidate any cursor,
417    * forcing the 1-arg findPosition to be executed
418    */
419  0 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
420  0 assertEquals(8, sq.findPosition(0));
421    // Sequence should now hold a cursor at [8, 0]
422  0 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
423    PA.getValue(sq, "cursor").toString());
424  0 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
425  0 int token = (int) PA.getValue(sq, "changeCount");
426  0 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
427   
428  0 sq.sequenceChanged();
429   
430    /*
431    * find F13 at column offset 5, cursor should update to [13, 6]
432    * endColumn is found and saved in cursor
433    */
434  0 assertEquals(13, sq.findPosition(5));
435  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
436  0 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
437  0 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
438  0 assertEquals("test:Pos13:Col6:startCol1:endCol6:tok2",
439    PA.getValue(sq, "cursor").toString());
440   
441    // assertEquals(-1, seq.findPosition(6)); // fails
442   
443  0 sq = new Sequence("test/8-11", "AB-C-D--");
444  0 token = (int) PA.getValue(sq, "changeCount"); // 1 for setStart
445  0 assertEquals(8, sq.findPosition(0));
446  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
447  0 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
448  0 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
449    PA.getValue(sq, "cursor").toString());
450   
451  0 sq.sequenceChanged();
452  0 assertEquals(9, sq.findPosition(1));
453  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
454  0 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
455  0 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
456    PA.getValue(sq, "cursor").toString());
457   
458  0 sq.sequenceChanged();
459    // gap position 'finds' residue to the right (not the left as per javadoc)
460    // cursor is set to the last residue position found [B 2]
461  0 assertEquals(10, sq.findPosition(2));
462  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
463  0 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
464  0 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok3",
465    PA.getValue(sq, "cursor").toString());
466   
467  0 sq.sequenceChanged();
468  0 assertEquals(10, sq.findPosition(3));
469  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
470  0 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
471  0 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
472    PA.getValue(sq, "cursor").toString());
473   
474  0 sq.sequenceChanged();
475    // column[4] is the gap after C - returns D11
476    // cursor is set to [C 4]
477  0 assertEquals(11, sq.findPosition(4));
478  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
479  0 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
480  0 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok5",
481    PA.getValue(sq, "cursor").toString());
482   
483  0 sq.sequenceChanged();
484  0 assertEquals(11, sq.findPosition(5)); // D
485  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
486  0 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
487    // lastCol has been found and saved in the cursor
488  0 assertEquals("test:Pos11:Col6:startCol1:endCol6:tok6",
489    PA.getValue(sq, "cursor").toString());
490   
491  0 sq.sequenceChanged();
492    // returns 1 more than sequence length if off the end ?!?
493  0 assertEquals(12, sq.findPosition(6));
494   
495  0 sq.sequenceChanged();
496  0 assertEquals(12, sq.findPosition(7));
497   
498    /*
499    * first findPosition should also set firstResCol in cursor
500    */
501  0 sq = new Sequence("test/8-13", "--AB-C-DEF--");
502  0 assertEquals(8, sq.findPosition(0));
503  0 assertNull(PA.getValue(sq, "cursor"));
504  0 assertEquals(1, PA.getValue(sq, "changeCount"));
505   
506  0 sq.sequenceChanged();
507  0 assertEquals(8, sq.findPosition(1));
508  0 assertNull(PA.getValue(sq, "cursor"));
509   
510  0 sq.sequenceChanged();
511  0 assertEquals(8, sq.findPosition(2));
512  0 assertEquals("test:Pos8:Col3:startCol3:endCol0:tok3",
513    PA.getValue(sq, "cursor").toString());
514   
515  0 sq.sequenceChanged();
516  0 assertEquals(9, sq.findPosition(3));
517  0 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
518    PA.getValue(sq, "cursor").toString());
519   
520  0 sq.sequenceChanged();
521    // column[4] is a gap, returns next residue pos (C10)
522    // cursor is set to last residue found [B]
523  0 assertEquals(10, sq.findPosition(4));
524  0 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok5",
525    PA.getValue(sq, "cursor").toString());
526   
527  0 sq.sequenceChanged();
528  0 assertEquals(10, sq.findPosition(5));
529  0 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
530    PA.getValue(sq, "cursor").toString());
531   
532  0 sq.sequenceChanged();
533    // column[6] is a gap, returns next residue pos (D11)
534    // cursor is set to last residue found [C]
535  0 assertEquals(11, sq.findPosition(6));
536  0 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok7",
537    PA.getValue(sq, "cursor").toString());
538   
539  0 sq.sequenceChanged();
540  0 assertEquals(11, sq.findPosition(7));
541  0 assertEquals("test:Pos11:Col8:startCol3:endCol0:tok8",
542    PA.getValue(sq, "cursor").toString());
543   
544  0 sq.sequenceChanged();
545  0 assertEquals(12, sq.findPosition(8));
546  0 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok9",
547    PA.getValue(sq, "cursor").toString());
548   
549    /*
550    * when the last residue column is found, it is set in the cursor
551    */
552  0 sq.sequenceChanged();
553  0 assertEquals(13, sq.findPosition(9));
554  0 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
555    PA.getValue(sq, "cursor").toString());
556   
557  0 sq.sequenceChanged();
558  0 assertEquals(14, sq.findPosition(10));
559  0 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
560    PA.getValue(sq, "cursor").toString());
561   
562    /*
563    * findPosition for column beyond sequence length
564    * returns 1 more than last residue position
565    */
566  0 sq.sequenceChanged();
567  0 assertEquals(14, sq.findPosition(11));
568  0 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
569    PA.getValue(sq, "cursor").toString());
570   
571  0 sq.sequenceChanged();
572  0 assertEquals(14, sq.findPosition(99));
573  0 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok13",
574    PA.getValue(sq, "cursor").toString());
575   
576    /*
577    * gapped sequence ending in non-gap
578    */
579  0 sq = new Sequence("test/8-13", "--AB-C-DEF");
580  0 assertEquals(13, sq.findPosition(9));
581  0 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
582    PA.getValue(sq, "cursor").toString());
583  0 sq.sequenceChanged();
584  0 assertEquals(12, sq.findPosition(8)); // E12
585    // sequenceChanged() invalidates cursor.lastResidueColumn
586  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
587  0 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok2",
588    cursor.toString());
589    // findPosition with cursor accepts base 1 column values
590  0 assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
591  0 assertEquals(13, sq.findPosition(9)); // F13
592    // lastResidueColumn has now been found and saved in cursor
593  0 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok2",
594    PA.getValue(sq, "cursor").toString());
595    }
596   
 
597  0 toggle @Test(groups = { "Functional" })
598    public void testDeleteChars()
599    {
600    /*
601    * internal delete
602    */
603  0 SequenceI sq = new Sequence("test", "ABCDEF");
604  0 assertNull(PA.getValue(sq, "datasetSequence"));
605  0 assertEquals(1, sq.getStart());
606  0 assertEquals(6, sq.getEnd());
607  0 sq.deleteChars(2, 3);
608  0 assertEquals("ABDEF", sq.getSequenceAsString());
609  0 assertEquals(1, sq.getStart());
610  0 assertEquals(5, sq.getEnd());
611  0 assertNull(PA.getValue(sq, "datasetSequence"));
612   
613    /*
614    * delete at start
615    */
616  0 sq = new Sequence("test", "ABCDEF");
617  0 sq.deleteChars(0, 2);
618  0 assertEquals("CDEF", sq.getSequenceAsString());
619  0 assertEquals(3, sq.getStart());
620  0 assertEquals(6, sq.getEnd());
621  0 assertNull(PA.getValue(sq, "datasetSequence"));
622   
623  0 sq = new Sequence("test", "ABCDE");
624  0 sq.deleteChars(0, 3);
625  0 assertEquals("DE", sq.getSequenceAsString());
626  0 assertEquals(4, sq.getStart());
627  0 assertEquals(5, sq.getEnd());
628  0 assertNull(PA.getValue(sq, "datasetSequence"));
629   
630    /*
631    * delete at end
632    */
633  0 sq = new Sequence("test", "ABCDEF");
634  0 sq.deleteChars(4, 6);
635  0 assertEquals("ABCD", sq.getSequenceAsString());
636  0 assertEquals(1, sq.getStart());
637  0 assertEquals(4, sq.getEnd());
638  0 assertNull(PA.getValue(sq, "datasetSequence"));
639   
640    /*
641    * delete more positions than there are
642    */
643  0 sq = new Sequence("test/8-11", "ABCD");
644  0 sq.deleteChars(0, 99);
645  0 assertEquals("", sq.getSequenceAsString());
646  0 assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
647  0 assertEquals(11, sq.getEnd());
648   
649  0 sq = new Sequence("test/8-11", "----");
650  0 sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
651  0 assertEquals("", sq.getSequenceAsString());
652  0 assertEquals(8, sq.getStart());
653  0 assertEquals(11, sq.getEnd());
654    }
655   
 
656  0 toggle @Test(groups = { "Functional" })
657    public void testDeleteChars_withDbRefsAndFeatures()
658    {
659    /*
660    * internal delete - new dataset sequence created
661    * gets a copy of any dbrefs
662    */
663  0 SequenceI sq = new Sequence("test", "ABCDEF");
664  0 sq.createDatasetSequence();
665  0 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
666  0 sq.addDBRef(dbr1);
667  0 Object ds = PA.getValue(sq, "datasetSequence");
668  0 assertNotNull(ds);
669  0 assertEquals(1, sq.getStart());
670  0 assertEquals(6, sq.getEnd());
671  0 sq.deleteChars(2, 3);
672  0 assertEquals("ABDEF", sq.getSequenceAsString());
673  0 assertEquals(1, sq.getStart());
674  0 assertEquals(5, sq.getEnd());
675  0 Object newDs = PA.getValue(sq, "datasetSequence");
676  0 assertNotNull(newDs);
677  0 assertNotSame(ds, newDs);
678  0 assertNotNull(sq.getDBRefs());
679  0 assertEquals(1, sq.getDBRefs().size());
680  0 assertNotSame(dbr1, sq.getDBRefs().get(0));
681  0 assertEquals(dbr1, sq.getDBRefs().get(0));
682   
683    /*
684    * internal delete with sequence features
685    * (failure case for JAL-2541)
686    */
687  0 sq = new Sequence("test", "ABCDEF");
688  0 sq.createDatasetSequence();
689  0 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
690    "CathGroup");
691  0 sq.addSequenceFeature(sf1);
692  0 ds = PA.getValue(sq, "datasetSequence");
693  0 assertNotNull(ds);
694  0 assertEquals(1, sq.getStart());
695  0 assertEquals(6, sq.getEnd());
696  0 sq.deleteChars(2, 4);
697  0 assertEquals("ABEF", sq.getSequenceAsString());
698  0 assertEquals(1, sq.getStart());
699  0 assertEquals(4, sq.getEnd());
700  0 newDs = PA.getValue(sq, "datasetSequence");
701  0 assertNotNull(newDs);
702  0 assertNotSame(ds, newDs);
703  0 List<SequenceFeature> sfs = sq.getSequenceFeatures();
704  0 assertEquals(1, sfs.size());
705  0 assertNotSame(sf1, sfs.get(0));
706  0 assertEquals(sf1, sfs.get(0));
707   
708    /*
709    * delete at start - no new dataset sequence created
710    * any sequence features remain as before
711    */
712  0 sq = new Sequence("test", "ABCDEF");
713  0 sq.createDatasetSequence();
714  0 ds = PA.getValue(sq, "datasetSequence");
715  0 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
716  0 sq.addSequenceFeature(sf1);
717  0 sq.deleteChars(0, 2);
718  0 assertEquals("CDEF", sq.getSequenceAsString());
719  0 assertEquals(3, sq.getStart());
720  0 assertEquals(6, sq.getEnd());
721  0 assertSame(ds, PA.getValue(sq, "datasetSequence"));
722  0 sfs = sq.getSequenceFeatures();
723  0 assertNotNull(sfs);
724  0 assertEquals(1, sfs.size());
725  0 assertSame(sf1, sfs.get(0));
726   
727    /*
728    * delete at end - no new dataset sequence created
729    * any dbrefs remain as before
730    */
731  0 sq = new Sequence("test", "ABCDEF");
732  0 sq.createDatasetSequence();
733  0 ds = PA.getValue(sq, "datasetSequence");
734  0 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
735  0 sq.addDBRef(dbr1);
736  0 sq.deleteChars(4, 6);
737  0 assertEquals("ABCD", sq.getSequenceAsString());
738  0 assertEquals(1, sq.getStart());
739  0 assertEquals(4, sq.getEnd());
740  0 assertSame(ds, PA.getValue(sq, "datasetSequence"));
741  0 assertNotNull(sq.getDBRefs());
742  0 assertEquals(1, sq.getDBRefs().size());
743  0 assertSame(dbr1, sq.getDBRefs().get(0));
744    }
745   
 
746  0 toggle @Test(groups = { "Functional" })
747    public void testInsertCharAt()
748    {
749    // non-static methods:
750  0 SequenceI sq = new Sequence("test", "ABCDEF");
751  0 sq.insertCharAt(0, 'z');
752  0 assertEquals("zABCDEF", sq.getSequenceAsString());
753  0 sq.insertCharAt(2, 2, 'x');
754  0 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
755   
756    // for static method see StringUtilsTest
757    }
758   
759    /**
760    * Test the method that returns an array of aligned sequence positions where
761    * the array index is the data sequence position (both base 0).
762    */
 
763  0 toggle @Test(groups = { "Functional" })
764    public void testGapMap()
765    {
766  0 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
767  0 sq.createDatasetSequence();
768  0 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
769    }
770   
771    /**
772    * Test the method that gets sequence features, either from the sequence or
773    * its dataset.
774    */
 
775  0 toggle @Test(groups = { "Functional" })
776    public void testGetSequenceFeatures()
777    {
778  0 SequenceI sq = new Sequence("test", "GATCAT");
779  0 sq.createDatasetSequence();
780   
781  0 assertTrue(sq.getSequenceFeatures().isEmpty());
782   
783    /*
784    * SequenceFeature on sequence
785    */
786  0 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f,
787    null);
788  0 sq.addSequenceFeature(sf);
789  0 List<SequenceFeature> sfs = sq.getSequenceFeatures();
790  0 assertEquals(1, sfs.size());
791  0 assertSame(sf, sfs.get(0));
792   
793    /*
794    * SequenceFeature on sequence and dataset sequence; returns that on
795    * sequence
796    *
797    * Note JAL-2046: spurious: we have no use case for this at the moment.
798    * This test also buggy - as sf2.equals(sf), no new feature is added
799    */
800  0 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
801    null);
802  0 sq.getDatasetSequence().addSequenceFeature(sf2);
803  0 sfs = sq.getSequenceFeatures();
804  0 assertEquals(1, sfs.size());
805  0 assertSame(sf, sfs.get(0));
806   
807    /*
808    * SequenceFeature on dataset sequence only
809    * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
810    */
811  0 sq.setSequenceFeatures(null);
812  0 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
813   
814    /*
815    * Corrupt case - no SequenceFeature, dataset's dataset is the original
816    * sequence. Test shows no infinite loop results.
817    */
818  0 sq.getDatasetSequence().setSequenceFeatures(null);
819    /**
820    * is there a usecase for this ? setDatasetSequence should throw an error if
821    * this actually occurs.
822    */
823  0 try
824    {
825  0 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
826  0 Assert.fail(
827    "Expected Error to be raised when calling setDatasetSequence with self reference");
828    } catch (IllegalArgumentException e)
829    {
830    // TODO Jalview error/exception class for raising implementation errors
831  0 assertTrue(e.getMessage().toLowerCase(Locale.ROOT)
832    .contains("implementation error"));
833    }
834  0 assertTrue(sq.getSequenceFeatures().isEmpty());
835    }
836   
837    /**
838    * Test the method that returns an array, indexed by sequence position, whose
839    * entries are the residue positions at the sequence position (or to the right
840    * if a gap)
841    */
 
842  0 toggle @Test(groups = { "Functional" })
843    public void testFindPositionMap()
844    {
845    /*
846    * Note: Javadoc for findPosition says it returns the residue position to
847    * the left of a gapped position; in fact it returns the position to the
848    * right. Also it returns a non-existent residue position for a gap beyond
849    * the sequence.
850    */
851  0 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
852  0 int[] map = sq.findPositionMap();
853  0 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
854    Arrays.toString(map));
855    }
856   
857    /**
858    * Test for getSubsequence
859    */
 
860  0 toggle @Test(groups = { "Functional" })
861    public void testGetSubsequence()
862    {
863  0 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
864  0 sq.createDatasetSequence();
865   
866    // positions are base 0, end position is exclusive
867  0 SequenceI subseq = sq.getSubSequence(2, 4);
868   
869  0 assertEquals("CD", subseq.getSequenceAsString());
870    // start/end are base 1 positions
871  0 assertEquals(3, subseq.getStart());
872  0 assertEquals(4, subseq.getEnd());
873    // subsequence shares the full dataset sequence
874  0 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
875    }
876   
877    /**
878    * test createDatasetSequence behaves to doc
879    */
 
880  0 toggle @Test(groups = { "Functional" })
881    public void testCreateDatasetSequence()
882    {
883  0 SequenceI sq = new Sequence("my", "ASDASD");
884  0 sq.addSequenceFeature(
885    new SequenceFeature("type", "desc", 1, 10, 1f, "group"));
886  0 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
887  0 assertNull(sq.getDatasetSequence());
888  0 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
889  0 assertNotNull(PA.getValue(sq, "dbrefs"));
890   
891  0 SequenceI rds = sq.createDatasetSequence();
892  0 assertNotNull(rds);
893  0 assertNull(rds.getDatasetSequence());
894  0 assertSame(sq.getDatasetSequence(), rds);
895   
896    // sequence features and dbrefs transferred to dataset sequence
897  0 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
898  0 assertNull(PA.getValue(sq, "dbrefs"));
899  0 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
900  0 assertNotNull(PA.getValue(rds, "dbrefs"));
901    }
902   
903    /**
904    * Test for deriveSequence applied to a sequence with a dataset
905    */
 
906  0 toggle @Test(groups = { "Functional" })
907    public void testDeriveSequence_existingDataset()
908    {
909  0 Sequence sq = new Sequence("Seq1", "CD");
910  0 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
911  0 sq.getDatasetSequence().addSequenceFeature(
912    new SequenceFeature("", "", 1, 2, 0f, null));
913  0 sq.setStart(3);
914  0 sq.setEnd(4);
915   
916  0 sq.setDescription("Test sequence description..");
917  0 sq.setVamsasId("TestVamsasId");
918  0 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
919   
920  0 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
921  0 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
922  0 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
923  0 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
924   
925  0 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
926  0 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
927  0 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
928  0 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
929   
930    // these are the same as ones already added
931  0 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
932  0 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
933   
934  0 List<DBRefEntry> primRefs = Arrays
935    .asList(new DBRefEntry[]
936    { pdb1pdb, pdb2pdb });
937   
938  0 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
939  0 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
940  0 sq.getDatasetSequence()
941    .addDBRef(new DBRefEntry("PDB", "version3", "3PDB")); // should do
942    // nothing
943  0 sq.getDatasetSequence()
944    .addDBRef(new DBRefEntry("PDB", "version4", "4PDB")); // should do
945    // nothing
946   
947  0 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
948  0 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
949  0 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
950    "filePath/test2");
951  0 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
952    "filePath/test2");
953  0 sq.getDatasetSequence().addPDBId(pdbe1a);
954  0 sq.getDatasetSequence().addPDBId(pdbe1b);
955  0 sq.getDatasetSequence().addPDBId(pdbe2a);
956  0 sq.getDatasetSequence().addPDBId(pdbe2b);
957   
958    /*
959    * test we added pdb entries to the dataset sequence
960    */
961  0 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(),
962    Arrays.asList(new PDBEntry[]
963    { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
964    "PDB Entries were not found on dataset sequence.");
965   
966    /*
967    * we should recover a pdb entry that is on the dataset sequence via PDBEntry
968    */
969  0 Assert.assertEquals(pdbe1a, sq.getDatasetSequence().getPDBEntry("1PDB"),
970    "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
971  0 ArrayList<Annotation> annotsList = new ArrayList<>();
972  0 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
973  0 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
974  0 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
975  0 Annotation[] annots = annotsList.toArray(new Annotation[0]);
976  0 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
977    "Test annot description", annots));
978  0 sq.getDatasetSequence().addAlignmentAnnotation(new AlignmentAnnotation(
979    "Test annot", "Test annot description", annots));
980  0 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
981  0 Assert.assertEquals(sq.getDBRefs().size(), 5); // DBRefs are on dataset
982    // sequence
983  0 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
984  0 Assert.assertNotNull(sq.getAnnotation());
985  0 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
986  0 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().size(), 5); // same
987    // as
988    // sq.getDBRefs()
989  0 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
990    4);
991  0 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
992   
993  0 Sequence derived = (Sequence) sq.deriveSequence();
994   
995  0 Assert.assertEquals(derived.getDescription(),
996    "Test sequence description..");
997  0 Assert.assertEquals(derived.getDBRefs().size(), 5); // come from dataset
998  0 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
999  0 Assert.assertNotNull(derived.getAnnotation());
1000  0 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
1001  0 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().size(), 5);
1002  0 Assert.assertEquals(
1003    derived.getDatasetSequence().getAllPDBEntries().size(), 4);
1004  0 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
1005   
1006  0 assertEquals("CD", derived.getSequenceAsString());
1007  0 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
1008   
1009    // derived sequence should access dataset sequence features
1010  0 assertNotNull(sq.getSequenceFeatures());
1011  0 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
1012   
1013    /*
1014    * verify we have primary db refs *just* for PDB IDs with associated
1015    * PDBEntry objects
1016    */
1017   
1018  0 assertEquals(primRefs, sq.getPrimaryDBRefs());
1019  0 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
1020   
1021  0 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
1022   
1023    }
1024   
1025    /**
1026    * Test for deriveSequence applied to an ungapped sequence with no dataset
1027    */
 
1028  0 toggle @Test(groups = { "Functional" })
1029    public void testDeriveSequence_noDatasetUngapped()
1030    {
1031  0 SequenceI sq = new Sequence("Seq1", "ABCDEF");
1032  0 assertEquals(1, sq.getStart());
1033  0 assertEquals(6, sq.getEnd());
1034  0 SequenceI derived = sq.deriveSequence();
1035  0 assertEquals("ABCDEF", derived.getSequenceAsString());
1036  0 assertEquals("ABCDEF",
1037    derived.getDatasetSequence().getSequenceAsString());
1038    }
1039   
1040    /**
1041    * Test for deriveSequence applied to a gapped sequence with no dataset
1042    */
 
1043  0 toggle @Test(groups = { "Functional" })
1044    public void testDeriveSequence_noDatasetGapped()
1045    {
1046  0 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
1047  0 assertEquals(1, sq.getStart());
1048  0 assertEquals(6, sq.getEnd());
1049  0 assertNull(sq.getDatasetSequence());
1050  0 SequenceI derived = sq.deriveSequence();
1051  0 assertEquals("AB-C.D EF", derived.getSequenceAsString());
1052  0 assertEquals("ABCDEF",
1053    derived.getDatasetSequence().getSequenceAsString());
1054    }
1055   
1056    /**
1057    * test that creating a copy of an existing sequence with dataset sequence and
1058    * associated contact matrix yields annotation associated with the same
1059    * contact matrix in the copy
1060    */
 
1061  0 toggle @Test(groups = { "Functional" })
1062    public void testCopyPasteStyleDerivesequence_withcontactMatrixAnn()
1063    {
1064  0 SequenceI seq1 = new Sequence("seq1", "ACDACDACD");
1065  0 seq1.createDatasetSequence();
1066  0 ContactMatrixI cm = new SeqDistanceContactMatrix(seq1.getLength());
1067    // addContactList needs to return annotation addable to the sequence
1068    // reference it was called from
1069  0 AlignmentAnnotation aann = seq1.addContactList(cm);
1070  0 assertTrue(aann.sequenceRef == seq1);
1071  0 assertEquals(1, seq1.getAnnotation().length);
1072  0 assertNotNull(seq1.getContactListFor(seq1.getAnnotation()[0], 1));
1073   
1074  0 SequenceI seq_derived = seq1.deriveSequence();
1075  0 assertEquals(1, seq_derived.getAnnotation().length);
1076  0 assertTrue(cm == seq_derived
1077    .getContactMatrixFor(seq_derived.getAnnotation()[0]));
1078  0 assertNotNull(seq_derived
1079    .getContactListFor(seq_derived.getAnnotation()[0], 1));
1080   
1081    // copy paste actually uses the copy constructor .. so
1082   
1083  0 SequenceI seq_copied = new Sequence((Sequence) seq_derived);
1084  0 assertEquals(1, seq_copied.getAnnotation().length);
1085  0 assertTrue(cm == seq_copied
1086    .getContactMatrixFor(seq_copied.getAnnotation()[0]));
1087  0 assertNotNull(
1088    seq_copied.getContactListFor(seq_copied.getAnnotation()[0], 1));
1089   
1090    }
1091   
 
1092  0 toggle @Test(groups = { "Functional" })
1093    public void testCopyConstructor_noDataset()
1094    {
1095  0 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1096  0 seq1.setDescription("description");
1097  0 seq1.addAlignmentAnnotation(
1098    new AlignmentAnnotation("label", "desc", 1.3d));
1099  0 seq1.addSequenceFeature(
1100    new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1101  0 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1102  0 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1103   
1104  0 SequenceI copy = new Sequence(seq1);
1105   
1106  0 assertNull(copy.getDatasetSequence());
1107   
1108  0 verifyCopiedSequence(seq1, copy);
1109   
1110    // copy has a copy of the DBRefEntry
1111    // this is murky - DBrefs are only copied for dataset sequences
1112    // where the test for 'dataset sequence' is 'dataset is null'
1113    // but that doesn't distinguish it from an aligned sequence
1114    // which has not yet generated a dataset sequence
1115    // NB getDBRef looks inside dataset sequence if not null
1116  0 List<DBRefEntry> dbrefs = copy.getDBRefs();
1117  0 assertEquals(1, dbrefs.size());
1118  0 assertFalse(dbrefs.get(0) == seq1.getDBRefs().get(0));
1119  0 assertTrue(dbrefs.get(0).equals(seq1.getDBRefs().get(0)));
1120    }
1121   
 
1122  0 toggle @Test(groups = { "Functional" })
1123    public void testCopyConstructor_withDataset()
1124    {
1125  0 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1126  0 seq1.createDatasetSequence();
1127  0 seq1.setDescription("description");
1128  0 seq1.addAlignmentAnnotation(
1129    new AlignmentAnnotation("label", "desc", 1.3d));
1130    // JAL-2046 - what is the contract for using a derived sequence's
1131    // addSequenceFeature ?
1132  0 seq1.addSequenceFeature(
1133    new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1134  0 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1135    // here we add DBRef to the dataset sequence:
1136  0 seq1.getDatasetSequence()
1137    .addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1138   
1139  0 SequenceI copy = new Sequence(seq1);
1140   
1141  0 assertNotNull(copy.getDatasetSequence());
1142  0 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1143   
1144  0 verifyCopiedSequence(seq1, copy);
1145   
1146    // getDBRef looks inside dataset sequence and this is shared,
1147    // so holds the same dbref objects
1148  0 List<DBRefEntry> dbrefs = copy.getDBRefs();
1149  0 assertEquals(1, dbrefs.size());
1150  0 assertSame(dbrefs.get(0), seq1.getDBRefs().get(0));
1151    }
1152   
1153    /**
1154    * Helper to make assertions about a copied sequence
1155    *
1156    * @param seq1
1157    * @param copy
1158    */
 
1159  0 toggle protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1160    {
1161    // verify basic properties:
1162  0 assertEquals(copy.getName(), seq1.getName());
1163  0 assertEquals(copy.getDescription(), seq1.getDescription());
1164  0 assertEquals(copy.getStart(), seq1.getStart());
1165  0 assertEquals(copy.getEnd(), seq1.getEnd());
1166  0 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1167   
1168    // copy has a copy of the annotation:
1169  0 AlignmentAnnotation[] anns = copy.getAnnotation();
1170  0 assertEquals(1, anns.length);
1171  0 assertFalse(anns[0] == seq1.getAnnotation()[0]);
1172  0 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1173  0 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1174  0 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1175   
1176    // copy has a copy of the sequence feature:
1177  0 List<SequenceFeature> sfs = copy.getSequenceFeatures();
1178  0 assertEquals(1, sfs.size());
1179  0 if (seq1.getDatasetSequence() != null
1180    && copy.getDatasetSequence() == seq1.getDatasetSequence())
1181    {
1182  0 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1183    }
1184    else
1185    {
1186  0 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1187    }
1188  0 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1189   
1190    // copy has a copy of the PDB entry
1191  0 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1192  0 assertEquals(1, pdbs.size());
1193  0 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1194  0 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1195    }
1196   
 
1197  0 toggle @Test(groups = "Functional")
1198    public void testGetCharAt()
1199    {
1200  0 SequenceI sq = new Sequence("", "abcde");
1201  0 assertEquals('a', sq.getCharAt(0));
1202  0 assertEquals('e', sq.getCharAt(4));
1203  0 assertEquals(' ', sq.getCharAt(5));
1204  0 assertEquals(' ', sq.getCharAt(-1));
1205    }
1206   
 
1207  0 toggle @Test(groups = { "Functional" })
1208    public void testAddSequenceFeatures()
1209    {
1210  0 SequenceI sq = new Sequence("", "abcde");
1211    // type may not be null
1212  0 assertFalse(sq.addSequenceFeature(
1213    new SequenceFeature(null, "desc", 4, 8, 0f, null)));
1214  0 assertTrue(sq.addSequenceFeature(
1215    new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1216    // can't add a duplicate feature
1217  0 assertFalse(sq.addSequenceFeature(
1218    new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1219    // can add a different feature
1220  0 assertTrue(sq.addSequenceFeature(
1221    new SequenceFeature("Scop", "desc", 4, 8, 0f, null))); // different
1222    // type
1223  0 assertTrue(sq.addSequenceFeature(
1224    new SequenceFeature("Cath", "description", 4, 8, 0f, null)));// different
1225    // description
1226  0 assertTrue(sq.addSequenceFeature(
1227    new SequenceFeature("Cath", "desc", 3, 8, 0f, null))); // different
1228    // start
1229    // position
1230  0 assertTrue(sq.addSequenceFeature(
1231    new SequenceFeature("Cath", "desc", 4, 9, 0f, null))); // different
1232    // end
1233    // position
1234  0 assertTrue(sq.addSequenceFeature(
1235    new SequenceFeature("Cath", "desc", 4, 8, 1f, null))); // different
1236    // score
1237  0 assertTrue(sq.addSequenceFeature(
1238    new SequenceFeature("Cath", "desc", 4, 8, Float.NaN, null))); // score
1239    // NaN
1240  0 assertTrue(sq.addSequenceFeature(
1241    new SequenceFeature("Cath", "desc", 4, 8, 0f, "Metal"))); // different
1242    // group
1243  0 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1244    }
1245   
1246    /**
1247    * Tests for adding (or updating) dbrefs
1248    *
1249    * @see DBRefEntry#updateFrom(DBRefEntry)
1250    */
 
1251  0 toggle @Test(groups = { "Functional" })
1252    public void testAddDBRef()
1253    {
1254  0 SequenceI sq = new Sequence("", "abcde");
1255  0 assertNull(sq.getDBRefs());
1256  0 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1257  0 sq.addDBRef(dbref);
1258  0 assertEquals(1, sq.getDBRefs().size());
1259  0 assertSame(dbref, sq.getDBRefs().get(0));
1260   
1261    /*
1262    * change of version - new entry
1263    */
1264  0 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1265  0 sq.addDBRef(dbref2);
1266  0 assertEquals(2, sq.getDBRefs().size());
1267  0 assertSame(dbref, sq.getDBRefs().get(0));
1268  0 assertSame(dbref2, sq.getDBRefs().get(1));
1269   
1270    /*
1271    * matches existing entry - not added
1272    */
1273  0 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1274  0 assertEquals(2, sq.getDBRefs().size());
1275   
1276    /*
1277    * different source = new entry
1278    */
1279  0 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1280  0 sq.addDBRef(dbref3);
1281  0 assertEquals(3, sq.getDBRefs().size());
1282  0 assertSame(dbref3, sq.getDBRefs().get(2));
1283   
1284    /*
1285    * different ref = new entry
1286    */
1287  0 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1288  0 sq.addDBRef(dbref4);
1289  0 assertEquals(4, sq.getDBRefs().size());
1290  0 assertSame(dbref4, sq.getDBRefs().get(3));
1291   
1292    /*
1293    * matching ref with a mapping - map updated
1294    */
1295  0 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1296  0 Mapping map = new Mapping(
1297    new MapList(new int[]
1298    { 1, 3 }, new int[] { 1, 1 }, 3, 1));
1299  0 dbref5.setMap(map);
1300  0 sq.addDBRef(dbref5);
1301  0 assertEquals(4, sq.getDBRefs().size());
1302  0 assertSame(dbref4, sq.getDBRefs().get(3));
1303  0 assertSame(map, dbref4.getMap());
1304   
1305    /*
1306    * 'real' version replaces "0" version
1307    */
1308  0 dbref2.setVersion("0");
1309  0 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1310    dbref2.getAccessionId());
1311  0 sq.addDBRef(dbref6);
1312  0 assertEquals(4, sq.getDBRefs().size());
1313  0 assertSame(dbref2, sq.getDBRefs().get(1));
1314  0 assertEquals("3", dbref2.getVersion());
1315   
1316    /*
1317    * 'real' version replaces "source:0" version
1318    */
1319  0 dbref3.setVersion("Uniprot:0");
1320  0 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1321    dbref3.getAccessionId());
1322  0 sq.addDBRef(dbref7);
1323  0 assertEquals(4, sq.getDBRefs().size());
1324  0 assertSame(dbref3, sq.getDBRefs().get(2));
1325  0 assertEquals("3", dbref2.getVersion());
1326    }
1327   
 
1328  0 toggle @Test(groups = { "Functional" })
1329    public void testGetPrimaryDBRefs_peptide()
1330    {
1331  0 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1332   
1333    // no dbrefs
1334  0 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1335  0 assertTrue(primaryDBRefs.isEmpty());
1336   
1337    // empty dbrefs
1338  0 sq.setDBRefs(null);
1339  0 primaryDBRefs = sq.getPrimaryDBRefs();
1340  0 assertTrue(primaryDBRefs.isEmpty());
1341   
1342    // primary - uniprot
1343  0 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1344  0 sq.addDBRef(upentry1);
1345   
1346    // primary - uniprot with congruent map
1347  0 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1348  0 upentry2.setMap(
1349    new Mapping(null, new MapList(new int[]
1350    { 10, 22 }, new int[] { 10, 22 }, 1, 1)));
1351  0 sq.addDBRef(upentry2);
1352   
1353    // primary - uniprot with map of enclosing sequence
1354  0 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1355  0 upentry3.setMap(
1356    new Mapping(null, new MapList(new int[]
1357    { 8, 24 }, new int[] { 8, 24 }, 1, 1)));
1358  0 sq.addDBRef(upentry3);
1359   
1360    // not primary - uniprot with map of sub-sequence (5')
1361  0 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1362  0 upentry4.setMap(
1363    new Mapping(null, new MapList(new int[]
1364    { 10, 18 }, new int[] { 10, 18 }, 1, 1)));
1365  0 sq.addDBRef(upentry4);
1366   
1367    // not primary - uniprot with map that overlaps 3'
1368  0 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1369  0 upentry5.setMap(
1370    new Mapping(null, new MapList(new int[]
1371    { 12, 22 }, new int[] { 12, 22 }, 1, 1)));
1372  0 sq.addDBRef(upentry5);
1373   
1374    // not primary - uniprot with map to different coordinates frame
1375  0 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1376  0 upentry6.setMap(
1377    new Mapping(null, new MapList(new int[]
1378    { 12, 18 }, new int[] { 112, 118 }, 1, 1)));
1379  0 sq.addDBRef(upentry6);
1380   
1381    // not primary - dbref to 'non-core' database
1382  0 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1383  0 sq.addDBRef(upentry7);
1384   
1385    // primary - type is PDB
1386  0 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1387  0 sq.addDBRef(pdbentry);
1388   
1389    // not primary - PDBEntry has no file
1390  0 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1391   
1392    // not primary - no PDBEntry
1393  0 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1394   
1395    // add corroborating PDB entry for primary DBref -
1396    // needs to have a file as well as matching ID
1397    // note PDB ID is not treated as case sensitive
1398  0 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB,
1399    new File("/blah").toString()));
1400   
1401    // not valid DBRef - no file..
1402  0 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1403   
1404  0 primaryDBRefs = sq.getPrimaryDBRefs();
1405  0 assertEquals(4, primaryDBRefs.size());
1406  0 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1407    primaryDBRefs.contains(upentry1));
1408  0 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1409    primaryDBRefs.contains(upentry2));
1410  0 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1411    primaryDBRefs.contains(upentry3));
1412  0 assertTrue("Couldn't find expected PDB primary reference",
1413    primaryDBRefs.contains(pdbentry));
1414    }
1415   
 
1416  0 toggle @Test(groups = { "Functional" })
1417    public void testGetPrimaryDBRefs_nucleotide()
1418    {
1419  0 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10,
1420    34);
1421   
1422    // primary - Ensembl
1423  0 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1424  0 sq.addDBRef(dbr1);
1425   
1426    // not primary - Ensembl 'transcript' mapping of sub-sequence
1427  0 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1428  0 dbr2.setMap(
1429    new Mapping(null, new MapList(new int[]
1430    { 15, 25 }, new int[] { 1, 11 }, 1, 1)));
1431  0 sq.addDBRef(dbr2);
1432   
1433    // primary - EMBL with congruent map
1434  0 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1435  0 dbr3.setMap(
1436    new Mapping(null, new MapList(new int[]
1437    { 10, 34 }, new int[] { 10, 34 }, 1, 1)));
1438  0 sq.addDBRef(dbr3);
1439   
1440    // not primary - to non-core database
1441  0 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1442  0 sq.addDBRef(dbr4);
1443   
1444    // not primary - to protein
1445  0 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1446  0 sq.addDBRef(dbr5);
1447   
1448  0 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1449  0 assertEquals(2, primaryDBRefs.size());
1450  0 assertTrue(primaryDBRefs.contains(dbr1));
1451  0 assertTrue(primaryDBRefs.contains(dbr3));
1452    }
1453   
1454    /**
1455    * Test the method that updates the list of PDBEntry from any new DBRefEntry
1456    * for PDB
1457    */
 
1458  0 toggle @Test(groups = { "Functional" })
1459    public void testUpdatePDBIds()
1460    {
1461  0 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1462  0 seq.addPDBId(pdbe1);
1463  0 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1464  0 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1465  0 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1466  0 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1467    // 7 is not a valid chain code:
1468  0 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1469   
1470  0 seq.updatePDBIds();
1471  0 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1472  0 assertEquals(4, pdbIds.size());
1473  0 assertSame(pdbe1, pdbIds.get(0));
1474    // chain code got added to 3A6S:
1475  0 assertEquals("B", pdbe1.getChainCode());
1476  0 assertEquals("1A70", pdbIds.get(1).getId());
1477    // 4BQGA is parsed into id + chain
1478  0 assertEquals("4BQG", pdbIds.get(2).getId());
1479  0 assertEquals("a", pdbIds.get(2).getChainCode());
1480  0 assertEquals("2GIS7", pdbIds.get(3).getId());
1481  0 assertNull(pdbIds.get(3).getChainCode());
1482    }
1483   
1484    /**
1485    * Test the method that either adds a pdbid or updates an existing one
1486    */
 
1487  0 toggle @Test(groups = { "Functional" })
1488    public void testAddPDBId()
1489    {
1490  0 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1491  0 seq.addPDBId(pdbe);
1492  0 assertEquals(1, seq.getAllPDBEntries().size());
1493  0 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1494  0 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1495   
1496    // add the same entry
1497  0 seq.addPDBId(pdbe);
1498  0 assertEquals(1, seq.getAllPDBEntries().size());
1499  0 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1500   
1501    // add an identical entry
1502  0 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1503  0 assertEquals(1, seq.getAllPDBEntries().size());
1504  0 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1505   
1506    // add a different entry
1507  0 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1508  0 seq.addPDBId(pdbe2);
1509  0 assertEquals(2, seq.getAllPDBEntries().size());
1510  0 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1511  0 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1512   
1513    // update pdbe with chain code, file, type
1514  0 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1515  0 seq.addPDBId(pdbe3);
1516  0 assertEquals(2, seq.getAllPDBEntries().size());
1517  0 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1518  0 assertEquals("3A6S", pdbe.getId()); // unchanged
1519  0 assertEquals("A", pdbe.getChainCode()); // updated
1520  0 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1521  0 assertEquals("filepath", pdbe.getFile()); // updated
1522  0 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1523   
1524    // add with a different file path
1525  0 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1526  0 seq.addPDBId(pdbe4);
1527  0 assertEquals(3, seq.getAllPDBEntries().size());
1528  0 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1529   
1530    // add with a different chain code
1531  0 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1532  0 seq.addPDBId(pdbe5);
1533  0 assertEquals(4, seq.getAllPDBEntries().size());
1534  0 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1535   
1536    // add with a fake pdbid
1537    // (models don't have an embedded ID)
1538  0 String realId = "RealIDQ";
1539  0 PDBEntry pdbe6 = new PDBEntry(realId, null, Type.PDB, "real/localpath");
1540  0 PDBEntry pdbe7 = new PDBEntry("RealID/real/localpath", "C", Type.MMCIF,
1541    "real/localpath");
1542  0 pdbe7.setFakedPDBId(true);
1543  0 seq.addPDBId(pdbe6);
1544  0 assertEquals(5, seq.getAllPDBEntries().size());
1545  0 seq.addPDBId(pdbe7);
1546  0 assertEquals(5, seq.getAllPDBEntries().size());
1547  0 assertFalse(pdbe6.fakedPDBId());
1548  0 assertSame(pdbe6, seq.getAllPDBEntries().get(4));
1549  0 assertEquals("C", pdbe6.getChainCode());
1550  0 assertEquals(realId, pdbe6.getId());
1551    }
1552   
 
1553  0 toggle @Test(
1554    groups =
1555    { "Functional" },
1556    expectedExceptions =
1557    { IllegalArgumentException.class })
1558    public void testSetDatasetSequence_toSelf()
1559    {
1560  0 seq.setDatasetSequence(seq);
1561    }
1562   
 
1563  0 toggle @Test(
1564    groups =
1565    { "Functional" },
1566    expectedExceptions =
1567    { IllegalArgumentException.class })
1568    public void testSetDatasetSequence_cascading()
1569    {
1570  0 SequenceI seq2 = new Sequence("Seq2", "xyz");
1571  0 seq2.createDatasetSequence();
1572  0 seq.setDatasetSequence(seq2);
1573    }
1574   
 
1575  0 toggle @Test(groups = { "Functional" })
1576    public void testFindFeatures()
1577    {
1578  0 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1579  0 sq.createDatasetSequence();
1580   
1581  0 assertTrue(sq.findFeatures(1, 99).isEmpty());
1582   
1583    // add non-positional feature
1584  0 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1585    null);
1586  0 sq.addSequenceFeature(sf0);
1587    // add feature on BCD
1588  0 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1589    null);
1590  0 sq.addSequenceFeature(sfBCD);
1591    // add feature on DE
1592  0 SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1593    null);
1594  0 sq.addSequenceFeature(sfDE);
1595    // add contact feature at [B, H]
1596  0 SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1597    "desc", 9, 15, 2f, null);
1598  0 sq.addSequenceFeature(sfContactBH);
1599    // add contact feature at [F, G]
1600  0 SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1601    "desc", 13, 14, 2f, null);
1602  0 sq.addSequenceFeature(sfContactFG);
1603    // add single position feature at [I]
1604  0 SequenceFeature sfI = new SequenceFeature("Disulfide Bond", "desc", 16,
1605    16, null);
1606  0 sq.addSequenceFeature(sfI);
1607   
1608    // no features in columns 1-2 (-A)
1609  0 List<SequenceFeature> found = sq.findFeatures(1, 2);
1610  0 assertTrue(found.isEmpty());
1611   
1612    // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1613  0 found = sq.findFeatures(1, 6);
1614  0 assertEquals(2, found.size());
1615  0 assertTrue(found.contains(sfBCD));
1616  0 assertTrue(found.contains(sfContactBH));
1617   
1618    // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1619  0 found = sq.findFeatures(5, 6);
1620  0 assertEquals(1, found.size());
1621  0 assertTrue(found.contains(sfBCD));
1622   
1623    // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1624  0 found = sq.findFeatures(7, 10);
1625  0 assertEquals(3, found.size());
1626  0 assertTrue(found.contains(sfBCD));
1627  0 assertTrue(found.contains(sfDE));
1628  0 assertTrue(found.contains(sfContactFG));
1629   
1630    // columns 10-11 (--) should find nothing
1631  0 found = sq.findFeatures(10, 11);
1632  0 assertEquals(0, found.size());
1633   
1634    // columns 14-14 (I) should find variant feature
1635  0 found = sq.findFeatures(14, 14);
1636  0 assertEquals(1, found.size());
1637  0 assertTrue(found.contains(sfI));
1638    }
1639   
 
1640  0 toggle @Test(groups = { "Functional" })
1641    public void testFindIndex_withCursor()
1642    {
1643  0 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1644  0 final int tok = (int) PA.getValue(sq, "changeCount");
1645  0 assertEquals(1, tok);
1646   
1647    // find F given A, check cursor is now at the found position
1648  0 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, tok)));
1649  0 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1650  0 assertEquals(13, cursor.residuePosition);
1651  0 assertEquals(10, cursor.columnPosition);
1652   
1653    // find A given F
1654  0 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, tok)));
1655  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1656  0 assertEquals(8, cursor.residuePosition);
1657  0 assertEquals(2, cursor.columnPosition);
1658   
1659    // find C given C (no cursor update is done for this case)
1660  0 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, tok)));
1661  0 SequenceCursor cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1662  0 assertSame(cursor2, cursor);
1663   
1664    /*
1665    * sequence 'end' beyond end of sequence returns length of sequence
1666    * (for compatibility with pre-cursor code)
1667    * - also verify the cursor is left in a valid state
1668    */
1669  0 sq = new Sequence("test/8-99", "-A--B-C-D-E-F--"); // trailing gap case
1670  0 assertEquals(7, sq.findIndex(10)); // establishes a cursor
1671  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1672  0 assertEquals(10, cursor.residuePosition);
1673  0 assertEquals(7, cursor.columnPosition);
1674  0 assertEquals(sq.getLength(), sq.findIndex(65));
1675  0 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1676  0 assertSame(cursor, cursor2); // not updated for this case!
1677   
1678  0 sq = new Sequence("test/8-99", "-A--B-C-D-E-F"); // trailing residue case
1679  0 sq.findIndex(10); // establishes a cursor
1680  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1681  0 assertEquals(sq.getLength(), sq.findIndex(65));
1682  0 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1683  0 assertSame(cursor, cursor2); // not updated for this case!
1684   
1685    /*
1686    * residue after sequence 'start' but before first residue should return
1687    * zero (for compatibility with pre-cursor code)
1688    */
1689  0 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
1690  0 sq.findIndex(10); // establishes a cursor
1691  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1692  0 assertEquals(0, sq.findIndex(3));
1693  0 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1694  0 assertSame(cursor, cursor2); // not updated for this case!
1695   
1696  0 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
1697  0 sq.findIndex(10); // establishes a cursor
1698  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1699  0 assertEquals(0, sq.findIndex(2));
1700  0 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1701  0 assertSame(cursor, cursor2); // not updated for this case!
1702    }
1703   
 
1704  0 toggle @Test(groups = { "Functional" })
1705    public void testFindPosition_withCursor()
1706    {
1707  0 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1708  0 final int tok = (int) PA.getValue(sq, "changeCount");
1709  0 assertEquals(1, tok);
1710   
1711    // find F pos given A - lastCol gets set in cursor
1712  0 assertEquals(13,
1713    sq.findPosition(10, new SequenceCursor(sq, 8, 2, tok)));
1714  0 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1715    PA.getValue(sq, "cursor").toString());
1716   
1717    // find A pos given F - first residue column is saved in cursor
1718  0 assertEquals(8,
1719    sq.findPosition(2, new SequenceCursor(sq, 13, 10, tok)));
1720  0 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok1",
1721    PA.getValue(sq, "cursor").toString());
1722   
1723    // find C pos given C (neither startCol nor endCol is set)
1724  0 assertEquals(10,
1725    sq.findPosition(6, new SequenceCursor(sq, 10, 6, tok)));
1726  0 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1727    PA.getValue(sq, "cursor").toString());
1728   
1729    // now the grey area - what residue position for a gapped column? JAL-2562
1730   
1731    // find 'residue' for column 3 given cursor for D (so working left)
1732    // returns B9; cursor is updated to [B 5]
1733  0 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, tok)));
1734  0 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok1",
1735    PA.getValue(sq, "cursor").toString());
1736   
1737    // find 'residue' for column 8 given cursor for D (so working right)
1738    // returns E12; cursor is updated to [D 7]
1739  0 assertEquals(12,
1740    sq.findPosition(8, new SequenceCursor(sq, 11, 7, tok)));
1741  0 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok1",
1742    PA.getValue(sq, "cursor").toString());
1743   
1744    // find 'residue' for column 12 given cursor for B
1745    // returns 1 more than last residue position; cursor is updated to [F 10]
1746    // lastCol position is saved in cursor
1747  0 assertEquals(14,
1748    sq.findPosition(12, new SequenceCursor(sq, 9, 5, tok)));
1749  0 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1750    PA.getValue(sq, "cursor").toString());
1751   
1752    /*
1753    * findPosition for column beyond length of sequence
1754    * returns 1 more than the last residue position
1755    * cursor is set to last real residue position [F 10]
1756    */
1757  0 assertEquals(14,
1758    sq.findPosition(99, new SequenceCursor(sq, 8, 2, tok)));
1759  0 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1760    PA.getValue(sq, "cursor").toString());
1761   
1762    /*
1763    * and the case without a trailing gap
1764    */
1765  0 sq = new Sequence("test/8-13", "-A--BCD-EF");
1766    // first find C from A
1767  0 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, tok)));
1768  0 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1769  0 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1770    cursor.toString());
1771    // now 'find' 99 from C
1772    // cursor is set to [F 10] and saved lastCol
1773  0 assertEquals(14, sq.findPosition(99, cursor));
1774  0 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1775    PA.getValue(sq, "cursor").toString());
1776    }
1777   
 
1778  0 toggle @Test
1779    public void testIsValidCursor()
1780    {
1781  0 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1782  0 assertFalse(sq.isValidCursor(null));
1783   
1784    /*
1785    * cursor is valid if it has valid sequence ref and changeCount token
1786    * and positions within the range of the sequence
1787    */
1788  0 int changeCount = (int) PA.getValue(sq, "changeCount");
1789  0 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1790  0 assertTrue(sq.isValidCursor(cursor));
1791   
1792    /*
1793    * column position outside [0 - length] is rejected
1794    */
1795  0 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1796  0 assertFalse(sq.isValidCursor(cursor));
1797  0 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1798  0 assertFalse(sq.isValidCursor(cursor));
1799  0 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1800  0 assertFalse(sq.isValidCursor(cursor));
1801  0 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1802  0 assertFalse(sq.isValidCursor(cursor));
1803   
1804    /*
1805    * wrong sequence is rejected
1806    */
1807  0 cursor = new SequenceCursor(null, 13, 1, changeCount);
1808  0 assertFalse(sq.isValidCursor(cursor));
1809  0 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1810    changeCount);
1811  0 assertFalse(sq.isValidCursor(cursor));
1812   
1813    /*
1814    * wrong token value is rejected
1815    */
1816  0 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1817  0 assertFalse(sq.isValidCursor(cursor));
1818  0 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1819  0 assertFalse(sq.isValidCursor(cursor));
1820    }
1821   
 
1822  0 toggle @Test(groups = { "Functional" })
1823    public void testFindPosition_withCursorAndEdits()
1824    {
1825  0 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1826   
1827    // find F pos given A
1828  0 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1829  0 int token = (int) PA.getValue(sq, "changeCount"); // 0
1830  0 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1831  0 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1832   
1833    /*
1834    * setSequence should invalidate the cursor cached by the sequence
1835    */
1836  0 sq.setSequence("-A-BCD-EF---"); // one gap removed
1837  0 assertEquals(8, sq.getStart()); // sanity check
1838  0 assertEquals(11, sq.findPosition(5)); // D11
1839    // cursor should now be at [D 6]
1840  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1841  0 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1842  0 assertEquals(0, cursor.lastColumnPosition); // not yet found
1843  0 assertEquals(13, sq.findPosition(8)); // E13
1844  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1845  0 assertEquals(9, cursor.lastColumnPosition); // found
1846   
1847    /*
1848    * deleteChars should invalidate the cached cursor
1849    */
1850  0 sq.deleteChars(2, 5); // delete -BC
1851  0 assertEquals("-AD-EF---", sq.getSequenceAsString());
1852  0 assertEquals(8, sq.getStart()); // sanity check
1853  0 assertEquals(10, sq.findPosition(4)); // E10
1854    // cursor should now be at [E 5]
1855  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1856  0 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1857   
1858    /*
1859    * Edit to insert gaps should invalidate the cached cursor
1860    * insert 2 gaps at column[3] to make -AD---EF---
1861    */
1862  0 SequenceI[] seqs = new SequenceI[] { sq };
1863  0 AlignmentI al = new Alignment(seqs);
1864  0 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1865  0 assertEquals("-AD---EF---", sq.getSequenceAsString());
1866  0 assertEquals(10, sq.findPosition(4)); // E10
1867    // cursor should now be at [D 3]
1868  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1869  0 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1870   
1871    /*
1872    * insertCharAt should invalidate the cached cursor
1873    * insert CC at column[4] to make -AD-CC--EF---
1874    */
1875  0 sq.insertCharAt(4, 2, 'C');
1876  0 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1877  0 assertEquals(13, sq.findPosition(9)); // F13
1878    // cursor should now be at [F 10]
1879  0 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1880  0 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1881   
1882    /*
1883    * changing sequence start should invalidate cursor
1884    */
1885  0 sq = new Sequence("test/8-13", "-A--BCD-EF--");
1886  0 assertEquals(8, sq.getStart());
1887  0 assertEquals(9, sq.findPosition(4)); // B(9)
1888  0 sq.setStart(7);
1889  0 assertEquals(8, sq.findPosition(4)); // is now B(8)
1890  0 sq.setStart(10);
1891  0 assertEquals(11, sq.findPosition(4)); // is now B(11)
1892    }
1893   
 
1894  0 toggle @Test(groups = { "Functional" })
1895    public void testGetSequence()
1896    {
1897  0 String seqstring = "-A--BCD-EF--";
1898  0 Sequence sq = new Sequence("test/8-13", seqstring);
1899  0 sq.createDatasetSequence();
1900  0 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1901  0 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1902    "ABCDEF".toCharArray()));
1903   
1904    // verify a copy of the sequence array is returned
1905  0 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1906  0 assertNotSame(theSeq, sq.getSequence());
1907  0 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1908  0 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1909    }
1910   
 
1911  0 toggle @Test(groups = { "Functional" })
1912    public void testReplace()
1913    {
1914  0 String seqstring = "-A--BCD-EF--";
1915  0 SequenceI sq = new Sequence("test/8-13", seqstring);
1916    // changeCount is incremented for setStart
1917  0 assertEquals(1, PA.getValue(sq, "changeCount"));
1918   
1919  0 assertEquals(0, sq.replace('A', 'A')); // same char
1920  0 assertEquals(seqstring, sq.getSequenceAsString());
1921  0 assertEquals(1, PA.getValue(sq, "changeCount"));
1922   
1923  0 assertEquals(0, sq.replace('X', 'Y')); // not there
1924  0 assertEquals(seqstring, sq.getSequenceAsString());
1925  0 assertEquals(1, PA.getValue(sq, "changeCount"));
1926   
1927  0 assertEquals(1, sq.replace('A', 'K'));
1928  0 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1929  0 assertEquals(2, PA.getValue(sq, "changeCount"));
1930   
1931  0 assertEquals(6, sq.replace('-', '.'));
1932  0 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1933  0 assertEquals(3, PA.getValue(sq, "changeCount"));
1934    }
1935   
 
1936  0 toggle @Test(groups = { "Functional" })
1937    public void testGapBitset()
1938    {
1939  0 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
1940  0 BitSet bs = sq.gapBitset();
1941  0 BitSet expected = new BitSet();
1942  0 expected.set(0);
1943  0 expected.set(4, 7);
1944  0 expected.set(9);
1945  0 expected.set(11, 13);
1946   
1947  0 assertTrue(bs.equals(expected));
1948   
1949    }
1950   
 
1951  0 toggle public void testFindFeatures_largeEndPos()
1952    {
1953    /*
1954    * imitate a PDB sequence where end is larger than end position
1955    */
1956  0 SequenceI sq = new Sequence("test", "-ABC--DEF--", 1, 20);
1957  0 sq.createDatasetSequence();
1958   
1959  0 assertTrue(sq.findFeatures(1, 9).isEmpty());
1960    // should be no array bounds exception - JAL-2772
1961  0 assertTrue(sq.findFeatures(1, 15).isEmpty());
1962   
1963    // add feature on BCD
1964  0 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 2, 4, 2f,
1965    null);
1966  0 sq.addSequenceFeature(sfBCD);
1967   
1968    // no features in columns 1-2 (-A)
1969  0 List<SequenceFeature> found = sq.findFeatures(1, 2);
1970  0 assertTrue(found.isEmpty());
1971   
1972    // columns 1-6 (-ABC--) includes BCD
1973  0 found = sq.findFeatures(1, 6);
1974  0 assertEquals(1, found.size());
1975  0 assertTrue(found.contains(sfBCD));
1976   
1977    // columns 10-11 (--) should find nothing
1978  0 found = sq.findFeatures(10, 11);
1979  0 assertEquals(0, found.size());
1980    }
1981   
 
1982  0 toggle @Test(groups = { "Functional" })
1983    public void testSetName()
1984    {
1985  0 SequenceI sq = new Sequence("test", "-ABC---DE-F--");
1986  0 assertEquals("test", sq.getName());
1987  0 assertEquals(1, sq.getStart());
1988  0 assertEquals(6, sq.getEnd());
1989   
1990  0 sq.setName("testing");
1991  0 assertEquals("testing", sq.getName());
1992   
1993  0 sq.setName("test/8-10");
1994  0 assertEquals("test", sq.getName());
1995  0 assertEquals(8, sq.getStart());
1996  0 assertEquals(13, sq.getEnd()); // note end is recomputed
1997   
1998  0 sq.setName("testing/7-99");
1999  0 assertEquals("testing", sq.getName());
2000  0 assertEquals(7, sq.getStart());
2001  0 assertEquals(99, sq.getEnd()); // end may be beyond physical end
2002   
2003  0 sq.setName("/2-3");
2004  0 assertEquals("", sq.getName());
2005  0 assertEquals(2, sq.getStart());
2006  0 assertEquals(7, sq.getEnd());
2007   
2008  0 sq.setName("test/"); // invalid
2009  0 assertEquals("test/", sq.getName());
2010  0 assertEquals(2, sq.getStart());
2011  0 assertEquals(7, sq.getEnd());
2012   
2013  0 sq.setName("test/6-13/7-99");
2014  0 assertEquals("test/6-13", sq.getName());
2015  0 assertEquals(7, sq.getStart());
2016  0 assertEquals(99, sq.getEnd());
2017   
2018  0 sq.setName("test/0-5"); // 0 is invalid - ignored
2019  0 assertEquals("test/0-5", sq.getName());
2020  0 assertEquals(7, sq.getStart());
2021  0 assertEquals(99, sq.getEnd());
2022   
2023  0 sq.setName("test/a-5"); // a is invalid - ignored
2024  0 assertEquals("test/a-5", sq.getName());
2025  0 assertEquals(7, sq.getStart());
2026  0 assertEquals(99, sq.getEnd());
2027   
2028  0 sq.setName("test/6-5"); // start > end is invalid - ignored
2029  0 assertEquals("test/6-5", sq.getName());
2030  0 assertEquals(7, sq.getStart());
2031  0 assertEquals(99, sq.getEnd());
2032   
2033  0 sq.setName("test/5"); // invalid - ignored
2034  0 assertEquals("test/5", sq.getName());
2035  0 assertEquals(7, sq.getStart());
2036  0 assertEquals(99, sq.getEnd());
2037   
2038  0 sq.setName("test/-5"); // invalid - ignored
2039  0 assertEquals("test/-5", sq.getName());
2040  0 assertEquals(7, sq.getStart());
2041  0 assertEquals(99, sq.getEnd());
2042   
2043  0 sq.setName("test/5-"); // invalid - ignored
2044  0 assertEquals("test/5-", sq.getName());
2045  0 assertEquals(7, sq.getStart());
2046  0 assertEquals(99, sq.getEnd());
2047   
2048  0 sq.setName("test/5-6-7"); // invalid - ignored
2049  0 assertEquals("test/5-6-7", sq.getName());
2050  0 assertEquals(7, sq.getStart());
2051  0 assertEquals(99, sq.getEnd());
2052   
2053  0 sq.setName(null); // invalid, gets converted to space
2054  0 assertEquals("", sq.getName());
2055  0 assertEquals(7, sq.getStart());
2056  0 assertEquals(99, sq.getEnd());
2057    }
2058   
 
2059  0 toggle @Test(groups = { "Functional" })
2060    public void testCheckValidRange()
2061    {
2062  0 Sequence sq = new Sequence("test/7-12", "-ABC---DE-F--");
2063  0 assertEquals(7, sq.getStart());
2064  0 assertEquals(12, sq.getEnd());
2065   
2066    /*
2067    * checkValidRange ensures end is at least the last residue position
2068    */
2069  0 PA.setValue(sq, "end", 2);
2070  0 sq.checkValidRange();
2071  0 assertEquals(12, sq.getEnd());
2072   
2073    /*
2074    * end may be beyond the last residue position
2075    */
2076  0 PA.setValue(sq, "end", 22);
2077  0 sq.checkValidRange();
2078  0 assertEquals(22, sq.getEnd());
2079    }
2080   
 
2081  0 toggle @Test(groups = { "Functional" })
2082    public void testDeleteChars_withGaps()
2083    {
2084    /*
2085    * delete gaps only
2086    */
2087  0 SequenceI sq = new Sequence("test/8-10", "A-B-C");
2088  0 sq.createDatasetSequence();
2089  0 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2090  0 sq.deleteChars(1, 2); // delete first gap
2091  0 assertEquals("AB-C", sq.getSequenceAsString());
2092  0 assertEquals(8, sq.getStart());
2093  0 assertEquals(10, sq.getEnd());
2094  0 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2095   
2096    /*
2097    * delete gaps and residues at start (no new dataset sequence)
2098    */
2099  0 sq = new Sequence("test/8-10", "A-B-C");
2100  0 sq.createDatasetSequence();
2101  0 sq.deleteChars(0, 3); // delete A-B
2102  0 assertEquals("-C", sq.getSequenceAsString());
2103  0 assertEquals(10, sq.getStart());
2104  0 assertEquals(10, sq.getEnd());
2105  0 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2106   
2107    /*
2108    * delete gaps and residues at end (no new dataset sequence)
2109    */
2110  0 sq = new Sequence("test/8-10", "A-B-C");
2111  0 sq.createDatasetSequence();
2112  0 sq.deleteChars(2, 5); // delete B-C
2113  0 assertEquals("A-", sq.getSequenceAsString());
2114  0 assertEquals(8, sq.getStart());
2115  0 assertEquals(8, sq.getEnd());
2116  0 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2117   
2118    /*
2119    * delete gaps and residues internally (new dataset sequence)
2120    * first delete from gap to residue
2121    */
2122  0 sq = new Sequence("test/8-10", "A-B-C");
2123  0 sq.createDatasetSequence();
2124  0 sq.deleteChars(1, 3); // delete -B
2125  0 assertEquals("A-C", sq.getSequenceAsString());
2126  0 assertEquals(8, sq.getStart());
2127  0 assertEquals(9, sq.getEnd());
2128  0 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2129  0 assertEquals(8, sq.getDatasetSequence().getStart());
2130  0 assertEquals(9, sq.getDatasetSequence().getEnd());
2131   
2132    /*
2133    * internal delete from gap to gap
2134    */
2135  0 sq = new Sequence("test/8-10", "A-B-C");
2136  0 sq.createDatasetSequence();
2137  0 sq.deleteChars(1, 4); // delete -B-
2138  0 assertEquals("AC", sq.getSequenceAsString());
2139  0 assertEquals(8, sq.getStart());
2140  0 assertEquals(9, sq.getEnd());
2141  0 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2142  0 assertEquals(8, sq.getDatasetSequence().getStart());
2143  0 assertEquals(9, sq.getDatasetSequence().getEnd());
2144   
2145    /*
2146    * internal delete from residue to residue
2147    */
2148  0 sq = new Sequence("test/8-10", "A-B-C");
2149  0 sq.createDatasetSequence();
2150  0 sq.deleteChars(2, 3); // delete B
2151  0 assertEquals("A--C", sq.getSequenceAsString());
2152  0 assertEquals(8, sq.getStart());
2153  0 assertEquals(9, sq.getEnd());
2154  0 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2155  0 assertEquals(8, sq.getDatasetSequence().getStart());
2156  0 assertEquals(9, sq.getDatasetSequence().getEnd());
2157    }
2158   
2159    /**
2160    * Test the code used to locate the reference sequence ruler origin
2161    */
 
2162  0 toggle @Test(groups = { "Functional" })
2163    public void testLocateVisibleStartofSequence()
2164    {
2165    // create random alignment
2166  0 AlignmentGenerator gen = new AlignmentGenerator(false);
2167  0 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
2168   
2169  0 HiddenColumns cs = al.getHiddenColumns();
2170  0 ColumnSelection colsel = new ColumnSelection();
2171   
2172  0 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
2173  0 assertEquals(2, seq.findIndex(seq.getStart()));
2174   
2175    // no hidden columns
2176  0 assertEquals(seq.findIndex(seq.getStart()) - 1,
2177    seq.firstResidueOutsideIterator(cs.iterator()));
2178   
2179    // hidden column on gap after end of sequence - should not affect bounds
2180  0 colsel.hideSelectedColumns(13, al.getHiddenColumns());
2181  0 assertEquals(seq.findIndex(seq.getStart()) - 1,
2182    seq.firstResidueOutsideIterator(cs.iterator()));
2183   
2184  0 cs.revealAllHiddenColumns(colsel);
2185    // hidden column on gap before beginning of sequence - should vis bounds by
2186    // one
2187  0 colsel.hideSelectedColumns(0, al.getHiddenColumns());
2188  0 assertEquals(seq.findIndex(seq.getStart()) - 2,
2189    cs.absoluteToVisibleColumn(
2190    seq.firstResidueOutsideIterator(cs.iterator())));
2191   
2192  0 cs.revealAllHiddenColumns(colsel);
2193    // hide columns around most of sequence - leave one residue remaining
2194  0 cs.hideColumns(1, 3);
2195  0 cs.hideColumns(6, 11);
2196   
2197  0 Iterator<int[]> it = cs.getVisContigsIterator(0, 6, false);
2198   
2199  0 assertEquals("-D", seq.getSequenceStringFromIterator(it));
2200    // cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
2201    // { seq })[0]);
2202   
2203  0 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2204  0 cs.revealAllHiddenColumns(colsel);
2205   
2206    // hide whole sequence - should just get location of hidden region
2207    // containing sequence
2208  0 cs.hideColumns(1, 11);
2209  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2210   
2211  0 cs.revealAllHiddenColumns(colsel);
2212  0 cs.hideColumns(0, 15);
2213  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2214   
2215  0 SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
2216   
2217  0 cs.revealAllHiddenColumns(colsel);
2218  0 cs.hideColumns(7, 17);
2219  0 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2220   
2221  0 cs.revealAllHiddenColumns(colsel);
2222  0 cs.hideColumns(3, 17);
2223  0 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2224   
2225  0 cs.revealAllHiddenColumns(colsel);
2226  0 cs.hideColumns(3, 19);
2227  0 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2228   
2229  0 cs.revealAllHiddenColumns(colsel);
2230  0 cs.hideColumns(0, 0);
2231  0 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2232   
2233  0 cs.revealAllHiddenColumns(colsel);
2234  0 cs.hideColumns(0, 1);
2235  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2236   
2237  0 cs.revealAllHiddenColumns(colsel);
2238  0 cs.hideColumns(0, 2);
2239  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2240   
2241  0 cs.revealAllHiddenColumns(colsel);
2242  0 cs.hideColumns(1, 1);
2243  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2244   
2245  0 cs.revealAllHiddenColumns(colsel);
2246  0 cs.hideColumns(1, 2);
2247  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2248   
2249  0 cs.revealAllHiddenColumns(colsel);
2250  0 cs.hideColumns(1, 3);
2251  0 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2252   
2253  0 cs.revealAllHiddenColumns(colsel);
2254  0 cs.hideColumns(0, 2);
2255  0 cs.hideColumns(5, 6);
2256  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2257   
2258  0 cs.revealAllHiddenColumns(colsel);
2259  0 cs.hideColumns(0, 2);
2260  0 cs.hideColumns(5, 6);
2261  0 cs.hideColumns(9, 10);
2262  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2263   
2264  0 cs.revealAllHiddenColumns(colsel);
2265  0 cs.hideColumns(0, 2);
2266  0 cs.hideColumns(7, 11);
2267  0 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2268   
2269  0 cs.revealAllHiddenColumns(colsel);
2270  0 cs.hideColumns(2, 4);
2271  0 cs.hideColumns(7, 11);
2272  0 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2273   
2274  0 cs.revealAllHiddenColumns(colsel);
2275  0 cs.hideColumns(2, 4);
2276  0 cs.hideColumns(7, 12);
2277  0 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2278   
2279  0 cs.revealAllHiddenColumns(colsel);
2280  0 cs.hideColumns(1, 11);
2281  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2282   
2283  0 cs.revealAllHiddenColumns(colsel);
2284  0 cs.hideColumns(0, 12);
2285  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2286   
2287  0 cs.revealAllHiddenColumns(colsel);
2288  0 cs.hideColumns(0, 4);
2289  0 cs.hideColumns(6, 12);
2290  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2291   
2292  0 cs.revealAllHiddenColumns(colsel);
2293  0 cs.hideColumns(0, 1);
2294  0 cs.hideColumns(3, 12);
2295  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2296   
2297  0 cs.revealAllHiddenColumns(colsel);
2298  0 cs.hideColumns(3, 14);
2299  0 cs.hideColumns(17, 19);
2300  0 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2301   
2302  0 cs.revealAllHiddenColumns(colsel);
2303  0 cs.hideColumns(3, 7);
2304  0 cs.hideColumns(9, 14);
2305  0 cs.hideColumns(17, 19);
2306  0 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2307   
2308  0 cs.revealAllHiddenColumns(colsel);
2309  0 cs.hideColumns(0, 1);
2310  0 cs.hideColumns(3, 4);
2311  0 cs.hideColumns(6, 8);
2312  0 cs.hideColumns(10, 12);
2313  0 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2314   
2315    }
2316   
 
2317  0 toggle @Test(groups = { "Functional" })
2318    public void testTransferAnnotation()
2319    {
2320  0 Sequence origSeq = new Sequence("MYSEQ", "THISISASEQ");
2321  0 Sequence toSeq = new Sequence("MYSEQ", "THISISASEQ");
2322  0 origSeq.setDescription("DESCRIPTION");
2323  0 origSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, true));
2324   
2325  0 toSeq.transferAnnotation(origSeq, null);
2326  0 assertEquals("DESCRIPTION", toSeq.getDescription());
2327  0 toSeq = new Sequence("MYSEQ", "THISISASEQ");
2328  0 toSeq.setDescription("unchanged");
2329  0 toSeq.transferAnnotation(origSeq, null);
2330  0 assertEquals("unchanged", toSeq.getDescription());
2331   
2332  0 assertTrue(toSeq.getDBRefs().size() == 1);
2333   
2334  0 assertTrue(toSeq.getDBRefs().get(0).isCanonical());
2335   
2336    // check for promotion of non-canonical
2337    // to canonical (e.g. fetch-db-refs on a jalview project pre 2.11.2)
2338  0 toSeq.setDBRefs(null);
2339  0 toSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, false));
2340  0 toSeq.transferAnnotation(origSeq, null);
2341  0 assertTrue(toSeq.getDBRefs().size() == 1);
2342   
2343  0 assertTrue("Promotion of non-canonical DBRefEntry failed",
2344    toSeq.getDBRefs().get(0).isCanonical());
2345   
2346    }
2347    }