Clover icon

Coverage Report

  1. Project Clover database Wed Jan 7 2026 02:39:37 GMT
  2. Package jalview.datamodel

File AlignmentTest.java

 

Code metrics

88
660
48
1
1,792
1,196
100
0.15
13.75
48
2.08

Classes

Class Line # Actions
AlignmentTest 59 660 100
0.8944723689.4%
 

Contributing tests

This file is covered by 36 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.assertNull;
27    import static org.testng.AssertJUnit.assertSame;
28    import static org.testng.AssertJUnit.assertTrue;
29   
30    import java.io.IOException;
31    import java.util.Arrays;
32    import java.util.Iterator;
33    import java.util.List;
34   
35    import org.testng.Assert;
36    import org.testng.annotations.BeforeClass;
37    import org.testng.annotations.BeforeMethod;
38    import org.testng.annotations.Test;
39   
40    import jalview.analysis.AlignmentGenerator;
41    import jalview.analysis.AlignmentUtils;
42    import jalview.analysis.CrossRef;
43    import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
44    import jalview.gui.JvOptionPane;
45    import jalview.io.DataSourceType;
46    import jalview.io.FastaFile;
47    import jalview.io.FileFormat;
48    import jalview.io.FileFormatI;
49    import jalview.io.FormatAdapter;
50    import jalview.util.Comparison;
51    import jalview.util.MapList;
52   
53    /**
54    * Unit tests for Alignment datamodel.
55    *
56    * @author gmcarstairs
57    *
58    */
 
59    public class AlignmentTest
60    {
61   
 
62  1 toggle @BeforeClass(alwaysRun = true)
63    public void setUpJvOptionPane()
64    {
65  1 JvOptionPane.setInteractiveMode(false);
66  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
67    }
68   
69    // @formatter:off
70    private static final String TEST_DATA =
71    "# STOCKHOLM 1.0\n" +
72    "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
73    "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
74    "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
75    "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
76    "#=GR D.melanogaster.1 SS ................((((\n" +
77    "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
78    "#=GR D.melanogaster.2 SS ................((((\n" +
79    "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
80    "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
81    "//";
82   
83    private static final String AA_SEQS_1 =
84    ">Seq1Name/5-8\n" +
85    "K-QY--L\n" +
86    ">Seq2Name/12-15\n" +
87    "-R-FP-W-\n";
88   
89    private static final String CDNA_SEQS_1 =
90    ">Seq1Name/100-111\n" +
91    "AC-GG--CUC-CAA-CT\n" +
92    ">Seq2Name/200-211\n" +
93    "-CG-TTA--ACG---AAGT\n";
94   
95    private static final String CDNA_SEQS_2 =
96    ">Seq1Name/50-61\n" +
97    "GCTCGUCGTACT\n" +
98    ">Seq2Name/60-71\n" +
99    "GGGTCAGGCAGT\n";
100   
101    private static final String AA_SEQS_2 =
102    ">Seq1Name/5-8\n" +
103    "K-QY-L\n" +
104    ">Seq2Name/12-15\n" +
105    "-R-FPW\n";
106    private static final String AA_SEQS_2_DS =
107    ">Seq1Name/5-8\n" +
108    "KQYL\n" +
109    ">Seq2Name/12-15\n" +
110    "RFPW\n";
111    private static final String TD_SEQS_2_DS =
112    ">Seq1Name/5-8\n" +
113    "NMPR\n" +
114    ">Seq2Name/12-15\n" +
115    "VXYA\n";
116    private static final String TD_SEQS_2 =
117    ">Seq1Name/5-8\n" +
118    "-NMP-R\n" +
119    ">Seq2Name/12-15\n" +
120    "VX--YA\n";
121   
122    // @formatter:on
123   
124    private AlignmentI al;
125   
126    /**
127    * Helper method to load an alignment and ensure dataset sequences are set up.
128    *
129    * @param data
130    * @param format
131    * TODO
132    * @return
133    * @throws IOException
134    */
 
135  55 toggle protected AlignmentI loadAlignment(final String data, FileFormatI format)
136    throws IOException
137    {
138  55 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
139    format);
140  55 a.setDataset(null);
141  55 return a;
142    }
143   
144    /**
145    * assert wrapper: tests all references in the given alignment are consistent
146    *
147    * @param alignment
148    */
 
149  0 toggle public static void assertAlignmentDatasetRefs(AlignmentI alignment)
150    {
151  0 verifyAlignmentDatasetRefs(alignment, true, null);
152    }
153   
154    /**
155    * assert wrapper: tests all references in the given alignment are consistent
156    *
157    * @param alignment
158    * @param message
159    * - prefixed to any assert failed messages
160    */
 
161  1 toggle public static void assertAlignmentDatasetRefs(AlignmentI alignment,
162    String message)
163    {
164  1 verifyAlignmentDatasetRefs(alignment, true, message);
165    }
166   
167    /**
168    * verify sequence and dataset references are properly contained within
169    * dataset
170    *
171    * @param alignment
172    * - the alignmentI object to verify (either alignment or dataset)
173    * @param raiseAssert
174    * - when set, testng assertions are raised.
175    * @param message
176    * - null or a string message to prepend to the assert failed
177    * messages.
178    * @return true if alignment references were in order, otherwise false.
179    */
 
180  44 toggle public static boolean verifyAlignmentDatasetRefs(AlignmentI alignment,
181    boolean raiseAssert, String message)
182    {
183  44 if (message == null)
184    {
185  24 message = "";
186    }
187  44 if (alignment == null)
188    {
189  0 if (raiseAssert)
190    {
191  0 Assert.fail(message + "Alignment for verification was null.");
192    }
193  0 return false;
194    }
195  44 if (alignment.getDataset() != null)
196    {
197  19 AlignmentI dataset = alignment.getDataset();
198    // check all alignment sequences have their dataset within the dataset
199  19 for (SequenceI seq : alignment.getSequences())
200    {
201  39 SequenceI seqds = seq.getDatasetSequence();
202  39 if (seqds.getDatasetSequence() != null)
203    {
204  0 if (raiseAssert)
205    {
206  0 Assert.fail(message
207    + " Alignment contained a sequence who's dataset sequence has a second dataset reference.");
208    }
209  0 return false;
210    }
211  39 if (dataset.findIndex(seqds) == -1)
212    {
213  0 if (raiseAssert)
214    {
215  0 Assert.fail(message
216    + " Alignment contained a sequence who's dataset sequence was not in the dataset.");
217    }
218  0 return false;
219    }
220    }
221  19 return verifyAlignmentDatasetRefs(alignment.getDataset(), raiseAssert,
222    message);
223    }
224    else
225    {
226  25 int dsp = -1;
227    // verify all dataset sequences
228  25 for (SequenceI seqds : alignment.getSequences())
229    {
230  80 dsp++;
231  80 if (seqds.getDatasetSequence() != null)
232    {
233  2 if (raiseAssert)
234    {
235  1 Assert.fail(message
236    + " Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)");
237    }
238  1 return false;
239    }
240  78 int foundp = alignment.findIndex(seqds);
241  78 if (foundp != dsp)
242    {
243  2 if (raiseAssert)
244    {
245  1 Assert.fail(message
246    + " Dataset sequence array contains a reference at "
247    + dsp + " to a sequence first seen at " + foundp + " ("
248    + seqds.toString() + ")");
249    }
250  1 return false;
251    }
252  76 if (seqds.getDBRefs() != null)
253    {
254  28 for (DBRefEntry dbr : seqds.getDBRefs())
255    {
256  28 if (dbr.getMap() != null)
257    {
258  28 SequenceI seqdbrmapto = dbr.getMap().getTo();
259  28 if (seqdbrmapto != null)
260    {
261  28 if (seqdbrmapto.getDatasetSequence() != null)
262    {
263  0 if (raiseAssert)
264    {
265  0 Assert.fail(message
266    + " DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence");
267    }
268  0 return false;
269   
270    }
271  28 if (alignment.findIndex(dbr.getMap().getTo()) == -1)
272    {
273  2 if (raiseAssert)
274    {
275  1 Assert.fail(message + " DBRefEntry " + dbr
276    + " for sequence " + seqds
277    + " in alignment has map to sequence not in dataset");
278    }
279  1 return false;
280    }
281    }
282    }
283    }
284    }
285    }
286    // finally, verify codonmappings involve only dataset sequences.
287  19 if (alignment.getCodonFrames() != null)
288    {
289  19 for (AlignedCodonFrame alc : alignment.getCodonFrames())
290    {
291  6 for (SequenceToSequenceMapping ssm : alc.getMappings())
292    {
293  6 if (ssm.getFromSeq().getDatasetSequence() != null)
294    {
295  0 if (raiseAssert)
296    {
297  0 Assert.fail(message
298    + " CodonFrame-SSM-FromSeq is not a dataset sequence");
299    }
300  0 return false;
301    }
302  6 if (alignment.findIndex(ssm.getFromSeq()) == -1)
303    {
304   
305  2 if (raiseAssert)
306    {
307  1 Assert.fail(message
308    + " CodonFrame-SSM-FromSeq is not contained in dataset");
309    }
310  1 return false;
311    }
312  4 if (ssm.getMapping().getTo().getDatasetSequence() != null)
313    {
314  0 if (raiseAssert)
315    {
316  0 Assert.fail(message
317    + " CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence");
318    }
319  0 return false;
320    }
321  4 if (alignment.findIndex(ssm.getMapping().getTo()) == -1)
322    {
323   
324  0 if (raiseAssert)
325    {
326  0 Assert.fail(message
327    + " CodonFrame-SSM-Mapping-ToSeq is not contained in dataset");
328    }
329  0 return false;
330    }
331    }
332    }
333    }
334    }
335  17 return true; // all relationships verified!
336    }
337   
338    /**
339    * call verifyAlignmentDatasetRefs with and without assertion raising enabled,
340    * to check expected pass/fail actually occurs in both conditions
341    *
342    * @param al
343    * @param expected
344    * @param msg
345    */
 
