Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.datamodel

File SequenceTest.java

 

Code metrics

2
1,194
58
1
2,394
1,615
61
0.05
20.59
58
1.05

Classes

Class Line # Actions
SequenceTest 54 1,194 61
0.970494497%
 

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  1 sq.getDatasetSequence().addPDBId(pdbe1a);
964  1 sq.getDatasetSequence().addPDBId(pdbe1b);
965  1 sq.getDatasetSequence().addPDBId(pdbe2a);
966  1 sq.getDatasetSequence().addPDBId(pdbe2b);
967   
968    /*
969    * test we added pdb entries to the dataset sequence
970    */
971  1 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(),
972    Arrays.asList(new PDBEntry[]
973    { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
974    "PDB Entries were not found on dataset sequence.");
975   
976    /*
977    * we should recover a pdb entry that is on the dataset sequence via PDBEntry
978    */
979  1 Assert.assertEquals(pdbe1a, sq.getDatasetSequence().getPDBEntry("1PDB"),
980    "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
981  1 ArrayList<Annotation> annotsList = new ArrayList<>();
982  1 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
983  1 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
984  1 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
985  1 Annotation[] annots = annotsList.toArray(new Annotation[0]);
986  1 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
987    "Test annot description", annots));
988  1 sq.getDatasetSequence().addAlignmentAnnotation(new AlignmentAnnotation(
989    "Test annot", "Test annot description", annots));
990  1 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
991  1 Assert.assertEquals(sq.getDBRefs().size(), 5); // DBRefs are on dataset
992    // sequence
993  1 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
994  1 Assert.assertNotNull(sq.getAnnotation());
995  1 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
996  1 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().size(), 5); // same
997    // as
998    // sq.getDBRefs()
999  1 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
1000    4);
1001  1 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
1002   
1003  1 Sequence derived = (Sequence) sq.deriveSequence();
1004   
1005  1 Assert.assertEquals(derived.getDescription(),
1006    "Test sequence description..");
1007  1 Assert.assertEquals(derived.getDBRefs().size(), 5); // come from dataset
1008  1 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
1009  1 Assert.assertNotNull(derived.getAnnotation());
1010  1 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
1011  1 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().size(), 5);
1012  1 Assert.assertEquals(
1013    derived.getDatasetSequence().getAllPDBEntries().size(), 4);
1014  1 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
1015   
1016  1 assertEquals("CD", derived.getSequenceAsString());
1017  1 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
1018   
1019    // derived sequence should access dataset sequence features
1020  1 assertNotNull(sq.getSequenceFeatures());
1021  1 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
1022   
1023    /*
1024    * verify we have primary db refs *just* for PDB IDs with associated
1025    * PDBEntry objects
1026    */
1027   
1028  1 assertEquals(primRefs, sq.getPrimaryDBRefs());
1029  1 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
1030   
1031  1 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
1032   
1033    }
1034   
1035    /**
1036    * Test for deriveSequence applied to an ungapped sequence with no dataset
1037    */
 
1038  1 toggle @Test(groups = { "Functional" })
1039    public void testDeriveSequence_noDatasetUngapped()
1040    {
1041  1 SequenceI sq = new Sequence("Seq1", "ABCDEF");
1042  1 assertEquals(1, sq.getStart());
1043  1 assertEquals(6, sq.getEnd());
1044  1 SequenceI derived = sq.deriveSequence();
1045  1 assertEquals("ABCDEF", derived.getSequenceAsString());
1046  1 assertEquals("ABCDEF",
1047    derived.getDatasetSequence().getSequenceAsString());
1048    }
1049   
1050    /**
1051    * Test for deriveSequence applied to a gapped sequence with no dataset
1052    */
 
1053  1 toggle @Test(groups = { "Functional" })
1054    public void testDeriveSequence_noDatasetGapped()
1055    {
1056  1 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
1057  1 assertEquals(1, sq.getStart());
1058  1 assertEquals(6, sq.getEnd());
1059  1 assertNull(sq.getDatasetSequence());
1060  1 SequenceI derived = sq.deriveSequence();
1061  1 assertEquals("AB-C.D EF", derived.getSequenceAsString());
1062  1 assertEquals("ABCDEF",
1063    derived.getDatasetSequence().getSequenceAsString());
1064    }
1065   
1066    /**
1067    * test that creating a copy of an existing sequence with dataset sequence and
1068    * associated contact matrix yields annotation associated with the same
1069    * contact matrix in the copy
1070    */
 
1071  1 toggle @Test(groups = { "Functional" })
1072    public void testCopyPasteStyleDerivesequence_withcontactMatrixAnn()
1073    {
1074  1 SequenceI seq1 = new Sequence("seq1", "ACDACDACD");
1075  1 seq1.createDatasetSequence();
1076  1 ContactMatrixI cm = new SeqDistanceContactMatrix(seq1.getLength());
1077    // addContactList needs to return annotation addable to the sequence
1078    // reference it was called from
1079  1 AlignmentAnnotation aann = seq1.addContactList(cm);
1080  1 assertTrue(aann.sequenceRef == seq1);
1081  1 assertEquals(1, seq1.getAnnotation().length);
1082  1 assertNotNull(seq1.getContactListFor(seq1.getAnnotation()[0], 1));
1083   
1084  1 SequenceI seq_derived = seq1.deriveSequence();
1085  1 assertEquals(1, seq_derived.getAnnotation().length);
1086  1 assertTrue(cm == seq_derived
1087    .getContactMatrixFor(seq_derived.getAnnotation()[0]));
1088  1 assertNotNull(seq_derived
1089    .getContactListFor(seq_derived.getAnnotation()[0], 1));
1090   
1091    // copy paste actually uses the copy constructor .. so
1092   
1093  1 SequenceI seq_copied = new Sequence((Sequence) seq_derived);
1094  1 assertEquals(1, seq_copied.getAnnotation().length);
1095  1 assertTrue(cm == seq_copied
1096    .getContactMatrixFor(seq_copied.getAnnotation()[0]));
1097  1 assertNotNull(
1098    seq_copied.getContactListFor(seq_copied.getAnnotation()[0], 1));
1099   
1100    }
1101   
 
