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

Changing backend #48

Open
ksjewell opened this issue Jun 30, 2023 · 3 comments
Open

Changing backend #48

ksjewell opened this issue Jun 30, 2023 · 3 comments

Comments

@ksjewell
Copy link

ksjewell commented Jun 30, 2023

I want to convert a Spectra object to MassBank-Backend object

From the documentation:
"Changing to a backend is only supported if that backend has a data
parameter in its backendInitialize method and if supportsSetBackend
returns TRUE for that backend"

supportsSetBackend does return true but
backendInitialize for MsBackendMassbank does not have a data parameter.

library(Spectra)
library(MsBackendMassbank)

spd <- DataFrame(msLevel = 1L, rtime = rt)
spd$mz <- list(spec$mz)
spd$intensity <- list(spec$intensity)
testSpec <- Spectra(spd)
setBackend(testSpec, MsBackendMassbank())

Returns:

Error in .local(object, ...) :    Parameter 'files' is mandatory for MsBackendMassbank

My current workaround is to load the example Massbank file, change the variables under spectraData, and then export again.

It seems to work initially, do you see any problems with doing it this way?

library(IRanges)
fls <- dir(system.file("extdata", package = "MsBackendMassbank"),
           full.names = TRUE, pattern = "txt$")
be <- backendInitialize(MsBackendMassbank(), fls[1])
tempmz <- NumericList(spec$mz)
tempint <- NumericList(spec$intensity)
names(tempmz) <- "mz"
names(tempint) <- "intensity"
be@spectraData$mz <- tempmz
be@spectraData$intensity <- tempint
be@spectraData$name <- list(name = list("Carbamazepine"))
export(MsBackendMassbank(), be, "export-to-massbank/test-mb-file2.txt")
@sneumann
Copy link

sneumann commented Jul 3, 2023

Hi,

thanks for reporting back and providing an example. However, the example does not work for me out of the box.

I modified the example to be straight cut&pasteable to reproduce, but did not get an error, and instead successfully wrote out a MassBank record:

library(IRanges)
library(Spectra)
library(MsBackendMassbank)

fls <- dir(system.file("extdata", package = "MsBackendMassbank"),
           full.names = TRUE, pattern = "txt$")
be <- backendInitialize(MsBackendMassbank(), fls[1])

spec <- be 

tempmz <- NumericList(spec$mz)
tempint <- NumericList(spec$intensity)
names(tempmz) <- "mz"
names(tempint) <- "intensity"
be@spectraData$mz <- tempmz
be@spectraData$intensity <- tempint
be@spectraData$name <- list(name = list("Carbamazepine2"))

export(MsBackendMassbank(), be, "test-mb-file2.txt")

Maybe the package versions are important, I might not have updated mine in a while, my sessionInfo() is shown below.

So, I'll need to be able to reproduce the error to help fixing it.
Yours,
Steffen

> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=de_DE.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Berlin
tzcode source: system (glibc)

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] MsBackendMassbank_1.6.1 Spectra_1.9.13          ProtGenerics_1.31.0    
[4] BiocParallel_1.32.6     IRanges_2.32.0          S4Vectors_0.36.2       
[7] BiocGenerics_0.44.0    

loaded via a namespace (and not attached):
 [1] codetools_0.2-19   clue_0.3-64        MsCoreUtils_1.11.5 parallel_4.3.0    
 [5] DBI_1.1.3          compiler_4.3.0     tools_4.3.0        cluster_2.1.4     
 [9] fs_1.6.2           MASS_7.3-60       

@jorainer
Copy link
Member

jorainer commented Jul 4, 2023

Actually, the expected workflow would be that you have a Spectra object (no matter what backend it uses) in R, eventually add additional spectra variables/fields that you would like to add to your spectra and then to export the data using the MsBackendMassbank to create a MassBank record file. There is no need to change the backend to a MsBackendMassbank using setBackend.

any Spectra can be exported in MassBank txt file format using export(MsBackendMassbank(), sps, file = <file name>).

And please, do not directly access slots within a backend/Spectra. Use sps$mz <- instead. Using be@spectraData$mz <- is not safe (we might change internals of backends at some point) and will also work only for this type of backend.

@ksjewell
Copy link
Author

ksjewell commented Jul 4, 2023

Oh great I got it to work as you say Johannes, thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants