X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fdatamodel%2FAlignmentTest.java;h=7958e9bea5be0c232f8f28b162bf2f3ee351ef5f;hb=37de9310bec3501cbc6381e0c3dcb282fcaad812;hp=da73c50876a0da2b217dffb2aa88a955954783fe;hpb=838e4f91d4a53dd315640dbc9ff6ef7a815ee576;p=jalview.git diff --git a/test/jalview/datamodel/AlignmentTest.java b/test/jalview/datamodel/AlignmentTest.java index da73c50..7958e9b 100644 --- a/test/jalview/datamodel/AlignmentTest.java +++ b/test/jalview/datamodel/AlignmentTest.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1) - * Copyright (C) 2015 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -22,15 +22,23 @@ package jalview.datamodel; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNotNull; +import static org.testng.AssertJUnit.assertNull; +import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; +import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping; import jalview.io.AppletFormatAdapter; import jalview.io.FormatAdapter; import jalview.util.MapList; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; +import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -95,6 +103,473 @@ public class AlignmentTest return a; } + /** + * assert wrapper: tests all references in the given alignment are consistent + * + * @param alignment + */ + public static void assertAlignmentDatasetRefs(AlignmentI alignment) + { + verifyAlignmentDatasetRefs(alignment, true, null); + } + + /** + * assert wrapper: tests all references in the given alignment are consistent + * + * @param alignment + * @param message + * - prefixed to any assert failed messages + */ + public static void assertAlignmentDatasetRefs(AlignmentI alignment, + String message) + { + verifyAlignmentDatasetRefs(alignment, true, message); + } + + /** + * verify sequence and dataset references are properly contained within + * dataset + * + * @param alignment + * - 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. + * @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 (alignment == null) + { + if (raiseAssert) + { + Assert.fail(message + "Alignment for verification was null."); + } + return false; + } + if (alignment.getDataset() != null) + { + AlignmentI dataset = alignment.getDataset(); + // check all alignment sequences have their dataset within the dataset + for (SequenceI seq : alignment.getSequences()) + { + SequenceI seqds = seq.getDatasetSequence(); + if (seqds.getDatasetSequence() != null) + { + if (raiseAssert) + { + Assert.fail(message + + " Alignment contained a sequence who's dataset sequence has a second dataset reference."); + } + return false; + } + if (dataset.findIndex(seqds) == -1) + { + if (raiseAssert) + { + Assert.fail(message + + " Alignment contained a sequence who's dataset sequence was not in the dataset."); + } + return false; + } + } + 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!)"); + } + 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; + } + if (seqds.getDBRefs() != null) + { + for (DBRefEntry dbr : seqds.getDBRefs()) + { + if (dbr.getMap() != null) + { + SequenceI seqdbrmapto = dbr.getMap().getTo(); + if (seqdbrmapto != null) + { + if (seqdbrmapto.getDatasetSequence() != null) + { + if (raiseAssert) + { + Assert.fail(message + + " DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence"); + } + return false; + + } + if (alignment.findIndex(dbr.getMap().getTo()) == -1) + { + if (raiseAssert) + { + Assert.fail(message + + " DBRefEntry for sequence in alignment had map to sequence not in dataset"); + } + return false; + } + } + } + } + } + } + // finally, verify codonmappings involve only dataset sequences. + if (alignment.getCodonFrames() != null) + { + for (AlignedCodonFrame alc : alignment.getCodonFrames()) + { + for (SequenceToSequenceMapping ssm : alc.getMappings()) + { + if (ssm.getFromSeq().getDatasetSequence() != null) + { + if (raiseAssert) + { + Assert.fail(message + + " CodonFrame-SSM-FromSeq is not a dataset sequence"); + } + return false; + } + if (alignment.findIndex(ssm.getFromSeq()) == -1) + { + + if (raiseAssert) + { + Assert.fail(message + + " CodonFrame-SSM-FromSeq is not contained in dataset"); + } + return false; + } + if (ssm.getMapping().getTo().getDatasetSequence() != null) + { + if (raiseAssert) + { + Assert.fail(message + + " CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence"); + } + return false; + } + if (alignment.findIndex(ssm.getMapping().getTo()) == -1) + { + + if (raiseAssert) + { + Assert.fail(message + + " CodonFrame-SSM-Mapping-ToSeq is not contained in dataset"); + } + return false; + } + } + } + } + } + return true; // all relationships verified! + } + + /** + * call verifyAlignmentDatasetRefs with and without assertion raising enabled, + * to check expected pass/fail actually occurs in both conditions + * + * @param al + * @param expected + * @param msg + */ + private void assertVerifyAlignment(AlignmentI al, boolean expected, + String msg) + { + if (expected) + { + try + { + + Assert.assertTrue(verifyAlignmentDatasetRefs(al, true, null), + "Valid test alignment failed when raiseAsserts enabled:" + + msg); + } catch (AssertionError ae) + { + ae.printStackTrace(); + Assert.fail( + "Valid test alignment raised assertion errors when raiseAsserts enabled: " + + msg, ae); + } + // also check validation passes with asserts disabled + Assert.assertTrue(verifyAlignmentDatasetRefs(al, false, null), + "Valid test alignment tested false when raiseAsserts disabled:" + + msg); + } + else + { + boolean assertRaised = false; + try + { + 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 tested true when raiseAsserts disabled:" + + msg); + } + } + + @Test(groups = { "Functional" }) + public void testVerifyAlignmentDatasetRefs() + { + SequenceI sq1 = new Sequence("sq1", "ASFDD"), sq2 = new Sequence("sq2", + "TTTTTT"); + + // construct simple valid alignment dataset + 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, + "didn't detect dataset sequence with a dataset sequence reference."); + + sq1.setDatasetSequence(null); + assertVerifyAlignment( + al, + true, + "didn't reinstate validity after nulling dataset sequence dataset reference"); + + // now create dataset and check again + al.createDatasetAlignment(); + assertNotNull(al.getDataset()); + + assertVerifyAlignment(al, true, + "verify failed after createDatasetAlignment"); + + // create a dbref on sq1 with a sequence ref to sq2 + DBRefEntry dbrs1tos2 = new DBRefEntry("UNIPROT", "1", "Q111111"); + dbrs1tos2.setMap(new Mapping(sq2.getDatasetSequence(), + new int[] { 1, 5 }, new int[] { 2, 6 }, 1, 1)); + sq1.getDatasetSequence().addDBRef(dbrs1tos2); + assertVerifyAlignment(al, true, + "verify failed after addition of valid DBRefEntry/map"); + // now create a dbref on a new sequence which maps to another sequence + // outside of the dataset + SequenceI sqout = new Sequence("sqout", "ututututucagcagcag"), sqnew = new Sequence( + "sqnew", "EEERRR"); + DBRefEntry sqnewsqout = new DBRefEntry("ENAFOO", "1", "R000001"); + sqnewsqout.setMap(new Mapping(sqout, new int[] { 1, 6 }, new int[] { 1, + 18 }, 1, 3)); + al.getDataset().addSequence(sqnew); + + assertVerifyAlignment(al, true, + "verify failed after addition of new sequence to dataset"); + // now start checking exception conditions + sqnew.addDBRef(sqnewsqout); + assertVerifyAlignment( + al, + false, + "verify passed when a dbref with map to sequence outside of dataset was added"); + // make the verify pass by adding the outsider back in + al.getDataset().addSequence(sqout); + assertVerifyAlignment(al, true, + "verify should have passed after adding dbref->to sequence in to dataset"); + // and now the same for a codon mapping... + SequenceI sqanotherout = new Sequence("sqanotherout", + "aggtutaggcagcagcag"); + + AlignedCodonFrame alc = new AlignedCodonFrame(); + alc.addMap(sqanotherout, sqnew, new MapList(new int[] { 1, 6 }, + new int[] { 1, 18 }, 3, 1)); + + al.addCodonFrame(alc); + Assert.assertEquals(al.getDataset().getCodonFrames().size(), 1); + + assertVerifyAlignment( + al, + false, + "verify passed when alCodonFrame mapping to sequence outside of dataset was added"); + // make the verify pass by adding the outsider back in + al.getDataset().addSequence(sqanotherout); + assertVerifyAlignment( + al, + true, + "verify should have passed once all sequences involved in alCodonFrame were added to dataset"); + 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. @@ -176,12 +651,12 @@ public class AlignmentTest * Make mappings between sequences. The 'aligned cDNA' is playing the role * of what would normally be protein here. */ - makeMappings(al2, al1); + makeMappings(al1, al2); ((Alignment) al2).alignAs(al1, false, true); - assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0) + assertEquals("GC-TC--GUC-GTACT", al2.getSequenceAt(0) .getSequenceAsString()); - assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1) + assertEquals("-GG-GTC--AGG--CAGT", al2.getSequenceAt(1) .getSequenceAsString()); } @@ -198,38 +673,21 @@ public class AlignmentTest AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA"); makeMappings(al1, al2); + // Fudge - alignProteinAsCdna expects mappings to be on protein + al2.getCodonFrames().addAll(al1.getCodonFrames()); + ((Alignment) al2).alignAs(al1, false, true); assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString()); assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString()); } /** - * Aligning protein from cDNA for a single sequence. This is the 'simple' case - * (as there is no need to compute codon 'alignments') but worth testing - * before tackling the multiple sequence case. - * - * @throws IOException - */ - @Test(groups = { "Functional" }) - public void testAlignAs_proteinAsCdna_singleSequence() throws IOException - { - /* - * simplest case remove all gaps - */ - verifyAlignAs(">protein\n-Q-K-\n", ">dna\nCAAaaa\n", "QK"); - - /* - * with sequence offsets - */ - verifyAlignAs(">protein/12-13\n-Q-K-\n", ">dna/20-25\nCAAaaa\n", "QK"); - } - - /** * Test aligning cdna as per protein alignment. * * @throws IOException */ - @Test(groups = { "Functional" }) + @Test(groups = { "Functional" }, enabled = true) + // TODO review / update this test after redesign of alignAs method public void testAlignAs_cdnaAsProtein() throws IOException { /* @@ -243,9 +701,9 @@ public class AlignmentTest * Realign DNA; currently keeping existing gaps in introns only */ ((Alignment) al1).alignAs(al2, false, true); - assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0) + assertEquals("ACG---GCUCCA------ACT---", al1.getSequenceAt(0) .getSequenceAsString()); - assertEquals("---CGT---TAACGA---AGT", al1.getSequenceAt(1) + assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1) .getSequenceAsString()); } @@ -254,7 +712,8 @@ public class AlignmentTest * * @throws IOException */ - @Test(groups = { "Functional" }) + @Test(groups = { "Functional" }, enabled = true) + // TODO review / update this test after redesign of alignAs method public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException { /* @@ -303,32 +762,34 @@ public class AlignmentTest } /** - * Helper method to make mappings from protein to dna sequences, and add the - * mappings to the protein alignment + * Helper method to make mappings between sequences, and add the mappings to + * the 'mapped from' alignment * * @param alFrom * @param alTo */ public void makeMappings(AlignmentI alFrom, AlignmentI alTo) { - AlignmentI prot = !alFrom.isNucleotide() ? alFrom : alTo; - AlignmentI nuc = alFrom == prot ? alTo : alFrom; - int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3); AlignedCodonFrame acf = new AlignedCodonFrame(); - for (int i = 0; i < nuc.getHeight(); i++) + for (int i = 0; i < alFrom.getHeight(); i++) { - SequenceI seqFrom = nuc.getSequenceAt(i); - SequenceI seqTo = prot.getSequenceAt(i); + SequenceI seqFrom = alFrom.getSequenceAt(i); + SequenceI seqTo = alTo.getSequenceAt(i); MapList ml = new MapList(new int[] { seqFrom.getStart(), seqFrom.getEnd() }, new int[] { seqTo.getStart(), seqTo.getEnd() }, ratio, 1); acf.addMap(seqFrom, seqTo, ml); } - prot.addCodonFrame(acf); + /* + * not sure whether mappings 'belong' or protein or nucleotide + * alignment, so adding to both ;~) + */ + alFrom.addCodonFrame(acf); + alTo.addCodonFrame(acf); } /** @@ -337,7 +798,8 @@ public class AlignmentTest * * @throws IOException */ - @Test(groups = { "Functional" }) + @Test(groups = { "Functional" }, enabled = false) + // TODO review / update this test after redesign of alignAs method public void testAlignAs_dnaAsProtein_withIntrons() throws IOException { /* @@ -345,14 +807,13 @@ public class AlignmentTest */ String dna1 = "A-Aa-gG-GCC-cT-TT"; String dna2 = "c--CCGgg-TT--T-AA-A"; - AlignmentI al1 = loadAlignment(">Seq1/6-17\n" + dna1 - + "\n>Seq2/20-31\n" + dna2 + "\n", "FASTA"); + AlignmentI al1 = loadAlignment(">Dna1/6-17\n" + dna1 + + "\n>Dna2/20-31\n" + dna2 + "\n", "FASTA"); AlignmentI al2 = loadAlignment( - ">Seq1/7-9\n-P--YK\n>Seq2/11-13\nG-T--F\n", "FASTA"); + ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", "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 - // TODO sequence offsets MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 }, new int[] { 7, 9 }, 3, 1); acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1); @@ -385,4 +846,277 @@ public class AlignmentTest assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1) .getSequenceAsString()); } + + @Test(groups = "Functional") + public void testCopyConstructor() throws IOException + { + AlignmentI protein = loadAlignment(AA_SEQS_1, FormatAdapter.PASTE); + // create sequence and alignment datasets + protein.setDataset(null); + AlignedCodonFrame acf = new AlignedCodonFrame(); + List acfList = Arrays.asList(new AlignedCodonFrame[] + { acf }); + protein.getDataset().setCodonFrames(acfList); + AlignmentI copy = new Alignment(protein); + + /* + * copy has different aligned sequences but the same dataset sequences + */ + assertFalse(copy.getSequenceAt(0) == protein.getSequenceAt(0)); + assertFalse(copy.getSequenceAt(1) == protein.getSequenceAt(1)); + assertSame(copy.getSequenceAt(0).getDatasetSequence(), protein + .getSequenceAt(0).getDatasetSequence()); + assertSame(copy.getSequenceAt(1).getDatasetSequence(), protein + .getSequenceAt(1).getDatasetSequence()); + + // TODO should the copy constructor copy the dataset? + // or make a new one referring to the same dataset sequences?? + assertNull(copy.getDataset()); + // TODO test metadata is copied when AlignmentI is a dataset + + // assertArrayEquals(copy.getDataset().getSequencesArray(), protein + // .getDataset().getSequencesArray()); + } + + /** + * Test behaviour of createDataset + * + * @throws IOException + */ + @Test(groups = "Functional") + public void testCreateDatasetAlignment() throws IOException + { + AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1, + AppletFormatAdapter.PASTE, "FASTA"); + /* + * create a dataset sequence on first sequence + * leave the second without one + */ + protein.getSequenceAt(0).createDatasetSequence(); + assertNotNull(protein.getSequenceAt(0).getDatasetSequence()); + assertNull(protein.getSequenceAt(1).getDatasetSequence()); + + /* + * add a mapping to the alignment + */ + AlignedCodonFrame acf = new AlignedCodonFrame(); + protein.addCodonFrame(acf); + assertNull(protein.getDataset()); + assertTrue(protein.getCodonFrames().contains(acf)); + + /* + * create the alignment dataset + * note this creates sequence datasets where missing + * as a side-effect (in this case, on seq2 + */ + // TODO promote this method to AlignmentI + ((Alignment) protein).createDatasetAlignment(); + + AlignmentI ds = protein.getDataset(); + + // side-effect: dataset created on second sequence + assertNotNull(protein.getSequenceAt(1).getDatasetSequence()); + // dataset alignment has references to dataset sequences + assertEquals(ds.getSequenceAt(0), protein.getSequenceAt(0) + .getDatasetSequence()); + assertEquals(ds.getSequenceAt(1), protein.getSequenceAt(1) + .getDatasetSequence()); + + // codon frames should have been moved to the dataset + // getCodonFrames() should delegate to the dataset: + assertTrue(protein.getCodonFrames().contains(acf)); + // prove the codon frames are indeed on the dataset: + assertTrue(ds.getCodonFrames().contains(acf)); + } + + /** + * tests the addition of *all* sequences referred to by a sequence being added + * to the dataset + */ + @Test(groups = "Functional") + public void testCreateDatasetAlignmentWithMappedToSeqs() + { + // Alignment with two sequences, gapped. + SequenceI sq1 = new Sequence("sq1", "A--SDF"); + SequenceI sq2 = new Sequence("sq2", "G--TRQ"); + + // cross-references to two more sequences. + DBRefEntry dbr = new DBRefEntry("SQ1", "", "sq3"); + SequenceI sq3 = new Sequence("sq3", "VWANG"); + dbr.setMap(new Mapping(sq3, new MapList(new int[] { 1, 4 }, new int[] { + 2, 5 }, 1, 1))); + sq1.addDBRef(dbr); + + SequenceI sq4 = new Sequence("sq4", "ERKWI"); + DBRefEntry dbr2 = new DBRefEntry("SQ2", "", "sq4"); + dbr2.setMap(new Mapping(sq4, new MapList(new int[] { 1, 4 }, new int[] { + 2, 5 }, 1, 1))); + sq2.addDBRef(dbr2); + // and a 1:1 codonframe mapping between them. + AlignedCodonFrame alc = new AlignedCodonFrame(); + alc.addMap(sq1, sq2, new MapList(new int[] { 1, 4 }, + new int[] { 1, 4 }, 1, 1)); + + AlignmentI protein = new Alignment(new SequenceI[] { sq1, sq2 }); + + /* + * create the alignment dataset + * note this creates sequence datasets where missing + * as a side-effect (in this case, on seq2 + */ + + // TODO promote this method to AlignmentI + ((Alignment) protein).createDatasetAlignment(); + + AlignmentI ds = protein.getDataset(); + + // should be 4 sequences in dataset - two materialised, and two propagated + // from dbref + assertEquals(4, ds.getHeight()); + assertTrue(ds.getSequences().contains(sq1.getDatasetSequence())); + assertTrue(ds.getSequences().contains(sq2.getDatasetSequence())); + assertTrue(ds.getSequences().contains(sq3)); + assertTrue(ds.getSequences().contains(sq4)); + // Should have one codon frame mapping between sq1 and sq2 via dataset + // sequences + assertEquals(ds.getCodonFrame(sq1.getDatasetSequence()), + ds.getCodonFrame(sq2.getDatasetSequence())); + } + + @Test(groups = "Functional") + public void testAddCodonFrame() + { + AlignmentI align = new Alignment(new SequenceI[] {}); + AlignedCodonFrame acf = new AlignedCodonFrame(); + align.addCodonFrame(acf); + assertEquals(1, align.getCodonFrames().size()); + assertTrue(align.getCodonFrames().contains(acf)); + // can't add the same object twice: + align.addCodonFrame(acf); + assertEquals(1, align.getCodonFrames().size()); + + // create dataset alignment - mappings move to dataset + ((Alignment) align).createDatasetAlignment(); + assertSame(align.getCodonFrames(), align.getDataset().getCodonFrames()); + assertEquals(1, align.getCodonFrames().size()); + + AlignedCodonFrame acf2 = new AlignedCodonFrame(); + align.addCodonFrame(acf2); + assertTrue(align.getDataset().getCodonFrames().contains(acf)); + } + + @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"); + AlignmentI align = new Alignment(new SequenceI[] { seq }); + ArrayList hiddenCols = new ArrayList(); + + int[] startEnd = align.getVisibleStartAndEndIndex(hiddenCols); + assertEquals(0, startEnd[0]); + assertEquals(25, startEnd[1]); + + hiddenCols.add(new int[] { 0, 0 }); + startEnd = align.getVisibleStartAndEndIndex(hiddenCols); + assertEquals(1, startEnd[0]); + assertEquals(25, startEnd[1]); + + hiddenCols.add(new int[] { 6, 9 }); + hiddenCols.add(new int[] { 11, 12 }); + startEnd = align.getVisibleStartAndEndIndex(hiddenCols); + assertEquals(1, startEnd[0]); + assertEquals(25, startEnd[1]); + + hiddenCols.add(new int[] { 24, 25 }); + startEnd = align.getVisibleStartAndEndIndex(hiddenCols); + System.out.println(startEnd[0] + " : " + startEnd[1]); + 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); + } + }