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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestPhyloXMLTreeReadWrite.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkAbstractArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkInformation.h"
#include "vtkInformationIterator.h"
#include "vtkInformationStringKey.h"
#include "vtkNew.h"
#include "vtkPhyloXMLTreeReader.h"
#include "vtkPhyloXMLTreeWriter.h"
#include "vtkTesting.h"
#include "vtkTestUtilities.h"
#include "vtkTree.h"
#include "vtkUnsignedCharArray.h"
//----------------------------------------------------------------------------
bool VerifyArrayValue(vtkTree *tree, vtkIdType index, const char* arrayName,
const char* baseline)
{
vtkAbstractArray *array =
tree->GetVertexData()->GetAbstractArray(arrayName);
if (!array)
{
cout << "could not find " << arrayName << endl;
return false;
}
std::string value = array->GetVariantValue(index).ToString();
if (value.compare(baseline) != 0)
{
cout << "value for " << arrayName << " is " << value << ", should be "
<< baseline << endl;
return false;
}
return true;
}
//----------------------------------------------------------------------------
bool VerifyArrayAttribute(vtkTree *tree, const char *arrayName,
const char *attributeName, const char *baseline)
{
vtkAbstractArray *array =
tree->GetVertexData()->GetAbstractArray(arrayName);
if (!array)
{
cout << "could not find " << arrayName << endl;
return false;
}
vtkInformation *info = array->GetInformation();
vtkNew<vtkInformationIterator> infoItr;
infoItr->SetInformation(info);
for (infoItr->InitTraversal(); !infoItr->IsDoneWithTraversal();
infoItr->GoToNextItem())
{
vtkInformationStringKey* key =
vtkInformationStringKey::SafeDownCast(infoItr->GetCurrentKey());
if (strcmp(key->GetName(), attributeName) == 0)
{
std::string value = info->Get(key);
if (value.compare(baseline) == 0)
{
return true;
}
else
{
cout << "found " << value << " for " << arrayName << "'s "
<< attributeName << " attribute. Expected " << baseline << endl;
return false;
}
}
}
cout << "could not find " << attributeName << " for " << arrayName << endl;
return false;
}
//----------------------------------------------------------------------------
bool VerifyColor(vtkTree *tree, vtkIdType vertex, unsigned char r, unsigned char g, unsigned char b)
{
vtkUnsignedCharArray *array = vtkUnsignedCharArray::SafeDownCast(
tree->GetVertexData()->GetAbstractArray("color"));
if (!array)
{
cout << "could not find color array" << endl;
return false;
}
if (array->GetNumberOfComponents() != 3)
{
cout << "color array does not have 3 components" << endl;
}
double *color = array->GetTuple3(vertex);
if (color[0] != r)
{
cout << "red value " << color[0] << " found for vertex " << vertex
<< ". Should be " << r << endl;
return false;
}
if (color[1] != g)
{
cout << "green value " << color[1] << " found for vertex " << vertex
<< ". Should be " << g << endl;
return false;
}
if (color[2] != b)
{
cout << "blue value " << color[2] << " found for vertex " << vertex
<< ". Should be " << b << endl;
return false;
}
return true;
}
//----------------------------------------------------------------------------
int TestPhyloXMLTreeReadWrite(int argc, char* argv[])
{
// get the full path to the input file
char* inputFile = vtkTestUtilities::ExpandDataFileName(
argc, argv, "Data/Infovis/XML/example_phylo.xml");
cout << "reading from a file: "<< inputFile << endl;
// read the input file into a vtkTree
vtkNew<vtkPhyloXMLTreeReader> reader;
reader->SetFileName(inputFile);
reader->Update();
vtkTree *tree = reader->GetOutput();
delete[] inputFile;
// time to verify that the tree was read correctly.
// 1: it exists
if (!tree)
{
cout << "No output tree" << endl;
return EXIT_FAILURE;
}
// 2: it has the right number of vertices
vtkIdType numVertices = tree->GetNumberOfVertices();
if (numVertices != 6)
{
cout << "tree has " << numVertices << " vertices (should be 6)." << endl;
return EXIT_FAILURE;
}
// 3: its topology seems correct
int numChildren[6] = {1, 2, 2, 0, 0, 0};
for (vtkIdType vertex = 0; vertex < 6; ++vertex)
{
if (tree->GetNumberOfChildren(vertex) != numChildren[vertex])
{
cout << "incorrect number of children for vertex " << vertex
<< "should be " << numChildren[vertex] << ", found "
<< tree->GetNumberOfChildren(vertex) << endl;
return EXIT_FAILURE;
}
}
// 4: verify vertex data
// tree-level data
if (!VerifyArrayValue(tree, 0, "phylogeny.name", "example tree"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 0, "phylogeny.description",
"example tree to test PhyloXML reader and writer"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 0, "phylogeny.confidence", "0.99"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "phylogeny.confidence", "type",
"probability"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 0, "phylogeny.property.length", "1"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "phylogeny.property.length", "authority",
"NOAA"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "phylogeny.property.length", "applies_to",
"phylogeny"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "phylogeny.property.length", "unit",
"METRIC:m"))
{
return EXIT_FAILURE;
}
// vertex names
if (!VerifyArrayValue(tree, 0, "node name", "root"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 1, "node name", "internalOne"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 2, "node name", "internalTwo"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 3, "node name", "a"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 4, "node name", "b"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 5, "node name", "c"))
{
return EXIT_FAILURE;
}
// vertex confidence
if (!VerifyArrayValue(tree, 0, "confidence", "0.95"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 1, "confidence", "0.9"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 2, "confidence", "0.85"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 3, "confidence", "0.8"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 4, "confidence", "0.75"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 5, "confidence", "0.85"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "confidence", "type", "probability"))
{
return EXIT_FAILURE;
}
// vertex length (custom property)
if (!VerifyArrayValue(tree, 0, "property.length", "0"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 1, "property.length", "2"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 2, "property.length", "3"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 3, "property.length", "4"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 4, "property.length", "5"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayValue(tree, 5, "property.length", "6"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "property.length", "authority", "NOAA"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "property.length", "applies_to", "clade"))
{
return EXIT_FAILURE;
}
if (!VerifyArrayAttribute(tree, "property.length", "unit", "METRIC:m"))
{
return EXIT_FAILURE;
}
// color
if (!VerifyColor(tree, 0, 0, 0, 0))
{
return EXIT_FAILURE;
}
if (!VerifyColor(tree, 1, 0, 0, 0))
{
return EXIT_FAILURE;
}
if (!VerifyColor(tree, 2, 255, 0, 0))
{
return EXIT_FAILURE;
}
if (!VerifyColor(tree, 3, 255, 0, 0))
{
return EXIT_FAILURE;
}
if (!VerifyColor(tree, 4, 0, 255, 0))
{
return EXIT_FAILURE;
}
if (!VerifyColor(tree, 5, 0, 0, 255))
{
return EXIT_FAILURE;
}
// 5: EdgeData (just weights for now)
double weights[5] = {1.0, 2.0, 1.0, 1.0, 3.0};
vtkAbstractArray *weightArray =
tree->GetEdgeData()->GetAbstractArray("weight");
if (!weightArray)
{
cout << "could not find weight array" << endl;
return EXIT_FAILURE;
}
for (vtkIdType edge = 0; edge < tree->GetNumberOfEdges(); ++edge)
{
double value = weightArray->GetVariantValue(edge).ToDouble();
if (value != weights[edge])
{
cout << "weight " << value << " found for edge #" << edge
<< ", expected " << weights[edge] << endl;
return EXIT_FAILURE;
}
}
// end of tree verification.
// next step:
// write this vtkTree out to to a string in PhyloXML format
vtkNew<vtkPhyloXMLTreeWriter> writer;
writer->SetInputData(tree);
writer->SetWriteToOutputString(1);
writer->IgnoreArray("node weight");
writer->Update();
std::string phyloXML = writer->GetOutputString();
// recreate a vtkTree from this PhyloXML string.
vtkNew<vtkPhyloXMLTreeReader> reader2;
reader2->SetReadFromInputString(1);
reader2->SetInputString(phyloXML);
reader2->Update();
vtkTree *tree2 = reader2->GetOutput();
// write it back out to PhyloXML again and verify that it is
// identical to our previous PhyloXML string.
vtkNew<vtkPhyloXMLTreeWriter> writer2;
writer2->SetInputData(tree2);
writer2->SetWriteToOutputString(1);
writer2->IgnoreArray("node weight");
writer2->Update();
std::string phyloXML2 = writer2->GetOutputString();
if (phyloXML.compare(phyloXML2) != 0)
{
cout << "output strings do not match." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|