Skip to content

Instantly share code, notes, and snippets.

@meowcat
Created April 3, 2018 15:13
Show Gist options
  • Save meowcat/cf98b8f3f78c2b1e4dd9337e3cfb511c to your computer and use it in GitHub Desktop.
Save meowcat/cf98b8f3f78c2b1e4dd9337e3cfb511c to your computer and use it in GitHub Desktop.
Fix incorrect precursor assignment in Lumos switching mode files converted to mzXML (ProteoWizard)
library(XML)
# dir <- "C:/Input_data/MassBank/20180219_Test"
# setwd(dir)
# fIn<- "01_4093_Flazasulfuron.mzXML"
#
# fixLumosMzXml <- function(f, prefix="fix_" out = paste0(dirname(f), prefix, basename(f)))
# {
# x <- xmlParse(f)
# ns <- c(n= xmlNamespaceDefinitions(x, simplify = TRUE)[[1]])
# #prec <- getNodeSet(x, "//n:precursorMz", ns)
# #
# # scansPos1 <- getNodeSet(x, "//n:scan[@polarity='+'][@msLevel=1]", ns)
# # scansPos2 <- getNodeSet(x, "//n:scan[@polarity='+'][@msLevel=2]/precursorMz", ns)
# # scansNeg1 <- getNodeSet(x, "//n:scan[@polarity='-'][@msLevel=1]", ns)
# # scansNeg2 <- getNodeSet(x, "//n:scan[@polarity='-'][@msLevel=2]", ns)
fixLumosMzXml <- function(inFile, prefix="fix_", outFile=paste0(prefix, inFile), outString=FALSE)
{
x <- xmlParse(inFile)
ns <- c(n= xmlNamespaceDefinitions(x, simplify = TRUE)[[1]])
for(polarity in c("+", "-"))
{
scans1 <- as.numeric(unlist(xpathApply(x, sprintf("//n:scan[@polarity='%s'][@msLevel=1]",polarity),
function(node)
{
xmlAttrs(node)[["num"]]
},
namespaces=ns)))
scans2.orig <- unlist(xpathApply(x, sprintf("//n:scan[@polarity='%s'][@msLevel=2]/n:precursorMz", polarity),
function(node){
xmlAttrs(node)[["precursorScanNum"]]
},
namespaces = ns))
scans2 <- unlist(xpathApply(x, sprintf("//n:scan[@polarity='%s'][@msLevel=2]/n:precursorMz", polarity),
function(node)
{
scanNo <- as.numeric(xmlAttrs(xmlParent(node))[["num"]])
parentNo <- max(scans1[scans1 < scanNo])
xmlAttrs(node)["precursorScanNum"] <- as.character(parentNo)
xmlAttrs(node)[["precursorScanNum"]]
},
namespaces=ns))
scans2.revised <- unlist(xpathApply(x, sprintf("//n:scan[@polarity='%s'][@msLevel=2]/n:precursorMz", polarity),
function(node){
xmlAttrs(node)[["precursorScanNum"]]
},
namespaces = ns))
}
if(!outString)
saveXML(x, outFile)
else
return(saveXML(x))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment