From: gmungoc Date: Thu, 7 Jul 2016 15:51:51 +0000 (+0100) Subject: JAL-2110 added alignAsSameSequences to handle 'direct' (copy) case X-Git-Tag: Release_2_10_0~140^2~5^2~11 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d2b08d9048af25114cff8deabbd320471119d88d;p=jalview.git JAL-2110 added alignAsSameSequences to handle 'direct' (copy) case --- diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index 20fcf25..17aab15 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -2535,6 +2535,17 @@ public class AlignmentUtils */ public static int alignAs(AlignmentI unaligned, AlignmentI aligned) { + /* + * easy case - aligning a copy of aligned sequences + */ + if (alignAsSameSequences(unaligned, aligned)) + { + return unaligned.getHeight(); + } + + /* + * fancy case - aligning via mappings between sequences + */ List unmapped = new ArrayList(); Map> columnMap = buildMappedColumnsMap( unaligned, aligned, unmapped); @@ -2587,6 +2598,54 @@ public class AlignmentUtils } /** + * If unaligned and aligned sequences share the same dataset sequences, then + * simply copies the aligned sequences to the unaligned sequences and returns + * true; else returns false + * + * @param unaligned + * @param aligned + * @return + */ + static boolean alignAsSameSequences(AlignmentI unaligned, + AlignmentI aligned) + { + if (aligned.getDataset() == null || unaligned.getDataset() == null) + { + return false; // should only pass alignments with datasets here + } + + Map alignedDatasets = new HashMap(); + for (SequenceI seq : aligned.getSequences()) + { + alignedDatasets.put(seq.getDatasetSequence(), seq); + } + + /* + * first pass - check whether all sequences to be aligned share a dataset + * sequence with an aligned sequence + */ + for (SequenceI seq : unaligned.getSequences()) + { + if (!alignedDatasets.containsKey(seq.getDatasetSequence())) + { + return false; + } + } + + /* + * second pass - copy aligned sequences + */ + for (SequenceI seq : unaligned.getSequences()) + { + SequenceI alignedSequence = alignedDatasets.get(seq + .getDatasetSequence()); + seq.setSequence(alignedSequence.getSequenceAsString()); + } + + return true; + } + + /** * Returns a map whose key is alignment column number (base 1), and whose * values are a map of sequence characters in that column. * diff --git a/test/jalview/analysis/AlignmentUtilsTests.java b/test/jalview/analysis/AlignmentUtilsTests.java index 727cf22..5c75992 100644 --- a/test/jalview/analysis/AlignmentUtilsTests.java +++ b/test/jalview/analysis/AlignmentUtilsTests.java @@ -2394,4 +2394,53 @@ public class AlignmentUtilsTests assertEquals(7, m.getStart()); assertEquals(9, m.getEnd()); } + + /** + * Test the method that just copies aligned sequences, provided all sequences + * to be aligned share the aligned sequence's dataset + */ + @Test(groups = "Functional") + public void testAlignAsSameSequences() + { + SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa"); + SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA"); + AlignmentI al1 = new Alignment(new SequenceI[] { dna1, dna2 }); + ((Alignment) al1).createDatasetAlignment(); + + SequenceI dna3 = new Sequence(dna1); + SequenceI dna4 = new Sequence(dna2); + assertSame(dna3.getDatasetSequence(), dna1.getDatasetSequence()); + assertSame(dna4.getDatasetSequence(), dna2.getDatasetSequence()); + String seq1 = "-cc-GG-GT-TT--aaa"; + dna3.setSequence(seq1); + String seq2 = "C--C-Cgg--gtt-tAA-A-"; + dna4.setSequence(seq2); + AlignmentI al2 = new Alignment(new SequenceI[] { dna3, dna4 }); + ((Alignment) al2).createDatasetAlignment(); + + assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2)); + assertEquals(seq1, al1.getSequenceAt(0).getSequenceAsString()); + assertEquals(seq2, al1.getSequenceAt(1).getSequenceAsString()); + + /* + * add another sequence to 'aligned' - should still succeed, since + * unaligned sequences still share a dataset with aligned sequences + */ + SequenceI dna5 = new Sequence("dna5", "CCCgggtttAAA"); + dna5.createDatasetSequence(); + al2.addSequence(dna5); + assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2)); + assertEquals(seq1, al1.getSequenceAt(0).getSequenceAsString()); + assertEquals(seq2, al1.getSequenceAt(1).getSequenceAsString()); + + /* + * add another sequence to 'unaligned' - should fail, since now not + * all unaligned sequences share a dataset with aligned sequences + */ + SequenceI dna6 = new Sequence("dna6", "CCCgggtttAAA"); + dna6.createDatasetSequence(); + al1.addSequence(dna6); + assertFalse(AlignmentUtils.alignAsSameSequences(al1, al2)); + } + }