X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fanalysis%2FAlignmentUtilsTests.java;h=f017662081eda164e5a0605f6c28743c97d82ec5;hb=279c6495df1a0de231600a2d283098c7411c4730;hp=8b6f67dcfee65e602f2d0ad6b55eb18b6797616d;hpb=3158d6fc128da5a06e30446e8eabb4afa9c53cb6;p=jalview.git diff --git a/test/jalview/analysis/AlignmentUtilsTests.java b/test/jalview/analysis/AlignmentUtilsTests.java index 8b6f67d..f017662 100644 --- a/test/jalview/analysis/AlignmentUtilsTests.java +++ b/test/jalview/analysis/AlignmentUtilsTests.java @@ -20,6 +20,7 @@ */ package jalview.analysis; +import static org.testng.Assert.assertNotEquals; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNotNull; @@ -54,6 +55,7 @@ import jalview.datamodel.SearchResultsI; import jalview.datamodel.SeqDistanceContactMatrix; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.gui.JvOptionPane; import jalview.io.AppletFormatAdapter; @@ -62,6 +64,7 @@ import jalview.io.FileFormat; import jalview.io.FileFormatI; import jalview.io.FormatAdapter; import jalview.io.gff.SequenceOntologyI; +import jalview.util.Comparison; import jalview.util.MapList; import jalview.util.MappingUtils; @@ -2603,20 +2606,112 @@ public class AlignmentUtilsTests } @Test(groups = "Functional") + 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 tipEntries = new TreeMap<>(); + Map> 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") public void testAddReferenceContactMap() { SequenceI sq = new Sequence("a", "SSSQ"); ContactMatrixI cm = new SeqDistanceContactMatrix(4); AlignmentAnnotation cm_aan = sq.addContactList(cm); + cm_aan.description = cm_aan.description + " cm1"; SequenceI dssq = sq.createDatasetSequence(); - Alignment ds = new Alignment(new SequenceI[] { dssq }); // remove annotation on our non-dataset sequence sq.removeAlignmentAnnotation(sq.getAnnotation()[0]); // test transfer Alignment al = new Alignment(new SequenceI[] { sq }); SortedMap tipEntries = new TreeMap<>(); - final Map> candidates = new LinkedHashMap<>(); + Map> candidates = new LinkedHashMap<>(); AlignmentUtils.findAddableReferenceAnnotations(al.getSequences(), tipEntries, candidates, al); @@ -2626,10 +2721,34 @@ public class AlignmentUtilsTests && al.getAlignmentAnnotation().length == 1); AlignmentAnnotation alan = al.findAnnotations(sq, null, cm_aan.label) .iterator().next(); + ContactMatrixI t_cm = al.getContactMatrixFor(alan); + assertNotNull("No contact map for the transferred annotation row.", + t_cm); + assertTrue(t_cm instanceof SeqDistanceContactMatrix); + assertTrue(((SeqDistanceContactMatrix) t_cm).hasReferenceSeq()); + ContactListI cl = al.getContactListFor(alan, 1); assertNotNull( "No contact matrix recovered after reference annotation transfer", cl); + // semantics of sequence associated contact list is slightly tricky - column + // 3 in alignment should have data + cl = al.getContactListFor(alan, 3); + assertNotNull( + "Contact matrix should have data for last position in sequence", + cl); + + ContactMatrixI cm2 = new SeqDistanceContactMatrix(4); + dssq.addContactList(cm2); + tipEntries = new TreeMap<>(); + candidates = new LinkedHashMap<>(); + + AlignmentUtils.findAddableReferenceAnnotations(al.getSequences(), + tipEntries, candidates, al); + AlignmentUtils.addReferenceAnnotations(candidates, al, null); + assertTrue("Expected two contact map annotation transferred", + al.getAlignmentAnnotation() != null + && al.getAlignmentAnnotation().length == 2); } }