| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class GapStrippedAlignment extends Alignment.Abstract |
| 42 |
{ |
| 43 |
Alignment alignment; |
| 44 |
boolean[] hasGap = null; |
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 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 |
|
| 82 |
|
| 83 |
|
| 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 |
} |