quantixed / TrackMateR

Analysis of TrackMate XML outputs in R
https://quantixed.github.io/TrackMateR/
Other
12 stars 1 forks source link

Custom Trackmate XML fails on analysis after loading, Error in displacementXMat #12

Closed shk44 closed 1 year ago

shk44 commented 1 year ago

After loading my TrackMate .xml that contains track and spot info, trackmateR properly detects the XY resolution and the time window. It collects spot data, matches track data, and calculates distance without error. However, doing any sort of analysis apart from plot_tm_allTracks returns the following error:

Error in displacementXMat[a$frame, i] <- a$x : 
  number of items to replace is not a multiple of replacement length

I have properly loaded the TrackMateR vignette data and generated the report, so I think it's something to do with my xml file, uploaded below. Any help in deciphering and fixing this issue would be much appreciated. Thank you! cell5.zip

TrackMate version 7.10.2, ImageJ 1.54f, data obtained using a spinning disk confocal imaging at roughly 0.10 micron / px and 1 sec / frame for a 60 sec recording. The tracks are fairly sparse and the track calling and definitely be optimized, but this was just an initial test.

Session Info:

R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.4.2    XML_3.99-0.14    foreach_1.5.2    TrackMateR_0.3.8

loaded via a namespace (and not attached):
 [1] gtable_0.3.3      htmlwidgets_1.6.2 devtools_2.4.5    remotes_2.4.2     processx_3.8.2    lattice_0.21-8    callr_3.7.3      
 [8] vctrs_0.6.2       tools_4.3.0       ps_1.7.5          generics_0.1.3    curl_5.0.0        parallel_4.3.0    tibble_3.2.1     
[15] fansi_1.0.4       pkgconfig_2.0.3   Matrix_1.5-4      desc_1.4.2        lifecycle_1.0.3   compiler_4.3.0    farver_2.1.1     
[22] stringr_1.5.0     munsell_0.5.0     ggforce_0.4.1     codetools_0.2-19  httpuv_1.6.11     htmltools_0.5.5   usethis_2.2.2    
[29] later_1.3.1       pillar_1.9.0      crayon_1.5.2      urlchecker_1.0.1  MASS_7.3-59       ellipsis_0.3.2    cachem_1.0.7     
[36] sessioninfo_1.2.2 iterators_1.0.14  nlme_3.1-162      mime_0.12         parallelly_1.36.0 tidyselect_1.2.0  digest_0.6.31    
[43] stringi_1.7.12    reshape2_1.4.4    dplyr_1.1.2       purrr_1.0.1       splines_4.3.0     labeling_0.4.2    polyclip_1.10-4  
[50] rprojroot_2.0.3   fastmap_1.1.1     grid_4.3.0        colorspace_2.1-0  cli_3.6.1         magrittr_2.0.3    patchwork_1.1.2  
[57] pkgbuild_1.4.2    utf8_1.2.3        withr_2.5.0       prettyunits_1.1.1 scales_1.2.1      promises_1.2.0.1  zoo_1.8-12       
[64] memoise_2.0.1     shiny_1.7.4.1     doParallel_1.0.17 miniUI_0.1.1.1    mgcv_1.8-42       profvis_0.3.8     rlang_1.1.0      
[71] Rcpp_1.0.11       xtable_1.8-4      glue_1.6.2        tweenr_2.0.2      pkgload_1.3.2.1   rstudioapi_0.14   plyr_1.8.8       
[78] R6_2.5.1          fs_1.6.2 
quantixed commented 1 year ago

Hi @shk44, thank you for the report (especially with all the detail and the file).

I think the problem is with the tracking result XML file. If I do:

library(TrackMateR)
library(ggplot2)
xmlPath <- file.choose()
tmObj <- readTrackMateXML(XMLpath = xmlPath)
  Units are:  0.1083333 micron and 1.318351 s 
  Collecting spot data. Using 20 cores
  Matching track data...
  Calculating distances...
# error here
reportDataset(tmObj)
Error in displacementXMat[a$frame, i] <- a$x : 
  number of items to replace is not a multiple of replacement length
# extract the data to data frame
tmDF <- tmobj[[1]]
View(tmDF)

Then I see that the first track (trace = "0") has three points at frame 0, three points at frame 1 etc. Other tracks also have multiple points in the same frame - each track should have only one point in each frame. I have checked the XML file and sure enough, TrackMate has made tracks with multiple points.

This is why TrackMateR is failing to process the file. It is reading the file correctly, but the error is in the file. So I think the solution will be to improve the tracking side.

shk44 commented 1 year ago

Thank you for the quick response. I tried using TrackMate's advanced Kalman filter as my spots are mainly moving in linear directions. Are there specific Tracking algorithms that work better with TrackMateR, or is it something off with my trackmate settings that cause the multiple spots per frame per track issue? For novices like me, some initial guidelines to what would work well with TrackMateR would be very helpful, though I understand it may not be in the immediate purview of maintaining the package.

Edit: I simply changed the tracking algorithm to the simple Kalman's tracking one over the advanced one and that solved the issue. I am now able to analyze the files since I have only one spot per track per frame. Thank you!

quantixed commented 1 year ago

TrackMateR is written with the assumption that there are single tracks. Looking at your file a bit more closely, I notice Allow Track Splitting and Allow Track Merging are set to TRUE. I suspect these settings could cause the multiple spots per frame per track issue. I don't know what you are tracking but if the spots are not dividing and merging, then probably unchecking these options is best and it might solve the problem.

The only other thing to try would be to update TrackMate as it is now on v 7.11.1 so the problem we're encountering here might be dealt with already.

There's no preferred tracking algorithms for TrackMateR. It's written to be able to take the TrackMate XML output (2D only) regardless of the detection and tracking. However, I haven't tested it with every possible output, so am prepared to fix any peculiarities that crop up.

As I was typing, I saw your edit. Glad that you have fixed it. Give a thumbs up if OK and I will close the issue.

shk44 commented 1 year ago

Ultimately, having merge and splitting options available would be important for my downstream analysis. However, there are many other interesting questions in the mean time I can use the package for that disregard splitting and merging, so I think you've addressed my issue. Thanks again for your hard work!