| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.arg; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.evolution.util.TaxonList; |
| 30 |
import dr.util.Identifiable; |
| 31 |
|
| 32 |
import java.io.BufferedReader; |
| 33 |
import java.io.IOException; |
| 34 |
import java.io.Reader; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.StringTokenizer; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class ARGTrace implements Identifiable { |
| 47 |
|
| 48 |
public ARGTrace() { |
| 49 |
} |
| 50 |
|
| 51 |
public int getTreeCount(int burnin) { |
| 52 |
int startIndex = (burnin - minState) / stepSize; |
| 53 |
if (startIndex < 0) { |
| 54 |
startIndex = 0; |
| 55 |
} |
| 56 |
return args.size() - startIndex; |
| 57 |
} |
| 58 |
|
| 59 |
public ARGModel getARG(int index, int burnin) { |
| 60 |
int startIndex = (burnin - minState) / stepSize; |
| 61 |
if (startIndex < 0) { |
| 62 |
startIndex = 0; |
| 63 |
} |
| 64 |
return (ARGModel) args.get(index + startIndex); |
| 65 |
} |
| 66 |
|
| 67 |
public void add(ARGModel arg) { |
| 68 |
args.add(arg); |
| 69 |
} |
| 70 |
|
| 71 |
public void setMinimumState(int minState) { |
| 72 |
this.minState = minState; |
| 73 |
} |
| 74 |
|
| 75 |
public int getMinimumState() { |
| 76 |
return minState; |
| 77 |
} |
| 78 |
|
| 79 |
public void setStepSize(int stepSize) { |
| 80 |
this.stepSize = stepSize; |
| 81 |
} |
| 82 |
|
| 83 |
public int getStepSize() { |
| 84 |
return stepSize; |
| 85 |
} |
| 86 |
|
| 87 |
public int getMaximumState() { |
| 88 |
return (args.size() - 1) * stepSize + minState; |
| 89 |
} |
| 90 |
|
| 91 |
public String getId() { |
| 92 |
return id; |
| 93 |
} |
| 94 |
|
| 95 |
public void setId(String id) { |
| 96 |
this.id = id; |
| 97 |
} |
| 98 |
|
| 99 |
private ArrayList<ARGModel> args = new ArrayList<ARGModel>(); |
| 100 |
|
| 101 |
private int minState; |
| 102 |
private int stepSize; |
| 103 |
private String id; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static ARGTrace loadARGTrace(Reader r) throws IOException, Importer.ImportException { |
| 111 |
|
| 112 |
BufferedReader reader = new BufferedReader(r); |
| 113 |
|
| 114 |
ARGTrace trace = new ARGTrace(); |
| 115 |
|
| 116 |
dr.evolution.util.TaxonList taxonList = null; |
| 117 |
|
| 118 |
int minState = -1; |
| 119 |
int stepSize = 0; |
| 120 |
|
| 121 |
String line; |
| 122 |
ARGModel nullARG = new ARGModel(null, null, 0, 0); |
| 123 |
ArrayList<String> nameList = new ArrayList<String>(); |
| 124 |
|
| 125 |
taxonList = (TaxonList) nullARG; |
| 126 |
|
| 127 |
while ((line = reader.readLine()) != null) { |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
line.trim(); |
| 132 |
|
| 133 |
if (line.toUpperCase().startsWith("ARG")) { |
| 134 |
StringTokenizer st = new StringTokenizer(line, "="); |
| 135 |
nameList.add(st.nextToken()); |
| 136 |
ARGModel arg = nullARG.fromGraphStringCompressed(st.nextToken().trim()); |
| 137 |
trace.add(arg); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
if (nameList.size() < 2) { |
| 144 |
throw new Importer.ImportException("Less than two ARGs in the trace file"); |
| 145 |
} |
| 146 |
|
| 147 |
minState = getStateNumber(nameList.get(0)); |
| 148 |
stepSize = getStateNumber(nameList.get(1)) - minState; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
trace.setMinimumState(minState); |
| 204 |
trace.setStepSize(stepSize); |
| 205 |
|
| 206 |
return trace; |
| 207 |
} |
| 208 |
|
| 209 |
private final static int getStateNumber(String id) throws Importer.ImportException { |
| 210 |
try { |
| 211 |
return Integer.parseInt(id.substring(id.indexOf('_') + 1).trim()); |
| 212 |
} catch (NumberFormatException nfe) { |
| 213 |
throw new Importer.ImportException("Bad state number in tree label '" + id + "', the state must be preceded by an underscore(_)."); |
| 214 |
} |
| 215 |
} |
| 216 |
} |