Merge develop to Release_2_8_3_Branch
[jalview.git] / src / jalview / datamodel / AlignedCodon.java
1 package jalview.datamodel;
2
3 /**
4  * Holds the aligned column positions (base 0) for one codon in a nucleotide
5  * sequence, and (optionally) its peptide translation. The object is immutable
6  * once created.
7  * 
8  * Example: in "G-AT-C-GA" the aligned codons are (0, 2, 3) and (5, 7, 8).
9  * 
10  * @author gmcarstairs
11  *
12  */
13 public final class AlignedCodon
14 {
15   public final int pos1;
16
17   public final int pos2;
18
19   public final int pos3;
20
21   public final String product;
22
23   public AlignedCodon(int i, int j, int k)
24   {
25     this(i, j, k, null);
26   }
27
28   public AlignedCodon(int i, int j, int k, String prod)
29   {
30     pos1 = i;
31     pos2 = j;
32     pos3 = k;
33     product = prod;
34   }
35
36   /**
37    * Returns the column position for the given base (1, 2, 3).
38    * 
39    * @param base
40    * @return
41    * @throws IllegalArgumentException
42    *           if an argument value other than 1, 2 or 3 is supplied
43    */
44   public int getBaseColumn(int base)
45   {
46     if (base < 1 || base > 3)
47     {
48       throw new IllegalArgumentException(Integer.toString(base));
49     }
50     return base == 1 ? pos1 : (base == 2 ? pos2 : pos3);
51   }
52
53   /**
54    * Two aligned codons are equal if all their base positions are the same. We
55    * don't care about the protein product. This test is required for correct
56    * alignment of translated gapped dna alignments (the same codon positions in
57    * different sequences occupy the same column in the translated alignment).
58    */
59   @Override
60   public boolean equals(Object o)
61   {
62     /*
63      * Equality with null value required for consistency with
64      * Dna.compareCodonPos
65      */
66     if (o == null)
67     {
68       return true;
69     }
70     if (!(o instanceof AlignedCodon))
71     {
72       return false;
73     }
74     AlignedCodon ac = (AlignedCodon) o;
75     return (pos1 == ac.pos1 && pos2 == ac.pos2 && pos3 == ac.pos3);
76   }
77
78   @Override
79   public String toString()
80   {
81     StringBuilder sb = new StringBuilder();
82     sb.append("[").append(pos1).append(", ").append(pos2).append(", ")
83             .append(pos3).append("]");
84     return sb.toString();
85   }
86 }