346  12 toggle private void assertVerifyAlignment(AlignmentI al, boolean expected,
347    String msg)
348    {
349  12 if (expected)
350    {
351  8 try
352    {
353   
354  8 Assert.assertTrue(verifyAlignmentDatasetRefs(al, true, null),
355    "Valid test alignment failed when raiseAsserts enabled:"
356    + msg);
357    } catch (AssertionError ae)
358    {
359  0 ae.printStackTrace();
360  0 Assert.fail(
361    "Valid test alignment raised assertion errors when raiseAsserts enabled: "
362    + msg,
363    ae);
364    }
365    // also check validation passes with asserts disabled
366  8 Assert.assertTrue(verifyAlignmentDatasetRefs(al, false, null),
367    "Valid test alignment tested false when raiseAsserts disabled:"
368    + msg);
369    }
370    else
371    {
372  4 boolean assertRaised = false;
373  4 try
374    {
375  4 verifyAlignmentDatasetRefs(al, true, null);
376    } catch (AssertionError ae)
377    {
378    // expected behaviour
379  4 assertRaised = true;
380    }
381  4 if (!assertRaised)
382    {
383  0 Assert.fail(
384    "Invalid test alignment passed when raiseAsserts enabled:"
385    + msg);
386    }
387    // also check validation passes with asserts disabled
388  4 Assert.assertFalse(verifyAlignmentDatasetRefs(al, false, null),
389    "Invalid test alignment tested true when raiseAsserts disabled:"
390    + msg);
391    }
392    }
393   
 
394  1 toggle @Test(groups = { "Functional" })
395    public void testVerifyAlignmentDatasetRefs()
396    {
397  1 SequenceI sq1 = new Sequence("sq1", "ASFDD"),
398    sq2 = new Sequence("sq2", "TTTTTT");
399   
400    // construct simple valid alignment dataset
401  1 Alignment al = new Alignment(new SequenceI[] { sq1, sq2 });
402    // expect this to pass
403  1 assertVerifyAlignment(al, true, "Simple valid alignment didn't verify");
404   
405    // check test for sequence->datasetSequence validity
406  1 sq1.setDatasetSequence(sq2);
407  1 assertVerifyAlignment(al, false,
408    "didn't detect dataset sequence with a dataset sequence reference.");
409   
410  1 sq1.setDatasetSequence(null);
411  1 assertVerifyAlignment(al, true,
412    "didn't reinstate validity after nulling dataset sequence dataset reference");
413   
414    // now create dataset and check again
415  1 al.createDatasetAlignment();
416  1 assertNotNull(al.getDataset());
417   
418  1 assertVerifyAlignment(al, true,
419    "verify failed after createDatasetAlignment");
420   
421    // create a dbref on sq1 with a sequence ref to sq2
422  1 DBRefEntry dbrs1tos2 = new DBRefEntry("UNIPROT", "1", "Q111111");
423  1 dbrs1tos2
424    .setMap(new Mapping(sq2.getDatasetSequence(), new int[]
425    { 1, 5 }, new int[] { 2, 6 }, 1, 1));
426  1 sq1.getDatasetSequence().addDBRef(dbrs1tos2);
427  1 assertVerifyAlignment(al, true,
428    "verify failed after addition of valid DBRefEntry/map");
429    // now create a dbref on a new sequence which maps to another sequence
430    // outside of the dataset
431  1 SequenceI sqout = new Sequence("sqout", "ututututucagcagcag"),
432    sqnew = new Sequence("sqnew", "EEERRR");
433  1 DBRefEntry sqnewsqout = new DBRefEntry("ENAFOO", "1", "R000001");
434  1 sqnewsqout
435    .setMap(new Mapping(sqout, new int[]
436    { 1, 6 }, new int[] { 1, 18 }, 1, 3));
437  1 al.getDataset().addSequence(sqnew);
438   
439  1 assertVerifyAlignment(al, true,
440    "verify failed after addition of new sequence to dataset");
441    // now start checking exception conditions
442  1 sqnew.addDBRef(sqnewsqout);
443  1 assertVerifyAlignment(al, false,
444    "verify passed when a dbref with map to sequence outside of dataset was added");
445    // make the verify pass by adding the outsider back in
446  1 al.getDataset().addSequence(sqout);
447  1 assertVerifyAlignment(al, true,
448    "verify should have passed after adding dbref->to sequence in to dataset");
449    // and now the same for a codon mapping...
450  1 SequenceI sqanotherout = new Sequence("sqanotherout",
451    "aggtutaggcagcagcag");
452   
453  1 AlignedCodonFrame alc = new AlignedCodonFrame();
454  1 alc.addMap(sqanotherout, sqnew,
455    new MapList(new int[]
456    { 1, 6 }, new int[] { 1, 18 }, 3, 1));
457   
458  1 al.addCodonFrame(alc);
459  1 Assert.assertEquals(al.getDataset().getCodonFrames().size(), 1);
460   
461  1 assertVerifyAlignment(al, false,
462    "verify passed when alCodonFrame mapping to sequence outside of dataset was added");
463    // make the verify pass by adding the outsider back in
464  1 al.getDataset().addSequence(sqanotherout);
465  1 assertVerifyAlignment(al, true,
466    "verify should have passed once all sequences involved in alCodonFrame were added to dataset");
467  1 al.getDataset().addSequence(sqanotherout);
468  1 assertVerifyAlignment(al, false,
469    "verify should have failed when a sequence was added twice to the dataset");
470  1 al.getDataset().deleteSequence(sqanotherout);
471  1 assertVerifyAlignment(al, true,
472    "verify should have passed after duplicate entry for sequence was removed");
473    }
474   
475    /**
476    * checks that the sequence data for an alignment's dataset is non-redundant.
477    * Fails if there are sequences with same id, sequence, start, and.
478    */
479   
 
480  6 toggle public static void assertDatasetIsNormalised(AlignmentI al)
481    {
482  6 assertDatasetIsNormalised(al, null);
483    }
484   
485    /**
486    * checks that the sequence data for an alignment's dataset is non-redundant.
487    * Fails if there are sequences with same id, sequence, start, and.
488    *
489    * @param al
490    * - alignment to verify
491    * @param message
492    * - null or message prepended to exception message.
493    */
 
494  12 toggle public static void assertDatasetIsNormalised(AlignmentI al,
495    String message)
496    {
497  12 if (al.getDataset() != null)
498    {
499  6 assertDatasetIsNormalised(al.getDataset(), message);
500  5 return;
501    }
502    /*
503    * look for pairs of sequences with same ID, start, end, and sequence
504    */
505  6 List<SequenceI> seqSet = al.getSequences();
506  18 for (int p = 0; p < seqSet.size(); p++)
507    {
508  13 SequenceI pSeq = seqSet.get(p);
509  27 for (int q = p + 1; q < seqSet.size(); q++)
510    {
511  15 SequenceI qSeq = seqSet.get(q);
512  15 if (pSeq.getStart() != qSeq.getStart())
513    {
514  4 continue;
515    }
516  11 if (pSeq.getEnd() != qSeq.getEnd())
517    {
518  0 continue;
519    }
520  11 if (!pSeq.getName().equals(qSeq.getName()))
521    {
522  7 continue;
523    }
524  4 if (!Arrays.equals(pSeq.getSequence(), qSeq.getSequence()))
525    {
526  3 continue;
527    }
528  1 Assert.fail((message == null ? "" : message + " :")
529    + "Found similar sequences at position " + p + " and " + q
530    + "\n" + pSeq.toString());
531    }
532    }
533    }
534   
 
535  1 toggle @Test(groups = { "Functional", "Asserts" })
536    public void testAssertDatasetIsNormalised()
537    {
538  1 Sequence sq1 = new Sequence("s1/1-4", "asdf");
539  1 Sequence sq1shift = new Sequence("s1/2-5", "asdf");
540  1 Sequence sq1seqd = new Sequence("s1/1-4", "asdt");
541  1 Sequence sq2 = new Sequence("s2/1-4", "asdf");
542  1 Sequence sq1dup = new Sequence("s1/1-4", "asdf");
543   
544  1 Alignment al = new Alignment(new SequenceI[] { sq1 });
545  1 al.setDataset(null);
546   
547  1 try
548    {
549  1 assertDatasetIsNormalised(al);
550    } catch (AssertionError ae)
551    {
552  0 Assert.fail("Single sequence should be valid normalised dataset.");
553    }
554  1 al.addSequence(sq2);
555  1 try
556    {
557  1 assertDatasetIsNormalised(al);
558    } catch (AssertionError ae)
559    {
560  0 Assert.fail(
561    "Two different sequences should be valid normalised dataset.");
562    }
563    /*
564    * now change sq2's name in the alignment. should still be valid
565    */
566  1 al.findName(sq2.getName()).setName("sq1");
567  1 try
568    {
569  1 assertDatasetIsNormalised(al);
570    } catch (AssertionError ae)
571    {
572  0 Assert.fail(
573    "Two different sequences in dataset, but same name in alignment, should be valid normalised dataset.");
574    }
575   
576  1 al.addSequence(sq1seqd);
577  1 try
578    {
579  1 assertDatasetIsNormalised(al);
580    } catch (AssertionError ae)
581    {
582  0 Assert.fail(
583    "sq1 and sq1 with different sequence should be distinct.");
584    }
585   
586  1 al.addSequence(sq1shift);
587  1 try
588    {
589  1 assertDatasetIsNormalised(al);
590    } catch (AssertionError ae)
591    {
592  0 Assert.fail(
593    "sq1 and sq1 with different start/end should be distinct.");
594    }
595    /*
596    * finally, the failure case
597    */
598  1 al.addSequence(sq1dup);
599  1 boolean ssertRaised = false;
600  1 try
601    {
602  1 assertDatasetIsNormalised(al);
603   
604    } catch (AssertionError ae)
605    {
606  1 ssertRaised = true;
607    }
608  1 if (!ssertRaised)
609    {
610  0 Assert.fail("Expected identical sequence to raise exception.");
611    }
612    }
613   
614    /*
615    * Read in Stockholm format test data including secondary structure
616    * annotations.
617    */
 
618  36 toggle @BeforeMethod(alwaysRun = true)
619    public void setUp() throws IOException
620    {
621  36 al = loadAlignment(TEST_DATA, FileFormat.Stockholm);
622  36 int i = 0;
623  36 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
624    {
625  108 ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
626  108 i++;
627    }
628    }
629   
630    /**
631    * Test method that returns annotations that match on calcId.
632    */
 
633  1 toggle @Test(groups = { "Functional" })
634    public void testFindAnnotation_byCalcId()
635    {
636  1 Iterable<AlignmentAnnotation> anns = al
637    .findAnnotation("CalcIdForD.melanogaster.2");
638  1 Iterator<AlignmentAnnotation> iter = anns.iterator();
639  1 assertTrue(iter.hasNext());
640  1 AlignmentAnnotation ann = iter.next();
641  1 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
642  1 assertFalse(iter.hasNext());
643   
644    // invalid id
645  1 anns = al.findAnnotation("CalcIdForD.melanogaster.?");
646  1 assertFalse(iter.hasNext());
647  1 anns = al.findAnnotation(null);
648  1 assertFalse(iter.hasNext());
649    }
650   
651    /**
652    * Test method that returns annotations that match on reference sequence,
653    * label, or calcId.
654    */
 
655  1 toggle @Test(groups = { "Functional" })
656    public void testFindAnnotations_bySeqLabelandorCalcId()
657    {
658    // TODO: finish testFindAnnotations_bySeqLabelandorCalcId test
659    /* Note - this is an incomplete test - need to check null or
660    * non-null [ matches, not matches ] behaviour for each of the three
661    * parameters..*/
662   
663    // search for a single, unique calcId with wildcards on other params
664  1 Iterable<AlignmentAnnotation> anns = al.findAnnotations(null,
665    "CalcIdForD.melanogaster.2", null);
666  1 Iterator<AlignmentAnnotation> iter = anns.iterator();
667  1 assertTrue(iter.hasNext());
668  1 AlignmentAnnotation ann = iter.next();
669  1 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
670  1 assertFalse(iter.hasNext());
671   
672    // save reference to test sequence reference parameter
673  1 SequenceI rseq = ann.sequenceRef;
674   
675    // search for annotation associated with a single sequence
676  1 anns = al.findAnnotations(rseq, null, null);
677  1 iter = anns.iterator();
678  1 assertTrue(iter.hasNext());
679  1 ann = iter.next();
680  1 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
681  1 assertFalse(iter.hasNext());
682   
683    // search for annotation with a non-existant calcId
684  1 anns = al.findAnnotations(null, "CalcIdForD.melanogaster.?", null);
685  1 iter = anns.iterator();
686  1 assertFalse(iter.hasNext());
687   
688    // search for annotation with a particular label - expect three
689  1 anns = al.findAnnotations(null, null, "Secondary Structure");
690  1 iter = anns.iterator();
691  1 assertTrue(iter.hasNext());
692  1 iter.next();
693  1 assertTrue(iter.hasNext());
694  1 iter.next();
695  1 assertTrue(iter.hasNext());
696  1 iter.next();
697    // third found.. so
698  1 assertFalse(iter.hasNext());
699   
700    // search for annotation on one sequence with a particular label - expect
701    // one
702  1 SequenceI sqfound;
703  1 anns = al.findAnnotations(sqfound = al.getSequenceAt(1), null,
704    "Secondary Structure");
705  1 iter = anns.iterator();
706  1 assertTrue(iter.hasNext());
707    // expect reference to sequence 1 in the alignment
708  1 assertTrue(sqfound == iter.next().sequenceRef);
709  1 assertFalse(iter.hasNext());
710   
711    // null on all parameters == find all annotations
712  1 anns = al.findAnnotations(null, null, null);
713  1 iter = anns.iterator();
714  1 int n = al.getAlignmentAnnotation().length;
715  4 while (iter.hasNext())
716    {
717  3 n--;
718  3 iter.next();
719    }
720  1 assertTrue("Found " + n + " fewer annotations from search.", n == 0);
721    }
722   
 
723  1 toggle @Test(groups = { "Functional" })
724    public void testDeleteAllAnnotations_includingAutocalculated()
725    {
726  1 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
727    "Consensus", 0.5);
728  1 aa.autoCalculated = true;
729  1 al.addAnnotation(aa);
730  1 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
731  1 assertEquals("Wrong number of annotations before deleting", 4,
732    anns.length);
733  1 al.deleteAllAnnotations(true);
734  1 assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
735    }
736   
 
737  1 toggle @Test(groups = { "Functional" })
738    public void testDeleteAllAnnotations_excludingAutocalculated()
739    {
740  1 AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
741    "Consensus", 0.5);
742  1 aa.autoCalculated = true;
743  1 al.addAnnotation(aa);
744  1 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
745  1 assertEquals("Wrong number of annotations before deleting", 4,
746    anns.length);
747  1 al.deleteAllAnnotations(false);
748  1 assertEquals("Not just one annotation left", 1,
749    al.getAlignmentAnnotation().length);
750    }
751   
 
752  1 toggle @Test(groups = { "Functional" })
753    public void testIsSecondaryStructurePresent()
754    {
755    // default - there are secondary structure annotations in test data
756  1 assertTrue(al.isSecondaryStructurePresent());
757  1 al.deleteAllAnnotations(true);
758  1 assertFalse(al.isSecondaryStructurePresent());
759   
760  1 AlignmentAnnotation aa = new AlignmentAnnotation("Secondary Structure",
761    "Secondary Structure", 0.5);
762  1 Annotation ae[] = new Annotation[] { new Annotation("H", "H", 'H', 0f),
763    new Annotation("H", "H", 'H', 0f),
764    new Annotation("H", "H", 'H', 0f),
765    new Annotation("H", "H", 'H', 0f) };
766  1 aa.annotations = ae;
767  1 aa.validateRangeAndDisplay();
768  1 al.addAnnotation(aa);
769    // Should return false when the alignment has secondary structure
770    // annotation but is not sequence associated
771  1 assertFalse(al.isSecondaryStructurePresent());
772  1 aa = new AlignmentAnnotation(aa);
773  1 al.getSequenceAt(0).addAlignmentAnnotation(aa);
774  1 al.addAnnotation(aa);
775    // Should return true when the alignment has secondary structure
776    // annotation which is sequence associated
777  1 assertTrue(al.isSecondaryStructurePresent());
778    }
779   
780    /**
781    * Tests for realigning as per a supplied alignment: Dna as Dna.
782    *
783    * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
784    * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
785    * as here.
786    *
787    * @throws IOException
788    */
 
