+ public void testAddReferenceAnnotations()
+ {
+ SequenceI longseq = new Sequence("longA", "ASDASDASDASDAASDASDASDASDA");
+ Annotation[] aa = new Annotation[longseq.getLength()];
+
+ for (int p = 0; p < aa.length; p++)
+ {
+ aa[p] = new Annotation("P", "pos " + (p + 1), (char) 0,
+ (float) p + 1);
+ }
+ AlignmentAnnotation refAnnot = new AlignmentAnnotation("LongSeqAnnot",
+ "Annotations", aa);
+ refAnnot.setCalcId("Test");
+ longseq.addAlignmentAnnotation(refAnnot);
+ verifyExpectedSequenceAnnotation(refAnnot);
+
+ Alignment ourAl = new Alignment(
+ new SequenceI[]
+ { longseq.getSubSequence(5, 10),
+ longseq.getSubSequence(7, 12) });
+ ourAl.createDatasetAlignment();
+
+ // transfer annotation
+ SortedMap<String, String> tipEntries = new TreeMap<>();
+ Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<>();
+
+ AlignmentUtils.findAddableReferenceAnnotations(ourAl.getSequences(),
+ tipEntries, candidates, ourAl);
+ AlignmentUtils.addReferenceAnnotations(candidates, ourAl, null);
+
+ assertNotNull(ourAl.getAlignmentAnnotation());
+ assertEquals(ourAl.getAlignmentAnnotation().length, 2);
+
+ for (AlignmentAnnotation alan : ourAl.getAlignmentAnnotation())
+ {
+ verifyExpectedSequenceAnnotation(alan);
+ }
+ // Everything above works for 2.11.3 and 2.11.2.x.
+ // now simulate copy/paste to new alignment
+ SequenceI[] newSeqAl = new SequenceI[2];
+ // copy sequences but no annotation
+ newSeqAl[0] = new Sequence(ourAl.getSequenceAt(0),
+ ourAl.getSequenceAt(0).getAnnotation());
+ newSeqAl[1] = new Sequence(ourAl.getSequenceAt(1),
+ ourAl.getSequenceAt(1).getAnnotation());
+
+ Alignment newAl = new Alignment(newSeqAl);
+ // delete annotation
+ for (SequenceI sq : newAl.getSequences())
+ {
+ sq.setAlignmentAnnotation(new AlignmentAnnotation[0]);
+ }
+ // JAL-4182 scenario test
+ SequenceGroup sg = new SequenceGroup(Arrays.asList(newSeqAl));
+ sg.setStartRes(0);
+ sg.setEndRes(newAl.getWidth());
+ AlignmentUtils.addReferenceAnnotationTo(newAl, newSeqAl[0],
+ newSeqAl[0].getDatasetSequence().getAnnotation()[0], sg);
+ AlignmentUtils.addReferenceAnnotationTo(newAl, newSeqAl[1],
+ newSeqAl[1].getDatasetSequence().getAnnotation()[0], sg);
+ for (AlignmentAnnotation alan : newAl.getAlignmentAnnotation())
+ {
+ verifyExpectedSequenceAnnotation(alan);
+ }
+ }
+
+ /**
+ * helper - tests annotation is mapped to position it was originally created
+ * for
+ *
+ * @param alan
+ */
+ private void verifyExpectedSequenceAnnotation(AlignmentAnnotation alan)
+ {
+ for (int c = 0; c < alan.annotations.length; c++)
+ {
+ Annotation a = alan.annotations[c];
+ if (a != null)
+ {
+ assertEquals("Misaligned annotation at " + c,
+ (float) alan.sequenceRef.findPosition(c), a.value);
+ }
+ else
+ {
+ assertTrue("Unexpected Null at position " + c,
+ c >= alan.sequenceRef.getLength()
+ || Comparison.isGap(alan.sequenceRef.getCharAt(c)));
+ }
+ }
+ }
+
+ @Test(groups = "Functional")