| 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.util.TaxonList; |
| 29 |
import dr.util.Citable; |
| 30 |
import dr.util.Citation; |
| 31 |
import dr.util.CommonCitations; |
| 32 |
|
| 33 |
import java.util.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class AscertainedSitePatterns extends SitePatterns implements Citable { |
| 46 |
|
| 47 |
protected int[] includePatterns; |
| 48 |
protected int[] excludePatterns; |
| 49 |
protected int ascertainmentIncludeCount; |
| 50 |
protected int ascertainmentExcludeCount; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public AscertainedSitePatterns(Alignment alignment) { |
| 56 |
super(alignment); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public AscertainedSitePatterns(Alignment alignment, TaxonList taxa) { |
| 63 |
super(alignment, taxa); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public AscertainedSitePatterns(Alignment alignment, int from, int to, int every) { |
| 70 |
super(alignment, from, to, every); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public AscertainedSitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every) { |
| 77 |
super(alignment, taxa, from, to, every); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public AscertainedSitePatterns(SiteList siteList) { |
| 84 |
super(siteList); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public AscertainedSitePatterns(SiteList siteList, int from, int to, int every) { |
| 91 |
super(siteList, from, to, every); |
| 92 |
} |
| 93 |
|
| 94 |
public AscertainedSitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every, |
| 95 |
int includeFrom, int includeTo, |
| 96 |
int excludeFrom, int excludeTo) { |
| 97 |
super(alignment, taxa, from, to, every); |
| 98 |
int[][] newPatterns = new int[patterns.length + (includeTo - includeFrom) + (excludeTo - excludeFrom)][]; |
| 99 |
double[] newWeights = new double[patterns.length + (includeTo - includeFrom) + (excludeTo - excludeFrom)]; |
| 100 |
for (int i = 0; i < patterns.length; ++i) { |
| 101 |
newPatterns[i] = patterns[i]; |
| 102 |
newWeights[i] = weights[i]; |
| 103 |
} |
| 104 |
patterns = newPatterns; |
| 105 |
weights = newWeights; |
| 106 |
|
| 107 |
if (includeTo - includeFrom >= 1) |
| 108 |
includePatterns(includeFrom, includeTo, every); |
| 109 |
if (excludeTo - excludeFrom >= 1) |
| 110 |
excludePatterns(excludeFrom, excludeTo, every); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public Citation.Category getCategory() { |
| 116 |
return Citation.Category.DATA_MODELS; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public String getDescription() { |
| 121 |
return "Ascertained Site Patterns"; |
| 122 |
} |
| 123 |
|
| 124 |
public List<Citation> getCitations() { |
| 125 |
return Collections.singletonList( |
| 126 |
CommonCitations.ALEKSEYENKO_2008 |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
public int getIncludePatternCount() { |
| 131 |
return ascertainmentIncludeCount; |
| 132 |
} |
| 133 |
|
| 134 |
public int[] getIncludePatternIndices() { |
| 135 |
return includePatterns; |
| 136 |
} |
| 137 |
|
| 138 |
protected void includePatterns(int includeFrom, int includeTo, int every) { |
| 139 |
if (includePatterns == null) { |
| 140 |
includePatterns = new int[includeTo - includeFrom]; |
| 141 |
} |
| 142 |
for (int i = includeFrom; i < includeTo; i += every) { |
| 143 |
int[] pattern = siteList.getPattern(i); |
| 144 |
int index = addAscertainmentPattern(pattern); |
| 145 |
includePatterns[ascertainmentIncludeCount] = index; |
| 146 |
ascertainmentIncludeCount += 1; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public int getExcludePatternCount() { |
| 151 |
return ascertainmentExcludeCount; |
| 152 |
} |
| 153 |
|
| 154 |
public int[] getExcludePatternIndices() { |
| 155 |
return excludePatterns; |
| 156 |
} |
| 157 |
|
| 158 |
protected void excludePatterns(int excludeFrom, int excludeTo, int every) { |
| 159 |
if (excludePatterns == null) |
| 160 |
excludePatterns = new int[excludeTo - excludeFrom]; |
| 161 |
|
| 162 |
for (int i = excludeFrom; i < excludeTo; i += every) { |
| 163 |
int[] pattern = siteList.getPattern(i); |
| 164 |
int index = addAscertainmentPattern(pattern); |
| 165 |
weights[index] = 0.0; |
| 166 |
excludePatterns[ascertainmentExcludeCount] = index; |
| 167 |
ascertainmentExcludeCount += 1; |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
public double getAscertainmentCorrection(double[] patternLogProbs) { |
| 173 |
double excludeProb = 0, includeProb = 0, returnProb = 1.0; |
| 174 |
|
| 175 |
int[] includeIndices = getIncludePatternIndices(); |
| 176 |
int[] excludeIndices = getExcludePatternIndices(); |
| 177 |
for (int i = 0; i < getIncludePatternCount(); i++) { |
| 178 |
int index = includeIndices[i]; |
| 179 |
includeProb += Math.exp(patternLogProbs[index]); |
| 180 |
} |
| 181 |
for (int j = 0; j < getExcludePatternCount(); j++) { |
| 182 |
int index = excludeIndices[j]; |
| 183 |
excludeProb += Math.exp(patternLogProbs[index]); |
| 184 |
} |
| 185 |
if (includeProb == 0.0) { |
| 186 |
returnProb -= excludeProb; |
| 187 |
} else if (excludeProb == 0.0) { |
| 188 |
returnProb = includeProb; |
| 189 |
} else { |
| 190 |
returnProb = includeProb - excludeProb; |
| 191 |
} |
| 192 |
return Math.log(returnProb); |
| 193 |
} |
| 194 |
|
| 195 |
private int addAscertainmentPattern(int[] pattern) { |
| 196 |
for (int i = 0; i < patternCount; i++) { |
| 197 |
if (comparePatterns(patterns[i], pattern)) { |
| 198 |
return i; |
| 199 |
} |
| 200 |
} |
| 201 |
int index = patternCount; |
| 202 |
patterns[index] = pattern; |
| 203 |
weights[index] = 0.0; |
| 204 |
patternCount++; |
| 205 |
|
| 206 |
return index; |
| 207 |
} |
| 208 |
} |