789  1 toggle @Test(groups = { "Functional" })
790    public void testAlignAs_dnaAsDna() throws IOException
791    {
792    // aligned cDNA:
793  1 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
794    // unaligned cDNA:
795  1 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, FileFormat.Fasta);
796   
797    /*
798    * Make mappings between sequences. The 'aligned cDNA' is playing the role
799    * of what would normally be protein here.
800    */
801  1 makeMappings(al1, al2);
802   
803  1 ((Alignment) al2).alignAs(al1, false, true);
804  1 assertEquals("GC-TC--GUC-GTACT",
805    al2.getSequenceAt(0).getSequenceAsString());
806  1 assertEquals("-GG-GTC--AGG--CAGT",
807    al2.getSequenceAt(1).getSequenceAsString());
808    }
809   
810    /**
811    * Aligning protein from cDNA.
812    *
813    * @throws IOException
814    */
 
815  1 toggle @Test(groups = { "Functional" })
816    public void testAlignAs_proteinAsCdna() throws IOException
817    {
818    // see also AlignmentUtilsTests
819  1 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
820  1 AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
821  1 makeMappings(al1, al2);
822   
823    // Fudge - alignProteinAsCdna expects mappings to be on protein
824  1 al2.getCodonFrames().addAll(al1.getCodonFrames());
825   
826  1 ((Alignment) al2).alignAs(al1, false, true);
827  1 assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
828  1 assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
829    }
830   
831   
832    /**
833    * Recover protein MSA from tdi msa
834    *
835    * @throws IOException
836    */
 
837  1 toggle @Test(groups = { "Functional" })
838    public void testAlignAs_prot_tdi() throws Exception
839    {
840    // see also AlignmentUtilsTests
841  1 AlignmentI al1 = loadAlignment(TD_SEQS_2, FileFormat.Fasta);
842  1 AlignmentI al2 = loadAlignment(AA_SEQS_2_DS, FileFormat.Fasta);
843  1 al1.setDataset(null);
844  1 al2.setDataset(al1.getDataset());
845  1 AlignmentI al1copy = new Alignment(al1);
846  1 AlignmentI al2copy = new Alignment(al2);
847  1 AlignmentUtils.map3diPeptideToProteinAligment(al2, al1);
848  1 if (al2.getCodonFrames().isEmpty()) {al2.getCodonFrames().addAll(al1.getCodonFrames()); }
849  1 else {al1.getCodonFrames().addAll(al2.getCodonFrames()); };
850   
851  1 ((Alignment) al2).alignAs(al1);
852  1 assertEquals("-NMP-R", al1.getSequenceAt(0).getSequenceAsString());
853  1 assertEquals("VX--YA", al1.getSequenceAt(1).getSequenceAsString());
854  1 assertEquals("-KQY-L", al2.getSequenceAt(0).getSequenceAsString());
855  1 assertEquals("RF--PW", al2.getSequenceAt(1).getSequenceAsString());
856   
857    }
858    /**
859    * Recover TdI MSA from protein msa
860    *
861    * @throws IOException
862    */
 
863  1 toggle @Test(groups = { "Functional" })
864    public void testAlignAs_tdi_prot() throws Exception
865    {
866    // see also AlignmentUtilsTests
867  1 AlignmentI al1 = loadAlignment(AA_SEQS_2, FileFormat.Fasta);
868  1 AlignmentI al2 = loadAlignment(TD_SEQS_2_DS, FileFormat.Fasta);
869  1 al1.setDataset(null);
870  1 al2.setDataset(al1.getDataset());
871  1 AlignmentI al1copy = new Alignment(al1);
872  1 AlignmentI al2copy = new Alignment(al2);
873  1 AlignmentUtils.map3diPeptideToProteinAligment(al1, al2);
874  1 if (al2.getCodonFrames().isEmpty()) {al2.getCodonFrames().addAll(al1.getCodonFrames()); }
875  0 else {al1.getCodonFrames().addAll(al2.getCodonFrames()); };
876   
877  1 ((Alignment) al2).alignAs(al1);
878  1 assertEquals("K-QY-L", al1.getSequenceAt(0).getSequenceAsString());
879  1 assertEquals("-R-FPW", al1.getSequenceAt(1).getSequenceAsString());
880  1 assertEquals("N-MP-R", al2.getSequenceAt(0).getSequenceAsString());
881  1 assertEquals("-V-XYA", al2.getSequenceAt(1).getSequenceAsString());
882   
883    }
884    /**
885    * Test aligning cdna as per protein alignment.
886    *
887    * @throws IOException
888    */
 
889  1 toggle @Test(groups = { "Functional" }, enabled = true)
890    // TODO review / update this test after redesign of alignAs method
891    public void testAlignAs_cdnaAsProtein() throws IOException
892    {
893    /*
894    * Load alignments and add mappings for cDNA to protein
895    */
896  1 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
897  1 AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
898  1 makeMappings(al1, al2);
899   
900    /*
901    * Realign DNA; currently keeping existing gaps in introns only
902    */
903  1 ((Alignment) al1).alignAs(al2, false, true);
904  1 assertEquals("ACG---GCUCCA------ACT---",
905    al1.getSequenceAt(0).getSequenceAsString());
906  1 assertEquals("---CGT---TAACGA---AGT---",
907    al1.getSequenceAt(1).getSequenceAsString());
908    }
909   
910    /**
911    * Test aligning cdna as per protein - single sequences
912    *
913    * @throws IOException
914    */
 
915  1 toggle @Test(groups = { "Functional" }, enabled = true)
916    // TODO review / update this test after redesign of alignAs method
917    public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException
918    {
919    /*
920    * simple case insert one gap
921    */
922  1 verifyAlignAs(">dna\nCAAaaa\n", ">protein\nQ-K\n", "CAA---aaa");
923   
924    /*
925    * simple case but with sequence offsets
926    */
927  1 verifyAlignAs(">dna/5-10\nCAAaaa\n", ">protein/20-21\nQ-K\n",
928    "CAA---aaa");
929   
930    /*
931    * insert gaps as per protein, drop gaps within codons
932    */
933  1 verifyAlignAs(">dna/10-18\nCA-Aa-aa--AGA\n", ">aa/6-8\n-Q-K--R\n",
934    "---CAA---aaa------AGA");
935    }
936   
937    /**
938    * test mapping between a protein and 3di sequence alignment. Assumes 1:1
939    * @throws IOException
940    */
 
941  1 toggle @Test(groups={"Functional"},enabled=true)
942    public void testAlignAs_3di() throws IOException
943    {
944  1 String protAl = ">1ji5_A\n"
945    + "-----------------------------DQPVLLLLLLQLLLLLVLLLQQLVVCLVQAD\n"
946    + "DPCNVVSNVVSVVSSVVSVVSNVVSQVVCVVVVHHHDDDVSSVVRYPQDHHDPP--DYPL\n"
947    + "RSLVSLLVSLVVVLVSLVVSLVSCVVVVNVVSNVSSVVVSVVSVVSNVVSCVVVVD----\n"
948    + "---------------------------------------------------\n"
949    + ">1jig_A\n"
950    + "---------------------------DALLVVLLLLLLQLLLALVLLLQQLVLCLVLAD\n"
951    + "DPCNVVSNVVSVVVSVVSVVSNVVSQVVCVVSVHHHDDDVSSVVRYPQDHDDSP--DYPL\n"
952    + "RSLVSLLVSLVVLLVSLVVSLVSCVVNVNPVSNVSSVVSSVVSVVSNVVSVVVND-----\n"
953    + "---------------------------------------------------\n"
954    + "\n";
955  1 String tdiAl = ">1ji5_A\n"
956    + "-----------------------------MNKQVIEVLNKQVADWSVLFTKLHNFHWYVK\n"
957    + "GPQFFTLHEKFEELYTESATHIDEIAERILAIGGKPVATKEYLEISSIQEAAYG--ETAE\n"
958    + "GMVEAIMKDYEMMLVELKKGMEIAQNSDDEMTSDLLLGIYTELEKHAWMLRAFLNQ----\n"
959    + "---------------------------------------------------\n"
960    + ">1jig_A\n"
961    + "---------------------------MSTKTNVVEVLNKQVANWNVLYVKLHNYHWYVT\n"
962    + "GPHFFTLHEKFEEFYNEAGTYIDELAERILALEGKPLATKEYLATSSVNEGTSK--ESAE\n"
963    + "EMVQTLVNDYSALIQELKEGMEVAGEAGDATSADMLLAIHTTLEQHVWMLSAFLK-----\n"
964    + "---------------------------------------------------\n" + "";
965  1 AlignmentI prot = loadAlignment(protAl, FileFormat.Fasta);
966  1 ((Alignment) prot).createDatasetAlignment();
967   
968  1 AlignmentI tdi = loadAlignment(tdiAl, FileFormat.Fasta);
969  1 assertTrue(AlignmentUtils.map3diPeptideToProteinAligment(prot, tdi));
970   
971  1 AlignmentI newProt = new Alignment(
972    new SequenceI[]
973    { prot.getSequenceAt(0).getSubSequence(25, 35),
974    prot.getSequenceAt(1).getSubSequence(35, 45) });
975  1 newProt.setDataset(prot.getDataset());
976   
977    // TODO Find matching tdi sequence and construct alignment mirroring
978    // the protein alignment
979    // Alignment newTdi = new CrossRef(newProt.getSequencesArray(),
980    // newProt.getDataset()).findXrefSequences("", false);
981    //
982    // newTdi.alignAs(newProt);
983    //
984    // System.out.println("newProt - aa\n"+new
985    // FastaFile().print(newProt.getSequencesArray(), true));
986    // System.out.println("newProt - 3di\n"+new
987    // FastaFile().print(newTdi.getSequencesArray(), true));
988   
989    }
990    /**
991    * Helper method that makes mappings and then aligns the first alignment as
992    * the second
993    *
994    * @param fromSeqs
995    * @param toSeqs
996    * @param expected
997    * @throws IOException
998    */
 
999  3 toggle public void verifyAlignAs(String fromSeqs, String toSeqs, String expected)
1000    throws IOException
1001    {
1002    /*
1003    * Load alignments and add mappings from nucleotide to protein (or from
1004    * first to second if both the same type)
1005    */
1006  3 AlignmentI al1 = loadAlignment(fromSeqs, FileFormat.Fasta);
1007  3 AlignmentI al2 = loadAlignment(toSeqs, FileFormat.Fasta);
1008  3 makeMappings(al1, al2);
1009   
1010    /*
1011    * Realign DNA; currently keeping existing gaps in introns only
1012    */
1013  3 ((Alignment) al1).alignAs(al2, false, true);
1014  3 assertEquals(expected, al1.getSequenceAt(0).getSequenceAsString());
1015    }
1016   
1017    /**
1018    * Helper method to make mappings between sequences, and add the mappings to
1019    * the 'mapped from' alignment. If alFrom.isNucleotide() == alTo.isNucleotide() then ratio is always 1:1
1020    *
1021    * @param alFrom
1022    * @param alTo
1023    */
 
1024  6 toggle public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
1025    {
1026  6 int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
1027   
1028  6 AlignedCodonFrame acf = new AlignedCodonFrame();
1029   
1030  15 for (int i = 0; i < alFrom.getHeight(); i++)
1031    {
1032  9 SequenceI seqFrom = alFrom.getSequenceAt(i);
1033  9 SequenceI seqTo = alTo.getSequenceAt(i);
1034  9 MapList ml = new MapList(
1035    new int[]
1036    { seqFrom.getStart(), seqFrom.getEnd() },
1037    new int[]
1038    { seqTo.getStart(), seqTo.getEnd() }, ratio, 1);
1039  9 acf.addMap(seqFrom, seqTo, ml);
1040    }
1041   
1042    /*
1043    * not sure whether mappings 'belong' or protein or nucleotide
1044    * alignment, so adding to both ;~)
1045    */
1046  6 alFrom.addCodonFrame(acf);
1047  6 alTo.addCodonFrame(acf);
1048    }
1049   
1050    /**
1051    * Test aligning dna as per protein alignment, for the case where there are
1052    * introns (i.e. some dna sites have no mapping from a peptide).
1053    *
1054    * @throws IOException
1055    */
 
1056  0 toggle @Test(groups = { "Functional" }, enabled = false)
1057    // TODO review / update this test after redesign of alignAs method
1058    public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
1059    {
1060    /*
1061    * Load alignments and add mappings for cDNA to protein
1062    */
1063  0 String dna1 = "A-Aa-gG-GCC-cT-TT";
1064  0 String dna2 = "c--CCGgg-TT--T-AA-A";
1065  0 AlignmentI al1 = loadAlignment(
1066    ">Dna1/6-17\n" + dna1 + "\n>Dna2/20-31\n" + dna2 + "\n",
1067    FileFormat.Fasta);
1068  0 AlignmentI al2 = loadAlignment(
1069    ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", FileFormat.Fasta);
1070  0 AlignedCodonFrame acf = new AlignedCodonFrame();
1071    // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
1072    // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
1073  0 MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 },
1074    new int[]
1075    { 7, 9 }, 3, 1);
1076  0 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
1077  0 MapList ml2 = new MapList(new int[] { 21, 23, 26, 31 },
1078    new int[]
1079    { 11, 13 }, 3, 1);
1080  0 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
1081  0 al2.addCodonFrame(acf);
1082   
1083    /*
1084    * Align ignoring gaps in dna introns and exons
1085    */
1086  0 ((Alignment) al1).alignAs(al2, false, false);
1087  0 assertEquals("---AAagG------GCCcTTT",
1088    al1.getSequenceAt(0).getSequenceAsString());
1089    // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
1090  0 assertEquals("cCCGgg-TTT------AAA",
1091    al1.getSequenceAt(1).getSequenceAsString());
1092   
1093    /*
1094    * Reset and realign, preserving gaps in dna introns and exons
1095    */
1096  0 al1.getSequenceAt(0).setSequence(dna1);
1097  0 al1.getSequenceAt(1).setSequence(dna2);
1098  0 ((Alignment) al1).alignAs(al2, true, true);
1099    // String dna1 = "A-Aa-gG-GCC-cT-TT";
1100    // String dna2 = "c--CCGgg-TT--T-AA-A";
1101    // assumption: we include 'the greater of' protein/dna gap lengths, not both
1102  0 assertEquals("---A-Aa-gG------GCC-cT-TT",
1103    al1.getSequenceAt(0).getSequenceAsString());
1104  0 assertEquals("c--CCGgg-TT--T------AA-A",
1105    al1.getSequenceAt(1).getSequenceAsString());
1106    }
1107   
 
1108  1 toggle @Test(groups = "Functional")
1109    public void testCopyConstructor() throws IOException
1110    {
1111  1 AlignmentI protein = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
1112    // create sequence and alignment datasets
1113  1 protein.setDataset(null);
1114  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1115  1 List<AlignedCodonFrame> acfList = Arrays
1116    .asList(new AlignedCodonFrame[]
1117    { acf });
1118  1 protein.getDataset().setCodonFrames(acfList);
1119  1 AlignmentI copy = new Alignment(protein);
1120   
1121    /*
1122    * copy has different aligned sequences but the same dataset sequences
1123    */
1124  1 assertFalse(copy.getSequenceAt(0) == protein.getSequenceAt(0));
1125  1 assertFalse(copy.getSequenceAt(1) == protein.getSequenceAt(1));
1126  1 assertSame(copy.getSequenceAt(0).getDatasetSequence(),
1127    protein.getSequenceAt(0).getDatasetSequence());
1128  1 assertSame(copy.getSequenceAt(1).getDatasetSequence(),
1129    protein.getSequenceAt(1).getDatasetSequence());
1130   
1131    // TODO should the copy constructor copy the dataset?
1132    // or make a new one referring to the same dataset sequences??
1133  1 assertNull(copy.getDataset());
1134    // TODO test metadata is copied when AlignmentI is a dataset
1135   
1136    // assertArrayEquals(copy.getDataset().getSequencesArray(), protein
1137    // .getDataset().getSequencesArray());
1138    }
1139   
1140    /**
1141    * Test behaviour of createDataset
1142    *
1143    * @throws IOException
1144    */
 
1145  1 toggle @Test(groups = "Functional")
1146    public void testCreateDatasetAlignment() throws IOException
1147    {
1148  1 AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1,
1149    DataSourceType.PASTE, FileFormat.Fasta);
1150    /*
1151    * create a dataset sequence on first sequence
1152    * leave the second without one
1153    */
1154  1 protein.getSequenceAt(0).createDatasetSequence();
1155  1 assertNotNull(protein.getSequenceAt(0).getDatasetSequence());
1156  1 assertNull(protein.getSequenceAt(1).getDatasetSequence());
1157   
1158    /*
1159    * add a mapping to the alignment
1160    */
1161  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1162  1 protein.addCodonFrame(acf);
1163  1 assertNull(protein.getDataset());
1164  1 assertTrue(protein.getCodonFrames().contains(acf));
1165   
1166    /*
1167    * create the alignment dataset
1168    * note this creates sequence datasets where missing
1169    * as a side-effect (in this case, on seq2
1170    */
1171    // TODO promote this method to AlignmentI
1172  1 ((Alignment) protein).createDatasetAlignment();
1173   
1174  1 AlignmentI ds = protein.getDataset();
1175   
1176    // side-effect: dataset created on second sequence
1177  1 assertNotNull(protein.getSequenceAt(1).getDatasetSequence());
1178    // dataset alignment has references to dataset sequences
1179  1 assertEquals(ds.getSequenceAt(0),
1180    protein.getSequenceAt(0).getDatasetSequence());
1181  1 assertEquals(ds.getSequenceAt(1),
1182    protein.getSequenceAt(1).getDatasetSequence());
1183   
1184    // codon frames should have been moved to the dataset
1185    // getCodonFrames() should delegate to the dataset:
1186  1 assertTrue(protein.getCodonFrames().contains(acf));
1187    // prove the codon frames are indeed on the dataset:
1188  1 assertTrue(ds.getCodonFrames().contains(acf));
1189    }
1190   
1191    /**
1192    * tests the addition of *all* sequences referred to by a sequence being added
1193    * to the dataset
1194    */
 
