1 package jalview.analysis;
3 import jalview.datamodel.AlignedCodon;
5 import java.util.Comparator;
8 * Implements rules for comparing two aligned codons, i.e. determining whether
9 * they should occupy the same position in a translated protein alignment, or
10 * one or the other should 'follow' (by preceded by a gap).
15 public final class CodonComparator implements Comparator<AlignedCodon>
19 public int compare(AlignedCodon ac1, AlignedCodon ac2)
21 if (ac1 == null || ac2 == null || ac1.equals(ac2))
28 * Case 1: if one starts before the other, and doesn't end after it, then it
29 * precedes. We ignore the middle base position here.
34 if (ac1.pos1 < ac2.pos1 && ac1.pos3 <= ac2.pos3)
38 if (ac2.pos1 < ac1.pos1 && ac2.pos3 <= ac1.pos3)
45 * Case 2: if one ends after the other, and doesn't start before it, then it
46 * follows. We ignore the middle base position here.
51 if (ac1.pos3 > ac2.pos3 && ac1.pos1 >= ac2.pos1)
55 if (ac2.pos3 > ac1.pos3 && ac2.pos1 >= ac1.pos1)
61 * Case 3: if start and end match, compare middle base positions.
63 if (ac1.pos1 == ac2.pos1 && ac1.pos3 == ac2.pos3)
65 return Integer.compare(ac1.pos2, ac2.pos2);
69 * That just leaves the 'enclosing' case - one codon starts after but ends
70 * before the other. If the middle bases don't match, use their comparison
73 int compareMiddles = Integer.compare(ac1.pos2, ac2.pos2);
74 if (compareMiddles != 0)
76 return compareMiddles;
81 * Finally just leaves overlap with matching middle base, e.g.
84 * In this case the choice is arbitrary whether to compare based on
85 * first or last base position. We pick the first. Note this preserves
86 * symmetricality of the comparison.
89 return Integer.compare(ac1.pos1, ac2.pos1);