Skip to content

Instantly share code, notes, and snippets.

@sneumann
Created October 18, 2022 10:53
Show Gist options
  • Save sneumann/dc3089bffeb40d0e0b17571defe45cb9 to your computer and use it in GitHub Desktop.
Save sneumann/dc3089bffeb40d0e0b17571defe45cb9 to your computer and use it in GitHub Desktop.
Convert an mzTab-M to MetaboLights MAF file using rmzTabM and metabolighteR
library(rmzTabM) ## https://lifs-tools.github.io/rmzTab-m/
library(metabolighteR) ## https://aberhrml.github.io/metabolighteR/
convertMzTab2MAF <- function(mzTabfile, MAFfile) {
mzTabTable <- readMzTab(mzTabfile)
## To turn these tables into objects, use the R6 class constructor method `new()`:
mzTabObject <- MzTab$new()$fromDataFrame(mzTabTable)
metadata <- mzTabObject$metadata
msassaynames <- sapply(metadata$assay, function(a) a$name)
smlTable <- extractSmallMoleculeSummary(mzTabTable)
smfTable <- extractSmallMoleculeFeatures(mzTabTable)
#smeTable <- extractSmallMoleculeEvidence(mzTabTable)
abundances <- as.matrix(smlTable[,which(grepl("abundance_assay", colnames(smlTable)))])
colnames(abundances) <- msassaynames
##
## Writing as MAF file
##
maf <- create.MAF(abundances=abundances)
## Adding in additional columns from MS-Dial output
#maf[,""] <- smlTable[,""]
maf[,"chemical_formula"] <- smlTable[,"chemical_formula"]
maf[,"smiles"] <- smlTable[,"smiles"]
maf[,"inchi"] <- smlTable[,"inchi"]
maf[,"metabolite_identification"] <- smlTable[,"chemical_name"]
##
## This only works if sml and smf have same dim and same order !!!
##
maf[,"retention_time"] <- smfTable[, "retention_time_in_seconds"]
maf[,"mass_to_charge"] <- smfTable[, "exp_mass_to_charge"]
##
## Database handling in mzTab is more powerful than in MAF,
## multiple databases are allowed. This is NOT considered here
maf[,"database"] <- metadata$database[[1]]$id ## https://github.com/lifs-tools/rmzTab-m/issues/24
maf[,"database_version"] <- metadata$database[[1]]$version
maf[,"database_identifier"] <- smlTable[,"database_identifier"] ## Mybe strip the mzTab PREFIX ?
## Would require normalisation/mapping between scoring systems:
maf[,"reliability"] <- smlTable[,"best_id_confidence_value"]
write.MAF(maf, MAFfile)
}
## Example invocation:
## Reading the mzTab created by MS-Dial
## wget https://raw.githubusercontent.com/HUPO-PSI/mzTab/master/examples/2_0-Metabolomics_Release/msdial/lcmsms_dda_hydrophilic_height_mzTab.txt
## convertMzTab2MAF(mzTabfile="lcmsms_dda_hydrophilic_height_mzTab.txt", MAFfile="m_MTBLS007_v2_maf.tsv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment