Debian Codegraph @

src/dr/evolution/coalescent/structure/ColouredTreeIntervals.java

raw symbols SCIP
1 /*
2 * ColouredTreeIntervals.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.structure;
27
28 import dr.evolution.coalescent.IntervalType;
29 import dr.evolution.colouring.BranchColouring;
30 import dr.evolution.colouring.TreeColouring;
31 import dr.evolution.tree.ColourChange;
32 import dr.evolution.tree.NodeRef;
33 import dr.evolution.tree.Tree;
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38
39 /**
40 * @author Alexei Drummond
41 * @version $Id: ColouredTreeIntervals.java,v 1.9 2005/12/08 13:48:41 rambaut Exp $
42 */
43 public class ColouredTreeIntervals implements StructuredIntervalList {
44
45 int intervalCount;
46 int sampleCount;
47 List<Event> eventList = new ArrayList<Event>();
48 int colourStateCount;
49 int[][] lineageCount;
50 Type units;
51
52 public ColouredTreeIntervals(Tree tree, TreeColouring colouring) {
53
54 colourStateCount = colouring.getColourCount();
55 units = tree.getUnits();
56 extractCoalescentEvents(tree, colouring, eventList);
57 sampleCount = extractSampleEvents(tree, colouring, eventList);
58 extractMigrationEvents(tree, colouring, eventList);
59 Collections.sort(eventList);
60
61 lineageCount = new int[eventList.size() + 1][colourStateCount];
62 int externalNodeCount = tree.getExternalNodeCount();
63 for (int i = 0; i < externalNodeCount; i++) {
64 NodeRef node = tree.getExternalNode(i);
65 double time = tree.getNodeHeight(node);
66 int colour = colouring.getNodeColour(node);
67 if (time == 0.0) {
68 lineageCount[0][colour] += 1;
69 }
70 }
71 for (int i = 0; i < eventList.size(); i++) {
72 Event event = eventList.get(i);
73 //System.out.println(event);
74
75 for (int j = 0; j < colourStateCount; j++) {
76 lineageCount[i + 1][j] = lineageCount[i][j] + event.lineageChanges[j];
77 if (lineageCount[i + 1][j] < 0) {
78 throw new RuntimeException("lineageCount[" + (i + 1) + "][" + j + "] = " + lineageCount[i + 1][j] + ". This is wrong!");
79 }
80 }
81 }
82 intervalCount = eventList.size();
83 }
84
85 public int getPopulationCount() {
86 return lineageCount[0].length;
87 }
88
89 /**
90 * get number of intervals
91 */
92 public int getIntervalCount() {
93 return intervalCount;
94 }
95
96 /**
97 * get the total number of sampling events.
98 */
99 public int getSampleCount() {
100 return sampleCount;
101 }
102
103 /**
104 * Gets an interval.
105 */
106 public double getInterval(int i) {
107 if (i == 0) return getEvent(i).time;
108 return getEvent(i).time - getEvent(i - 1).time;
109 }
110
111 /**
112 * @param population the population of interest
113 * @return the number of lineages residing in the given population over this interval.
114 */
115 public int getLineageCount(int interval, int population) {
116 return lineageCount[interval][population];
117 }
118
119 /**
120 * @param interval the interval of interest
121 * @return the number of lineages residing in all populations over this interval.
122 */
123 public int getLineageCount(int interval) {
124 int totalLineages = 0;
125 for (int i = 0; i < colourStateCount; i++) {
126 totalLineages += lineageCount[interval][i];
127 }
128 return totalLineages;
129 }
130
131 /**
132 * Returns the number coalescent events in an interval
133 */
134 public int getCoalescentEvents(int i) {
135 if (getEvent(i).getType() == IntervalType.COALESCENT) return 1;
136 return 0;
137 }
138
139 /**
140 * Returns the type of interval observed.
141 */
142 public IntervalType getIntervalType(int i) {
143 return getEvent(i).getType();
144 }
145
146 /**
147 * get the total duration of these intervals.
148 */
149 public double getTotalDuration() {
150 return getEvent(getIntervalCount()).time;
151 }
152
153 /**
154 * Checks whether this set of coalescent intervals is fully resolved
155 * (i.e. whether is has exactly one coalescent event in each
156 * subsequent interval)
157 */
158 public boolean isBinaryCoalescent() {
159 return true;
160 }
161
162 /**
163 * Checks whether this set of coalescent intervals coalescent only
164 * (i.e. whether is has exactly one or more coalescent event in each
165 * subsequent interval)
166 */
167 public boolean isCoalescentOnly() {
168 return false;
169 }
170
171 /**
172 * Gets the units for this object.
173 */
174 public Type getUnits() {
175 return units;
176 }
177
178 /**
179 * Sets the units for this object.
180 */
181 public void setUnits(Type units) {
182 this.units = units;
183 }
184
185 private void extractCoalescentEvents(Tree tree, TreeColouring colouring, List<Event> eventList) {
186 int internalNodeCount = tree.getInternalNodeCount();
187 for (int i = 0; i < internalNodeCount; i++) {
188 NodeRef node = tree.getInternalNode(i);
189 double time = tree.getNodeHeight(node);
190 int colour = colouring.getNodeColour(node);
191 eventList.add(Event.createCoalescentEvent(time, colour, colouring.getColourCount()));
192 }
193 }
194
195 private int extractSampleEvents(Tree tree, TreeColouring colouring, List<Event> eventList) {
196 int externalNodeCount = tree.getExternalNodeCount();
197 int sampleEventCount = 0;
198 for (int i = 0; i < externalNodeCount; i++) {
199 NodeRef node = tree.getExternalNode(i);
200 double time = tree.getNodeHeight(node);
201 int colour = colouring.getNodeColour(node);
202 if (time != 0.0) {
203 eventList.add(Event.createAddSampleEvent(time, colour, colouring.getColourCount()));
204 sampleEventCount += 1;
205 }
206 }
207 return sampleEventCount;
208 }
209
210 private void extractMigrationEvents(Tree tree, TreeColouring colouring, List<Event> eventList) {
211 int nodeCount = tree.getNodeCount();
212 for (int i = 0; i < nodeCount; i++) {
213 NodeRef node = tree.getNode(i);
214 if (!tree.isRoot(node)) {
215
216 BranchColouring branchColouring = colouring.getBranchColouring(node);
217 List<ColourChange> changes = branchColouring.getColourChanges();
218 int belowColour = colouring.getNodeColour(node);
219 for (ColourChange change : changes) {
220
221 double time = change.getTime();
222 int aboveColour = change.getColourAbove();
223 eventList.add(Event.createMigrationEvent(time, belowColour, aboveColour, colouring.getColourCount()));
224 belowColour = aboveColour;
225 }
226 }
227 }
228 }
229
230 public final Event getEvent(int i) {
231 return eventList.get(i);
232 }
233
234 }