Debian Codegraph @

src/dr/evomodel/epidemiology/casetocase/AbstractOutbreak.java

raw symbols SCIP
1 /*
2 * AbstractOutbreak.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.evomodel.epidemiology.casetocase;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.datatype.DataType;
30 import dr.evolution.datatype.GeneralDataType;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.TaxonList;
34 import dr.inference.model.AbstractModel;
35 import dr.inference.model.Parameter;
36
37 import java.util.*;
38
39 /**
40 * Abstract class for outbreaks. Implements PatternList for ease of compatibility with AbstractTreeLikelihood,
41 * but there is one and only one pattern.
42 *
43 * User: Matthew Hall
44 * Date: 14/04/13
45 */
46
47 public abstract class AbstractOutbreak extends AbstractModel implements PatternList {
48
49 protected GeneralDataType caseDataType;
50 protected TaxonList taxa;
51 private boolean hasLatentPeriods;
52 protected final boolean hasGeography;
53 private final String CASE_NAME = "hostID";
54 protected ArrayList<AbstractCase> cases;
55 protected int infectedSize = 0;
56
57 public AbstractOutbreak(String name, Taxa taxa){
58 this(name, taxa, false, true);
59 }
60
61 public AbstractOutbreak(String name, Taxa taxa, boolean hasLatentPeriods, boolean hasGeography){
62 super(name);
63 this.taxa = taxa;
64 ArrayList<String> caseNames = new ArrayList<String>();
65 for(int i=0; i<taxa.getTaxonCount(); i++){
66 caseNames.add((String)taxa.getTaxonAttribute(i, CASE_NAME));
67 }
68 caseDataType = new GeneralDataType(caseNames);
69 this.hasLatentPeriods = hasLatentPeriods;
70 this.hasGeography = hasGeography;
71 }
72
73 public ArrayList<AbstractCase> getCases(){
74 return new ArrayList<AbstractCase>(cases);
75 }
76
77 public boolean hasLatentPeriods() {
78 return hasLatentPeriods;
79 }
80
81 public boolean hasGeography(){
82 return hasGeography;
83 }
84
85 // todo this should be in terms of arbitary distance functions, not kernels
86
87 public abstract double getLatentPeriod(AbstractCase aCase);
88
89 public double getKernelValue(AbstractCase a, AbstractCase b, SpatialKernel kernel){
90 if(!hasGeography){
91 return 1;
92 } else {
93 return kernel.value(getDistance(a,b));
94 }
95 }
96
97 // all the kernel values going TO case a (this is symmetric, usually, but potentially might not be)
98
99 public double[] getKernelValues(AbstractCase aCase, SpatialKernel kernel){
100 double[] out = new double[cases.size()];
101 for(int i=0; i<out.length; i++){
102 out[i] = kernel.value(getDistance(aCase, cases.get(i)));
103 }
104 return out;
105 }
106
107 public int getCaseIndex(AbstractCase thisCase){
108 return cases.indexOf(thisCase);
109 }
110
111 public int size(){
112 return cases.size();
113 }
114
115 public int infectedSize(){
116 return infectedSize;
117 }
118
119 public abstract double getDistance(AbstractCase a, AbstractCase b);
120
121 public AbstractCase getCase(int i){
122 return cases.get(i);
123 }
124
125 public AbstractCase getCase(String name){
126 for(AbstractCase thisCase: cases){
127 if(thisCase.getName().equals(name)){
128 return thisCase;
129 }
130 }
131 return null;
132 }
133
134 public TaxonList getTaxa(){
135 return taxa;
136 }
137
138
139 //************************************************************************
140 // PatternList implementation
141 //************************************************************************
142
143 // not considering the possibility that we are simultaneously reconstructing more than one transmission tree!
144
145 public int getPatternCount(){
146 return 1;
147 }
148
149 public int getStateCount(){
150 return size();
151 }
152
153 public int getPatternLength(){
154 return taxa.getTaxonCount();
155 }
156
157 // @todo if these are never going to be used, get them to throw exceptions
158
159 public int[] getPattern(int patternIndex){
160 int[] out = new int[cases.size()];
161 for(int i=0; i<cases.size(); i++){
162 out[i] = i;
163 }
164 return out;
165 }
166
167 @Override
168 public double[][] getUncertainPattern(int patternIndex) {
169 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
170 }
171
172 public int getPatternState(int taxonIndex, int patternIndex){
173 return taxonIndex;
174 }
175
176 @Override
177 public double[] getUncertainPatternState(int taxonIndex, int patternIndex) {
178 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
179 }
180
181 public double getPatternWeight(int patternIndex){
182 return 1;
183 }
184
185 public double[] getPatternWeights(){
186 return new double[]{1};
187 }
188
189 public double[] getStateFrequencies(){
190 double[] out = new double[cases.size()];
191 Arrays.fill(out, 1/cases.size());
192 return out;
193 }
194
195 @Override
196 public boolean areUnique() {
197 return false;
198 }
199
200 @Override
201 public boolean areUncertain() {
202 return false;
203 }
204
205 public DataType getDataType(){
206 return caseDataType;
207 }
208
209 //************************************************************************
210 // TaxonList implementation
211 //************************************************************************
212
213 public int getTaxonCount(){
214 return taxa.getTaxonCount();
215 }
216
217 public Taxon getTaxon(int taxonIndex){
218 return taxa.getTaxon(taxonIndex);
219 }
220
221 public String getTaxonId(int taxonIndex){
222 return taxa.getTaxonId(taxonIndex);
223 }
224
225 public int getTaxonIndex(String id){
226 return taxa.getTaxonIndex(id);
227 }
228
229 public int getTaxonIndex(Taxon taxon){
230 return taxa.getTaxonIndex(taxon);
231 }
232
233 public List<Taxon> asList(){
234 return taxa.asList();
235 }
236
237 public Object getTaxonAttribute(int taxonIndex, String name){
238 return taxa.getTaxonAttribute(taxonIndex, name);
239 }
240
241 public Iterator<Taxon> iterator() {
242 if (taxa == null) throw new RuntimeException("Patterns has no TaxonList");
243 return taxa.iterator();
244 }
245
246 }