Debian Codegraph @

src/dr/app/beagle/tools/BeagleSequenceSimulator.java

raw symbols SCIP
1 /*
2 * BeagleSequenceSimulator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Filip Bielejec
48 * @version $Id$
49 *
50 */
51 public class BeagleSequenceSimulator {
52
53 // Constructor fields
54 private ArrayList<Partition> partitions;
55 private int siteCount;
56
57 // Alignment fields
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 // private boolean outputAncestralSequences = true;
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 // outputAncestralSequences = partition.isOutputAncestralSequences();
86 fieldsSet = true;
87 // System.err.println(dataType);
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 }// END: partitions loop
99
100 this.siteCount = siteCount + 1;
101 }// END: Constructor
102
103 public SimpleAlignment simulate(boolean parallel, boolean outputAncestralSequences) {
104
105 try {
106
107 // Executor for threads
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 // , partitionCount
126 ));
127 partitionCount++;
128
129 }// END: partitions loop
130
131 executor.invokeAll(simulatePartitionCallers);
132
133 // Wait until all threads are finished
134 executor.shutdown();
135 while (!executor.isTerminated()) {
136 }
137
138 alignment = compileAlignment();
139
140 } catch (Exception e) {
141 e.printStackTrace();
142 }// END: try-catch block
143
144 return alignment;
145 }// END: simulate
146
147 private class SimulatePartitionCallable implements Callable<Void> {
148
149 private Partition partition;
150 // private int partitionNumber;
151
152 private SimulatePartitionCallable(Partition partition
153 // , int partitionNumber
154 ) {
155 this.partition = partition;
156 // this.partitionNumber = partitionNumber;
157 }// END: Constructor
158
159 public Void call() {
160
161 try {
162
163 partition.simulatePartition();
164 // partitionSequencesMap.put(partitionNumber, partition.getSequenceMap());
165
166 } catch (Exception e) {
167 Utils.handleException(e);
168 }
169
170 return null;
171 }// END: call
172
173 }// END: SimulatePartitionCallable class
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 // compile the alignment
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 }// END: i loop
207
208 } else {
209
210 int[] sequence = new int[siteCount];
211 // dirty solution for gaps when taxa between the tree
212 // topologies don't match
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 }// END: i loop
222
223 alignmentMap.put(taxon, sequence);
224 }// END: key check
225
226 }// END: iterate seqMap
227
228 }// END: partitions loop
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 // sequence.setDataType(dataType);
245
246 simpleAlignment.addSequence(sequence);
247
248 iterator.remove();
249
250 }// END: while has next
251
252 return simpleAlignment;
253 }// END: compileAlignment
254
255 public LinkedHashMap<Integer, LinkedHashMap<NodeRef, int[]>> getPartitionSequencesMap() {
256 return partitionSequencesMap;
257 }//END: getPartitionSequencesMap
258
259 // private Sequence intArray2Sequence(Taxon taxon, int[] seq, int gapFlag) {
260 //
261 // StringBuilder sSeq = new StringBuilder();
262 //
263 // if (dataType instanceof Codons) {
264 //
265 // for (int i = 0; i < siteCount; i++) {
266 //
267 // int state = seq[i];
268 //
269 // if (state == gapFlag) {
270 // sSeq.append(dataType.getTriplet(dataType.getGapState()));
271 // } else {
272 // sSeq.append(dataType.getTriplet(seq[i]));
273 // }// END: gap check
274 //
275 // }// END: replications loop
276 //
277 // } else {
278 //
279 // for (int i = 0; i < siteCount; i++) {
280 //
281 // int state = seq[i];
282 //
283 // if (state == gapFlag) {
284 // sSeq.append(dataType.getCode(dataType.getGapState()));
285 // } else {
286 // sSeq.append(dataType.getCode(seq[i]));
287 // }// END: gap check
288 //
289 // }// END: replications loop
290 //
291 // }// END: dataType check
292 //
293 // return new Sequence(taxon, sSeq.toString());
294 // }// END: intArray2Sequence
295
296 } // END: class