JAL-2089 patch broken merge to master for Release 2.10.0b1
[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  * @author gmcarstairs
31  *
32  */
33 public final class AlignedCodon
34 {
35   // base 1 aligned sequence position (base 0)
36   public final int pos1;
37
38   // base 2 aligned sequence position (base 0)
39   public final int pos2;
40
41   // base 3 aligned sequence position (base 0)
42   public final int pos3;
43
44   // peptide aligned sequence position (base 0)
45   public final int peptideCol;
46
47   // peptide coded for by this codon
48   public final String product;
49
50   public AlignedCodon(int i, int j, int k)
51   {
52     this(i, j, k, null, 0);
53   }
54
55   public AlignedCodon(int i, int j, int k, String prod, int prodCol)
56   {
57     pos1 = i;
58     pos2 = j;
59     pos3 = k;
60     product = prod;
61     peptideCol = prodCol;
62   }
63
64   /**
65    * Returns the column position for the given base (1, 2, 3).
66    * 
67    * @param base
68    * @return
69    * @throws IllegalArgumentException
70    *           if an argument value other than 1, 2 or 3 is supplied
71    */
72   public int getBaseColumn(int base)
73   {
74     if (base < 1 || base > 3)
75     {
76       throw new IllegalArgumentException(Integer.toString(base));
77     }
78     return base == 1 ? pos1 : (base == 2 ? pos2 : pos3);
79   }
80
81   /**
82    * Two aligned codons are equal if all their base positions are the same. We
83    * don't care about the protein product. This test is required for correct
84    * alignment of translated gapped dna alignments (the same codon positions in
85    * different sequences occupy the same column in the translated alignment).
86    */
87   @Override
88   public boolean equals(Object o)
89   {
90     /*
91      * Equality with null value required for consistency with
92      * Dna.compareCodonPos
93      */
94     if (o == null)
95     {
96       return true;
97     }
98     if (!(o instanceof AlignedCodon))
99     {
100       return false;
101     }
102     AlignedCodon ac = (AlignedCodon) o;
103     return (pos1 == ac.pos1 && pos2 == ac.pos2 && pos3 == ac.pos3);
104   }
105
106   @Override
107   public String toString()
108   {
109     StringBuilder sb = new StringBuilder();
110     sb.append("[").append(pos1).append(", ").append(pos2).append(", ")
111             .append(pos3).append("]");
112     return sb.toString();
113   }
114 }