61798314218485fc414a64c61f8d18d106630bfe
[jalview.git] / src / jalview / datamodel / AlignedCodon.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel;
22
23 /**
24  * Holds the aligned column positions (base 0) for one codon in a nucleotide
25  * sequence, and (optionally) its peptide translation. The object is immutable
26  * once created.
27  * 
28  * Example: in "G-AT-C-GA" the aligned codons are (0, 2, 3) and (5, 7, 8).
29  * 
30  * JBPComment: Is this useful anywhere other than jalview.analysis.Dna ?
31  * 
32  * @author gmcarstairs
33  *
34  */
35 public final class AlignedCodon
36 {
37   public final int pos1;
38
39   public final int pos2;
40
41   public final int pos3;
42
43   public final String product;
44
45   public AlignedCodon(int i, int j, int k)
46   {
47     this(i, j, k, null);
48   }
49
50   public AlignedCodon(int i, int j, int k, String prod)
51   {
52     pos1 = i;
53     pos2 = j;
54     pos3 = k;
55     product = prod;
56   }
57
58   /**
59    * Returns the column position for the given base (1, 2, 3).
60    * 
61    * @param base
62    * @return
63    * @throws IllegalArgumentException
64    *           if an argument value other than 1, 2 or 3 is supplied
65    */
66   public int getBaseColumn(int base)
67   {
68     if (base < 1 || base > 3)
69     {
70       throw new IllegalArgumentException(Integer.toString(base));
71     }
72     return base == 1 ? pos1 : (base == 2 ? pos2 : pos3);
73   }
74
75   /**
76    * Two aligned codons are equal if all their base positions are the same. We
77    * don't care about the protein product. This test is required for correct
78    * alignment of translated gapped dna alignments (the same codon positions in
79    * different sequences occupy the same column in the translated alignment).
80    */
81   @Override
82   public boolean equals(Object o)
83   {
84     /*
85      * Equality with null value required for consistency with
86      * Dna.compareCodonPos
87      */
88     if (o == null)
89     {
90       return true;
91     }
92     if (!(o instanceof AlignedCodon))
93     {
94       return false;
95     }
96     AlignedCodon ac = (AlignedCodon) o;
97     return (pos1 == ac.pos1 && pos2 == ac.pos2 && pos3 == ac.pos3);
98   }
99
100   @Override
101   public String toString()
102   {
103     StringBuilder sb = new StringBuilder();
104     sb.append("[").append(pos1).append(", ").append(pos2).append(", ")
105             .append(pos3).append("]");
106     return sb.toString();
107   }
108 }