X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fdatamodel%2FAlignmentTest.java;h=7af77f54f79623c7fc35a89bc185601a2e54d173;hb=7d6bf4ac01167499c0f4e07de63edcdd72a6d595;hp=915d860017f613bee10c3af5119bc58e365dbf87;hpb=dedd44136e37982fc2c713ae229b5a97a9b0b639;p=jalview.git diff --git a/test/jalview/datamodel/AlignmentTest.java b/test/jalview/datamodel/AlignmentTest.java index 915d860..7af77f5 100644 --- a/test/jalview/datamodel/AlignmentTest.java +++ b/test/jalview/datamodel/AlignmentTest.java @@ -28,7 +28,10 @@ import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping; -import jalview.io.AppletFormatAdapter; +import jalview.gui.JvOptionPane; +import jalview.io.DataSourceType; +import jalview.io.FileFormat; +import jalview.io.FileFormatI; import jalview.io.FormatAdapter; import jalview.util.MapList; @@ -39,6 +42,7 @@ import java.util.Iterator; import java.util.List; import org.testng.Assert; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -50,6 +54,14 @@ import org.testng.annotations.Test; */ public class AlignmentTest { + + @BeforeClass(alwaysRun = true) + public void setUpJvOptionPane() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + // @formatter:off private static final String TEST_DATA = "# STOCKHOLM 1.0\n" + @@ -94,11 +106,11 @@ public class AlignmentTest * @return * @throws IOException */ - protected AlignmentI loadAlignment(final String data, String format) + protected AlignmentI loadAlignment(final String data, FileFormatI format) throws IOException { - AlignmentI a = new FormatAdapter().readFile(data, - AppletFormatAdapter.PASTE, format); + AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE, + format); a.setDataset(null); return a; } @@ -134,19 +146,23 @@ public class AlignmentTest * - the alignmentI object to verify (either alignment or dataset) * @param raiseAssert * - when set, testng assertions are raised. - * @param message - * - null or a string message to prepend to the assert failed messages. + * @param message + * - null or a string message to prepend to the assert failed + * messages. * @return true if alignment references were in order, otherwise false. */ public static boolean verifyAlignmentDatasetRefs(AlignmentI alignment, boolean raiseAssert, String message) { - if (message==null) { message = ""; } + if (message == null) + { + message = ""; + } if (alignment == null) { if (raiseAssert) { - Assert.fail(message+"Alignment for verification was null."); + Assert.fail(message + "Alignment for verification was null."); } return false; } @@ -161,7 +177,8 @@ public class AlignmentTest { if (raiseAssert) { - Assert.fail(message+" Alignment contained a sequence who's dataset sequence has a second dataset reference."); + Assert.fail(message + + " Alignment contained a sequence who's dataset sequence has a second dataset reference."); } return false; } @@ -169,23 +186,40 @@ public class AlignmentTest { if (raiseAssert) { - Assert.fail(message+" Alignment contained a sequence who's dataset sequence was not in the dataset."); + Assert.fail(message + + " Alignment contained a sequence who's dataset sequence was not in the dataset."); } return false; } } - return verifyAlignmentDatasetRefs(alignment.getDataset(), raiseAssert, message); + return verifyAlignmentDatasetRefs(alignment.getDataset(), + raiseAssert, message); } else { + int dsp = -1; // verify all dataset sequences for (SequenceI seqds : alignment.getSequences()) { + dsp++; if (seqds.getDatasetSequence() != null) { if (raiseAssert) { - Assert.fail(message+" Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)"); + Assert.fail(message + + " Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)"); + } + return false; + } + int foundp = alignment.findIndex(seqds); + if (foundp != dsp) + { + if (raiseAssert) + { + Assert.fail(message + + " Dataset sequence array contains a reference at " + + dsp + " to a sequence first seen at " + foundp + " (" + + seqds.toString() + ")"); } return false; } @@ -202,7 +236,8 @@ public class AlignmentTest { if (raiseAssert) { - Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence"); + Assert.fail(message + + " DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence"); } return false; @@ -211,7 +246,8 @@ public class AlignmentTest { if (raiseAssert) { - Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence not in dataset"); + Assert.fail(message + + " DBRefEntry for sequence in alignment had map to sequence not in dataset"); } return false; } @@ -231,7 +267,8 @@ public class AlignmentTest { if (raiseAssert) { - Assert.fail(message+" CodonFrame-SSM-FromSeq is not a dataset sequence"); + Assert.fail(message + + " CodonFrame-SSM-FromSeq is not a dataset sequence"); } return false; } @@ -240,7 +277,8 @@ public class AlignmentTest if (raiseAssert) { - Assert.fail(message+" CodonFrame-SSM-FromSeq is not contained in dataset"); + Assert.fail(message + + " CodonFrame-SSM-FromSeq is not contained in dataset"); } return false; } @@ -248,7 +286,8 @@ public class AlignmentTest { if (raiseAssert) { - Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence"); + Assert.fail(message + + " CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence"); } return false; } @@ -257,7 +296,8 @@ public class AlignmentTest if (raiseAssert) { - Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not contained in dataset"); + Assert.fail(message + + " CodonFrame-SSM-Mapping-ToSeq is not contained in dataset"); } return false; } @@ -296,26 +336,32 @@ public class AlignmentTest } // also check validation passes with asserts disabled Assert.assertTrue(verifyAlignmentDatasetRefs(al, false, null), - "Valid test alignment failed when raiseAsserts disabled:" + "Valid test alignment tested false when raiseAsserts disabled:" + msg); } else { + boolean assertRaised = false; try { - Assert.assertFalse(verifyAlignmentDatasetRefs(al, true, null)); - Assert.fail("Invalid test alignment passed but no assertion raised when raiseAsserts enabled:" - + msg); + verifyAlignmentDatasetRefs(al, true, null); } catch (AssertionError ae) { // expected behaviour + assertRaised = true; + } + if (!assertRaised) + { + Assert.fail("Invalid test alignment passed when raiseAsserts enabled:" + + msg); } // also check validation passes with asserts disabled Assert.assertFalse(verifyAlignmentDatasetRefs(al, false, null), - "Invalid test alignment passed when raiseAsserts disabled:" + "Invalid test alignment tested true when raiseAsserts disabled:" + msg); } } + @Test(groups = { "Functional" }) public void testVerifyAlignmentDatasetRefs() { @@ -323,16 +369,13 @@ public class AlignmentTest "TTTTTT"); // construct simple valid alignment dataset - Alignment al = new Alignment(new SequenceI[] { - sq1, sq2 }); + Alignment al = new Alignment(new SequenceI[] { sq1, sq2 }); // expect this to pass assertVerifyAlignment(al, true, "Simple valid alignment didn't verify"); // check test for sequence->datasetSequence validity sq1.setDatasetSequence(sq2); - assertVerifyAlignment( - al, - false, + assertVerifyAlignment(al, false, "didn't detect dataset sequence with a dataset sequence reference."); sq1.setDatasetSequence(null); @@ -400,8 +443,145 @@ public class AlignmentTest al.getDataset().addSequence(sqanotherout); assertVerifyAlignment(al, false, "verify should have failed when a sequence was added twice to the dataset"); + al.getDataset().deleteSequence(sqanotherout); + assertVerifyAlignment(al, true, + "verify should have passed after duplicate entry for sequence was removed"); + } + /** + * checks that the sequence data for an alignment's dataset is non-redundant. + * Fails if there are sequences with same id, sequence, start, and. + */ + + public static void assertDatasetIsNormalised(AlignmentI al) + { + assertDatasetIsNormalised(al, null); + } + + /** + * checks that the sequence data for an alignment's dataset is non-redundant. + * Fails if there are sequences with same id, sequence, start, and. + * + * @param al + * - alignment to verify + * @param message + * - null or message prepended to exception message. + */ + public static void assertDatasetIsNormalised(AlignmentI al, String message) + { + if (al.getDataset() != null) + { + assertDatasetIsNormalised(al.getDataset(), message); + return; + } + /* + * look for pairs of sequences with same ID, start, end, and sequence + */ + List seqSet = al.getSequences(); + for (int p = 0; p < seqSet.size(); p++) + { + SequenceI pSeq = seqSet.get(p); + for (int q = p + 1; q < seqSet.size(); q++) + { + SequenceI qSeq = seqSet.get(q); + if (pSeq.getStart() != qSeq.getStart()) + { + continue; + } + if (pSeq.getEnd() != qSeq.getEnd()) + { + continue; + } + if (!pSeq.getName().equals(qSeq.getName())) + { + continue; + } + if (!Arrays.equals(pSeq.getSequence(), qSeq.getSequence())) + { + continue; + } + Assert.fail((message == null ? "" : message + " :") + + "Found similar sequences at position " + p + " and " + q + + "\n" + pSeq.toString()); + } + } } + + @Test(groups = { "Functional", "Asserts" }) + public void testAssertDatasetIsNormalised() + { + Sequence sq1 = new Sequence("s1/1-4", "asdf"); + Sequence sq1shift = new Sequence("s1/2-5", "asdf"); + Sequence sq1seqd = new Sequence("s1/1-4", "asdt"); + Sequence sq2 = new Sequence("s2/1-4", "asdf"); + Sequence sq1dup = new Sequence("s1/1-4", "asdf"); + + Alignment al = new Alignment(new SequenceI[] { sq1 }); + al.setDataset(null); + + try + { + assertDatasetIsNormalised(al); + } catch (AssertionError ae) + { + Assert.fail("Single sequence should be valid normalised dataset."); + } + al.addSequence(sq2); + try + { + assertDatasetIsNormalised(al); + } catch (AssertionError ae) + { + Assert.fail("Two different sequences should be valid normalised dataset."); + } + /* + * now change sq2's name in the alignment. should still be valid + */ + al.findName(sq2.getName()).setName("sq1"); + try + { + assertDatasetIsNormalised(al); + } catch (AssertionError ae) + { + Assert.fail("Two different sequences in dataset, but same name in alignment, should be valid normalised dataset."); + } + + al.addSequence(sq1seqd); + try + { + assertDatasetIsNormalised(al); + } catch (AssertionError ae) + { + Assert.fail("sq1 and sq1 with different sequence should be distinct."); + } + + al.addSequence(sq1shift); + try + { + assertDatasetIsNormalised(al); + } catch (AssertionError ae) + { + Assert.fail("sq1 and sq1 with different start/end should be distinct."); + } + /* + * finally, the failure case + */ + al.addSequence(sq1dup); + boolean ssertRaised = false; + try + { + assertDatasetIsNormalised(al); + + } catch (AssertionError ae) + { + ssertRaised = true; + } + if (!ssertRaised) + { + Assert.fail("Expected identical sequence to raise exception."); + } + } + /* * Read in Stockholm format test data including secondary structure * annotations. @@ -409,7 +589,7 @@ public class AlignmentTest @BeforeMethod(alwaysRun = true) public void setUp() throws IOException { - al = loadAlignment(TEST_DATA, "STH"); + al = loadAlignment(TEST_DATA, FileFormat.Stockholm); int i = 0; for (AlignmentAnnotation ann : al.getAlignmentAnnotation()) { @@ -431,6 +611,12 @@ public class AlignmentTest AlignmentAnnotation ann = iter.next(); assertEquals("D.melanogaster.2", ann.sequenceRef.getName()); assertFalse(iter.hasNext()); + + // invalid id + anns = al.findAnnotation("CalcIdForD.melanogaster.?"); + assertFalse(iter.hasNext()); + anns = al.findAnnotation(null); + assertFalse(iter.hasNext()); } @Test(groups = { "Functional" }) @@ -475,9 +661,9 @@ public class AlignmentTest public void testAlignAs_dnaAsDna() throws IOException { // aligned cDNA: - AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA"); + AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta); // unaligned cDNA: - AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA"); + AlignmentI al2 = loadAlignment(CDNA_SEQS_2, FileFormat.Fasta); /* * Make mappings between sequences. The 'aligned cDNA' is playing the role @@ -501,8 +687,8 @@ public class AlignmentTest public void testAlignAs_proteinAsCdna() throws IOException { // see also AlignmentUtilsTests - AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA"); - AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA"); + AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta); + AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta); makeMappings(al1, al2); // Fudge - alignProteinAsCdna expects mappings to be on protein @@ -525,8 +711,8 @@ public class AlignmentTest /* * Load alignments and add mappings for cDNA to protein */ - AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA"); - AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA"); + AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta); + AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta); makeMappings(al1, al2); /* @@ -582,8 +768,8 @@ public class AlignmentTest * Load alignments and add mappings from nucleotide to protein (or from * first to second if both the same type) */ - AlignmentI al1 = loadAlignment(fromSeqs, "FASTA"); - AlignmentI al2 = loadAlignment(toSeqs, "FASTA"); + AlignmentI al1 = loadAlignment(fromSeqs, FileFormat.Fasta); + AlignmentI al2 = loadAlignment(toSeqs, FileFormat.Fasta); makeMappings(al1, al2); /* @@ -640,9 +826,9 @@ public class AlignmentTest String dna1 = "A-Aa-gG-GCC-cT-TT"; String dna2 = "c--CCGgg-TT--T-AA-A"; AlignmentI al1 = loadAlignment(">Dna1/6-17\n" + dna1 - + "\n>Dna2/20-31\n" + dna2 + "\n", "FASTA"); + + "\n>Dna2/20-31\n" + dna2 + "\n", FileFormat.Fasta); AlignmentI al2 = loadAlignment( - ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", "FASTA"); + ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", FileFormat.Fasta); AlignedCodonFrame acf = new AlignedCodonFrame(); // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA @@ -682,7 +868,7 @@ public class AlignmentTest @Test(groups = "Functional") public void testCopyConstructor() throws IOException { - AlignmentI protein = loadAlignment(AA_SEQS_1, FormatAdapter.PASTE); + AlignmentI protein = loadAlignment(AA_SEQS_1, FileFormat.Fasta); // create sequence and alignment datasets protein.setDataset(null); AlignedCodonFrame acf = new AlignedCodonFrame(); @@ -719,7 +905,7 @@ public class AlignmentTest public void testCreateDatasetAlignment() throws IOException { AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1, - AppletFormatAdapter.PASTE, "FASTA"); + DataSourceType.PASTE, FileFormat.Fasta); /* * create a dataset sequence on first sequence * leave the second without one @@ -838,6 +1024,28 @@ public class AlignmentTest } @Test(groups = "Functional") + public void testAddSequencePreserveDatasetIntegrity() + { + Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + Alignment align = new Alignment(new SequenceI[] { seq }); + align.createDatasetAlignment(); + AlignmentI ds = align.getDataset(); + SequenceI copy = new Sequence(seq); + copy.insertCharAt(3, 5, '-'); + align.addSequence(copy); + Assert.assertEquals(align.getDataset().getHeight(), 1, + "Dataset shouldn't have more than one sequence."); + + Sequence seq2 = new Sequence("newtestSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + align.addSequence(seq2); + Assert.assertEquals(align.getDataset().getHeight(), 2, + "Dataset should now have two sequences."); + + assertAlignmentDatasetRefs(align, + "addSequence broke dataset reference integrity"); + } + + @Test(groups = "Functional") public void getVisibleStartAndEndIndexTest() { Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); @@ -865,4 +1073,127 @@ public class AlignmentTest assertEquals(1, startEnd[0]); assertEquals(23, startEnd[1]); } + + /** + * Tests that dbrefs with mappings to sequence get updated if the sequence + * acquires a dataset sequence + */ + @Test(groups = "Functional") + public void testCreateDataset_updateDbrefMappings() + { + SequenceI pep = new Sequence("pep", "ASD"); + SequenceI dna = new Sequence("dna", "aaaGCCTCGGATggg"); + SequenceI cds = new Sequence("cds", "GCCTCGGAT"); + + // add dbref from dna to peptide + DBRefEntry dbr = new DBRefEntry("UNIPROT", "", "pep"); + dbr.setMap(new Mapping(pep, new MapList(new int[] { 4, 15 }, new int[] { + 1, 4 }, 3, 1))); + dna.addDBRef(dbr); + + // add dbref from dna to peptide + DBRefEntry dbr2 = new DBRefEntry("UNIPROT", "", "pep"); + dbr2.setMap(new Mapping(pep, new MapList(new int[] { 1, 12 }, new int[] + { 1, 4 }, 3, 1))); + cds.addDBRef(dbr2); + + // add dbref from peptide to dna + DBRefEntry dbr3 = new DBRefEntry("EMBL", "", "dna"); + dbr3.setMap(new Mapping(dna, new MapList(new int[] { 1, 4 }, new int[] { + 4, 15 }, 1, 3))); + pep.addDBRef(dbr3); + + // add dbref from peptide to cds + DBRefEntry dbr4 = new DBRefEntry("EMBLCDS", "", "cds"); + dbr4.setMap(new Mapping(cds, new MapList(new int[] { 1, 4 }, new int[] { + 1, 12 }, 1, 3))); + pep.addDBRef(dbr4); + + AlignmentI protein = new Alignment(new SequenceI[] { pep }); + + /* + * create the alignment dataset + */ + ((Alignment) protein).createDatasetAlignment(); + + AlignmentI ds = protein.getDataset(); + + // should be 3 sequences in dataset + assertEquals(3, ds.getHeight()); + assertTrue(ds.getSequences().contains(pep.getDatasetSequence())); + assertTrue(ds.getSequences().contains(dna)); + assertTrue(ds.getSequences().contains(cds)); + + /* + * verify peptide.cdsdbref.peptidedbref is now mapped to peptide dataset + */ + DBRefEntry[] dbRefs = pep.getDBRefs(); + assertEquals(2, dbRefs.length); + assertSame(dna, dbRefs[0].map.to); + assertSame(cds, dbRefs[1].map.to); + assertEquals(1, dna.getDBRefs().length); + assertSame(pep.getDatasetSequence(), dna.getDBRefs()[0].map.to); + assertEquals(1, cds.getDBRefs().length); + assertSame(pep.getDatasetSequence(), cds.getDBRefs()[0].map.to); + } + + @Test(groups = { "Functional" }) + public void testFindGroup() + { + SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI"); + SequenceI seq2 = new Sequence("seq2", "---JKLMNO---"); + AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 }); + + assertNull(a.findGroup(null, 0)); + assertNull(a.findGroup(seq1, 1)); + assertNull(a.findGroup(seq1, -1)); + + /* + * add a group consisting of just "DEF" + */ + SequenceGroup sg1 = new SequenceGroup(); + sg1.addSequence(seq1, false); + sg1.setStartRes(3); + sg1.setEndRes(5); + a.addGroup(sg1); + + assertNull(a.findGroup(seq1, 2)); // position not in group + assertNull(a.findGroup(seq1, 6)); // position not in group + assertNull(a.findGroup(seq2, 5)); // sequence not in group + assertSame(a.findGroup(seq1, 3), sg1); // yes + assertSame(a.findGroup(seq1, 4), sg1); + assertSame(a.findGroup(seq1, 5), sg1); + + /* + * add a group consisting of + * EF-- + * KLMN + */ + SequenceGroup sg2 = new SequenceGroup(); + sg2.addSequence(seq1, false); + sg2.addSequence(seq2, false); + sg2.setStartRes(4); + sg2.setEndRes(7); + a.addGroup(sg2); + + assertNull(a.findGroup(seq1, 2)); // unchanged + assertSame(a.findGroup(seq1, 3), sg1); // unchanged + /* + * if a residue is in more than one group, method returns + * the first found (in order groups were added) + */ + assertSame(a.findGroup(seq1, 4), sg1); + assertSame(a.findGroup(seq1, 5), sg1); + + /* + * seq2 only belongs to the second group + */ + assertSame(a.findGroup(seq2, 4), sg2); + assertSame(a.findGroup(seq2, 5), sg2); + assertSame(a.findGroup(seq2, 6), sg2); + assertSame(a.findGroup(seq2, 7), sg2); + assertNull(a.findGroup(seq2, 3)); + assertNull(a.findGroup(seq2, 8)); + } + }