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