JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.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  * JBPComment: Is this useful anywhere other than jalview.analysis.Dna ?
11  * 
12  * @author gmcarstairs
13  *
14  */
15 public final class AlignedCodon
16 {
17   public final int pos1;
18
19   public final int pos2;
20
21   public final int pos3;
22
23   public final String product;
24
25   public AlignedCodon(int i, int j, int k)
26   {
27     this(i, j, k, null);
28   }
29
30   public AlignedCodon(int i, int j, int k, String prod)
31   {
32     pos1 = i;
33     pos2 = j;
34     pos3 = k;
35     product = prod;
36   }
37
38   /**
39    * Returns the column position for the given base (1, 2, 3).
40    * 
41    * @param base
42    * @return
43    * @throws IllegalArgumentException
44    *           if an argument value other than 1, 2 or 3 is supplied
45    */
46   public int getBaseColumn(int base)
47   {
48     if (base < 1 || base > 3)
49     {
50       throw new IllegalArgumentException(Integer.toString(base));
51     }
52     return base == 1 ? pos1 : (base == 2 ? pos2 : pos3);
53   }
54
55   /**
56    * Two aligned codons are equal if all their base positions are the same. We
57    * don't care about the protein product. This test is required for correct
58    * alignment of translated gapped dna alignments (the same codon positions in
59    * different sequences occupy the same column in the translated alignment).
60    */
61   @Override
62   public boolean equals(Object o)
63   {
64     /*
65      * Equality with null value required for consistency with
66      * Dna.compareCodonPos
67      */
68     if (o == null)
69     {
70       return true;
71     }
72     if (!(o instanceof AlignedCodon))
73     {
74       return false;
75     }
76     AlignedCodon ac = (AlignedCodon) o;
77     return (pos1 == ac.pos1 && pos2 == ac.pos2 && pos3 == ac.pos3);
78   }
79
80   @Override
81   public String toString()
82   {
83     StringBuilder sb = new StringBuilder();
84     sb.append("[").append(pos1).append(", ").append(pos2).append(", ")
85             .append(pos3).append("]");
86     return sb.toString();
87   }
88 }