Clover icon

Coverage Report

  1. Project Clover database Thu Jun 4 2026 14:16:38 BST
  2. Package jalview.datamodel

File SequenceTest.java

 

Code metrics

2
1,196
58
1
2,399
1,617
61
0.05
20.62
58
1.05

Classes

Class Line # Actions
SequenceTest 54 1,196 61
0.970541497.1%
 

Contributing tests

This file is covered by 51 tests. .

Source view

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