1195  1 toggle @Test(groups = "Functional")
1196    public void testCreateDatasetAlignmentWithMappedToSeqs()
1197    {
1198    // Alignment with two sequences, gapped.
1199  1 SequenceI sq1 = new Sequence("sq1", "A--SDF");
1200  1 SequenceI sq2 = new Sequence("sq2", "G--TRQ");
1201   
1202    // cross-references to two more sequences.
1203  1 DBRefEntry dbr = new DBRefEntry("SQ1", "", "sq3");
1204  1 SequenceI sq3 = new Sequence("sq3", "VWANG");
1205  1 dbr.setMap(
1206    new Mapping(sq3, new MapList(new int[]
1207    { 1, 4 }, new int[] { 2, 5 }, 1, 1)));
1208  1 sq1.addDBRef(dbr);
1209   
1210  1 SequenceI sq4 = new Sequence("sq4", "ERKWI");
1211  1 DBRefEntry dbr2 = new DBRefEntry("SQ2", "", "sq4");
1212  1 dbr2.setMap(
1213    new Mapping(sq4, new MapList(new int[]
1214    { 1, 4 }, new int[] { 2, 5 }, 1, 1)));
1215  1 sq2.addDBRef(dbr2);
1216    // and a 1:1 codonframe mapping between them.
1217  1 AlignedCodonFrame alc = new AlignedCodonFrame();
1218  1 alc.addMap(sq1, sq2,
1219    new MapList(new int[]
1220    { 1, 4 }, new int[] { 1, 4 }, 1, 1));
1221   
1222  1 AlignmentI protein = new Alignment(new SequenceI[] { sq1, sq2 });
1223   
1224    /*
1225    * create the alignment dataset
1226    * note this creates sequence datasets where missing
1227    * as a side-effect (in this case, on seq2
1228    */
1229   
1230    // TODO promote this method to AlignmentI
1231  1 ((Alignment) protein).createDatasetAlignment();
1232   
1233  1 AlignmentI ds = protein.getDataset();
1234   
1235    // should be 4 sequences in dataset - two materialised, and two propagated
1236    // from dbref
1237  1 assertEquals(4, ds.getHeight());
1238  1 assertTrue(ds.getSequences().contains(sq1.getDatasetSequence()));
1239  1 assertTrue(ds.getSequences().contains(sq2.getDatasetSequence()));
1240  1 assertTrue(ds.getSequences().contains(sq3));
1241  1 assertTrue(ds.getSequences().contains(sq4));
1242    // Should have one codon frame mapping between sq1 and sq2 via dataset
1243    // sequences
1244  1 assertEquals(ds.getCodonFrame(sq1.getDatasetSequence()),
1245    ds.getCodonFrame(sq2.getDatasetSequence()));
1246    }
1247   
 
1248  1 toggle @Test(groups = "Functional")
1249    public void testAddCodonFrame()
1250    {
1251  1 AlignmentI align = new Alignment(new SequenceI[] {});
1252  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1253  1 align.addCodonFrame(acf);
1254  1 assertEquals(1, align.getCodonFrames().size());
1255  1 assertTrue(align.getCodonFrames().contains(acf));
1256    // can't add the same object twice:
1257  1 align.addCodonFrame(acf);
1258  1 assertEquals(1, align.getCodonFrames().size());
1259   
1260    // create dataset alignment - mappings move to dataset
1261  1 ((Alignment) align).createDatasetAlignment();
1262  1 assertSame(align.getCodonFrames(), align.getDataset().getCodonFrames());
1263  1 assertEquals(1, align.getCodonFrames().size());
1264   
1265  1 AlignedCodonFrame acf2 = new AlignedCodonFrame();
1266  1 align.addCodonFrame(acf2);
1267  1 assertTrue(align.getDataset().getCodonFrames().contains(acf));
1268    }
1269   
 
