Skip to content

Commit ecff440

Browse files
committed
Adding sortBlocks method
1 parent fdb5cb0 commit ecff440

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/align/multiple/util/MultipleAlignmentTools.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.Comparator;
57
import java.util.List;
68
import java.util.SortedSet;
79
import java.util.TreeSet;
@@ -670,4 +672,36 @@ public static List<Integer> getCorePositions(Block block){
670672
}
671673
return corePositions;
672674
}
675+
676+
/**
677+
* Sort blocks so that the specified row is in sequential order.
678+
* The sort happens in place.
679+
* @param blocks List of blocks
680+
* @param sortedIndex Index of the row to be sorted
681+
*/
682+
public static void sortBlocks(List<Block> blocks,final int sortedIndex) {
683+
Collections.sort(blocks, new Comparator<Block>() {
684+
@Override
685+
public int compare(Block o1, Block o2) {
686+
// Compare the first non-null residue of each block
687+
List<Integer> alignres1 = o1.getAlignRes().get(sortedIndex);
688+
List<Integer> alignres2 = o2.getAlignRes().get(sortedIndex);
689+
Integer res1 = null;
690+
Integer res2 = null;
691+
for(Integer r : alignres1) {
692+
if( r != null) {
693+
res1 = r;
694+
break;
695+
}
696+
}
697+
for(Integer r : alignres2) {
698+
if( r != null) {
699+
res2 = r;
700+
break;
701+
}
702+
}
703+
return res1.compareTo(res2);
704+
}
705+
});
706+
}
673707
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)