drieslab / Giotto

Spatial omics analysis toolbox
https://drieslab.github.io/Giotto_website/
Other
258 stars 98 forks source link

Unable to save Giotto object using saveGiotto #486

Closed acjordan333 closed 1 year ago

acjordan333 commented 1 year ago

Hi,

Thanks so much for creating an excellent package to analyze digital-spatial profiling data. I've been working with the tutorial to create a Giotto object from NanoString CosMx data and I've nearly succeeded, but I've been unable to save the object using the saveGiotto function. When I try to run the following code:

saveGiotto(gobject = fov_join_1_5, foldername = "saveGiottoDir", dir = getwd(), method = "RDS", overwrite = TRUE)

I was expecting it to save the object in a structured directory, but instead, I get this error:

  1. Start writing feature information For feature: rna Warning: .shp file is unreadable, or corrupt. (GDAL error 1)Error: [writeVector] Layer creation failed

It seems there is a GDAL error, but I am not sure how to approach this. I tried saving as a .RDS file, but as your tutorial pointed out, that then leads to errors when the pointers don't function. Do you know of any solutions to this issue?

Thanks for your help.

jiajic commented 1 year ago

Hi @acjordan333,

I am afraid that we have not come across this error yet when running saveGiotto(). Just as a point of reference, I checked my GDAL version using the terminal with gdalinfo --version and found that GDAL 3.4.3, released 2022/04/22 has been working for me when saving. Another guess could be memory issues since this data is large.

It's experimental, but you can also try the following as another option for saving the object as a single .RDS

functions to source

wrap_gpoly = function(x) {
  x@spatVector = terra::wrap(x@spatVector)
  x@spatVectorCentroids = terra::wrap(x@spatVectorCentroids)
  x@overlaps = lapply(x@overlaps, function(sv) {
    if(inherits(sv, 'SpatVector')) {
      terra::wrap(sv)
    } else {
      sv
    }
  })
  return(x)
}  

wrap_gpoints = function(x) {
  x@spatVector = terra::wrap(x@spatVector)
  return(x)
}

wrap_giotto = function(x) {
  x@spatial_info = lapply(x@spatial_info, wrap_gpoly)
  x@feat_info = lapply(x@feat_info, wrap_gpoints)
  return(x)
}

unwrap_gpoly = function(x) {
  x@spatVector = terra::unwrap(x@spatVector)
  x@spatVectorCentroids = terra::unwrap(x@spatVectorCentroids)
  x@overlaps = lapply(x@overlaps, function(sv) {
    if(inherits(sv, 'PackedSpatVector')) {
      terra::unwrap(sv)
    } else {
      sv
    }
  })
  return(x)
}

unwrap_gpoints = function(x) {
  x@spatVector = terra::unwrap(x@spatVector)
  return(x)
}

unwrap_giotto = function(x) {
  x@spatial_info = lapply(x@spatial_info, unwrap_gpoly)
  x@feat_info = lapply(x@feat_info, unwrap_gpoints)
  return(x)
}

wrapping and saving

gobject = wrap_giotto(gobject)
saveRDS(gobject, file = '???')

loading, unwrapping, and reconnecting images

gobject = readRDS('???')
gobject = unwrap_giotto(gobject)
gobject = reconnectGiottoImage(gobject)

reconnectGiottoImage() reloads the pointer information from the associated filepaths (if any) when the image objects were created, so this isn't a self-contained solution for images right now.

acjordan333 commented 1 year ago

Ok, thanks so much for these suggestions. I will try them out and get back to you!

swbioinf commented 1 year ago

I've noticed I get this error if I try to save to a directory using the ~ shortcut (or if I typo the dir parameter to something that doesn't exist).

Whereas it works ok with my full home directory path.

Using Giotto 3.2 (giotto suite).

> saveGiotto(go, dir = "~/temp",foldername = "gosave2", overwrite = T)

  1. Start writing feature information For feature: rna Error: [writeVector] Layer creation failed In addition: Warning message: Failed to create file ~/temp/gosave2/Features/rna_feature_spatVector.shp: No such file or directory (GDAL error 1)

UPDATE: Apologies, I realised my error is slightly different. Will leave up in case anyone else hits this though.

RubD commented 1 year ago

@swbioinf thanks for the feedback and this should be easy to patch up by expanding to a full path expand.path() and by checking if the directory exist dir.exists(). We will update the saveGiotto() soon.

acjordan333 commented 1 year ago

I was able to save the object when I changed the file path. I think the file path was too long. Thanks everyone for your help.

RubD commented 1 year ago

Ok, thanks for the update. We have also added path.expand() to the suite_dev branch.