1270  1 toggle @Test(groups = "Functional")
1271    public void testAddSequencePreserveDatasetIntegrity()
1272    {
1273  1 Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1274  1 Alignment align = new Alignment(new SequenceI[] { seq });
1275  1 align.createDatasetAlignment();
1276  1 AlignmentI ds = align.getDataset();
1277  1 SequenceI copy = new Sequence(seq);
1278  1 copy.insertCharAt(3, 5, '-');
1279  1 align.addSequence(copy);
1280  1 Assert.assertEquals(align.getDataset().getHeight(), 1,
1281    "Dataset shouldn't have more than one sequence.");
1282   
1283  1 Sequence seq2 = new Sequence("newtestSeq",
1284    "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1285  1 align.addSequence(seq2);
1286  1 Assert.assertEquals(align.getDataset().getHeight(), 2,
1287    "Dataset should now have two sequences.");
1288   
1289  1 assertAlignmentDatasetRefs(align,
1290    "addSequence broke dataset reference integrity");
1291    }
1292   
1293    /**
1294    * Tests that dbrefs with mappings to sequence get updated if the sequence
1295    * acquires a dataset sequence
1296    */
 
1297  1 toggle @Test(groups = "Functional")
1298    public void testCreateDataset_updateDbrefMappings()
1299    {
1300  1 SequenceI pep = new Sequence("pep", "ASD");
1301  1 SequenceI dna = new Sequence("dna", "aaaGCCTCGGATggg");
1302  1 SequenceI cds = new Sequence("cds", "GCCTCGGAT");
1303   
1304    // add dbref from dna to peptide
1305  1 DBRefEntry dbr = new DBRefEntry("UNIPROT", "", "pep");
1306  1 dbr.setMap(
1307    new Mapping(pep, new MapList(new int[]
1308    { 4, 15 }, new int[] { 1, 4 }, 3, 1)));
1309  1 dna.addDBRef(dbr);
1310   
1311    // add dbref from dna to peptide
1312  1 DBRefEntry dbr2 = new DBRefEntry("UNIPROT", "", "pep");
1313  1 dbr2.setMap(
1314    new Mapping(pep, new MapList(new int[]
1315    { 1, 12 }, new int[] { 1, 4 }, 3, 1)));
1316  1 cds.addDBRef(dbr2);
1317   
1318    // add dbref from peptide to dna
1319  1 DBRefEntry dbr3 = new DBRefEntry("EMBL", "", "dna");
1320  1 dbr3.setMap(
1321    new Mapping(dna, new MapList(new int[]
1322    { 1, 4 }, new int[] { 4, 15 }, 1, 3)));
1323  1 pep.addDBRef(dbr3);
1324   
1325    // add dbref from peptide to cds
1326  1 DBRefEntry dbr4 = new DBRefEntry("EMBLCDS", "", "cds");
1327  1 dbr4.setMap(
1328    new Mapping(cds, new MapList(new int[]
1329    { 1, 4 }, new int[] { 1, 12 }, 1, 3)));
1330  1 pep.addDBRef(dbr4);
1331   
1332  1 AlignmentI protein = new Alignment(new SequenceI[] { pep });
1333   
1334    /*
1335    * create the alignment dataset
1336    */
1337  1 ((Alignment) protein).createDatasetAlignment();
1338   
1339  1 AlignmentI ds = protein.getDataset();
1340   
1341    // should be 3 sequences in dataset
1342  1 assertEquals(3, ds.getHeight());
1343  1 assertTrue(ds.getSequences().contains(pep.getDatasetSequence()));
1344  1 assertTrue(ds.getSequences().contains(dna));
1345  1 assertTrue(ds.getSequences().contains(cds));
1346   
1347    /*
1348    * verify peptide.cdsdbref.peptidedbref is now mapped to peptide dataset
1349    */
1350  1 List<DBRefEntry> dbRefs = pep.getDBRefs();
1351  1 assertEquals(2, dbRefs.size());
1352  1 assertSame(dna, dbRefs.get(0).map.to);
1353  1 assertSame(cds, dbRefs.get(1).map.to);
1354  1 assertEquals(1, dna.getDBRefs().size());
1355  1 assertSame(pep.getDatasetSequence(), dna.getDBRefs().get(0).map.to);
1356  1 assertEquals(1, cds.getDBRefs().size());
1357  1 assertSame(pep.getDatasetSequence(), cds.getDBRefs().get(0).map.to);
1358    }
1359   
 
1360  1 toggle @Test(groups = { "Functional" })
1361    public void testFindGroup()
1362    {
1363  1 SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
1364  1 SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
1365  1 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
1366   
1367  1 assertNull(a.findGroup(null, 0));
1368  1 assertNull(a.findGroup(seq1, 1));
1369  1 assertNull(a.findGroup(seq1, -1));
1370   
1371    /*
1372    * add a group consisting of just "DEF"
1373    */
1374  1 SequenceGroup sg1 = new SequenceGroup();
1375  1 sg1.addSequence(seq1, false);
1376  1 sg1.setStartRes(3);
1377  1 sg1.setEndRes(5);
1378  1 a.addGroup(sg1);
1379   
1380  1 assertNull(a.findGroup(seq1, 2)); // position not in group
1381  1 assertNull(a.findGroup(seq1, 6)); // position not in group
1382  1 assertNull(a.findGroup(seq2, 5)); // sequence not in group
1383  1 assertSame(a.findGroup(seq1, 3), sg1); // yes
1384  1 assertSame(a.findGroup(seq1, 4), sg1);
1385  1 assertSame(a.findGroup(seq1, 5), sg1);
1386   
1387    /*
1388    * add a group consisting of
1389    * EF--
1390    * KLMN
1391    */
1392  1 SequenceGroup sg2 = new SequenceGroup();
1393  1 sg2.addSequence(seq1, false);
1394  1 sg2.addSequence(seq2, false);
1395  1 sg2.setStartRes(4);
1396  1 sg2.setEndRes(7);
1397  1 a.addGroup(sg2);
1398   
1399  1 assertNull(a.findGroup(seq1, 2)); // unchanged
1400  1 assertSame(a.findGroup(seq1, 3), sg1); // unchanged
1401    /*
1402    * if a residue is in more than one group, method returns
1403    * the first found (in order groups were added)
1404    */
1405  1 assertSame(a.findGroup(seq1, 4), sg1);
1406  1 assertSame(a.findGroup(seq1, 5), sg1);
1407   
1408    /*
1409    * seq2 only belongs to the second group
1410    */
1411  1 assertSame(a.findGroup(seq2, 4), sg2);
1412  1 assertSame(a.findGroup(seq2, 5), sg2);
1413  1 assertSame(a.findGroup(seq2, 6), sg2);
1414  1 assertSame(a.findGroup(seq2, 7), sg2);
1415  1 assertNull(a.findGroup(seq2, 3));
1416  1 assertNull(a.findGroup(seq2, 8));
1417    }
1418   
 
1419  1 toggle @Test(groups = { "Functional" })
1420    public void testDeleteSequenceByIndex()
1421    {
1422    // create random alignment
1423  1 AlignmentGenerator gen = new AlignmentGenerator(false);
1424  1 AlignmentI a = gen.generate(20, 15, 123, 5, 5);
1425   
1426    // delete sequence 10, alignment reduced by 1
1427  1 int height = a.getAbsoluteHeight();
1428  1 a.deleteSequence(10);
1429  1 assertEquals(a.getAbsoluteHeight(), height - 1);
1430   
1431    // try to delete -ve index, nothing happens
1432  1 a.deleteSequence(-1);
1433  1 assertEquals(a.getAbsoluteHeight(), height - 1);
1434   
1435    // try to delete beyond end of alignment, nothing happens
1436  1 a.deleteSequence(14);
1437  1 assertEquals(a.getAbsoluteHeight(), height - 1);
1438    }
1439   
 
1440  1 toggle @Test(groups = { "Functional" })
1441    public void testDeleteSequenceBySeq()
1442    {
1443    // create random alignment
1444  1 AlignmentGenerator gen = new AlignmentGenerator(false);
1445  1 AlignmentI a = gen.generate(20, 15, 123, 5, 5);
1446   
1447    // delete sequence 10, alignment reduced by 1
1448  1 int height = a.getAbsoluteHeight();
1449  1 SequenceI seq = a.getSequenceAt(10);
1450  1 a.deleteSequence(seq);
1451  1 assertEquals(a.getAbsoluteHeight(), height - 1);
1452   
1453    // try to delete non-existent sequence, nothing happens
1454  1 seq = new Sequence("cds", "GCCTCGGAT");
1455  1 assertEquals(a.getAbsoluteHeight(), height - 1);
1456    }
1457   
 
1458  1 toggle @Test(groups = { "Functional" })
1459    public void testDeleteHiddenSequence()
1460    {
1461    // create random alignment
1462  1 AlignmentGenerator gen = new AlignmentGenerator(false);
1463  1 AlignmentI a = gen.generate(20, 15, 123, 5, 5);
1464   
1465    // delete a sequence which is hidden, check it is NOT removed from hidden
1466    // sequences
1467  1 int height = a.getAbsoluteHeight();
1468  1 SequenceI seq = a.getSequenceAt(2);
1469  1 a.getHiddenSequences().hideSequence(seq);
1470  1 assertEquals(a.getHiddenSequences().getSize(), 1);
1471  1 a.deleteSequence(2);
1472  1 assertEquals(a.getAbsoluteHeight(), height - 1);
1473  1 assertEquals(a.getHiddenSequences().getSize(), 1);
1474   
1475    // delete a sequence which is not hidden, check hiddenSequences are not
1476    // affected
1477  1 a.deleteSequence(10);
1478  1 assertEquals(a.getAbsoluteHeight(), height - 2);
1479  1 assertEquals(a.getHiddenSequences().getSize(), 1);
1480    }
1481   
 
1482  1 toggle @Test(
1483    groups = "Functional",
1484    expectedExceptions =
1485    { IllegalArgumentException.class })
1486    public void testSetDataset_selfReference()
1487    {
1488  1 SequenceI seq = new Sequence("a", "a");
1489  1 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1490  1 alignment.setDataset(alignment);
1491    }
1492   
 
1493  1 toggle @Test(groups = "Functional")
1494    public void testAppend()
1495    {
1496  1 SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
1497  1 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1498  1 alignment.setGapCharacter('-');
1499  1 SequenceI seq2 = new Sequence("seq1", "KP..L.FQII.");
1500  1 AlignmentI alignment2 = new Alignment(new SequenceI[] { seq2 });
1501  1 alignment2.setGapCharacter('.');
1502   
1503  1 alignment.append(alignment2);
1504   
1505  1 assertEquals('-', alignment.getGapCharacter());
1506  1 assertSame(seq, alignment.getSequenceAt(0));
1507  1 assertEquals("KP--L-FQII-",
1508    alignment.getSequenceAt(1).getSequenceAsString());
1509   
1510    // todo test coverage for annotations, mappings, groups,
1511    // hidden sequences, properties
1512    }
1513   
1514    /**
1515    * test that calcId == null on findOrCreate doesn't raise an NPE, and yields
1516    * an annotation with a null calcId
1517    *
1518    */
 
1519  1 toggle @Test(groups = "Functional")
1520    public void testFindOrCreateForNullCalcId()
1521    {
1522  1 SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
1523  1 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1524   
1525  1 AlignmentAnnotation ala = alignment.findOrCreateAnnotation(
1526    "Temperature Factor", null, false, seq, null);
1527  1 assertNotNull(ala);
1528  1 assertEquals(seq, ala.sequenceRef);
1529  1 assertEquals("", ala.getCalcId());
1530    }
1531   
 
1532  1 toggle @Test(groups = {"Functional"})
1533    public void testUpdateFromOrAddAnnotation()
1534    {
1535  1 SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
1536  1 AlignmentI alignment = new Alignment(new SequenceI[] { seq });
1537   
1538  1 AlignmentAnnotation ala = alignment.findOrCreateAnnotation(
1539    "Temperature Factor", null, false, seq, null);
1540   
1541  1 assertNotNull(ala);
1542  1 assertEquals(seq, ala.sequenceRef);
1543  1 assertEquals("", ala.getCalcId());
1544   
1545    // Assuming findOrCreateForNullCalcId passed then this should work
1546   
1547  1 assertTrue(ala == alignment.updateFromOrCopyAnnotation(ala));
1548  1 AlignmentAnnotation updatedAla = new AlignmentAnnotation(ala);
1549  1 updatedAla.description = "updated Description";
1550  1 Assert.assertTrue(
1551    ala == alignment.updateFromOrCopyAnnotation(updatedAla));
1552  1 Assert.assertEquals(ala.toString(), updatedAla.toString());
1553  1 updatedAla.calcId = "newCalcId";
1554  1 AlignmentAnnotation newUpdatedAla = alignment
1555    .updateFromOrCopyAnnotation(updatedAla);
1556  1 Assert.assertTrue(updatedAla != newUpdatedAla);
1557  1 Assert.assertEquals(updatedAla.toString(), newUpdatedAla.toString());
1558    }
1559   
 
1560  1 toggle @Test(groups = "Functional")
1561    public void testPropagateInsertions()
1562    {
1563    // create an alignment with no gaps - this will be the profile seq and other
1564    // JPRED seqs
1565  1 AlignmentGenerator gen = new AlignmentGenerator(false);
1566  1 AlignmentI al = gen.generate(25, 10, 1234, 0, 0);
1567   
1568    // get the profileseq
1569  1 SequenceI profileseq = al.getSequenceAt(0);
1570  1 SequenceI gappedseq = new Sequence(profileseq);
1571  1 gappedseq.insertCharAt(5, al.getGapCharacter());
1572  1 gappedseq.insertCharAt(6, al.getGapCharacter());
1573  1 gappedseq.insertCharAt(7, al.getGapCharacter());
1574  1 gappedseq.insertCharAt(8, al.getGapCharacter());
1575   
1576    // force different kinds of padding
1577  1 al.getSequenceAt(3).deleteChars(2, 23);
1578  1 al.getSequenceAt(4).deleteChars(2, 27);
1579  1 al.getSequenceAt(5).deleteChars(10, 27);
1580   
1581    // create an alignment view with the gapped sequence
1582  1 SequenceI[] seqs = new SequenceI[1];
1583  1 seqs[0] = gappedseq;
1584  1 AlignmentI newal = new Alignment(seqs);
1585  1 HiddenColumns hidden = new HiddenColumns();
1586  1 hidden.hideColumns(15, 17);
1587   
1588  1 AlignmentView view = new AlignmentView(newal, hidden, null, true, false,
1589    false);
1590   
1591    // confirm that original contigs are as expected
1592  1 Iterator<int[]> visible = hidden.getVisContigsIterator(0, 25, false);
1593  1 int[] region = visible.next();
1594  1 assertEquals("[0, 14]", Arrays.toString(region));
1595  1 region = visible.next();
1596  1 assertEquals("[18, 24]", Arrays.toString(region));
1597   
1598    // propagate insertions
1599  1 HiddenColumns result = al.propagateInsertions(profileseq, view);
1600   
1601    // confirm that the contigs have changed to account for the gaps
1602  1 visible = result.getVisContigsIterator(0, 25, false);
1603  1 region = visible.next();
1604  1 assertEquals("[0, 10]", Arrays.toString(region));
1605  1 region = visible.next();
1606  1 assertEquals("[14, 24]", Arrays.toString(region));
1607   
1608    // confirm the alignment has been changed so that the other sequences have
1609    // gaps inserted where the columns are hidden
1610  1 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[10]));
1611  1 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[11]));
1612  1 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[12]));
1613  1 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[13]));
1614  1 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[14]));
1615   
1616    }
1617   
 
