rformassspectrometry / Spectra

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

Add to Spectra object when backend is MsBackendCompDb #135

Open stanstrup opened 4 years ago

stanstrup commented 4 years ago

With a MsBackendDataFrame backed object you can concatenate like this and thus add to your object:

library(Spectra)
spd <- DataFrame(msLevel = c(1L, 2L), rtime = c(1.1, 1.2))
spd$mz <- list(c(100, 103.2, 104.3, 106.5), c(45.6, 120.4, 190.2))
spd$intensity <- list(c(200, 400, 34.2, 17), c(12.3, 15.2, 6.8))

data <- Spectra(spd)

c(data,data)

This does not work when the backend is MsBackendCompDb. Is there any way to add to such an object?

I am building a GUI for curating spectra and making a database. My idea was to use a Spectra object backed by MsBackendCompDb so that the sql file is in effect the database.

library(CompoundDb)

hmdb <- system.file("sdf/HMDB_sub.sdf.gz", package = "CompoundDb")
cmps <- compound_tbl_sdf(hmdb)

dr <- system.file("xml/", package = "CompoundDb")
msms_spctra <- msms_spectra_hmdb(dr)

metad2 <- data.frame(name = c("source", "url", "source_version",
                              "source_date", "organism"),
                     value = c("HMDB_spctra", "http://www.hmdb.ca",
                               "v4", "2017-08-27", "Hsapiens"))
db_spctra_file <- createCompDb(cmps, metadata = metad2, path = tempdir(),
                               msms_spectra = msms_spctra)
cmp_spctra_db <- CompDb(db_spctra_file)

cmp_spctra_db_S <- Spectra(cmp_spctra_db)

c(cmp_spctra_db_S, cmp_spctra_db_S)
jorainer commented 4 years ago

Firstly, I think this issue would better fit in the CompoundDb repo.

Interesting use case. So, what you would like to do is to add new spectra to a CompDb database that contains already e.g. spectra from HMDB? The MsBackendCompDb is read-only at present, so that's why you can't add data to it. So what you would need is the possibility to change the content of the database, right?

stanstrup commented 4 years ago

No I would not use HMDB data. That was just for the example.

I would probably like to create an empty DB that I can keep adding to as I process the spectra. So yes, it would need a way to change the database. I thought that was already there but I can see now that it was just the database connection that can be made writeable for direct re-use.

Unless you see a better way to make a dynamic in-house database?

Feel free to transfer the issues if it is possible to transfer between orgs.

jorainer commented 4 years ago

I you just want/need to have MS2 spectra it might be enough to use the MsBackendDataFrame and save/load the object with save/load - or you could export the Spectra to an mgf file with the MsBackendMgf.

I will also think of a way to have a better way to expand a CompDb database, but this might not be that straight forward. I'll open an issue for that in CompoundDb.

stanstrup commented 4 years ago

Sure. But an R file is not such a proper database to me and not safe for multiple users in the same way. Ideally the curating tool could be used by multiple people at the same time working on one common database.

jorainer commented 4 years ago

Makes sense. For multiple users working on the same database it might even make more sense to use MySQL/MariaDB as a database engine.

jorainer commented 4 years ago

Guess we should continue discussing this over in CompoundDb (https://github.com/EuracBiomedicalResearch/CompoundDb/issues/61)