1102  1 toggle @Test(groups = { "Functional" })
1103    public void testCopyConstructor_noDataset()
1104    {
1105  1 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1106  1 seq1.setDescription("description");
1107  1 seq1.addAlignmentAnnotation(
1108    new AlignmentAnnotation("label", "desc", 1.3d));
1109  1 seq1.addSequenceFeature(
1110    new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1111  1 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1112  1 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1113   
1114  1 SequenceI copy = new Sequence(seq1);
1115   
1116  1 assertNull(copy.getDatasetSequence());
1117   
1118  1 verifyCopiedSequence(seq1, copy);
1119   
1120    // copy has a copy of the DBRefEntry
1121    // this is murky - DBrefs are only copied for dataset sequences
1122    // where the test for 'dataset sequence' is 'dataset is null'
1123    // but that doesn't distinguish it from an aligned sequence
1124    // which has not yet generated a dataset sequence
1125    // NB getDBRef looks inside dataset sequence if not null
1126  1 List<DBRefEntry> dbrefs = copy.getDBRefs();
1127  1 assertEquals(1, dbrefs.size());
1128  1 assertFalse(dbrefs.get(0) == seq1.getDBRefs().get(0));
1129  1 assertTrue(dbrefs.get(0).equals(seq1.getDBRefs().get(0)));
1130    }
1131   
 
1132  1 toggle @Test(groups = { "Functional" })
1133    public void testCopyConstructor_withDataset()
1134    {
1135  1 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1136  1 seq1.createDatasetSequence();
1137  1 seq1.setDescription("description");
1138  1 seq1.addAlignmentAnnotation(
1139    new AlignmentAnnotation("label", "desc", 1.3d));
1140    // JAL-2046 - what is the contract for using a derived sequence's
1141    // addSequenceFeature ?
1142  1 seq1.addSequenceFeature(
1143    new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1144  1 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1145    // here we add DBRef to the dataset sequence:
1146  1 seq1.getDatasetSequence()
1147    .addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1148   
1149  1 SequenceI copy = new Sequence(seq1);
1150   
1151  1 assertNotNull(copy.getDatasetSequence());
1152  1 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1153   
1154  1 verifyCopiedSequence(seq1, copy);
1155   
1156    // getDBRef looks inside dataset sequence and this is shared,
1157    // so holds the same dbref objects
1158  1 List<DBRefEntry> dbrefs = copy.getDBRefs();
1159  1 assertEquals(1, dbrefs.size());
1160  1 assertSame(dbrefs.get(0), seq1.getDBRefs().get(0));
1161    }
1162   
1163    /**
1164    * Helper to make assertions about a copied sequence
1165    *
1166    * @param seq1
1167    * @param copy
1168    */
 
1169  2 toggle protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1170    {
1171    // verify basic properties:
1172  2 assertEquals(copy.getName(), seq1.getName());
1173  2 assertEquals(copy.getDescription(), seq1.getDescription());
1174  2 assertEquals(copy.getStart(), seq1.getStart());
1175  2 assertEquals(copy.getEnd(), seq1.getEnd());
1176  2 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1177   
1178    // copy has a copy of the annotation:
1179  2 AlignmentAnnotation[] anns = copy.getAnnotation();
1180  2 assertEquals(1, anns.length);
1181  2 assertFalse(anns[0] == seq1.getAnnotation()[0]);
1182  2 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1183  2 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1184  2 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1185   
1186    // copy has a copy of the sequence feature:
1187  2 List<SequenceFeature> sfs = copy.getSequenceFeatures();
1188  2 assertEquals(1, sfs.size());
1189  2 if (seq1.getDatasetSequence() != null
1190    && copy.getDatasetSequence() == seq1.getDatasetSequence())
1191    {
1192  1 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1193    }
1194    else
1195    {
1196  1 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1197    }
1198  2 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1199   
1200    // copy has a copy of the PDB entry
1201  2 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1202  2 assertEquals(1, pdbs.size());
1203  2 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1204  2 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1205    }
1206   
 
1207  1 toggle @Test(groups = "Functional")
1208    public void testGetCharAt()
1209    {
1210  1 SequenceI sq = new Sequence("", "abcde");
1211  1 assertEquals('a', sq.getCharAt(0));
1212  1 assertEquals('e', sq.getCharAt(4));
1213  1 assertEquals(' ', sq.getCharAt(5));
1214  1 assertEquals(' ', sq.getCharAt(-1));
1215    }
1216   
 
1217  1 toggle @Test(groups = { "Functional" })
1218    public void testAddSequenceFeatures()
1219    {
1220  1 SequenceI sq = new Sequence("", "abcde");
1221    // type may not be null
1222  1 assertFalse(sq.addSequenceFeature(
1223    new SequenceFeature(null, "desc", 4, 8, 0f, null)));
1224  1 assertTrue(sq.addSequenceFeature(
1225    new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1226    // can't add a duplicate feature
1227  1 assertFalse(sq.addSequenceFeature(
1228    new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1229    // can add a different feature
1230  1 assertTrue(sq.addSequenceFeature(
1231    new SequenceFeature("Scop", "desc", 4, 8, 0f, null))); // different
1232    // type
1233  1 assertTrue(sq.addSequenceFeature(
1234    new SequenceFeature("Cath", "description", 4, 8, 0f, null)));// different
1235    // description
1236  1 assertTrue(sq.addSequenceFeature(
1237    new SequenceFeature("Cath", "desc", 3, 8, 0f, null))); // different
1238    // start
1239    // position
1240  1 assertTrue(sq.addSequenceFeature(
1241    new SequenceFeature("Cath", "desc", 4, 9, 0f, null))); // different
1242    // end
1243    // position
1244  1 assertTrue(sq.addSequenceFeature(
1245    new SequenceFeature("Cath", "desc", 4, 8, 1f, null))); // different
1246    // score
1247  1 assertTrue(sq.addSequenceFeature(
1248    new SequenceFeature("Cath", "desc", 4, 8, Float.NaN, null))); // score
1249    // NaN
1250  1 assertTrue(sq.addSequenceFeature(
1251    new SequenceFeature("Cath", "desc", 4, 8, 0f, "Metal"))); // different
1252    // group
1253  1 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1254    }
1255   
1256    /**
1257    * Tests for adding (or updating) dbrefs
1258    *
1259    * @see DBRefEntry#updateFrom(DBRefEntry)
1260    */
 
1261  1 toggle @Test(groups = { "Functional" })
1262    public void testAddDBRef()
1263    {
1264  1 SequenceI sq = new Sequence("", "abcde");
1265  1 assertNull(sq.getDBRefs());
1266  1 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1267  1 sq.addDBRef(dbref);
1268  1 assertEquals(1, sq.getDBRefs().size());
1269  1 assertSame(dbref, sq.getDBRefs().get(0));
1270   
1271    /*
1272    * change of version - new entry
1273    */
1274  1 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1275  1 sq.addDBRef(dbref2);
1276  1 assertEquals(2, sq.getDBRefs().size());
1277  1 assertSame(dbref, sq.getDBRefs().get(0));
1278  1 assertSame(dbref2, sq.getDBRefs().get(1));
1279   
1280    /*
1281    * matches existing entry - not added
1282    */
1283  1 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1284  1 assertEquals(2, sq.getDBRefs().size());
1285   
1286    /*
1287    * different source = new entry
1288    */
1289  1 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1290  1 sq.addDBRef(dbref3);
1291  1 assertEquals(3, sq.getDBRefs().size());
1292  1 assertSame(dbref3, sq.getDBRefs().get(2));
1293   
1294    /*
1295    * different ref = new entry
1296    */
1297  1 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1298  1 sq.addDBRef(dbref4);
1299  1 assertEquals(4, sq.getDBRefs().size());
1300  1 assertSame(dbref4, sq.getDBRefs().get(3));
1301   
1302    /*
1303    * matching ref with a mapping - map updated
1304    */
1305  1 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1306  1 Mapping map = new Mapping(
1307    new MapList(new int[]
1308    { 1, 3 }, new int[] { 1, 1 }, 3, 1));
1309  1 dbref5.setMap(map);
1310  1 sq.addDBRef(dbref5);
1311  1 assertEquals(4, sq.getDBRefs().size());
1312  1 assertSame(dbref4, sq.getDBRefs().get(3));
1313  1 assertSame(map, dbref4.getMap());
1314   
1315    /*
1316    * 'real' version replaces "0" version
1317    */
1318  1 dbref2.setVersion("0");
1319  1 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1320    dbref2.getAccessionId());
1321  1 sq.addDBRef(dbref6);
1322  1 assertEquals(4, sq.getDBRefs().size());
1323  1 assertSame(dbref2, sq.getDBRefs().get(1));
1324  1 assertEquals("3", dbref2.getVersion());
1325   
1326    /*
1327    * 'real' version replaces "source:0" version
1328    */
1329  1 dbref3.setVersion("Uniprot:0");
1330  1 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1331    dbref3.getAccessionId());
1332  1 sq.addDBRef(dbref7);
1333  1 assertEquals(4, sq.getDBRefs().size());
1334  1 assertSame(dbref3, sq.getDBRefs().get(2));
1335  1 assertEquals("3", dbref2.getVersion());
1336    }
1337   
 
1338  1 toggle @Test(groups = { "Functional" })
1339    public void testGetPrimaryDBRefs_peptide()
1340    {
1341  1 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1342   
1343    // no dbrefs
1344  1 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1345  1 assertTrue(primaryDBRefs.isEmpty());
1346   
1347    // empty dbrefs
1348  1 sq.setDBRefs(null);
1349  1 primaryDBRefs = sq.getPrimaryDBRefs();
1350  1 assertTrue(primaryDBRefs.isEmpty());
1351   
1352    // primary - uniprot
1353  1 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1354  1 sq.addDBRef(upentry1);
1355   
1356    // primary - uniprot with congruent map
1357  1 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1358  1 upentry2.setMap(
1359    new Mapping(null, new MapList(new int[]
1360    { 10, 22 }, new int[] { 10, 22 }, 1, 1)));
1361  1 sq.addDBRef(upentry2);
1362   
1363    // primary - uniprot with map of enclosing sequence
1364  1 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1365  1 upentry3.setMap(
1366    new Mapping(null, new MapList(new int[]
1367    { 8, 24 }, new int[] { 8, 24 }, 1, 1)));
1368  1 sq.addDBRef(upentry3);
1369   
1370    // not primary - uniprot with map of sub-sequence (5')
1371  1 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1372  1 upentry4.setMap(
1373    new Mapping(null, new MapList(new int[]
1374    { 10, 18 }, new int[] { 10, 18 }, 1, 1)));
1375  1 sq.addDBRef(upentry4);
1376   
1377    // not primary - uniprot with map that overlaps 3'
1378  1 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1379  1 upentry5.setMap(
1380    new Mapping(null, new MapList(new int[]
1381    { 12, 22 }, new int[] { 12, 22 }, 1, 1)));
1382  1 sq.addDBRef(upentry5);
1383   
1384    // not primary - uniprot with map to different coordinates frame
1385  1 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1386  1 upentry6.setMap(
1387    new Mapping(null, new MapList(new int[]
1388    { 12, 18 }, new int[] { 112, 118 }, 1, 1)));
1389  1 sq.addDBRef(upentry6);
1390   
1391    // not primary - dbref to 'non-core' database
1392  1 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1393  1 sq.addDBRef(upentry7);
1394   
1395    // primary - type is PDB
1396  1 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1397  1 sq.addDBRef(pdbentry);
1398   
1399    // not primary - PDBEntry has no file
1400  1 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1401   
1402    // not primary - no PDBEntry
1403  1 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1404   
1405    // add corroborating PDB entry for primary DBref -
1406    // needs to have a file as well as matching ID
1407    // note PDB ID is not treated as case sensitive
1408  1 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB,
1409    new File("/blah").toString()));
1410   
1411    // not valid DBRef - no file..
1412  1 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1413   
1414  1 primaryDBRefs = sq.getPrimaryDBRefs();
1415  1 assertEquals(4, primaryDBRefs.size());
1416  1 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1417    primaryDBRefs.contains(upentry1));
1418  1 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1419    primaryDBRefs.contains(upentry2));
1420  1 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1421    primaryDBRefs.contains(upentry3));
1422  1 assertTrue("Couldn't find expected PDB primary reference",
1423    primaryDBRefs.contains(pdbentry));
1424    }
1425   
 
