Debian Codegraph @

src/dr/evolution/coalescent/Intervals.java

raw symbols SCIP
1 /*
2 * Intervals.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.evolution.coalescent;
27
28 import dr.evolution.util.Units;
29
30 import java.util.Arrays;
31
32 /**
33 * A concrete class for a set of coalescent intervals.
34 *
35 * @author Andrew Rambaut
36 * @author Alexei Drummond
37 * @version $Id: Intervals.java,v 1.19 2005/09/21 18:47:58 gerton Exp $
38 */
39 public class Intervals implements IntervalList {
40
41 public Intervals(int maxEventCount) {
42 events = new Event[maxEventCount];
43 for (int i = 0; i < maxEventCount; i++) {
44 events[i] = new Event();
45 }
46 eventCount = 0;
47 sampleCount = 0;
48
49 intervals = new double[maxEventCount - 1];
50 intervalTypes = new IntervalType[maxEventCount - 1];
51 lineageCounts = new int[maxEventCount - 1];
52
53 intervalsKnown = false;
54 }
55
56 public void copyIntervals(Intervals source) {
57 intervalsKnown = source.intervalsKnown;
58 eventCount = source.eventCount;
59 sampleCount = source.sampleCount;
60
61 //don't copy the actual events..
62 /*
63 for (int i = 0; i < events.length; i++) {
64 events[i].time = source.events[i].time;
65 events[i].type = source.events[i].type;
66 }*/
67
68 if (intervalsKnown) {
69 System.arraycopy(source.intervals, 0, intervals, 0, intervals.length);
70 System.arraycopy(source.intervalTypes, 0, intervalTypes, 0, intervals.length);
71 System.arraycopy(source.lineageCounts, 0, lineageCounts, 0, intervals.length);
72 }
73 }
74
75 public void resetEvents() {
76 intervalsKnown = false;
77 eventCount = 0;
78 sampleCount = 0;
79 }
80
81 public void addSampleEvent(double time) {
82 events[eventCount].time = time;
83 events[eventCount].type = IntervalType.SAMPLE;
84 eventCount++;
85 sampleCount++;
86 intervalsKnown = false;
87 }
88
89 public void addCoalescentEvent(double time) {
90 events[eventCount].time = time;
91 events[eventCount].type = IntervalType.COALESCENT;
92 eventCount++;
93 intervalsKnown = false;
94 }
95
96 public void addMigrationEvent(double time, int destination) {
97 events[eventCount].time = time;
98 events[eventCount].type = IntervalType.MIGRATION;
99 events[eventCount].info = destination;
100 eventCount++;
101 intervalsKnown = false;
102 }
103
104 public void addNothingEvent(double time) {
105 events[eventCount].time = time;
106 events[eventCount].type = IntervalType.NOTHING;
107 eventCount++;
108 intervalsKnown = false;
109 }
110
111 public int getSampleCount() {
112 return sampleCount;
113 }
114
115 public int getIntervalCount() {
116 if (!intervalsKnown) calculateIntervals();
117 return intervalCount;
118 }
119
120 public double getInterval(int i) {
121 if (!intervalsKnown) calculateIntervals();
122 return intervals[i];
123 }
124
125 public int getLineageCount(int i) {
126 if (!intervalsKnown) calculateIntervals();
127 return lineageCounts[i];
128 }
129
130 public int getCoalescentEvents(int i) {
131 if (!intervalsKnown) calculateIntervals();
132 if (i < intervalCount - 1) {
133 return lineageCounts[i] - lineageCounts[i + 1];
134 } else {
135 return lineageCounts[i] - 1;
136 }
137 }
138
139 public IntervalType getIntervalType(int i) {
140 if (!intervalsKnown) calculateIntervals();
141 return intervalTypes[i];
142 }
143
144 public double getTotalDuration() {
145
146 if (!intervalsKnown) calculateIntervals();
147 return events[eventCount - 1].time;
148 }
149
150 public boolean isBinaryCoalescent() {
151 return true;
152 }
153
154 public boolean isCoalescentOnly() {
155 return true;
156 }
157
158 private void calculateIntervals() {
159
160 if (eventCount < 2) {
161 throw new IllegalArgumentException("Too few events to construct intervals");
162 }
163
164 Arrays.sort(events, 0, eventCount);
165
166 if (events[0].type != IntervalType.SAMPLE) {
167 throw new IllegalArgumentException("First event is not a sample event");
168 }
169
170 intervalCount = eventCount - 1;
171
172 double lastTime = events[0].time;
173
174 int lineages = 1;
175 for (int i = 1; i < eventCount; i++) {
176
177 intervals[i - 1] = events[i].time - lastTime;
178 intervalTypes[i - 1] = events[i].type;
179 lineageCounts[i - 1] = lineages;
180 if (events[i].type == IntervalType.SAMPLE) {
181 lineages++;
182 } else if (events[i].type == IntervalType.COALESCENT) {
183 lineages--;
184 }
185 lastTime = events[i].time;
186 }
187 intervalsKnown = true;
188 }
189
190 private Units.Type units = Units.Type.GENERATIONS;
191
192 public final Units.Type getUnits() {
193 return units;
194 }
195
196 public final void setUnits(Type units) {
197 this.units = units;
198 }
199
200 private class Event implements Comparable {
201
202 public int compareTo(Object o) {
203 double t = ((Event) o).time;
204 if (t < time) {
205 return 1;
206 } else if (t > time) {
207 return -1;
208 } else {
209 // events are at exact same time so sort by type
210 return type.compareTo(((Event) o).type);
211 }
212 }
213
214 /**
215 * The type of event
216 */
217 IntervalType type;
218
219 /**
220 * The time of the event
221 */
222 double time;
223
224 /**
225 * Some extra information for the event (e.g., destination of a migration)
226 */
227 int info;
228
229 }
230
231 private Event[] events;
232 private int eventCount;
233 private int sampleCount;
234
235 private boolean intervalsKnown = false;
236 private double[] intervals;
237 private int[] lineageCounts;
238 private IntervalType[] intervalTypes;
239 //private int[] destinations;
240 private int intervalCount = 0;
241 }