Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An empty giottoPoints object can corrupt the giotto obj #946

Open
rbutleriii opened this issue Apr 25, 2024 · 5 comments
Open

An empty giottoPoints object can corrupt the giotto obj #946

rbutleriii opened this issue Apr 25, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@rbutleriii
Copy link

Noting this here for the future, I am getting errors with a newer set of CosMx slides that had missing neg_probe information. And I think that is leading to the following innocuous error when you write the object to file:

...
For image information: fov063-composite
For image information: fov063-overlay
For image information: fov063-compartment
Warning message:
[writeVector] nothing to write 
TIME                
57:35               

The feature_info:

For Feature info:  rna 

An object of class giottoPoints
feat_type : "rna"
Feature Information:
 class       : SpatVector 
 geometry    : points 
 dimensions  : 7645205, 5  (geometries, attributes)
 extent      : -485706, -464985.1, 8295.25, 29619.7  (xmin, xmax, ymin, ymax)
 coord. ref. :  
 names       : feat_ID     z   fov CellComp feat_ID_uniq
 type        :   <chr> <int> <int>    <chr>        <chr>
 values      : Ndufa13    -1    41     None     fov041-1
                Il1rap    -1    41     None     fov041-2
                 Ntrk1    -1    41     None     fov041-3

-----------------------------
 
For Feature info:  neg_probe 

An object of class giottoPoints
feat_type : "neg_probe"
Feature Information:
 class       : SpatVector 
 geometry    : none ################### that's not right...
 dimensions  : 0, 1  (geometries, attributes)
 extent      : -484041.2, -484041.2, 8284.57, 8284.57  (xmin, xmax, ymin, ymax)
 coord. ref. :  
 names       : feat_ID_uniq
 type        :        <chr>

-----------------------------

The problem I think is with those feature dims at 0,1, when you try to open it again, you will get an issue with the SpatVector dimensions for the rna features? or the message to the terminal just comes after the neg_probe_feature_spatVector is attempted to be read:

[1] "Loading s23 giotto object..."
1. read Giotto object
2. read Giotto feature information
[1] "cortex/s23.WT-V.giotto_obj/Features/rna_feature_spatVector.shp"
Error: [names<-,SpatVector] incorrect number of names
Execution halted

> traceback()
5: stop("[", f, "] ", emsg, ..., call. = FALSE)
4: error("names<-,SpatVector", "incorrect number of names")
3: `names<-`(`*tmp*`, value = spatVector_names)
2: `names<-`(`*tmp*`, value = spatVector_names)
1: loadGiotto("cortex/s23.WT-V.giotto_obj")

Trying to remove the neg_probe feature from the object to see if it helps with setFeatureInfo(gobj, NULL, feat_type = "neg_probe")

@rbutleriii rbutleriii added the bug Something isn't working label Apr 25, 2024
@rbutleriii
Copy link
Author

Apparently that upsets Giotto quite a bit, if you attempt to remove just the feature_info:

setFeatureInfo(gobj, NULL, feat_type = "neg_probe")
NULL passed to gpoints.
 Removing specified feature information.
Error in .check_feat_metadata(gobject = .Object) : 
  No expression or polygon information discovered for feat_type: neg_probe
 Please add expression or polygon information for this feature type first
Calls: setFeatureInfo ... set_feature_info -> initialize -> initialize -> .check_feat_metadata
Execution halted

Or if you try to remove the feature_metadata first:

# Drop neg_probe as it is sometimes missing
setFeatureMetadata(gobj, NULL, feat_type = "neg_probe")
setFeatureInfo(gobj, NULL, feat_type = "neg_probe")
NULL passed to metadata.
 Removing specified metadata.
Error in `*tmp*`[[spat_unit]] : 
  attempt to select less than one element in get1index
Calls: setFeatureMetadata -> set_feature_metadata
Execution halted

Is there a way to tell createGiottoCosMxObject to not read the neg_probes in the first place? Might be easier than trying to hunt down all the places in the gobj it hides.

@rbutleriii
Copy link
Author