1426  1 toggle @Test(groups = { "Functional" })
1427    public void testGetPrimaryDBRefs_nucleotide()
1428    {
1429  1 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10,
1430    34);
1431   
1432    // primary - Ensembl
1433  1 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1434  1 sq.addDBRef(dbr1);
1435   
1436    // not primary - Ensembl 'transcript' mapping of sub-sequence
1437  1 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1438  1 dbr2.setMap(
1439    new Mapping(null, new MapList(new int[]
1440    { 15, 25 }, new int[] { 1, 11 }, 1, 1)));
1441  1 sq.addDBRef(dbr2);
1442   
1443    // primary - EMBL with congruent map
1444  1 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1445  1 dbr3.setMap(
1446    new Mapping(null, new MapList(new int[]
1447    { 10, 34 }, new int[] { 10, 34 }, 1, 1)));
1448  1 sq.addDBRef(dbr3);
1449   
1450    // not primary - to non-core database
1451  1 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1452  1 sq.addDBRef(dbr4);
1453   
1454    // not primary - to protein
1455  1 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1456  1 sq.addDBRef(dbr5);
1457   
1458  1 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1459  1 assertEquals(2, primaryDBRefs.size());
1460  1 assertTrue(primaryDBRefs.contains(dbr1));
1461  1 assertTrue(primaryDBRefs.contains(dbr3));
1462    }
1463   
1464    /**
1465    * Test the method that updates the list of PDBEntry from any new DBRefEntry
1466    * for PDB
1467    */
 
1468  1 toggle @Test(groups = { "Functional" })
1469    public void testUpdatePDBIds()
1470    {
1471  1 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1472  1 seq.addPDBId(pdbe1);
1473  1 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1474  1 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1475  1 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1476  1 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1477    // 7 is not a valid chain code:
1478  1 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1479   
1480  1 seq.updatePDBIds();
1481  1 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1482  1 assertEquals(4, pdbIds.size());
1483  1 assertSame(pdbe1, pdbIds.get(0));
1484    // chain code got added to 3A6S:
1485  1 assertEquals("B", pdbe1.getChainCode());
1486  1 assertEquals("1A70", pdbIds.get(1).getId());
1487    // 4BQGA is parsed into id + chain
1488  1 assertEquals("4BQG", pdbIds.get(2).getId());
1489  1 assertEquals("a", pdbIds.get(2).getChainCode());
1490  1 assertEquals("2GIS7", pdbIds.get(3).getId());
1491  1 assertNull(pdbIds.get(3).getChainCode());
1492    }
1493   
1494    /**
1495    * Test the method that either adds a pdbid or updates an existing one
1496    */
 
1497  1 toggle @Test(groups = { "Functional" })
1498    public void testAddPDBId()
1499    {
1500  1 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1501  1 seq.addPDBId(pdbe);
1502  1 assertEquals(1, seq.getAllPDBEntries().size());
1503  1 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1504  1 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1505   
1506    // add the same entry
1507  1 seq.addPDBId(pdbe);
1508  1 assertEquals(1, seq.getAllPDBEntries().size());
1509  1 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1510   
1511    // add an identical entry
1512  1 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1513  1 assertEquals(1, seq.getAllPDBEntries().size());
1514  1 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1515   
1516    // add a different entry
1517  1 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1518  1 seq.addPDBId(pdbe2);
1519  1 assertEquals(2, seq.getAllPDBEntries().size());
1520  1 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1521  1 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1522   
1523    // update pdbe with chain code, file, type
1524  1 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1525  1 seq.addPDBId(pdbe3);
1526  1 assertEquals(2, seq.getAllPDBEntries().size());
1527  1 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1528  1 assertEquals("3A6S", pdbe.getId()); // unchanged
1529  1 assertEquals("A", pdbe.getChainCode()); // updated
1530  1 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1531  1 assertEquals("filepath", pdbe.getFile()); // updated
1532  1 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1533   
1534    // add with a different file path
1535  1 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1536  1 seq.addPDBId(pdbe4);
1537  1 assertEquals(3, seq.getAllPDBEntries().size());
1538  1 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1539   
1540    // add with a different chain code
1541  1 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1542  1 seq.addPDBId(pdbe5);
1543  1 assertEquals(4, seq.getAllPDBEntries().size());
1544  1 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1545   
1546    // add with a fake pdbid
1547    // (models don't have an embedded ID)
1548  1 String realId = "RealIDQ";
1549  1 PDBEntry pdbe6 = new PDBEntry(realId, null, Type.PDB, "real/localpath");
1550  1 PDBEntry pdbe7 = new PDBEntry("RealID/real/localpath", "C", Type.MMCIF,
1551    "real/localpath");
1552  1 pdbe7.setFakedPDBId(true);
1553  1 seq.addPDBId(pdbe6);
1554  1 assertEquals(5, seq.getAllPDBEntries().size());
1555  1 seq.addPDBId(pdbe7);
1556  1 assertEquals(5, seq.getAllPDBEntries().size());
1557  1 assertFalse(pdbe6.fakedPDBId());
1558  1 assertSame(pdbe6, seq.getAllPDBEntries().get(4));
1559  1 assertEquals("C", pdbe6.getChainCode());
1560  1 assertEquals(realId, pdbe6.getId());
1561    }
1562   
 
1563  1 toggle @Test(
1564    groups =
1565    { "Functional" },
1566    expectedExceptions =
1567    { IllegalArgumentException.class })
1568    public void testSetDatasetSequence_toSelf()
1569    {
1570  1 seq.setDatasetSequence(seq);
1571    }
1572   
 
1573  1 toggle @Test(
1574    groups =
1575    { "Functional" },
1576    expectedExceptions =
1577    { IllegalArgumentException.class })
1578    public void testSetDatasetSequence_cascading()
1579    {
1580  1 SequenceI seq2 = new Sequence("Seq2", "xyz");
1581  1 seq2.createDatasetSequence();
1582  1 seq.setDatasetSequence(seq2);
1583    }
1584   
 
1585  1 toggle @Test(groups = { "Functional" })
1586    public void testFindFeatures()
1587    {
1588  1 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1589  1 sq.createDatasetSequence();
1590   
1591  1 assertTrue(sq.findFeatures(1, 99).isEmpty());
1592   
1593    // add non-positional feature
1594  1 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1595    null);
1596  1 sq.addSequenceFeature(sf0);
1597    // add feature on BCD
1598  1 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1599    null);
1600  1 sq.addSequenceFeature(sfBCD);
1601    // add feature on DE
1602  1 SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1603    null);
1604  1 sq.addSequenceFeature(sfDE);
1605    // add contact feature at [B, H]
1606  1 SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1607    "desc", 9, 15, 2f, null);
1608  1 sq.addSequenceFeature(sfContactBH);
1609    // add contact feature at [F, G]
1610  1 SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1611    "desc", 13, 14, 2f, null);
1612  1 sq.addSequenceFeature(sfContactFG);
1613    // add single position feature at [I]
1614  1 SequenceFeature sfI = new SequenceFeature("Disulfide Bond", "desc", 16,
1615    16, null);
1616  1 sq.addSequenceFeature(sfI);
1617   
1618    // no features in columns 1-2 (-A)
1619  1 List<SequenceFeature> found = sq.findFeatures(1, 2);
1620  1 assertTrue(found.isEmpty());
1621   
1622    // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1623  1 found = sq.findFeatures(1, 6);
1624  1 assertEquals(2, found.size());
1625  1 assertTrue(found.contains(sfBCD));
1626  1 assertTrue(found.contains(sfContactBH));
1627   
1628    // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1629  1 found = sq.findFeatures(5, 6);
1630  1 assertEquals(1, found.size());
1631  1 assertTrue(found.contains(sfBCD));
1632   
1633    // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1634  1 found = sq.findFeatures(7, 10);
1635  1 assertEquals(3, found.size());
1636  1 assertTrue(found.contains(sfBCD));
1637  1 assertTrue(found.contains(sfDE));
1638  1 assertTrue(found.contains(sfContactFG));
1639   
1640    // columns 10-11 (--) should find nothing
1641  1 found = sq.findFeatures(10, 11);
1642  1 assertEquals(0, found.size());
1643   
1644    // columns 14-14 (I) should find variant feature
1645  1 found = sq.findFeatures(14, 14);
1646  1 assertEquals(1, found.size());
1647  1 assertTrue(found.contains(sfI));
1648    }
1649   
 
1650  1 toggle @Test(groups = { "Functional"} )
1651    public void testGetRootDatasetSequence_datasetIsNull_returnNull()
1652    {
1653  1 Sequence dseq = new Sequence("test", "ABCDEF");
1654  1 assertNull(dseq.getRootDatasetSequence());
1655    }
1656   
 
1657  1 toggle @Test(groups = { "Functional" })
1658    public void testGetRootDatasetSequence_datasetImplicit_returnDataset()
1659    {
1660  1 Sequence seq = new Sequence("test", "--AB-CDEF");
1661  1 SequenceI dseq = seq.createDatasetSequence();
1662  1 assertEquals(seq.getRootDatasetSequence(), dseq);
1663    }
1664   
 
1665  1 toggle @Test(groups = { "Functional" })
1666    public void testGetRootDatasetSequence_datasetExplicit_returnDataset()
1667    {
1668  1 Sequence seq = new Sequence("test", "--AB-CDEF");
1669  1 Sequence dseq = new Sequence("test", "ABCDEF");
1670  1 seq.setDatasetSequence(dseq);
1671  1 assertEquals(seq.getRootDatasetSequence(), dseq);
1672    }
1673   
 
1674  1 toggle @Test(groups = { "Functional" })
1675    public void testGetRootDatasetSequence_nestedDatasets_returnLastDataset()
1676    {
1677  1 Sequence seq = new Sequence("test", "--AB-CDEFE");
1678  1 Sequence dseq = new Sequence("test", "ABCDEF");
1679  1 seq.setDatasetSequence(dseq);
1680  1 Sequence ddseq = new Sequence("test", "ABCDEF");
1681  1 dseq.setDatasetSequence(ddseq);
1682  1 assertEquals(seq.getDatasetSequence(), dseq);
1683  1 assertEquals(seq.getRootDatasetSequence(), ddseq);
1684    }
1685   
1686   
 
1687  1 toggle @Test(groups = { "Functional" })
1688    public void testFindIndex_withCursor()
1689    {
1690  1 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1691  1 final int tok = (int) PA.getValue(sq, "changeCount");
1692  1 assertEquals(1, tok);
1693   
1694    // find F given A, check cursor is now at the found position
1695  1 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, tok)));
1696  1 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1697  1 assertEquals(13, cursor.residuePosition);
1698  1 assertEquals(10, cursor.columnPosition);
1699   
1700    // find A given F
1701  1 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, tok)));
1702  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1703  1 assertEquals(8, cursor.residuePosition);
1704  1 assertEquals(2, cursor.columnPosition);
1705   
1706    // find C given C (no cursor update is done for this case)
1707  1 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, tok)));
1708  1 SequenceCursor cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1709  1 assertSame(cursor2, cursor);
1710   
1711    /*
1712    * sequence 'end' beyond end of sequence returns length of sequence
1713    * (for compatibility with pre-cursor code)
1714    * - also verify the cursor is left in a valid state
1715    */
1716  1 sq = new Sequence("test/8-99", "-A--B-C-D-E-F--"); // trailing gap case
1717  1 assertEquals(7, sq.findIndex(10)); // establishes a cursor
1718  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1719  1 assertEquals(10, cursor.residuePosition);
1720  1 assertEquals(7, cursor.columnPosition);
1721  1 assertEquals(sq.getLength(), sq.findIndex(65));
1722  1 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1723  1 assertSame(cursor, cursor2); // not updated for this case!
1724   
1725  1 sq = new Sequence("test/8-99", "-A--B-C-D-E-F"); // trailing residue case
1726  1 sq.findIndex(10); // establishes a cursor
1727  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1728  1 assertEquals(sq.getLength(), sq.findIndex(65));
1729  1 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1730  1 assertSame(cursor, cursor2); // not updated for this case!
1731   
1732    /*
1733    * residue after sequence 'start' but before first residue should return
1734    * zero (for compatibility with pre-cursor code)
1735    */
1736  1 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
1737  1 sq.findIndex(10); // establishes a cursor
1738  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1739  1 assertEquals(0, sq.findIndex(3));
1740  1 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1741  1 assertSame(cursor, cursor2); // not updated for this case!
1742   
1743  1 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
1744  1 sq.findIndex(10); // establishes a cursor
1745  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1746  1 assertEquals(0, sq.findIndex(2));
1747  1 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1748  1 assertSame(cursor, cursor2); // not updated for this case!
1749    }
1750   
 
1751  1 toggle @Test(groups = { "Functional" })
1752    public void testFindPosition_withCursor()
1753    {
1754  1 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1755  1 final int tok = (int) PA.getValue(sq, "changeCount");
1756  1 assertEquals(1, tok);
1757   
1758    // find F pos given A - lastCol gets set in cursor
1759  1 assertEquals(13,
1760    sq.findPosition(10, new SequenceCursor(sq, 8, 2, tok)));
1761  1 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1762    PA.getValue(sq, "cursor").toString());
1763   
1764    // find A pos given F - first residue column is saved in cursor
1765  1 assertEquals(8,
1766    sq.findPosition(2, new SequenceCursor(sq, 13, 10, tok)));
1767  1 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok1",
1768    PA.getValue(sq, "cursor").toString());
1769   
1770    // find C pos given C (neither startCol nor endCol is set)
1771  1 assertEquals(10,
1772    sq.findPosition(6, new SequenceCursor(sq, 10, 6, tok)));
1773  1 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1774    PA.getValue(sq, "cursor").toString());
1775   
1776    // now the grey area - what residue position for a gapped column? JAL-2562
1777   
1778    // find 'residue' for column 3 given cursor for D (so working left)
1779    // returns B9; cursor is updated to [B 5]
1780  1 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, tok)));
1781  1 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok1",
1782    PA.getValue(sq, "cursor").toString());
1783   
1784    // find 'residue' for column 8 given cursor for D (so working right)
1785    // returns E12; cursor is updated to [D 7]
1786  1 assertEquals(12,
1787    sq.findPosition(8, new SequenceCursor(sq, 11, 7, tok)));
1788  1 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok1",
1789    PA.getValue(sq, "cursor").toString());
1790   
1791    // find 'residue' for column 12 given cursor for B
1792    // returns 1 more than last residue position; cursor is updated to [F 10]
1793    // lastCol position is saved in cursor
1794  1 assertEquals(14,
1795    sq.findPosition(12, new SequenceCursor(sq, 9, 5, tok)));
1796  1 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1797    PA.getValue(sq, "cursor").toString());
1798   
1799    /*
1800    * findPosition for column beyond length of sequence
1801    * returns 1 more than the last residue position
1802    * cursor is set to last real residue position [F 10]
1803    */
1804  1 assertEquals(14,
1805    sq.findPosition(99, new SequenceCursor(sq, 8, 2, tok)));
1806  1 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1807    PA.getValue(sq, "cursor").toString());
1808   
1809    /*
1810    * and the case without a trailing gap
1811    */
1812  1 sq = new Sequence("test/8-13", "-A--BCD-EF");
1813    // first find C from A
1814  1 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, tok)));
1815  1 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1816  1 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1817    cursor.toString());
1818    // now 'find' 99 from C
1819    // cursor is set to [F 10] and saved lastCol
1820  1 assertEquals(14, sq.findPosition(99, cursor));
1821  1 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1822    PA.getValue(sq, "cursor").toString());
1823    }
1824   
 
1825  0 toggle @Test
1826    public void testIsValidCursor()
1827    {
1828  0 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1829  0 assertFalse(sq.isValidCursor(null));
1830   
1831    /*
1832    * cursor is valid if it has valid sequence ref and changeCount token
1833    * and positions within the range of the sequence
1834    */
1835  0 int changeCount = (int) PA.getValue(sq, "changeCount");
1836  0 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1837  0 assertTrue(sq.isValidCursor(cursor));
1838   
1839    /*
1840    * column position outside [0 - length] is rejected
1841    */
1842  0 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1843  0 assertFalse(sq.isValidCursor(cursor));
1844  0 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1845  0 assertFalse(sq.isValidCursor(cursor));
1846  0 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1847  0 assertFalse(sq.isValidCursor(cursor));
1848  0 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1849  0 assertFalse(sq.isValidCursor(cursor));
1850   
1851    /*
1852    * wrong sequence is rejected
1853    */
1854  0 cursor = new SequenceCursor(null, 13, 1, changeCount);
1855  0 assertFalse(sq.isValidCursor(cursor));
1856  0 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1857    changeCount);
1858  0 assertFalse(sq.isValidCursor(cursor));
1859   
1860    /*
1861    * wrong token value is rejected
1862    */
1863  0 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1864  0 assertFalse(sq.isValidCursor(cursor));
1865  0 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1866  0 assertFalse(sq.isValidCursor(cursor));
1867    }
1868   
 
1869  1 toggle @Test(groups = { "Functional" })
1870    public void testFindPosition_withCursorAndEdits()
1871    {
1872  1 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1873   
1874    // find F pos given A
1875  1 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1876  1 int token = (int) PA.getValue(sq, "changeCount"); // 0
1877  1 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1878  1 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1879   
1880    /*
1881    * setSequence should invalidate the cursor cached by the sequence
1882    */
1883  1 sq.setSequence("-A-BCD-EF---"); // one gap removed
1884  1 assertEquals(8, sq.getStart()); // sanity check
1885  1 assertEquals(11, sq.findPosition(5)); // D11
1886    // cursor should now be at [D 6]
1887  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1888  1 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1889  1 assertEquals(0, cursor.lastColumnPosition); // not yet found
1890  1 assertEquals(13, sq.findPosition(8)); // E13
1891  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1892  1 assertEquals(9, cursor.lastColumnPosition); // found
1893   
1894    /*
1895    * deleteChars should invalidate the cached cursor
1896    */
1897  1 sq.deleteChars(2, 5); // delete -BC
1898  1 assertEquals("-AD-EF---", sq.getSequenceAsString());
1899  1 assertEquals(8, sq.getStart()); // sanity check
1900  1 assertEquals(10, sq.findPosition(4)); // E10
1901    // cursor should now be at [E 5]
1902  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1903  1 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1904   
1905    /*
1906    * Edit to insert gaps should invalidate the cached cursor
1907    * insert 2 gaps at column[3] to make -AD---EF---
1908    */
1909  1 SequenceI[] seqs = new SequenceI[] { sq };
1910  1 AlignmentI al = new Alignment(seqs);
1911  1 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1912  1 assertEquals("-AD---EF---", sq.getSequenceAsString());
1913  1 assertEquals(10, sq.findPosition(4)); // E10
1914    // cursor should now be at [D 3]
1915  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1916  1 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1917   
1918    /*
1919    * insertCharAt should invalidate the cached cursor
1920    * insert CC at column[4] to make -AD-CC--EF---
1921    */
1922  1 sq.insertCharAt(4, 2, 'C');
1923  1 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1924  1 assertEquals(13, sq.findPosition(9)); // F13
1925    // cursor should now be at [F 10]
1926  1 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1927  1 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1928   
1929    /*
1930    * changing sequence start should invalidate cursor
1931    */
1932  1 sq = new Sequence("test/8-13", "-A--BCD-EF--");
1933  1 assertEquals(8, sq.getStart());
1934  1 assertEquals(9, sq.findPosition(4)); // B(9)
1935  1 sq.setStart(7);
1936  1 assertEquals(8, sq.findPosition(4)); // is now B(8)
1937  1 sq.setStart(10);
1938  1 assertEquals(11, sq.findPosition(4)); // is now B(11)
1939    }
1940   
 
1941  1 toggle @Test(groups = { "Functional" })
1942    public void testGetSequence()
1943    {
1944  1 String seqstring = "-A--BCD-EF--";
1945  1 Sequence sq = new Sequence("test/8-13", seqstring);
1946  1 sq.createDatasetSequence();
1947  1 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1948  1 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1949    "ABCDEF".toCharArray()));
1950   
1951    // verify a copy of the sequence array is returned
1952  1 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1953  1 assertNotSame(theSeq, sq.getSequence());
1954  1 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1955  1 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1956    }
1957   
 
1958  1 toggle @Test(groups = { "Functional" })
1959    public void testReplace()
1960    {
1961  1 String seqstring = "-A--BCD-EF--";
1962  1 SequenceI sq = new Sequence("test/8-13", seqstring);
1963    // changeCount is incremented for setStart
1964  1 assertEquals(1, PA.getValue(sq, "changeCount"));
1965   
1966  1 assertEquals(0, sq.replace('A', 'A')); // same char
1967  1 assertEquals(seqstring, sq.getSequenceAsString());
1968  1 assertEquals(1, PA.getValue(sq, "changeCount"));
1969   
1970  1 assertEquals(0, sq.replace('X', 'Y')); // not there
1971  1 assertEquals(seqstring, sq.getSequenceAsString());
1972  1 assertEquals(1, PA.getValue(sq, "changeCount"));
1973   
1974  1 assertEquals(1, sq.replace('A', 'K'));
1975  1 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1976  1 assertEquals(2, PA.getValue(sq, "changeCount"));
1977   
1978  1 assertEquals(6, sq.replace('-', '.'));
1979  1 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1980  1 assertEquals(3, PA.getValue(sq, "changeCount"));
1981    }
1982   
 
1983  1 toggle @Test(groups = { "Functional" })
1984    public void testGapBitset()
1985    {
1986  1 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
1987  1 BitSet bs = sq.gapBitset();
1988  1 BitSet expected = new BitSet();
1989  1 expected.set(0);
1990  1 expected.set(4, 7);
1991  1 expected.set(9);
1992  1 expected.set(11, 13);
1993   
1994  1 assertTrue(bs.equals(expected));
1995   
1996    }
1997   
 
1998  0 toggle public void testFindFeatures_largeEndPos()
1999    {
2000    /*
2001    * imitate a PDB sequence where end is larger than end position
2002    */
2003  0 SequenceI sq = new Sequence("test", "-ABC--DEF--", 1, 20);
2004  0 sq.createDatasetSequence();
2005   
2006  0 assertTrue(sq.findFeatures(1, 9).isEmpty());
2007    // should be no array bounds exception - JAL-2772
2008  0 assertTrue(sq.findFeatures(1, 15).isEmpty());
2009   
2010    // add feature on BCD
2011  0 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 2, 4, 2f,
2012    null);
2013  0 sq.addSequenceFeature(sfBCD);
2014   
2015    // no features in columns 1-2 (-A)
2016  0 List<SequenceFeature> found = sq.findFeatures(1, 2);
2017  0 assertTrue(found.isEmpty());
2018   
2019    // columns 1-6 (-ABC--) includes BCD
2020  0 found = sq.findFeatures(1, 6);
2021  0 assertEquals(1, found.size());
2022  0 assertTrue(found.contains(sfBCD));
2023   
2024    // columns 10-11 (--) should find nothing
2025  0 found = sq.findFeatures(10, 11);
2026  0 assertEquals(0, found.size());
2027    }
2028   
 
