| 1 |
package dr.evomodel.antigenic.phyloclustering.misc.obsolete; |
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
import dr.evolution.io.Importer; |
| 30 |
import dr.evolution.tree.FlexibleNode; |
| 31 |
import dr.evolution.tree.FlexibleTree; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.evolution.util.Taxon; |
| 34 |
import dr.evolution.util.TaxonList; |
| 35 |
|
| 36 |
import java.io.EOFException; |
| 37 |
import java.io.IOException; |
| 38 |
import java.io.Reader; |
| 39 |
import java.io.StringReader; |
| 40 |
import java.util.ArrayList; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class TiterImporter extends Importer { |
| 50 |
public static final String COMMENT = "comment"; |
| 51 |
|
| 52 |
public class BranchMissingException extends ImportException { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private static final long serialVersionUID = 777435104809244693L; |
| 57 |
|
| 58 |
public BranchMissingException() { |
| 59 |
super(); |
| 60 |
} |
| 61 |
|
| 62 |
public BranchMissingException(String msg) { |
| 63 |
super("Branch missing: " + msg); |
| 64 |
System.err.println(msg); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public TiterImporter(Reader reader) { |
| 72 |
super(reader); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public TiterImporter(String treeString) { |
| 79 |
this(new StringReader(treeString)); |
| 80 |
} |
| 81 |
|
| 82 |
private String virusStrain[]; |
| 83 |
private String transformed_titer[]; |
| 84 |
private String titer[]; |
| 85 |
|
| 86 |
public void readTiter(int numNodes) throws IOException, ImportException{ |
| 87 |
|
| 88 |
virusStrain = new String[numNodes]; |
| 89 |
transformed_titer = new String[numNodes]; |
| 90 |
titer = new String[numNodes]; |
| 91 |
|
| 92 |
readToken(); |
| 93 |
readToken(); |
| 94 |
readToken(); |
| 95 |
readToken(); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
for(int i=0; i < (numNodes); i++){ |
| 102 |
|
| 103 |
readInteger(); |
| 104 |
virusStrain[i] = readToken(); |
| 105 |
|
| 106 |
|
| 107 |
transformed_titer[i] = readToken(); |
| 108 |
|
| 109 |
|
| 110 |
try{ |
| 111 |
|
| 112 |
titer[i] = readLine(); |
| 113 |
|
| 114 |
} catch (EOFException e) { |
| 115 |
|
| 116 |
System.out.println("err"); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
public String getVirusStrain(int i) { |
| 125 |
return virusStrain[i]; |
| 126 |
} |
| 127 |
|
| 128 |
public String getTransformed_titer(int i) { |
| 129 |
return transformed_titer[i]; |
| 130 |
} |
| 131 |
|
| 132 |
public String getTiter(int i) { |
| 133 |
return titer[i]; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public Tree importTree(TaxonList taxonList) throws IOException, ImportException { |
| 140 |
setCommentDelimiters('[', ']', '\0', '\0', '&'); |
| 141 |
|
| 142 |
try { |
| 143 |
skipUntil("("); |
| 144 |
unreadCharacter('('); |
| 145 |
|
| 146 |
final FlexibleNode root = readInternalNode(taxonList); |
| 147 |
if (getLastMetaComment() != null) { |
| 148 |
root.setAttribute(COMMENT, getLastMetaComment()); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
return new FlexibleTree(root, false, true); |
| 155 |
|
| 156 |
} catch (EOFException e) { |
| 157 |
throw new ImportException("incomplete tree"); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException { |
| 165 |
boolean done = false; |
| 166 |
ArrayList<FlexibleTree> array = new ArrayList<FlexibleTree>(); |
| 167 |
|
| 168 |
do { |
| 169 |
|
| 170 |
try { |
| 171 |
|
| 172 |
skipUntil("("); |
| 173 |
unreadCharacter('('); |
| 174 |
|
| 175 |
FlexibleNode root = readInternalNode(taxonList); |
| 176 |
FlexibleTree tree = new FlexibleTree(root, false, true); |
| 177 |
array.add(tree); |
| 178 |
|
| 179 |
if (taxonList == null) { |
| 180 |
taxonList = tree; |
| 181 |
} |
| 182 |
|
| 183 |
if (readCharacter() != ';') { |
| 184 |
throw new BadFormatException("Expecting ';' after tree"); |
| 185 |
} |
| 186 |
|
| 187 |
} catch (EOFException e) { |
| 188 |
done = true; |
| 189 |
} |
| 190 |
} while (!done); |
| 191 |
|
| 192 |
Tree[] trees = new Tree[array.size()]; |
| 193 |
array.toArray(trees); |
| 194 |
|
| 195 |
return trees; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public boolean hasTree() throws IOException, ImportException { |
| 202 |
try { |
| 203 |
skipUntil("("); |
| 204 |
unreadCharacter('('); |
| 205 |
} catch (EOFException e) { |
| 206 |
lastTree = null; |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
private Tree lastTree = null; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public Tree importNextTree() throws IOException, ImportException { |
| 220 |
FlexibleTree tree = null; |
| 221 |
|
| 222 |
try { |
| 223 |
skipUntil("("); |
| 224 |
unreadCharacter('('); |
| 225 |
|
| 226 |
FlexibleNode root = readInternalNode(lastTree); |
| 227 |
|
| 228 |
tree = new FlexibleTree(root, false, true); |
| 229 |
|
| 230 |
} catch (EOFException e) { |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
lastTree = tree; |
| 235 |
|
| 236 |
return tree; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
private FlexibleNode readBranch(TaxonList taxonList) throws IOException, ImportException { |
| 245 |
double length = 0.0; |
| 246 |
FlexibleNode branch; |
| 247 |
|
| 248 |
if (nextCharacter() == '(') { |
| 249 |
|
| 250 |
branch = readInternalNode(taxonList); |
| 251 |
|
| 252 |
} else { |
| 253 |
|
| 254 |
branch = readExternalNode(taxonList); |
| 255 |
} |
| 256 |
|
| 257 |
final String comment = getLastMetaComment(); |
| 258 |
if (comment != null) { |
| 259 |
branch.setAttribute(COMMENT, comment); |
| 260 |
clearLastMetaComment(); |
| 261 |
} |
| 262 |
|
| 263 |
if (getLastDelimiter() == ':') { |
| 264 |
length = readDouble(",():;"); |
| 265 |
} |
| 266 |
|
| 267 |
branch.setLength(length); |
| 268 |
|
| 269 |
return branch; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
private FlexibleNode readInternalNode(TaxonList taxonList) throws IOException, ImportException { |
| 277 |
FlexibleNode node = new FlexibleNode(); |
| 278 |
|
| 279 |
|
| 280 |
final char ch = readCharacter(); |
| 281 |
assert ch == '('; |
| 282 |
|
| 283 |
|
| 284 |
node.addChild(readBranch(taxonList)); |
| 285 |
|
| 286 |
|
| 287 |
if (getLastDelimiter() != ',') { |
| 288 |
throw new BadFormatException("Expecting ',' in tree, but got '" + (char) getLastDelimiter() + "'"); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
do { |
| 293 |
node.addChild(readBranch(taxonList)); |
| 294 |
|
| 295 |
} while (getLastDelimiter() == ','); |
| 296 |
|
| 297 |
|
| 298 |
if (getLastDelimiter() != ')') { |
| 299 |
throw new BadFormatException("Missing closing ')' in tree"); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
try { |
| 304 |
String label = readToken(",():;"); |
| 305 |
if (label.length() > 0) { |
| 306 |
node.setAttribute("label", label); |
| 307 |
} |
| 308 |
} catch (IOException ioe) { |
| 309 |
|
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
return node; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException { |
| 320 |
FlexibleNode node = new FlexibleNode(); |
| 321 |
|
| 322 |
String label = readToken(":(),;"); |
| 323 |
|
| 324 |
Taxon taxon; |
| 325 |
|
| 326 |
if (taxonList != null) { |
| 327 |
|
| 328 |
int index = taxonList.getTaxonIndex(label); |
| 329 |
if (index != -1) { |
| 330 |
taxon = taxonList.getTaxon(index); |
| 331 |
} else { |
| 332 |
throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown"); |
| 333 |
} |
| 334 |
} else { |
| 335 |
|
| 336 |
taxon = new Taxon(label); |
| 337 |
} |
| 338 |
|
| 339 |
node.setTaxon(taxon); |
| 340 |
return node; |
| 341 |
} |
| 342 |
|
| 343 |
} |