Debian Codegraph @

src/dr/evolution/alignment/HypermutantAlignment.java

raw symbols SCIP
1 /*
2 * HypermutantAlignment.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
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36 /**
37 * An alignment class that takes another alignment and converts it on the fly
38 * to a different dataType.
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 *
43 * @version $Id: ConvertAlignment.java,v 1.29 2005/05/24 20:25:55 rambaut Exp $
44 */
45 public class HypermutantAlignment extends WrappedAlignment
46 {
47 public enum APOBECType {
48 ALL("all"),
49 BOTH("both"),
50 HA3G("hA3G"),
51 HA3F("hA3F");
52
53
54 APOBECType(String label) {
55 this.label = label;
56 }
57
58 public String toString() {
59 return label;
60 }
61
62 final String label;
63 }
64
65
66 /**
67 * Constructor.
68 */
69 public HypermutantAlignment(APOBECType type, Alignment alignment) {
70 super(alignment);
71 this.type = type;
72
73 if (alignment.getDataType().getType() != DataType.NUCLEOTIDES) {
74 throw new RuntimeException("HypermutantAlignment can only convert nucleotide alignments");
75 }
76
77 mutatedContextCounts = new int[getTaxonCount()];
78 unmutatedContextCounts = new int[getTaxonCount()];
79
80 countContexts();
81
82 }
83
84 /**
85 * @return the sequence state at (taxon, site)
86 */
87 public int getState(int taxonIndex, int siteIndex) {
88 int state = alignment.getState(taxonIndex, siteIndex);
89 if (state == Nucleotides.A_STATE) {
90 int nextState = getNextContextState(taxonIndex, siteIndex);
91
92 if ( (type == APOBECType.ALL) || // consider all As as G->As
93 (type == APOBECType.HA3G && nextState == Nucleotides.G_STATE) ||
94 (type == APOBECType.HA3F && nextState == Nucleotides.A_STATE) ||
95 (type == APOBECType.BOTH && (nextState == Nucleotides.G_STATE || nextState == Nucleotides.A_STATE))
96 ) {
97 state = Nucleotides.R_STATE;
98 }
99 }
100
101 return state;
102 }
103
104 /**
105 * This finds the next state downstream of siteIndex, skipping over gaps
106 */
107 private int getNextContextState(int taxonIndex, int siteIndex) {
108 int nextState = Nucleotides.GAP_STATE;
109
110 int i = siteIndex + 1;
111 while (nextState == Nucleotides.GAP_STATE && i < getSiteCount()) {
112 nextState = alignment.getState(taxonIndex, i);
113 i++;
114 }
115
116 return nextState;
117 }
118
119 private void countContexts() {
120 for (int i = 0; i < getTaxonCount(); i++) {
121 for (int j = 0; j < getSiteCount(); j++) {
122 int state = alignment.getState(i, j);
123 if (state == Nucleotides.A_STATE || state == Nucleotides.G_STATE) {
124 int nextState = getNextContextState(i, j);
125
126 if ( (type == APOBECType.ALL) || // consider all As as G->As
127 (type == APOBECType.HA3G && nextState == Nucleotides.G_STATE) ||
128 (type == APOBECType.HA3F && nextState == Nucleotides.A_STATE) ||
129 (type == APOBECType.BOTH && (nextState == Nucleotides.G_STATE || nextState == Nucleotides.A_STATE))
130 ) {
131 if (state == Nucleotides.A_STATE) {
132 // a mutated context
133 mutatedContextCounts[i] ++;
134 } else {
135 // an unmutated context
136 unmutatedContextCounts[i] ++;
137 }
138 }
139 }
140 }
141 }
142 }
143
144 public int[] getMutatedContextCounts() {
145 return mutatedContextCounts;
146 }
147
148 public int[] getUnmutatedContextCounts() {
149 return unmutatedContextCounts;
150 }
151
152 public int getMutatedContextCount() {
153 int total = 0;
154 for (int count: mutatedContextCounts) {
155 total += count;
156 }
157 return total;
158 }
159
160 public int getUnmutatedContextCount() {
161 int total = 0;
162 for (int count: unmutatedContextCounts) {
163 total += count;
164 }
165 return total;
166 }
167
168 // **************************************************************
169 // INSTANCE VARIABLES
170 // **************************************************************
171
172 private APOBECType type = null;
173 private int[] mutatedContextCounts;
174 private int[] unmutatedContextCounts;
175 }