2029  1 toggle @Test(groups = { "Functional" })
2030    public void testSetName()
2031    {
2032  1 SequenceI sq = new Sequence("test", "-ABC---DE-F--");
2033  1 assertEquals("test", sq.getName());
2034  1 assertEquals(1, sq.getStart());
2035  1 assertEquals(6, sq.getEnd());
2036   
2037  1 sq.setName("testing");
2038  1 assertEquals("testing", sq.getName());
2039   
2040  1 sq.setName("test/8-10");
2041  1 assertEquals("test", sq.getName());
2042  1 assertEquals(8, sq.getStart());
2043  1 assertEquals(13, sq.getEnd()); // note end is recomputed
2044   
2045  1 sq.setName("testing/7-99");
2046  1 assertEquals("testing", sq.getName());
2047  1 assertEquals(7, sq.getStart());
2048  1 assertEquals(99, sq.getEnd()); // end may be beyond physical end
2049   
2050  1 sq.setName("/2-3");
2051  1 assertEquals("", sq.getName());
2052  1 assertEquals(2, sq.getStart());
2053  1 assertEquals(7, sq.getEnd());
2054   
2055  1 sq.setName("test/"); // invalid
2056  1 assertEquals("test/", sq.getName());
2057  1 assertEquals(2, sq.getStart());
2058  1 assertEquals(7, sq.getEnd());
2059   
2060  1 sq.setName("test/6-13/7-99");
2061  1 assertEquals("test/6-13", sq.getName());
2062  1 assertEquals(7, sq.getStart());
2063  1 assertEquals(99, sq.getEnd());
2064   
2065  1 sq.setName("test/0-5"); // 0 is invalid - ignored
2066  1 assertEquals("test/0-5", sq.getName());
2067  1 assertEquals(7, sq.getStart());
2068  1 assertEquals(99, sq.getEnd());
2069   
2070  1 sq.setName("test/a-5"); // a is invalid - ignored
2071  1 assertEquals("test/a-5", sq.getName());
2072  1 assertEquals(7, sq.getStart());
2073  1 assertEquals(99, sq.getEnd());
2074   
2075  1 sq.setName("test/6-5"); // start > end is invalid - ignored
2076  1 assertEquals("test/6-5", sq.getName());
2077  1 assertEquals(7, sq.getStart());
2078  1 assertEquals(99, sq.getEnd());
2079   
2080  1 sq.setName("test/5"); // invalid - ignored
2081  1 assertEquals("test/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-6-7"); // invalid - ignored
2096  1 assertEquals("test/5-6-7", sq.getName());
2097  1 assertEquals(7, sq.getStart());
2098  1 assertEquals(99, sq.getEnd());
2099   
2100  1 sq.setName(null); // invalid, gets converted to space
2101  1 assertEquals("", sq.getName());
2102  1 assertEquals(7, sq.getStart());
2103  1 assertEquals(99, sq.getEnd());
2104    }
2105   
 
2106  1 toggle @Test(groups = { "Functional" })
2107    public void testCheckValidRange()
2108    {
2109  1 Sequence sq = new Sequence("test/7-12", "-ABC---DE-F--");
2110  1 assertEquals(7, sq.getStart());
2111  1 assertEquals(12, sq.getEnd());
2112   
2113    /*
2114    * checkValidRange ensures end is at least the last residue position
2115    */
2116  1 PA.setValue(sq, "end", 2);
2117  1 sq.checkValidRange();
2118  1 assertEquals(12, sq.getEnd());
2119   
2120    /*
2121    * end may be beyond the last residue position
2122    */
2123  1 PA.setValue(sq, "end", 22);
2124  1 sq.checkValidRange();
2125  1 assertEquals(22, sq.getEnd());
2126    }
2127   
 
2128  1 toggle @Test(groups = { "Functional" })
2129    public void testDeleteChars_withGaps()
2130    {
2131    /*
2132    * delete gaps only
2133    */
2134  1 SequenceI sq = new Sequence("test/8-10", "A-B-C");
2135  1 sq.createDatasetSequence();
2136  1 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2137  1 sq.deleteChars(1, 2); // delete first gap
2138  1 assertEquals("AB-C", sq.getSequenceAsString());
2139  1 assertEquals(8, sq.getStart());
2140  1 assertEquals(10, sq.getEnd());
2141  1 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2142   
2143    /*
2144    * delete gaps and residues at start (no new dataset sequence)
2145    */
2146  1 sq = new Sequence("test/8-10", "A-B-C");
2147  1 sq.createDatasetSequence();
2148  1 sq.deleteChars(0, 3); // delete A-B
2149  1 assertEquals("-C", sq.getSequenceAsString());
2150  1 assertEquals(10, sq.getStart());
2151  1 assertEquals(10, sq.getEnd());
2152  1 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2153   
2154    /*
2155    * delete gaps and residues at end (no new dataset sequence)
2156    */
2157  1 sq = new Sequence("test/8-10", "A-B-C");
2158  1 sq.createDatasetSequence();
2159  1 sq.deleteChars(2, 5); // delete B-C
2160  1 assertEquals("A-", sq.getSequenceAsString());
2161  1 assertEquals(8, sq.getStart());
2162  1 assertEquals(8, sq.getEnd());
2163  1 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2164   
2165    /*
2166    * delete gaps and residues internally (new dataset sequence)
2167    * first delete from gap to residue
2168    */
2169  1 sq = new Sequence("test/8-10", "A-B-C");
2170  1 sq.createDatasetSequence();
2171  1 sq.deleteChars(1, 3); // delete -B
2172  1 assertEquals("A-C", sq.getSequenceAsString());
2173  1 assertEquals(8, sq.getStart());
2174  1 assertEquals(9, sq.getEnd());
2175  1 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2176  1 assertEquals(8, sq.getDatasetSequence().getStart());
2177  1 assertEquals(9, sq.getDatasetSequence().getEnd());
2178   
2179    /*
2180    * internal delete from gap to gap
2181    */
2182  1 sq = new Sequence("test/8-10", "A-B-C");
2183  1 sq.createDatasetSequence();
2184  1 sq.deleteChars(1, 4); // delete -B-
2185  1 assertEquals("AC", sq.getSequenceAsString());
2186  1 assertEquals(8, sq.getStart());
2187  1 assertEquals(9, sq.getEnd());
2188  1 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2189  1 assertEquals(8, sq.getDatasetSequence().getStart());
2190  1 assertEquals(9, sq.getDatasetSequence().getEnd());
2191   
2192    /*
2193    * internal delete from residue to residue
2194    */
2195  1 sq = new Sequence("test/8-10", "A-B-C");
2196  1 sq.createDatasetSequence();
2197  1 sq.deleteChars(2, 3); // delete B
2198  1 assertEquals("A--C", sq.getSequenceAsString());
2199  1 assertEquals(8, sq.getStart());
2200  1 assertEquals(9, sq.getEnd());
2201  1 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2202  1 assertEquals(8, sq.getDatasetSequence().getStart());
2203  1 assertEquals(9, sq.getDatasetSequence().getEnd());
2204    }
2205   
2206    /**
2207    * Test the code used to locate the reference sequence ruler origin
2208    */
 
