drieslab / Giotto

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

An empty giottoPoints object can corrupt the giotto obj #946

Open rbutleriii opened 2 months ago

rbutleriii commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 1 month ago

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 commented 1 month ago