|
| 1 | +package org.biojava.nbio.structure.align.multiple.util; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.biojava.nbio.structure.align.multiple.Block; |
| 10 | +import org.biojava.nbio.structure.align.multiple.BlockImpl; |
| 11 | +import org.biojava.nbio.structure.align.multiple.BlockSet; |
| 12 | +import org.biojava.nbio.structure.align.multiple.BlockSetImpl; |
| 13 | +import org.biojava.nbio.structure.align.multiple.MultipleAlignment; |
| 14 | +import org.biojava.nbio.structure.align.multiple.MultipleAlignmentImpl; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @author Spencer Bliven |
| 20 | + * |
| 21 | + */ |
| 22 | +public class MultipleAlignmentToolsTest { |
| 23 | + |
| 24 | + /** |
| 25 | + * Override the toString method to give shorter output for errors |
| 26 | + * @author blivens |
| 27 | + * |
| 28 | + */ |
| 29 | + public static class NamedBlock extends BlockImpl { |
| 30 | + private static final long serialVersionUID = 5060618718423340848L; |
| 31 | + private String name; |
| 32 | + public NamedBlock(String name, BlockSet bs) { |
| 33 | + super(bs); |
| 34 | + this.name = name; |
| 35 | + } |
| 36 | + @Override |
| 37 | + public String toString() { |
| 38 | + return String.format("Block[%s]", name); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @SuppressWarnings("unchecked") |
| 43 | + @Test |
| 44 | + public void testSortBlocks() { |
| 45 | + |
| 46 | + // Sample alignment with blocks out of order |
| 47 | + |
| 48 | + // Row0: Already sorted |
| 49 | + // Row1: Unsorted |
| 50 | + // Row2: Test nulls at start |
| 51 | + // Row3: Test all nulls |
| 52 | + // Row4: Test overlapping ranges |
| 53 | + // Row5: Test only fist element used |
| 54 | + MultipleAlignment align = new MultipleAlignmentImpl(); |
| 55 | + BlockSet bs = new BlockSetImpl(align); |
| 56 | + |
| 57 | + Block one = new NamedBlock("1",bs); |
| 58 | + one.setAlignRes(Arrays.asList( |
| 59 | + Arrays.asList( 10, 11, 12), |
| 60 | + Arrays.asList( 20, 21, 22), |
| 61 | + Arrays.asList( null, 21, 22), |
| 62 | + Arrays.asList( 20, 21, 22), |
| 63 | + Arrays.asList( 20, 21, 22), |
| 64 | + Arrays.asList( 20, 21, 22) |
| 65 | + )); |
| 66 | + Block two = new NamedBlock("2",bs); |
| 67 | + two.setAlignRes(Arrays.asList( |
| 68 | + Arrays.asList( 20, 21, 22), |
| 69 | + Arrays.asList( 10, 11, 12), |
| 70 | + Arrays.asList( 10, null, 12), |
| 71 | + Arrays.asList( (Integer)null,null,null), |
| 72 | + Arrays.asList( 10, 11, 12), |
| 73 | + Arrays.asList( 10, 11, 12) |
| 74 | + )); |
| 75 | + Block three = new NamedBlock("3",bs); |
| 76 | + three.setAlignRes(Arrays.asList( |
| 77 | + Arrays.asList( 30, 31, 32), |
| 78 | + Arrays.asList( 40, 41, 42), |
| 79 | + Arrays.asList( 40, 41, null), |
| 80 | + Arrays.asList( 40, 41, 42), |
| 81 | + Arrays.asList( 40, 41, 42), |
| 82 | + Arrays.asList( 20, 41, 42) |
| 83 | + )); |
| 84 | + Block four = new NamedBlock("4",bs); |
| 85 | + four.setAlignRes(Arrays.asList( |
| 86 | + Arrays.asList( 40, 41, 42), |
| 87 | + Arrays.asList( 30, 31, 32), |
| 88 | + Arrays.asList( null, 31, 32), |
| 89 | + Arrays.asList( (Integer)null,null,null), |
| 90 | + Arrays.asList( 30, 51, 52), |
| 91 | + Arrays.asList( 30, 31, 32) |
| 92 | + )); |
| 93 | + |
| 94 | + List<Block> blocks; |
| 95 | + int index; |
| 96 | + List<Block> expected; |
| 97 | + |
| 98 | + index = 0; |
| 99 | + blocks = align.getBlocks(); |
| 100 | + MultipleAlignmentTools.sortBlocks(blocks, index); |
| 101 | + expected = Arrays.asList(one,two,three,four); |
| 102 | + assertEquals("Bad comparison of row "+index, expected,blocks); |
| 103 | + |
| 104 | + index = 1; |
| 105 | + blocks = align.getBlocks(); |
| 106 | + MultipleAlignmentTools.sortBlocks(blocks, index); |
| 107 | + expected = Arrays.asList(two,one,four,three); |
| 108 | + assertEquals("Bad comparison of row "+index, expected,blocks); |
| 109 | + |
| 110 | + index = 2; |
| 111 | + blocks = align.getBlocks(); |
| 112 | + MultipleAlignmentTools.sortBlocks(blocks, index); |
| 113 | + expected = Arrays.asList(two,one,four,three); |
| 114 | + assertEquals("Bad comparison of row "+index, expected,blocks); |
| 115 | + |
| 116 | + index = 3; |
| 117 | + blocks = align.getBlocks(); |
| 118 | + try { |
| 119 | + MultipleAlignmentTools.sortBlocks(blocks, index); |
| 120 | + fail("Row "+index+" should throw NPE"); |
| 121 | + } catch(NullPointerException e) {} |
| 122 | + |
| 123 | + index = 4; |
| 124 | + blocks = align.getBlocks(); |
| 125 | + MultipleAlignmentTools.sortBlocks(blocks, index); |
| 126 | + expected = Arrays.asList(two,one,four,three); |
| 127 | + assertEquals("Bad comparison of row "+index, expected,blocks); |
| 128 | + |
| 129 | + index = 5; |
| 130 | + blocks = align.getBlocks(); |
| 131 | + MultipleAlignmentTools.sortBlocks(blocks, index); |
| 132 | + expected = Arrays.asList(two,one,three,four); |
| 133 | + assertEquals("Bad comparison of row "+index, expected,blocks); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments