| 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.app.beagle.tools; |
| 27 |
|
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Arrays; |
| 30 |
import java.util.Iterator; |
| 31 |
import java.util.LinkedHashMap; |
| 32 |
import java.util.List; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.Map.Entry; |
| 35 |
import java.util.concurrent.Callable; |
| 36 |
import java.util.concurrent.ExecutorService; |
| 37 |
import java.util.concurrent.Executors; |
| 38 |
|
| 39 |
import dr.app.bss.Utils; |
| 40 |
import dr.evolution.alignment.SimpleAlignment; |
| 41 |
import dr.evolution.datatype.DataType; |
| 42 |
import dr.evolution.sequence.Sequence; |
| 43 |
import dr.evolution.tree.NodeRef; |
| 44 |
import dr.evolution.util.Taxon; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class BeagleSequenceSimulator { |
| 52 |
|
| 53 |
|
| 54 |
private ArrayList<Partition> partitions; |
| 55 |
private int siteCount; |
| 56 |
|
| 57 |
|
| 58 |
public static final int gapFlag = Integer.MAX_VALUE; |
| 59 |
private SimpleAlignment alignment; |
| 60 |
private DataType dataType; |
| 61 |
private boolean fieldsSet = false; |
| 62 |
LinkedHashMap<Integer, LinkedHashMap<NodeRef, int[]>> partitionSequencesMap; |
| 63 |
|
| 64 |
|
| 65 |
public BeagleSequenceSimulator(ArrayList<Partition> partitions) { |
| 66 |
|
| 67 |
this.partitions = partitions; |
| 68 |
this.alignment = new SimpleAlignment(); |
| 69 |
alignment.setReportCountStatistics(false); |
| 70 |
|
| 71 |
partitionSequencesMap = new LinkedHashMap<Integer, LinkedHashMap<NodeRef,int[]>>(); |
| 72 |
|
| 73 |
int siteCount = 0; |
| 74 |
int to = 0; |
| 75 |
for (Partition partition : partitions) { |
| 76 |
|
| 77 |
to = partition.to; |
| 78 |
if (to > siteCount) { |
| 79 |
siteCount = to; |
| 80 |
} |
| 81 |
|
| 82 |
if (!fieldsSet) { |
| 83 |
|
| 84 |
dataType = partition.getDataType(); |
| 85 |
|
| 86 |
fieldsSet = true; |
| 87 |
|
| 88 |
|
| 89 |
} else { |
| 90 |
|
| 91 |
if (dataType.getType() != partition.getDataType().getType()) { |
| 92 |
throw new RuntimeException( |
| 93 |
"Partitions must have the same data type."); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
this.siteCount = siteCount + 1; |
| 101 |
} |
| 102 |
|
| 103 |
public SimpleAlignment simulate(boolean parallel, boolean outputAncestralSequences) { |
| 104 |
|
| 105 |
try { |
| 106 |
|
| 107 |
|
| 108 |
int NTHREDS = 1; |
| 109 |
if (parallel) { |
| 110 |
NTHREDS = Math.min(partitions.size(), Runtime.getRuntime() |
| 111 |
.availableProcessors()); |
| 112 |
} |
| 113 |
|
| 114 |
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS); |
| 115 |
List<Callable<Void>> simulatePartitionCallers = new ArrayList<Callable<Void>>(); |
| 116 |
|
| 117 |
int partitionCount = 0; |
| 118 |
for (Partition partition : partitions) { |
| 119 |
|
| 120 |
partition.setPartitionNumber(partitionCount); |
| 121 |
partition.setOutputAncestralSequences(outputAncestralSequences); |
| 122 |
|
| 123 |
simulatePartitionCallers.add(new SimulatePartitionCallable( |
| 124 |
partition |
| 125 |
|
| 126 |
)); |
| 127 |
partitionCount++; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
executor.invokeAll(simulatePartitionCallers); |
| 132 |
|
| 133 |
|
| 134 |
executor.shutdown(); |
| 135 |
while (!executor.isTerminated()) { |
| 136 |
} |
| 137 |
|
| 138 |
alignment = compileAlignment(); |
| 139 |
|
| 140 |
} catch (Exception e) { |
| 141 |
e.printStackTrace(); |
| 142 |
} |
| 143 |
|
| 144 |
return alignment; |
| 145 |
} |
| 146 |
|
| 147 |
private class SimulatePartitionCallable implements Callable<Void> { |
| 148 |
|
| 149 |
private Partition partition; |
| 150 |
|
| 151 |
|
| 152 |
private SimulatePartitionCallable(Partition partition |
| 153 |
|
| 154 |
) { |
| 155 |
this.partition = partition; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
public Void call() { |
| 160 |
|
| 161 |
try { |
| 162 |
|
| 163 |
partition.simulatePartition(); |
| 164 |
|
| 165 |
|
| 166 |
} catch (Exception e) { |
| 167 |
Utils.handleException(e); |
| 168 |
} |
| 169 |
|
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
private SimpleAlignment compileAlignment() { |
| 176 |
|
| 177 |
SimpleAlignment simpleAlignment = new SimpleAlignment(); |
| 178 |
simpleAlignment.setReportCountStatistics(false); |
| 179 |
simpleAlignment.setDataType(dataType); |
| 180 |
|
| 181 |
LinkedHashMap<Taxon, int[]> alignmentMap = new LinkedHashMap<Taxon, int[]>(); |
| 182 |
|
| 183 |
|
| 184 |
for (Partition partition : partitions) { |
| 185 |
|
| 186 |
Map<Taxon, int[]> sequenceMap = partition.getTaxonSequencesMap(); |
| 187 |
Iterator<Entry<Taxon, int[]>> iterator = sequenceMap.entrySet() |
| 188 |
.iterator(); |
| 189 |
|
| 190 |
while (iterator.hasNext()) { |
| 191 |
|
| 192 |
Entry<Taxon, int[]> pairs = (Entry<Taxon, int[]>) iterator |
| 193 |
.next(); |
| 194 |
|
| 195 |
Taxon taxon = pairs.getKey(); |
| 196 |
int[] partitionSequence = pairs.getValue(); |
| 197 |
|
| 198 |
if (alignmentMap.containsKey(taxon)) { |
| 199 |
|
| 200 |
int j = 0; |
| 201 |
for (int i = partition.from; i <= partition.to; i += partition.every) { |
| 202 |
|
| 203 |
alignmentMap.get(taxon)[i] = partitionSequence[j]; |
| 204 |
j++; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} else { |
| 209 |
|
| 210 |
int[] sequence = new int[siteCount]; |
| 211 |
|
| 212 |
|
| 213 |
Arrays.fill(sequence, gapFlag); |
| 214 |
|
| 215 |
int j = 0; |
| 216 |
for (int i = partition.from; i <= partition.to; i += partition.every) { |
| 217 |
|
| 218 |
sequence[i] = partitionSequence[j]; |
| 219 |
j++; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
alignmentMap.put(taxon, sequence); |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
Iterator<Entry<Taxon, int[]>> iterator = alignmentMap.entrySet() |
| 231 |
.iterator(); |
| 232 |
while (iterator.hasNext()) { |
| 233 |
|
| 234 |
Entry<Taxon, int[]> pairs = (Entry<Taxon, int[]>) iterator.next(); |
| 235 |
Taxon taxon = (Taxon) pairs.getKey(); |
| 236 |
int[] intSequence = (int[]) pairs.getValue(); |
| 237 |
|
| 238 |
Sequence sequence = Utils.intArray2Sequence(taxon, |
| 239 |
intSequence, |
| 240 |
gapFlag, |
| 241 |
dataType |
| 242 |
); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
simpleAlignment.addSequence(sequence); |
| 247 |
|
| 248 |
iterator.remove(); |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
return simpleAlignment; |
| 253 |
} |
| 254 |
|
| 255 |
public LinkedHashMap<Integer, LinkedHashMap<NodeRef, int[]>> getPartitionSequencesMap() { |
| 256 |
return partitionSequencesMap; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
} |