1618  1 toggle @Test(groups = "Functional")
1619    public void testPropagateInsertionsOverlap()
1620    {
1621    // test propagateInsertions where gaps and hiddenColumns overlap
1622   
1623    // create an alignment with no gaps - this will be the profile seq and other
1624    // JPRED seqs
1625  1 AlignmentGenerator gen = new AlignmentGenerator(false);
1626  1 AlignmentI al = gen.generate(20, 10, 1234, 0, 0);
1627   
1628    // get the profileseq
1629  1 SequenceI profileseq = al.getSequenceAt(0);
1630  1 SequenceI gappedseq = new Sequence(profileseq);
1631  1 gappedseq.insertCharAt(5, al.getGapCharacter());
1632  1 gappedseq.insertCharAt(6, al.getGapCharacter());
1633  1 gappedseq.insertCharAt(7, al.getGapCharacter());
1634  1 gappedseq.insertCharAt(8, al.getGapCharacter());
1635   
1636    // create an alignment view with the gapped sequence
1637  1 SequenceI[] seqs = new SequenceI[1];
1638  1 seqs[0] = gappedseq;
1639  1 AlignmentI newal = new Alignment(seqs);
1640   
1641    // hide columns so that some overlap with the gaps
1642  1 HiddenColumns hidden = new HiddenColumns();
1643  1 hidden.hideColumns(7, 10);
1644   
1645  1 AlignmentView view = new AlignmentView(newal, hidden, null, true, false,
1646    false);
1647   
1648    // confirm that original contigs are as expected
1649  1 Iterator<int[]> visible = hidden.getVisContigsIterator(0, 20, false);
1650  1 int[] region = visible.next();
1651  1 assertEquals("[0, 6]", Arrays.toString(region));
1652  1 region = visible.next();
1653  1 assertEquals("[11, 19]", Arrays.toString(region));
1654  1 assertFalse(visible.hasNext());
1655   
1656    // propagate insertions
1657  1 HiddenColumns result = al.propagateInsertions(profileseq, view);
1658   
1659    // confirm that the contigs have changed to account for the gaps
1660  1 visible = result.getVisContigsIterator(0, 20, false);
1661  1 region = visible.next();
1662  1 assertEquals("[0, 4]", Arrays.toString(region));
1663  1 region = visible.next();
1664  1 assertEquals("[7, 19]", Arrays.toString(region));
1665  1 assertFalse(visible.hasNext());
1666   
1667    // confirm the alignment has been changed so that the other sequences have
1668    // gaps inserted where the columns are hidden
1669  1 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[4]));
1670  1 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[5]));
1671  1 assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[6]));
1672  1 assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[7]));
1673    }
1674   
 
1675  1 toggle @Test(groups = { "Functional" })
1676    public void testPadGaps()
1677    {
1678  1 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1679  1 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1680  1 SequenceI seq3 = new Sequence("seq2", "-PQR");
1681  1 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1682  1 a.setGapCharacter('.'); // this replaces existing gaps
1683  1 assertEquals("ABCDEF..", seq1.getSequenceAsString());
1684  1 a.padGaps();
1685    // trailing gaps are pruned, short sequences padded with gap character
1686  1 assertEquals("ABCDEF.", seq1.getSequenceAsString());
1687  1 assertEquals(".JKLMNO", seq2.getSequenceAsString());
1688  1 assertEquals(".PQR...", seq3.getSequenceAsString());
1689    }
1690   
1691    /**
1692    * Test for setHiddenColumns, to check it returns true if the hidden columns
1693    * have changed, else false
1694    */
 
1695  1 toggle @Test(groups = { "Functional" })
1696    public void testSetHiddenColumns()
1697    {
1698  1 AlignmentI al = new Alignment(new SequenceI[] {});
1699  1 assertFalse(al.getHiddenColumns().hasHiddenColumns());
1700   
1701  1 HiddenColumns hc = new HiddenColumns();
1702  1 assertFalse(al.setHiddenColumns(hc)); // no change
1703  1 assertSame(hc, al.getHiddenColumns());
1704   
1705  1 hc.hideColumns(2, 4);
1706  1 assertTrue(al.getHiddenColumns().hasHiddenColumns());
1707   
1708    /*
1709    * set a different object but with the same columns hidden
1710    */
1711  1 HiddenColumns hc2 = new HiddenColumns();
1712  1 hc2.hideColumns(2, 4);
1713  1 assertFalse(al.setHiddenColumns(hc2)); // no change
1714  1 assertSame(hc2, al.getHiddenColumns());
1715   
1716  1 assertTrue(al.setHiddenColumns(null));
1717  1 assertNull(al.getHiddenColumns());
1718  1 assertTrue(al.setHiddenColumns(hc));
1719  1 assertSame(hc, al.getHiddenColumns());
1720   
1721  1 al.getHiddenColumns().hideColumns(10, 12);
1722  1 hc2.hideColumns(10, 12);
1723  1 assertFalse(al.setHiddenColumns(hc2)); // no change
1724   
1725    /*
1726    * hide columns 15-16 then 17-18 in hc
1727    * hide columns 15-18 in hc2
1728    * these are not now 'equal' objects even though they
1729    * represent the same set of columns
1730    */
1731  1 assertSame(hc2, al.getHiddenColumns());
1732  1 hc.hideColumns(15, 16);
1733  1 hc.hideColumns(17, 18);
1734  1 hc2.hideColumns(15, 18);
1735  1 assertFalse(hc.equals(hc2));
1736  1 assertTrue(al.setHiddenColumns(hc)); // 'changed'
1737    }
1738   
 
1739  1 toggle @Test(groups = { "Functional" })
1740    public void testGetWidth()
1741    {
1742  1 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1743  1 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1744  1 SequenceI seq3 = new Sequence("seq2", "-PQR");
1745  1 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1746   
1747  1 assertEquals(9, a.getWidth());
1748   
1749    // width includes hidden columns
1750  1 a.getHiddenColumns().hideColumns(2, 5);
1751  1 assertEquals(9, a.getWidth());
1752    }
1753   
 
1754  1 toggle @Test(groups = { "Functional" })
1755    public void testGetVisibleWidth()
1756    {
1757  1 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1758  1 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1759  1 SequenceI seq3 = new Sequence("seq2", "-PQR");
1760  1 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1761   
1762  1 assertEquals(9, a.getVisibleWidth());
1763   
1764    // width excludes hidden columns
1765  1 a.getHiddenColumns().hideColumns(2, 5);
1766  1 assertEquals(5, a.getVisibleWidth());
1767    }
1768   
 
1769  1 toggle @Test(groups = { "Functional" })
1770    public void testGetContactMap()
1771    {
1772    // TODO
1773    // 1. test adding/removing/manipulating contact maps with/without associated
1774    // sequence(s) or groups
1775    // 2. For sequence associated - ensure that inserting a gap in sequence
1776    // results in the contact map being relocated accordingly
1777    // 3. RENDERER QUESTION - should contact maps reflect gaps in the alignment
1778    // ?
1779   
1780    }
1781   
 
1782  1 toggle @Test(groups = { "Functional" })
1783    public void testEquals()
1784    {
1785  1 SequenceI seq1 = new Sequence("seq1", "ABCDEF--");
1786  1 SequenceI seq2 = new Sequence("seq2", "-JKLMNO--");
1787  1 SequenceI seq3 = new Sequence("seq2", "-PQR");
1788  1 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
1789  1 a.setDataset(null);
1790  1 assertEquals(a.getDataset(), a.getDataset());
1791    }
1792    }