Debian Codegraph @

src/dr/evolution/alignment/GapStrippedAlignment.java

raw symbols SCIP
1 /*
2 * GapStrippedAlignment.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.DataType;
29 import dr.evolution.sequence.Sequence;
30 import dr.evolution.util.Taxon;
31
32 import java.util.*;
33
34 /**
35 * Provides bootstrap replicate patterns
36 *
37 * @version $Id: GapStrippedAlignment.java,v 1.4 2005/05/24 20:25:55 rambaut Exp $
38 *
39 * @author Andrew Rambaut
40 */
41 public class GapStrippedAlignment extends Alignment.Abstract
42 {
43 Alignment alignment;
44 boolean[] hasGap = null;
45
46 /**
47 * Constructor
48 */
49 public GapStrippedAlignment(Alignment a) {
50 this.alignment = a;
51 DataType dataType = a.getDataType();
52 hasGap = new boolean[a.getSiteCount()];
53 for (int i = 0; i < hasGap.length; i++) {
54 for (int j = 0; j < a.getSequenceCount(); j++) {
55 if (dataType.isGapState(alignment.getState(j,i))) {
56 hasGap[i] = true;
57 break;
58 }
59 }
60 }
61 }
62
63 public final void setDataType(DataType dataType) {
64 throw new UnsupportedOperationException();
65 //alignment.setDataType(dataType);
66 }
67
68 public final String getAlignedSequenceString(int sequenceIndex) {
69 return getSequence(sequenceIndex).getSequenceString();
70 }
71
72 public final String getUnalignedSequenceString(int sequenceIndex) {
73 return getSequence(sequenceIndex).getSequenceString();
74 }
75
76 public final int getSequenceCount() {
77 return alignment.getSequenceCount();
78 }
79
80 /**
81 * Very inefficient implementation, use sparingly
82 * @param sequenceIndex
83 * @return
84 */
85 public final Sequence getSequence(int sequenceIndex) {
86
87 DataType dataType = getDataType();
88
89 StringBuffer buffer = new StringBuffer();
90 for (int i = 0; i < alignment.getSiteCount(); i++) {
91 if (!hasGap[i]) {
92 buffer.append(dataType.getChar(alignment.getState(sequenceIndex,i)));
93 }
94 }
95 return new Sequence(buffer.toString());
96 }
97
98 public final void setSequenceAttribute(int index, String name, Object value) {
99 throw new UnsupportedOperationException();
100 }
101
102 public final Object getSequenceAttribute(int index, String name) {
103 throw new UnsupportedOperationException();
104 }
105
106 public final int getTaxonCount() {
107 return alignment.getTaxonCount();
108 }
109
110 public final Taxon getTaxon(int taxonIndex) {
111 return alignment.getTaxon(taxonIndex);
112 }
113
114 public final String getTaxonId(int taxonIndex) {
115 return alignment.getTaxonId(taxonIndex);
116 }
117
118 public final int getTaxonIndex(String id) {
119 return alignment.getTaxonIndex(id);
120 }
121
122 public final int getTaxonIndex(Taxon taxon) {
123 return alignment.getTaxonIndex(taxon);
124 }
125
126 public final Object getTaxonAttribute(int taxonIndex, String name) {
127 return alignment.getTaxonAttribute(taxonIndex, name);
128 }
129
130 public List<Taxon> asList() {
131 return alignment.asList();
132 }
133
134 public Iterator<Taxon> iterator() {
135 return alignment.iterator();
136 }
137
138 public final int getSiteCount() {
139 int siteCount = 0;
140 for (int i = 0; i < hasGap.length; i++) {
141 if (!hasGap[i]) siteCount += 1;
142 }
143 return siteCount;
144 }
145
146 public final int[] getSitePattern(int siteIndex) {
147 return alignment.getSitePattern(fullIndex(siteIndex));
148 }
149
150 @Override
151 public double[][] getUncertainSitePattern(int siteIndex) {
152 return alignment.getUncertainSitePattern(fullIndex(siteIndex));
153 }
154
155 public final int getPatternIndex(int siteIndex) {
156 return alignment.getPatternIndex(fullIndex(siteIndex));
157 }
158
159 public final int getState(int taxonIndex, int siteIndex) {
160
161 return alignment.getState(taxonIndex, fullIndex(siteIndex));
162 }
163
164 @Override
165 public double[] getUncertainState(int taxonIndex, int siteIndex) {
166 return alignment.getUncertainState(taxonIndex, fullIndex(siteIndex));
167 }
168
169 @Override
170 public double[][] getUncertainPattern(int patternIndex) {
171 return alignment.getUncertainPattern(fullIndex(patternIndex));
172 }
173
174 @Override
175 public double[] getUncertainPatternState(int taxonIndex, int patternIndex) {
176 return new double[0];
177 }
178
179 public final DataType getDataType() {
180 return alignment.getDataType();
181 }
182
183 @Override
184 public boolean areUncertain() {
185 return false;
186 }
187
188 private final int fullIndex(int gapStrippedIndex) {
189 int index = 0;
190 int fullIndex = 0;
191 while (index < gapStrippedIndex) {
192 if (!hasGap[fullIndex]) index += 1;
193 fullIndex += 1;
194 }
195 return fullIndex;
196 }
197 }