| 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.evomodel.tipstatesmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.Nucleotides; |
| 29 |
import dr.evolution.util.TaxonList; |
| 30 |
import dr.evomodelxml.tipstatesmodel.SequenceErrorModelParser; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.inference.model.Statistic; |
| 33 |
import dr.util.Author; |
| 34 |
import dr.util.Citable; |
| 35 |
import dr.util.Citation; |
| 36 |
|
| 37 |
import java.util.Arrays; |
| 38 |
import java.util.Collections; |
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class SequenceErrorModel extends TipStatesModel implements Citable { |
| 53 |
public enum ErrorType { |
| 54 |
TYPE_1_TRANSITIONS("type1Transitions"), |
| 55 |
TYPE_2_TRANSITIONS("type2Transitions"), |
| 56 |
TRANSITIONS_ONLY("transitionsOnly"), |
| 57 |
ALL_SUBSTITUTIONS("allSubstitutions"); |
| 58 |
|
| 59 |
|
| 60 |
ErrorType(String label) { |
| 61 |
this.label = label; |
| 62 |
} |
| 63 |
|
| 64 |
public String toString() { |
| 65 |
return label; |
| 66 |
} |
| 67 |
|
| 68 |
final String label; |
| 69 |
} |
| 70 |
|
| 71 |
public SequenceErrorModel(TaxonList includeTaxa, TaxonList excludeTaxa, |
| 72 |
ErrorType errorType, Parameter baseErrorRateParameter, |
| 73 |
Parameter ageRelatedErrorRateParameter, |
| 74 |
Parameter indicatorParameter) { |
| 75 |
super(SequenceErrorModelParser.SEQUENCE_ERROR_MODEL, includeTaxa, excludeTaxa); |
| 76 |
|
| 77 |
this.errorType = errorType; |
| 78 |
|
| 79 |
if (baseErrorRateParameter != null) { |
| 80 |
this.baseErrorRateParameter = baseErrorRateParameter; |
| 81 |
addVariable(this.baseErrorRateParameter); |
| 82 |
} else { |
| 83 |
this.baseErrorRateParameter = null; |
| 84 |
} |
| 85 |
|
| 86 |
if (ageRelatedErrorRateParameter != null) { |
| 87 |
this.ageRelatedErrorRateParameter = ageRelatedErrorRateParameter; |
| 88 |
addVariable(ageRelatedErrorRateParameter); |
| 89 |
} else { |
| 90 |
this.ageRelatedErrorRateParameter = null; |
| 91 |
} |
| 92 |
|
| 93 |
if (indicatorParameter != null) { |
| 94 |
this.indicatorParameter = indicatorParameter; |
| 95 |
addVariable(indicatorParameter); |
| 96 |
} else { |
| 97 |
this.indicatorParameter = null; |
| 98 |
} |
| 99 |
|
| 100 |
if (indicatorParameter != null) { |
| 101 |
addStatistic(new TaxonHasErrorsStatistic()); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
protected void taxaChanged() { |
| 106 |
if (indicatorParameter != null && indicatorParameter.getDimension() <= 1) { |
| 107 |
this.indicatorParameter.setDimension(tree.getExternalNodeCount()); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public Type getModelType() { |
| 113 |
return Type.PARTIALS; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void getTipStates(int nodeIndex, int[] tipStates) { |
| 118 |
throw new IllegalArgumentException("This model emits only tip partials"); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void getTipPartials(int nodeIndex, double[] partials) { |
| 123 |
|
| 124 |
int[] states = this.states[nodeIndex]; |
| 125 |
if (indicatorParameter == null || indicatorParameter.getParameterValue(nodeIndex) > 0.0) { |
| 126 |
|
| 127 |
double pUndamaged = 1.0; |
| 128 |
double pDamagedTS = 0.0; |
| 129 |
double pDamagedTV = 0.0; |
| 130 |
|
| 131 |
if (!excluded[nodeIndex]) { |
| 132 |
if (baseErrorRateParameter != null) { |
| 133 |
pUndamaged = pUndamaged - baseErrorRateParameter.getParameterValue(0); |
| 134 |
} |
| 135 |
|
| 136 |
if (ageRelatedErrorRateParameter != null) { |
| 137 |
double rate = ageRelatedErrorRateParameter.getParameterValue(0); |
| 138 |
double age = tree.getNodeHeight(tree.getExternalNode(nodeIndex)); |
| 139 |
pUndamaged *= Math.exp(-rate * age); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if (errorType == ErrorType.ALL_SUBSTITUTIONS) { |
| 144 |
pDamagedTS = (1.0 - pUndamaged) / 3.0; |
| 145 |
pDamagedTV = pDamagedTS; |
| 146 |
|
| 147 |
} else if (errorType == ErrorType.TRANSITIONS_ONLY) { |
| 148 |
pDamagedTS = 1.0 - pUndamaged; |
| 149 |
pDamagedTV = 0.0; |
| 150 |
} else { |
| 151 |
throw new IllegalArgumentException("only TRANSITIONS_ONLY and ALL_SUBSTITUTIONS are supported"); |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
int k = 0; |
| 157 |
for (int j = 0; j < patternCount; j++) { |
| 158 |
switch (states[j]) { |
| 159 |
case Nucleotides.A_STATE: |
| 160 |
partials[k] = pUndamaged; |
| 161 |
partials[k + 1] = pDamagedTV; |
| 162 |
partials[k + 2] = pDamagedTS; |
| 163 |
partials[k + 3] = pDamagedTV; |
| 164 |
break; |
| 165 |
case Nucleotides.C_STATE: |
| 166 |
partials[k] = pDamagedTV; |
| 167 |
partials[k + 1] = pUndamaged; |
| 168 |
partials[k + 2] = pDamagedTV; |
| 169 |
partials[k + 3] = pDamagedTS; |
| 170 |
break; |
| 171 |
case Nucleotides.G_STATE: |
| 172 |
partials[k] = pDamagedTS; |
| 173 |
partials[k + 1] = pDamagedTV; |
| 174 |
partials[k + 2] = pUndamaged; |
| 175 |
partials[k + 3] = pDamagedTV; |
| 176 |
break; |
| 177 |
case Nucleotides.UT_STATE: |
| 178 |
partials[k] = pDamagedTV; |
| 179 |
partials[k + 1] = pDamagedTS; |
| 180 |
partials[k + 2] = pDamagedTV; |
| 181 |
partials[k + 3] = pUndamaged; |
| 182 |
break; |
| 183 |
default: |
| 184 |
partials[k] = 1.0; |
| 185 |
partials[k + 1] = 1.0; |
| 186 |
partials[k + 2] = 1.0; |
| 187 |
partials[k + 3] = 1.0; |
| 188 |
} |
| 189 |
k += stateCount; |
| 190 |
} |
| 191 |
} else { |
| 192 |
int k = 0; |
| 193 |
for (int j = 0; j < patternCount; j++) { |
| 194 |
|
| 195 |
switch (states[j]) { |
| 196 |
case Nucleotides.A_STATE: |
| 197 |
partials[k] = 1.0; |
| 198 |
partials[k + 1] = 0.0; |
| 199 |
partials[k + 2] = 0.0; |
| 200 |
partials[k + 3] = 0.0; |
| 201 |
break; |
| 202 |
case Nucleotides.C_STATE: |
| 203 |
partials[k] = 0.0; |
| 204 |
partials[k + 1] = 1.0; |
| 205 |
partials[k + 2] = 0.0; |
| 206 |
partials[k + 3] = 0.0; |
| 207 |
break; |
| 208 |
case Nucleotides.G_STATE: |
| 209 |
partials[k] = 0.0; |
| 210 |
partials[k + 1] = 0.0; |
| 211 |
partials[k + 2] = 1.0; |
| 212 |
partials[k + 3] = 0.0; |
| 213 |
break; |
| 214 |
case Nucleotides.UT_STATE: |
| 215 |
partials[k] = 0.0; |
| 216 |
partials[k + 1] = 0.0; |
| 217 |
partials[k + 2] = 0.0; |
| 218 |
partials[k + 3] = 1.0; |
| 219 |
break; |
| 220 |
default: |
| 221 |
partials[k] = 1.0; |
| 222 |
partials[k + 1] = 1.0; |
| 223 |
partials[k + 2] = 1.0; |
| 224 |
partials[k + 3] = 1.0; |
| 225 |
} |
| 226 |
|
| 227 |
k += stateCount; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
public class TaxonHasErrorsStatistic extends Statistic.Abstract { |
| 233 |
|
| 234 |
public TaxonHasErrorsStatistic() { |
| 235 |
super("hasErrors"); |
| 236 |
} |
| 237 |
|
| 238 |
public int getDimension() { |
| 239 |
if (indicatorParameter == null) return 0; |
| 240 |
return indicatorParameter.getDimension(); |
| 241 |
} |
| 242 |
|
| 243 |
public String getDimensionName(int dim) { |
| 244 |
return taxonMap.get(dim); |
| 245 |
} |
| 246 |
|
| 247 |
public double getStatisticValue(int dim) { |
| 248 |
return indicatorParameter.getParameterValue(dim); |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
private final ErrorType errorType; |
| 255 |
private final Parameter baseErrorRateParameter; |
| 256 |
private final Parameter ageRelatedErrorRateParameter; |
| 257 |
private final Parameter indicatorParameter; |
| 258 |
|
| 259 |
@Override |
| 260 |
public Citation.Category getCategory() { |
| 261 |
return Citation.Category.SUBSTITUTION_MODELS; |
| 262 |
} |
| 263 |
|
| 264 |
@Override |
| 265 |
public String getDescription() { |
| 266 |
return "Sequence error model"; |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
public List<Citation> getCitations() { |
| 271 |
return Arrays.asList(new Citation( |
| 272 |
new Author[]{ |
| 273 |
new Author("A", "Rambaut"), |
| 274 |
new Author("SYW", "Ho"), |
| 275 |
new Author("AJ", "Drummond"), |
| 276 |
new Author("B", "Shapiro"), |
| 277 |
}, |
| 278 |
"Accommodating the effect of ancient DNA damage on inferences of demographic histories", |
| 279 |
2008, |
| 280 |
"Mol Biol Evol", |
| 281 |
26, |
| 282 |
245, 248, |
| 283 |
"10.1093/molbev/msn256" |
| 284 |
), |
| 285 |
new Citation( |
| 286 |
new Author[]{ |
| 287 |
new Author("J", "Felsenstein"), |
| 288 |
}, |
| 289 |
"Inferring Phylogenies", |
| 290 |
2004, |
| 291 |
"Sinauer Associates", |
| 292 |
"" |
| 293 |
)); |
| 294 |
} |
| 295 |
} |