2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.datamodel.AlignedCodon;
25 import java.util.Comparator;
28 * Implements rules for comparing two aligned codons, i.e. determining whether
29 * they should occupy the same position in a translated protein alignment, or
30 * one or the other should 'follow' (by preceded by a gap).
35 public final class CodonComparator implements Comparator<AlignedCodon>
39 public int compare(AlignedCodon ac1, AlignedCodon ac2)
41 if (ac1 == null || ac2 == null || ac1.equals(ac2))
48 * Case 1: if one starts before the other, and doesn't end after it, then it
49 * precedes. We ignore the middle base position here.
54 if (ac1.pos1 < ac2.pos1 && ac1.pos3 <= ac2.pos3)
58 if (ac2.pos1 < ac1.pos1 && ac2.pos3 <= ac1.pos3)
65 * Case 2: if one ends after the other, and doesn't start before it, then it
66 * follows. We ignore the middle base position here.
71 if (ac1.pos3 > ac2.pos3 && ac1.pos1 >= ac2.pos1)
75 if (ac2.pos3 > ac1.pos3 && ac2.pos1 >= ac1.pos1)
81 * Case 3: if start and end match, compare middle base positions.
83 if (ac1.pos1 == ac2.pos1 && ac1.pos3 == ac2.pos3)
85 return Integer.compare(ac1.pos2, ac2.pos2);
89 * That just leaves the 'enclosing' case - one codon starts after but ends
90 * before the other. If the middle bases don't match, use their comparison
93 int compareMiddles = Integer.compare(ac1.pos2, ac2.pos2);
94 if (compareMiddles != 0)
96 return compareMiddles;
101 * Finally just leaves overlap with matching middle base, e.g.
104 * In this case the choice is arbitrary whether to compare based on
105 * first or last base position. We pick the first. Note this preserves
106 * symmetricality of the comparison.
109 return Integer.compare(ac1.pos1, ac2.pos1);