2209  1 toggle @Test(groups = { "Functional" })
2210    public void testLocateVisibleStartofSequence()
2211    {
2212    // create random alignment
2213  1 AlignmentGenerator gen = new AlignmentGenerator(false);
2214  1 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
2215   
2216  1 HiddenColumns cs = al.getHiddenColumns();
2217  1 ColumnSelection colsel = new ColumnSelection();
2218   
2219  1 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
2220  1 assertEquals(2, seq.findIndex(seq.getStart()));
2221   
2222    // no hidden columns
2223  1 assertEquals(seq.findIndex(seq.getStart()) - 1,
2224    seq.firstResidueOutsideIterator(cs.iterator()));
2225   
2226    // hidden column on gap after end of sequence - should not affect bounds
2227  1 colsel.hideSelectedColumns(13, al.getHiddenColumns());
2228  1 assertEquals(seq.findIndex(seq.getStart()) - 1,
2229    seq.firstResidueOutsideIterator(cs.iterator()));
2230   
2231  1 cs.revealAllHiddenColumns(colsel);
2232    // hidden column on gap before beginning of sequence - should vis bounds by
2233    // one
2234  1 colsel.hideSelectedColumns(0, al.getHiddenColumns());
2235  1 assertEquals(seq.findIndex(seq.getStart()) - 2,
2236    cs.absoluteToVisibleColumn(
2237    seq.firstResidueOutsideIterator(cs.iterator())));
2238   
2239  1 cs.revealAllHiddenColumns(colsel);
2240    // hide columns around most of sequence - leave one residue remaining
2241  1 cs.hideColumns(1, 3);
2242  1 cs.hideColumns(6, 11);
2243   
2244  1 Iterator<int[]> it = cs.getVisContigsIterator(0, 6, false);
2245   
2246  1 assertEquals("-D", seq.getSequenceStringFromIterator(it));
2247    // cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
2248    // { seq })[0]);
2249   
2250  1 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2251  1 cs.revealAllHiddenColumns(colsel);
2252   
2253    // hide whole sequence - should just get location of hidden region
2254    // containing sequence
2255  1 cs.hideColumns(1, 11);
2256  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2257   
2258  1 cs.revealAllHiddenColumns(colsel);
2259  1 cs.hideColumns(0, 15);
2260  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2261   
2262  1 SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
2263   
2264  1 cs.revealAllHiddenColumns(colsel);
2265  1 cs.hideColumns(7, 17);
2266  1 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2267   
2268  1 cs.revealAllHiddenColumns(colsel);
2269  1 cs.hideColumns(3, 17);
2270  1 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2271   
2272  1 cs.revealAllHiddenColumns(colsel);
2273  1 cs.hideColumns(3, 19);
2274  1 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2275   
2276  1 cs.revealAllHiddenColumns(colsel);
2277  1 cs.hideColumns(0, 0);
2278  1 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2279   
2280  1 cs.revealAllHiddenColumns(colsel);
2281  1 cs.hideColumns(0, 1);
2282  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2283   
2284  1 cs.revealAllHiddenColumns(colsel);
2285  1 cs.hideColumns(0, 2);
2286  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2287   
2288  1 cs.revealAllHiddenColumns(colsel);
2289  1 cs.hideColumns(1, 1);
2290  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2291   
2292  1 cs.revealAllHiddenColumns(colsel);
2293  1 cs.hideColumns(1, 2);
2294  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2295   
2296  1 cs.revealAllHiddenColumns(colsel);
2297  1 cs.hideColumns(1, 3);
2298  1 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2299   
2300  1 cs.revealAllHiddenColumns(colsel);
2301  1 cs.hideColumns(0, 2);
2302  1 cs.hideColumns(5, 6);
2303  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2304   
2305  1 cs.revealAllHiddenColumns(colsel);
2306  1 cs.hideColumns(0, 2);
2307  1 cs.hideColumns(5, 6);
2308  1 cs.hideColumns(9, 10);
2309  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2310   
2311  1 cs.revealAllHiddenColumns(colsel);
2312  1 cs.hideColumns(0, 2);
2313  1 cs.hideColumns(7, 11);
2314  1 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2315   
2316  1 cs.revealAllHiddenColumns(colsel);
2317  1 cs.hideColumns(2, 4);
2318  1 cs.hideColumns(7, 11);
2319  1 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2320   
2321  1 cs.revealAllHiddenColumns(colsel);
2322  1 cs.hideColumns(2, 4);
2323  1 cs.hideColumns(7, 12);
2324  1 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2325   
2326  1 cs.revealAllHiddenColumns(colsel);
2327  1 cs.hideColumns(1, 11);
2328  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2329   
2330  1 cs.revealAllHiddenColumns(colsel);
2331  1 cs.hideColumns(0, 12);
2332  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2333   
2334  1 cs.revealAllHiddenColumns(colsel);
2335  1 cs.hideColumns(0, 4);
2336  1 cs.hideColumns(6, 12);
2337  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2338   
2339  1 cs.revealAllHiddenColumns(colsel);
2340  1 cs.hideColumns(0, 1);
2341  1 cs.hideColumns(3, 12);
2342  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2343   
2344  1 cs.revealAllHiddenColumns(colsel);
2345  1 cs.hideColumns(3, 14);
2346  1 cs.hideColumns(17, 19);
2347  1 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2348   
2349  1 cs.revealAllHiddenColumns(colsel);
2350  1 cs.hideColumns(3, 7);
2351  1 cs.hideColumns(9, 14);
2352  1 cs.hideColumns(17, 19);
2353  1 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2354   
2355  1 cs.revealAllHiddenColumns(colsel);
2356  1 cs.hideColumns(0, 1);
2357  1 cs.hideColumns(3, 4);
2358  1 cs.hideColumns(6, 8);
2359  1 cs.hideColumns(10, 12);
2360  1 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2361   
2362    }
2363   
 
2364  1 toggle @Test(groups = { "Functional" })
2365    public void testTransferAnnotation()
2366    {
2367  1 Sequence origSeq = new Sequence("MYSEQ", "THISISASEQ");
2368  1 Sequence toSeq = new Sequence("MYSEQ", "THISISASEQ");
2369  1 origSeq.setDescription("DESCRIPTION");
2370  1 origSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, true));
2371   
2372  1 toSeq.transferAnnotation(origSeq, null);
2373  1 assertEquals("DESCRIPTION", toSeq.getDescription());
2374  1 toSeq = new Sequence("MYSEQ", "THISISASEQ");
2375  1 toSeq.setDescription("unchanged");
2376  1 toSeq.transferAnnotation(origSeq, null);
2377  1 assertEquals("unchanged", toSeq.getDescription());
2378   
2379  1 assertTrue(toSeq.getDBRefs().size() == 1);
2380   
2381  1 assertTrue(toSeq.getDBRefs().get(0).isCanonical());
2382   
2383    // check for promotion of non-canonical
2384    // to canonical (e.g. fetch-db-refs on a jalview project pre 2.11.2)
2385  1 toSeq.setDBRefs(null);
2386  1 toSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, false));
2387  1 toSeq.transferAnnotation(origSeq, null);
2388  1 assertTrue(toSeq.getDBRefs().size() == 1);
2389   
2390  1 assertTrue("Promotion of non-canonical DBRefEntry failed",
2391    toSeq.getDBRefs().get(0).isCanonical());
2392   
2393    }
2394    }