rformassspectrometry / Spectra

Low level infrastructure to handle MS spectra
https://rformassspectrometry.github.io/Spectra/
34 stars 24 forks source link

`setBackend` to `MsBackendMemory` fails for empty `Spectra` #268

Closed meowcat closed 1 year ago

meowcat commented 1 year ago

setBackend(sp, MsBackendMemory())`` fails for emptysp`:

fls <- dir(system.file("sciex", package = "msdata"), full.names = TRUE)
sp <- Spectra(fls, source = MsBackendMzR())
sp
# MSn data (Spectra) with 1862 spectra in a MsBackendMzR backend:
sp_zero <- sp[c()]
sp_zero
# MSn data (Spectra) with 0 spectra in a MsBackendMzR backend:
sp_zero_mem <- setBackend(sp_zero, MsBackendMemory())
# Error in validObject(object) : 
# invalid class “MsBackendMemory” object: The following columns have a wrong data type: mz, intensity. 
# The expected data type(s) is/are: NumericList, NumericList.

This is not specific to MsBackendMzR, but also happens with MsBackendDataFrame:

sp_df <- setBackend(sp, MsBackendDataFrame())
sp_df_zero <- sp_df[c()]
sp_df_zero_mem <- setBackend(sp_df_zero, MsBackendMemory())
# Error in validObject(object) : 
# invalid class “MsBackendMemory” object: The following columns have a wrong data type: mz, intensity. 
# The expected data type(s) is/are: NumericList, NumericList.

On the other hand, converting an empty Spectra to MsBackendDataFrame works fine, therefore I believe (and I think) this is the expected behaviour, and MsBackendMemory is the "wrong" one:

sp_zero <- sp[c()]
sp_zero_df <- setBackend(sp_zero, MsBackendDataFrame())
sp_zero_df 
# MSn data (Spectra) with 0 spectra in a MsBackendDataFrame backend:
# Processing:
# Switch backend from MsBackendMzR to MsBackendDataFrame [Tue Jan 24 13:50:08 2023] 
jorainer commented 1 year ago

Thanks for pointing this out - I will have a look into it and fix. I believe the standard behavior for setBackend should be to allow empty spectra too.

jorainer commented 1 year ago

this should be fixed with the PR above.