Debian Codegraph @

src/dr/evolution/alignment/ConvertAlignment.java

raw symbols SCIP
1 /*
2 * ConvertAlignment.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.alignment;
27
28 import dr.evolution.datatype.*;
29 import dr.evolution.sequence.Sequence;
30 import dr.evolution.util.Taxon;
31 import java.util.*;
32
33 /**
34 * An alignment class that takes another alignment and converts it on the fly
35 * to a different dataType.
36 *
37 * @author Andrew Rambaut
38 * @author Alexei Drummond
39 *
40 * @version $Id: ConvertAlignment.java,v 1.29 2005/05/24 20:25:55 rambaut Exp $
41 */
42 public class ConvertAlignment extends WrappedAlignment implements dr.util.XHTMLable
43 {
44
45 /**
46 * Constructor.
47 */
48 public ConvertAlignment(DataType dataType) {
49 this(dataType, null, null);
50 }
51
52 /**
53 * Constructor.
54 */
55 public ConvertAlignment(DataType dataType, CodonTable codonTable) {
56 this(dataType, codonTable, null);
57 }
58
59 /**
60 * Constructor.
61 */
62 public ConvertAlignment(DataType dataType, Alignment alignment) {
63 this(dataType, null, alignment);
64 }
65
66 /**
67 * Constructor.
68 */
69 public ConvertAlignment(DataType dataType, CodonTable codonTable, Alignment alignment) {
70 super(alignment);
71 setDataType(dataType);
72 setCodonTable(codonTable);
73 setAlignment(alignment);
74 }
75
76 /**
77 * Sets the CodonTable of this alignment.
78 */
79 public void setCodonTable(CodonTable codonTable) {
80 this.codonTable = codonTable;
81 }
82
83 /**
84 * Sets the contained.
85 */
86 public void setAlignment(Alignment alignment) {
87 if (dataType == null) {
88 dataType = alignment.getDataType();
89 }
90
91 this.alignment = alignment;
92
93 int newType = dataType.getType();
94 int originalType = alignment.getDataType().getType();
95
96 //TODO: this logic does not work for pibuss
97 if (originalType == DataType.NUCLEOTIDES) {
98
99 if (newType != DataType.CODONS && newType != DataType.AMINO_ACIDS) {
100 throw new RuntimeException("Incompatible alignment DataType for ConversionAlignment");
101 }
102
103 } else if (originalType == DataType.CODONS) {
104
105 if (!(newType == DataType.AMINO_ACIDS || newType == DataType.NUCLEOTIDES)) {
106
107 System.err.println("originalType = " + originalType);
108 System.err.println("newType = " + newType);
109 throw new RuntimeException("Incompatible alignment DataType for ConversionAlignment");
110 }
111
112 } else {
113
114 throw new RuntimeException("Incompatible alignment DataType for ConversionAlignment");
115
116 }//END: original type check
117 }
118
119 /**
120 * Sets the dataType of this alignment. This can be different from
121 * the dataTypes of the contained alignment - they will be translated
122 * as required.
123 */
124 public void setDataType(DataType dataType) {
125 this.dataType = dataType;
126 }
127
128 /**
129 * @return the DataType of this siteList
130 */
131 public DataType getDataType() {
132 return dataType;
133 }
134
135 /**
136 * @return number of sites
137 */
138 public int getSiteCount() {
139 if (alignment == null) throw new RuntimeException("ConvertAlignment has no alignment");
140
141 int originalType = alignment.getDataType().getType();
142 int count = alignment.getSiteCount();
143
144 if (originalType == DataType.NUCLEOTIDES) {
145 count /= 3;
146 }
147
148 return count;
149 }
150
151 /**
152 * @return the sequence state at (taxon, site)
153 */
154 public int getState(int taxonIndex, int siteIndex) {
155 if (alignment == null) throw new RuntimeException("ConvertAlignment has no alignment");
156
157 int newType = dataType.getType();
158 int originalType = alignment.getDataType().getType();
159
160 int state = 0;
161
162 if (originalType == DataType.NUCLEOTIDES) {
163 int siteIndex3 = siteIndex * 3;
164 int state1 = alignment.getState(taxonIndex, siteIndex3);
165 int state2 = alignment.getState(taxonIndex, siteIndex3 + 1);
166 int state3 = alignment.getState(taxonIndex, siteIndex3 + 2);
167
168 if (newType == DataType.CODONS) {
169 state = ((Codons)dataType).getState(state1, state2, state3);
170 } else { // newType == DataType.AMINO_ACIDS
171 state = codonTable.getAminoAcidState(((Codons)dataType).getCanonicalState(((Codons)dataType).getState(state1, state2, state3)));
172 }
173
174 } else if (originalType == DataType.CODONS) {
175 if (newType == DataType.AMINO_ACIDS) {
176 state = codonTable.getAminoAcidState(alignment.getState(taxonIndex, siteIndex));
177 } else { // newType == DataType.CODONS
178 String string = alignment.getAlignedSequenceString(taxonIndex);
179 state = Nucleotides.INSTANCE.getState(string.charAt(siteIndex));
180 }
181 }
182
183 return state;
184 }
185
186 public String toXHTML() {
187 String xhtml = "<p><em>Converted Alignment</em> data type = ";
188 xhtml += getDataType().getDescription();
189 xhtml += ", no. taxa = ";
190 xhtml += getTaxonCount();
191 xhtml += ", no. sites = ";
192 xhtml += getSiteCount();
193 xhtml += "</p>";
194
195 xhtml += "<pre>";
196
197 int length, maxLength = 0;
198 for (int i =0; i < getTaxonCount(); i++) {
199 length = getTaxonId(i).length();
200 if (length > maxLength)
201 maxLength = length;
202 }
203
204 int count, state;
205 int type = getDataType().getType();
206
207 for (int i = 0; i < getTaxonCount(); i++) {
208 length = getTaxonId(i).length();
209 xhtml += getTaxonId(i);
210 for (int j = length; j <= maxLength; j++)
211 xhtml += " ";
212
213 count = getSiteCount();
214 for (int j = 0; j < count; j++) {
215 state = getState(i, j);
216 if (type == DataType.CODONS)
217 xhtml += Codons.UNIVERSAL.getTriplet(state) + " ";
218 else
219 xhtml += AminoAcids.INSTANCE.getTriplet(state) + " ";
220 }
221 xhtml += "\n";
222 }
223 xhtml += "</pre>";
224 return xhtml;
225 }
226
227 private DataType dataType = null;
228 private CodonTable codonTable = null;
229 }