Converting RDS to cardinal object

Skip to first unread message

Jason Rodencal

Jun 3, 2024, 10:21:12 AMJun 3
to Cardinal MSI Help

I love Cardinal, but I often run into memory issues trying to do even basic operations on my dataset (280k pixels). Furthermore, I have ion mobility data that doesn't seem to be compatible with Cardinal. One workaround for this issue has been using SCiLS and metaboscape, which I fortunately have access to. I can get molecular annotations through metaboscape, and then using the SCiLS API I can essentially peak-pick only the annotated list (a massive data compression) and then export the resulting data frame as an R Data Structure (.RDS). Actually, it's three .RDS files (the intensity values, the feature names, and the coordinates). I've been able to do a lot of work on this dataset in standard R, but obviously Cardinal has a lot better functionality for working with and visualizing MSI data. I especially want to use spatial shrunken centroids to cluster my pixels and see if I get informative biological patterns.

I'd really love to be able to convert my .RDS into a Cardinal object. Is this something that's been done before, or is feasible?

Alternatively, I am currently able to load my dataset into memory, it's just doing operations that fails (even a TIC normalization prior to PCA fails). I suppose a "hack" would be to load the entire Cardinal dataset and then filter it by whichever mz features I have annotated in a separate document. Is this functionality possible in Cardinal?

Thank you!

Jason Rodencal

Jun 5, 2024, 2:07:37 PMJun 5
to Cardinal MSI Help
I managed to figure out conversion of the .RDS to Cardinal using this very helpful video posted recently ( right around the 1:10 mark. The code is below if it's helpful to anyone else. If you manage to spot an error please let me know.

intensity_matrix = readRDS("[path]/1638_ann.RDS")
positional_data = readRDS("[path]/1638_ann_coords.RDS")
feature_names_raw = readRDS("[path]/1638_ann_features.RDS")

#extract the mzLow values as numeric
#Note that this produces a value of class vector
mz_values <- feature_names_raw$mzLow %>% as.numeric()

# Add the mzLow values to the column names of the intensity_matrix.
# This is important because we need to sort the mzLow values before making them a MassDataFrame
colnames(intensity_matrix) <- mz_values

# Re-ordered the intensity matrix by ascending mz values
#The order function returns the index at which the lowest value is present, then the next lowest, etc.
intensity_matrix_ordered <- intensity_matrix[,order(as.numeric(colnames(intensity_matrix)))]
mz_values_ordered <- mz_values[order(mz_values)]

#Transpose the intensity matrix (output = features as rows, spectra as columns)
#Seems to be a requirement/expectation of cardinal
intensity_transposed <- t(intensity_matrix_ordered)

#Format the positional data as expected by Cardinal
positional_data_filtered <- positional_data[,c("x","y")]
run_exp <- factor(rep("run0", nrow(positional_data_filtered)))
# Note that Kylie plots the y axis reversed. There doesn't seem to be a way to fix this on the plotting function.
# So I just force flip the axis on my data here.
positional_data_filtered$y <- positional_data_filtered$y * -1

#Convert the tables into the correct Cardinal objects
feature_names = MassDataFrame(mz = mz_values_ordered)
position_cardinal = PositionDataFrame(run = run_exp, coord = positional_data_filtered)
cardinal_dataframe <- MSImagingExperiment(spectraData = intensity_transposed,
                                          featureData = feature_names,
                                          pixelData = position_cardinal)

Reply all
Reply to author
0 new messages