| 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.treedatalikelihood.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.math.matrixAlgebra.Vector; |
| 30 |
|
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class PartiallyMissingInformation { |
| 39 |
|
| 40 |
public PartiallyMissingInformation(Tree tree, ContinuousTraitPartialsProvider dataModel, |
| 41 |
ContinuousDataLikelihoodDelegate likelihoodDelegate) { |
| 42 |
this.tipCount = tree.getExternalNodeCount(); |
| 43 |
this.numTraits = dataModel.getTraitCount(); |
| 44 |
this.dimTrait = dataModel.getTraitDimension(); |
| 45 |
|
| 46 |
this.rawMissingIndices = dataModel.getMissingIndices(); |
| 47 |
|
| 48 |
final int length = tipCount * numTraits; |
| 49 |
anyMissing = new boolean[length]; |
| 50 |
allMissing = new boolean[length]; |
| 51 |
missingIndices = new HashedIntArray[length]; |
| 52 |
|
| 53 |
setupIndices(); |
| 54 |
} |
| 55 |
|
| 56 |
public boolean isPartiallyMissing(final int tip, final int trait) { |
| 57 |
return anyMissing[getIndex(tip, trait)]; |
| 58 |
} |
| 59 |
|
| 60 |
public boolean isCompletelyMissing(final int tip, final int trait) { |
| 61 |
return allMissing[getIndex(tip, trait)]; |
| 62 |
} |
| 63 |
|
| 64 |
public HashedIntArray getMissingIndices(final int tip, final int trait) { |
| 65 |
return missingIndices[getIndex(tip, trait)]; |
| 66 |
} |
| 67 |
|
| 68 |
private void setupIndices() { |
| 69 |
|
| 70 |
for (int taxon = 0; taxon < tipCount; ++taxon) { |
| 71 |
|
| 72 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 73 |
|
| 74 |
final int index = getIndex(taxon, trait); |
| 75 |
|
| 76 |
int count = 0; |
| 77 |
for (int dim = 0; dim < dimTrait; ++dim) { |
| 78 |
if (isObservationMissing(index, dim)) { |
| 79 |
++count; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (count > 0) { |
| 84 |
|
| 85 |
int[] missing = new int[count]; |
| 86 |
int[] notMissing = new int[dimTrait - count]; |
| 87 |
|
| 88 |
int offsetMissing = 0; |
| 89 |
int offsetNotMissing = 0; |
| 90 |
for (int dim = 0; dim < dimTrait; ++dim) { |
| 91 |
if (isObservationMissing(index, dim)) { |
| 92 |
missing[offsetMissing] = dim; |
| 93 |
++offsetMissing; |
| 94 |
} else { |
| 95 |
notMissing[offsetNotMissing] = dim; |
| 96 |
++offsetNotMissing; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
anyMissing[index] = true; |
| 101 |
missingIndices[index] = new HashedIntArray(missing, notMissing); |
| 102 |
|
| 103 |
if (count == dimTrait) { |
| 104 |
allMissing[index] = true; |
| 105 |
} |
| 106 |
} else { |
| 107 |
anyMissing[index] = false; |
| 108 |
allMissing[index] = false; |
| 109 |
missingIndices[index] = null; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private int getIndex(final int tip, final int trait) { |
| 116 |
return tip * numTraits + trait; |
| 117 |
} |
| 118 |
|
| 119 |
private boolean isObservationMissing(final int index, final int dim) { |
| 120 |
final int id = index * dimTrait + dim; |
| 121 |
return rawMissingIndices.contains(id); |
| 122 |
} |
| 123 |
|
| 124 |
public class HashedIntArray { |
| 125 |
final private int[] array; |
| 126 |
final private int[] complement; |
| 127 |
|
| 128 |
HashedIntArray(final int[] array, final int[] complement) { |
| 129 |
this.array = array; |
| 130 |
this.complement = complement; |
| 131 |
} |
| 132 |
|
| 133 |
public int[] getArray() { |
| 134 |
return array; |
| 135 |
} |
| 136 |
|
| 137 |
public int[] getComplement() { |
| 138 |
return complement; |
| 139 |
} |
| 140 |
|
| 141 |
public int get(int index) { |
| 142 |
return array[index]; |
| 143 |
} |
| 144 |
|
| 145 |
public int getLength() { |
| 146 |
return array.length; |
| 147 |
} |
| 148 |
|
| 149 |
public int getComplementLength() { |
| 150 |
return complement.length; |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public int hashCode() { |
| 155 |
return Arrays.hashCode(array); |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public boolean equals(Object obj) { |
| 160 |
if (obj instanceof int[]) { |
| 161 |
return Arrays.equals(array, (int[]) obj); |
| 162 |
} else { |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
public String toString() { |
| 168 |
return new Vector(array).toString(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
final private int tipCount; |
| 173 |
final private int numTraits; |
| 174 |
final private int dimTrait; |
| 175 |
|
| 176 |
final private List<Integer> rawMissingIndices; |
| 177 |
|
| 178 |
final private boolean[] anyMissing; |
| 179 |
final private boolean[] allMissing; |
| 180 |
final private HashedIntArray[] missingIndices; |
| 181 |
} |
| 182 |
|