| 1 | /* |
| 2 | * IntervalList.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 | /** |
| 31 | * An interface for a set of coalescent intevals. |
| 32 | * |
| 33 | * @version $Id: IntervalList.java,v 1.7 2005/05/24 20:25:56 rambaut Exp $ |
| 34 | * |
| 35 | * @author Andrew Rambaut |
| 36 | * @author Alexei Drummond |
| 37 | */ |
| 38 | public interface IntervalList extends Units { |
| 39 | |
| 40 | /** |
| 41 | * get number of intervals |
| 42 | */ |
| 43 | int getIntervalCount(); |
| 44 | |
| 45 | /** |
| 46 | * get the total number of sampling events. |
| 47 | */ |
| 48 | int getSampleCount(); |
| 49 | |
| 50 | /** |
| 51 | * Gets an interval. |
| 52 | */ |
| 53 | double getInterval(int i); |
| 54 | |
| 55 | /** |
| 56 | * Returns the number of uncoalesced lineages within this interval. |
| 57 | * Required for s-coalescents, where new lineages are added as |
| 58 | * earlier samples are come across. |
| 59 | */ |
| 60 | int getLineageCount(int i); |
| 61 | |
| 62 | /** |
| 63 | * Returns the number coalescent events in an interval |
| 64 | */ |
| 65 | int getCoalescentEvents(int i); |
| 66 | |
| 67 | /** |
| 68 | * Returns the type of interval observed. |
| 69 | */ |
| 70 | IntervalType getIntervalType(int i); |
| 71 | |
| 72 | /** |
| 73 | * get the total duration of these intervals. |
| 74 | */ |
| 75 | double getTotalDuration(); |
| 76 | |
| 77 | /** |
| 78 | * Checks whether this set of coalescent intervals is fully resolved |
| 79 | * (i.e. whether is has exactly one coalescent event in each |
| 80 | * subsequent interval) |
| 81 | */ |
| 82 | boolean isBinaryCoalescent(); |
| 83 | |
| 84 | /** |
| 85 | * Checks whether this set of coalescent intervals coalescent only |
| 86 | * (i.e. whether is has exactly one or more coalescent event in each |
| 87 | * subsequent interval) |
| 88 | */ |
| 89 | boolean isCoalescentOnly(); |
| 90 | |
| 91 | |
| 92 | public class Utils { |
| 93 | |
| 94 | /** |
| 95 | * @return the number of lineages at time t. |
| 96 | * @param t the time that you are counting the number of lineages |
| 97 | */ |
| 98 | public static int getLineageCount(IntervalList intervals, double t) { |
| 99 | |
| 100 | int i = 0; |
| 101 | while (i < intervals.getIntervalCount() && t > intervals.getInterval(i)) { |
| 102 | t -= intervals.getInterval(i); |
| 103 | i+= 1; |
| 104 | } |
| 105 | if (i == intervals.getIntervalCount()) return 1; |
| 106 | return intervals.getLineageCount(i); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return the delta parameter of Pybus et al (Node spread statistic) |
| 111 | * @param intervals the intervals for which the delta parameter is calculated. |
| 112 | */ |
| 113 | public static double getDelta(IntervalList intervals) { |
| 114 | |
| 115 | // Assumes ultrametric tree! |
| 116 | if (!intervals.isCoalescentOnly()) { |
| 117 | throw new IllegalArgumentException("Assumes ultrametric tree!"); |
| 118 | } |
| 119 | |
| 120 | int n = intervals.getIntervalCount(); |
| 121 | |
| 122 | int numTips = n + 1; |
| 123 | |
| 124 | double transTreeDepth = 0.0; |
| 125 | double cumInts = 0.0; |
| 126 | double sum = 0.0; |
| 127 | |
| 128 | // transform intervals |
| 129 | for (int j=0; j<n; j++) { // move from tips to root |
| 130 | |
| 131 | double transInt = intervals.getInterval(j) * |
| 132 | dr.math.Binomial.choose2(intervals.getLineageCount(j)); // coalescent version |
| 133 | //intLenCopy[j] = getInterval(j)*getLineageCount(j); // birth-death version |
| 134 | |
| 135 | // don't include the last interval so put this before... |
| 136 | sum += cumInts; |
| 137 | |
| 138 | // ...incrementing the cumInts |
| 139 | cumInts += transInt; |
| 140 | |
| 141 | transTreeDepth += transInt; |
| 142 | } |
| 143 | |
| 144 | double halfTreeDepth = transTreeDepth / 2.0; |
| 145 | |
| 146 | sum *= (1.0/(numTips-2.0)); |
| 147 | double top = halfTreeDepth - sum; |
| 148 | double bottom = transTreeDepth * Math.sqrt((1.0/(12.0*(numTips-2.0)))); |
| 149 | |
| 150 | return (top / bottom); |
| 151 | } |
| 152 | } |
| 153 | } |