|
| 1 | +test_that("readTrackMateXML handles non-existent file gracefully", { |
| 2 | + res <- tryCatch(readTrackMateXML("nonexistent.xml"), error = function(e) e) |
| 3 | + expect_true(is.null(res) || inherits(res, "error")) |
| 4 | +}) |
| 5 | + |
| 6 | +test_that("readTrackMateXML handles wrong file suffix", { |
| 7 | + tmp <- tempfile(fileext = ".csv") |
| 8 | + file.create(tmp) |
| 9 | + on.exit(unlink(tmp)) |
| 10 | + |
| 11 | + res <- tryCatch(readTrackMateXML(tmp), error = function(e) e) |
| 12 | + expect_true(is.null(res) || inherits(res, "error")) |
| 13 | +}) |
| 14 | + |
| 15 | +test_that("readTrackMateXML handles XML with no tracks", { |
| 16 | + xml_content <- '<?xml version="1.0" encoding="UTF-8"?> |
| 17 | + <TrackMate> |
| 18 | + <Model spatialunits="pixel" timeunits="sec"/> |
| 19 | + <AllSpots/> |
| 20 | + </TrackMate>' |
| 21 | + |
| 22 | + tmp <- tempfile(fileext = ".xml") |
| 23 | + writeLines(xml_content, tmp) |
| 24 | + on.exit(unlink(tmp)) |
| 25 | + |
| 26 | + res <- tryCatch(readTrackMateXML(tmp), error = function(e) e) |
| 27 | + expect_true(is.null(res) || inherits(res, "error")) |
| 28 | +}) |
| 29 | + |
| 30 | +test_that("readTrackMateXML handles XML with no filtered tracks", { |
| 31 | + xml_content <- '<?xml version="1.0" encoding="UTF-8"?> |
| 32 | + <TrackMate> |
| 33 | + <Model spatialunits="pixel" timeunits="sec"/> |
| 34 | + <AllSpots/> |
| 35 | + <AllTracks> |
| 36 | + <Track TRACK_ID="0"/> |
| 37 | + </AllTracks> |
| 38 | + <FilteredTracks/> |
| 39 | + </TrackMate>' |
| 40 | + |
| 41 | + tmp <- tempfile(fileext = ".xml") |
| 42 | + writeLines(xml_content, tmp) |
| 43 | + on.exit(unlink(tmp)) |
| 44 | + |
| 45 | + res <- tryCatch(readTrackMateXML(tmp), error = function(e) e) |
| 46 | + expect_true(is.null(res) || inherits(res, "error")) |
| 47 | +}) |
| 48 | + |
| 49 | +test_that("readTrackMateXML parses valid XML correctly", { |
| 50 | + xml_content <- '<?xml version="1.0" encoding="UTF-8"?> |
| 51 | + <TrackMate> |
| 52 | + <Model spatialunits="micron" timeunits="sec"/> |
| 53 | + <AllSpots> |
| 54 | + <SpotsInFrame frame="0"> |
| 55 | + <Spot ID="1" POSITION_X="10.0" POSITION_Y="20.0" POSITION_Z="0.0" POSITION_T="0.0" FRAME="0" RADIUS="2.5" QUALITY="100"/> |
| 56 | + <Spot ID="2" POSITION_X="15.0" POSITION_Y="25.0" POSITION_Z="0.0" POSITION_T="0.5" FRAME="1" RADIUS="2.5" QUALITY="100"/> |
| 57 | + </SpotsInFrame> |
| 58 | + </AllSpots> |
| 59 | + <AllTracks> |
| 60 | + <Track TRACK_ID="0"> |
| 61 | + <Edge SPOT_SOURCE_ID="1" SPOT_TARGET_ID="2"/> |
| 62 | + </Track> |
| 63 | + </AllTracks> |
| 64 | + <FilteredTracks> |
| 65 | + <TrackID TRACK_ID="0"/> |
| 66 | + </FilteredTracks> |
| 67 | + </TrackMate>' |
| 68 | + |
| 69 | + tmp <- tempfile(fileext = ".xml") |
| 70 | + writeLines(xml_content, tmp) |
| 71 | + on.exit(unlink(tmp)) |
| 72 | + |
| 73 | + res <- tryCatch(readTrackMateXML(tmp), error = function(e) { skip(paste("readTrackMateXML failed:", e$message)) }) |
| 74 | + |
| 75 | + # result is expected to be a list: first element the data frame, second calibration |
| 76 | + expect_true(is.list(res)) |
| 77 | + result <- res[[1]] |
| 78 | + expect_true(is.data.frame(result)) |
| 79 | + expect_true(all(c("track", "frame", "x", "y", "z", "time") %in% names(result))) |
| 80 | +}) |
| 81 | + |
| 82 | +test_that("readTrackMateXML only includes filtered tracks", { |
| 83 | + xml_content <- '<?xml version="1.0" encoding="UTF-8"?> |
| 84 | + <TrackMate> |
| 85 | + <Model spatialunits="micron" timeunits="sec"/> |
| 86 | + <AllSpots> |
| 87 | + <SpotsInFrame frame="0"> |
| 88 | + <Spot ID="1" POSITION_X="10.0" POSITION_Y="20.0" POSITION_Z="0.0" POSITION_T="0.0" FRAME="0"/> |
| 89 | + <Spot ID="2" POSITION_X="15.0" POSITION_Y="25.0" POSITION_Z="0.0" POSITION_T="1.0" FRAME="1"/> |
| 90 | + <Spot ID="3" POSITION_X="100.0" POSITION_Y="200.0" POSITION_Z="0.0" POSITION_T="0.0" FRAME="0"/> |
| 91 | + <Spot ID="4" POSITION_X="105.0" POSITION_Y="205.0" POSITION_Z="0.0" POSITION_T="1.0" FRAME="1"/> |
| 92 | + </SpotsInFrame> |
| 93 | + </AllSpots> |
| 94 | + <AllTracks> |
| 95 | + <Track TRACK_ID="0"> |
| 96 | + <Edge SPOT_SOURCE_ID="1" SPOT_TARGET_ID="2"/> |
| 97 | + </Track> |
| 98 | + <Track TRACK_ID="1"> |
| 99 | + <Edge SPOT_SOURCE_ID="3" SPOT_TARGET_ID="4"/> |
| 100 | + </Track> |
| 101 | + </AllTracks> |
| 102 | + <FilteredTracks> |
| 103 | + <TrackID TRACK_ID="0"/> |
| 104 | + </FilteredTracks> |
| 105 | + </TrackMate>' |
| 106 | + |
| 107 | + tmp <- tempfile(fileext = ".xml") |
| 108 | + writeLines(xml_content, tmp) |
| 109 | + on.exit(unlink(tmp)) |
| 110 | + |
| 111 | + res <- tryCatch(readTrackMateXML(tmp), error = function(e) { skip(paste("readTrackMateXML failed:", e$message)) }) |
| 112 | + result <- res[[1]] |
| 113 | + |
| 114 | + expect_equal(nrow(result), 2) |
| 115 | + expect_equal(nlevels(result$track), 1) |
| 116 | + expect_equal(as.character(unique(result$track)), "0") |
| 117 | + expect_false(any(result$x > 50)) |
| 118 | +}) |
| 119 | + |
| 120 | +test_that("readTrackMateXML warns on duplicate track-frame combinations", { |
| 121 | + xml_content <- '<?xml version="1.0" encoding="UTF-8"?> |
| 122 | + <TrackMate> |
| 123 | + <Model spatialunits="micron" timeunits="sec"/> |
| 124 | + <AllSpots> |
| 125 | + <SpotsInFrame frame="0"> |
| 126 | + <Spot ID="1" POSITION_X="10.0" POSITION_Y="20.0" POSITION_Z="0.0" POSITION_T="0.0" FRAME="0"/> |
| 127 | + <Spot ID="2" POSITION_X="15.0" POSITION_Y="25.0" POSITION_Z="0.0" POSITION_T="0.0" FRAME="0"/> |
| 128 | + </SpotsInFrame> |
| 129 | + </AllSpots> |
| 130 | + <AllTracks> |
| 131 | + <Track TRACK_ID="0"> |
| 132 | + <Edge SPOT_SOURCE_ID="1" SPOT_TARGET_ID="2"/> |
| 133 | + </Track> |
| 134 | + </AllTracks> |
| 135 | + <FilteredTracks> |
| 136 | + <TrackID TRACK_ID="0"/> |
| 137 | + </FilteredTracks> |
| 138 | + </TrackMate>' |
| 139 | + |
| 140 | + tmp <- tempfile(fileext = ".xml") |
| 141 | + writeLines(xml_content, tmp) |
| 142 | + on.exit(unlink(tmp)) |
| 143 | + |
| 144 | + res <- tryCatch(readTrackMateXML(tmp), error = function(e) { skip(paste("readTrackMateXML failed:", e$message)) }) |
| 145 | + expect_output(invisible(res <- readTrackMateXML(tmp)), "duplicate") |
| 146 | +}) |
0 commit comments