Update, for some of the setters, i.e. setFeatureMetadata, you can avoid the second error by specifying the spat_unit = "cell" as they are structured with features under the spat. However, that still produces the Error in .check_feat_metadata, as it still has an incomplete feat_type. So most of the setters won't allow you to remove the info either! There is probably a right order of getter/setter operations for this to work, but the hacky workaround is just to start ripping things out piecemeal:

# Drop neg_probe as it is sometimes missing
# gobj <- setFeatureInfo(gobj, NULL, feat_type = "neg_probe")
gobj@feat_info$neg_probe = NULL
gobj@feat_ID$neg_probe = NULL
gobj@feat_metadata$cell$neg_probe = NULL
gobj@cell_metadata$cell$neg_probe = NULL
gobj@expression$cell$neg_probe = NULL
gobj@expression_feat = gobj@expression_feat[gobj@expression_feat != "neg_probe"]

Since it is a created object from subcellular, the expression data doesn't exist yet, but just in case I added those lines. This also doesn't store a record of steps in gobj@parameters, but...I have a functional object.

@jiajic
Copy link
Member

jiajic commented May 23, 2024

Yeah Giotto is particularly touchy about raw data (polys and points) and expression information. Giotto's initialize() method immediately grabs the IDs information and sets up the ID and metadata slots. This behavior allows data to be added to the Giotto object outside of the createGiottoObject() framework, but removal is a lot harder, as you have noticed, and there currently is not a better way than your approach.

The way that createGiottoCosMxObject() reads the transcripts and splits it into RNA and neg_probe is hardcoded, unfortunately. But on the dev @modular_readers branch, it is now possible to have finer control over how transcript data is split and which items are then included in the Giotto object.

@jiajic
Copy link
Member

jiajic commented May 23, 2024

  • An actual fix to the empty objects issue should probably be something like having the Giotto object send a warning and toss out any nrow == 0 objects and dim of c(1, 1) NA matrices

  • How to deal with the setter fighting initialize() is probably going to be a bit more difficult. Possible solutions are:

    1. more logic in initialize() to automatically drop hierarchically dependent data (such as automatically removing metadata info if it is no longer supported by a piece of expression information, or raw spatial data e.g. polys/points) -- which may be an unexpected effect in itself.
    2. Relaxation of those hierarchical checks.
    3. Something like a new sliceGiotto() function that allows specific spat_units and feat_types to be extracted as a new giotto object.

@jiajic
Copy link
Member

jiajic commented Sep 12, 2024

These are now addressed on the GiottoClass dev branch.

Hierarchical/slot checks can now be toggled with options('giotto.check_valid')

library(GiottoClass)
g <- giotto()
sl <- GiottoData::loadSubObjectMini("spatLocsObj")

Expect an error

g <- setGiotto(g, sl)
Error in setSpatialLocations(gobject = gobject, x = x, ...) : 
  Add expression or spatial (polygon) information first

Turn off checking so issues can be fixed.
...

options("giotto.check_valid" = FALSE)
g <- setGiotto(g, sl)
list_spatial_locations(g)
   spat_unit   name
      <char> <char>
1: aggregate    raw
g <- setSpatialLocations(g, NULL, spat_unit = "aggregate")
NULL passed to spatlocs. Removing specified spatial
 locations.
list_spatial_locations(g)
NULL

....
turn checking back on when things are fixed

options("giotto.check_valid" = TRUE)

sliceGiotto() can be used to extract specific spatial units and feature types

> g <- GiottoData::loadGiottoMini("viz")
> agg <- sliceGiotto(g, spat_unit = "aggregate")
> activeSpatUnit(agg) <- "aggregate"
> agg
An object of class giotto 
>Active spat_unit:  aggregate 
>Active feat_type:  rna 
[SUBCELLULAR INFO]
polygons      : aggregate 
features      : rna 
[AGGREGATE INFO]
expression -----------------------
  [aggregate][rna] raw normalized scaled pearson
spatial locations ----------------
  [aggregate] raw
spatial networks -----------------
  [aggregate] Delaunay_network kNN_network
spatial enrichments --------------
  [aggregate][rna] cluster_metagene
dim reduction --------------------
  [aggregate][rna] pca umap tsne
nearest neighbor networks --------
  [aggregate][rna] sNN.pca
attached images ------------------
images      : 4 items...


Use objHistory() to see steps and params used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants