rformassspectrometry / Spectra

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

Backend confusion #143

Closed ococrook closed 3 years ago

ococrook commented 3 years ago

Hi - me again...

I'm confused by what "backend = " is doing.

The following code chunk doesn't seem to do what I expected. Specifying 'backend = MsBackendDataFrame()' seems to set MsBackendMzR. Though it happily changes the backend using setBackend.

fl <- "data/191115_PhosB_HDMSe_0sec_01_vial01.mzML"
PhosB_raw <- Spectra(fl, backend = MsBackendDataFrame())
PhosB_raw
PhosB_raw <- setBackend(PhosB_raw, MsBackendDataFrame())
PhosB_raw

MSn data (Spectra) with 362200 spectra in a MsBackendMzR backend: msLevel rtime scanIndex

1 1 1.422 1 2 1 1.422 2 3 1 1.422 3 4 1 1.422 4 5 1 1.422 5 ... ... ... ... 362196 2 659.956 362196 362197 2 659.956 362197 362198 2 659.956 362198 362199 2 659.956 362199 362200 2 659.956 362200 ... 33 more variables/columns. file(s): 191115_PhosB_HDMSe_0sec_01_vial01.mzML MSn data (Spectra) with 362200 spectra in a MsBackendDataFrame backend: msLevel rtime scanIndex 1 1 1.422 1 2 1 1.422 2 3 1 1.422 3 4 1 1.422 4 5 1 1.422 5 ... ... ... ... 362196 2 659.956 362196 362197 2 659.956 362197 362198 2 659.956 362198 362199 2 659.956 362199 362200 2 659.956 362200 ... 33 more variables/columns. Processing: Switch backend from MsBackendMzR to MsBackendDataFrame [Thu Oct 15 11:28:18 2020]
jorainer commented 3 years ago

What happens when you use

PhosB_raw <- Spectra(fl, source = MsBackendMzR(), backend = MsBackendDataFrame())

With source you would then specify that you use the mzR backend for import and then it should change (after import) to the MsBackendDataFrame(). I know it's a little confusing...

ococrook commented 3 years ago

It looks the same to me

> fl <- "data/191115_PhosB_HDMSe_0sec_01_vial01.mzML"
> PhosB_raw <- Spectra(fl, source = MsBackendMzR(), backend = MsBackendDataFrame())
> PhosB_raw
MSn data (Spectra) with 362200 spectra in a MsBackendMzR backend:
         msLevel     rtime scanIndex
       <integer> <numeric> <integer>
1              1     1.422         1
2              1     1.422         2
3              1     1.422         3
4              1     1.422         4
5              1     1.422         5
...          ...       ...       ...
362196         2   659.956    362196
362197         2   659.956    362197
362198         2   659.956    362198
362199         2   659.956    362199
362200         2   659.956    362200
 ... 33 more variables/columns.

file(s):
191115_PhosB_HDMSe_0sec_01_vial01.mzML
jorainer commented 3 years ago

Now, that's a little tricky - I had to check the code to understand what's going on. The problem is that MsBackendMzR inherits from MsBackendDataFrame. Inside the Spectra constructor function the data is read using the source = MsBackendMzR() and then it checks if the backend is the same class than the source. If they are different setBackend is called to change the backend.

For now I would suggest to call the setBackend after reading the data. I will have to investigate a little how to change this without breaking anything.

ococrook commented 3 years ago

Thanks for the investigating! I'll keep to my original solution then

jorainer commented 3 years ago

I made already a PR (https://github.com/rformassspectrometry/Spectra/pull/144) which fixes this.