Skip to content

Commit

Permalink
merging sirius annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
NorStorz committed Sep 19, 2024
1 parent b6eaf84 commit 2c72f7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
3 changes: 2 additions & 1 deletion R/FragmentMatrixFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ convertToProjectFile <- function(filePeakMatrixPath,

filePeakMatrixQF <- readMSDial(filePeakMatrixPath)
if (!is.null(fileAnnotation)){
# TODO: determine colums to merge by
addSiriusAnnotations(filePeakMatrixQF,fileAnnotation)
}

Expand Down Expand Up @@ -1593,7 +1594,7 @@ convertToProjectFile2 <- function(filePeakMatrixQF,
#temporary fix
#filePeakMatrix <- NULL

if(!is.null(filePeakMatrix)){
if(!is.null(filePeakMatrixQF)){
## allHits: dataFrame$"Average Mz" --> precursorMz; allHits indexes the spectraList
diffAll <- abs(outer(X = precursorMz, Y = dataFrame$"Average Mz", FUN = function(x, y){abs(x-y)}))
allHits <- apply(X = diffAll, MARGIN = 2, FUN = function(x){which(x == min(x[x < parameterSet$mzDeviationAbsolute_mapping], Inf))})
Expand Down
41 changes: 9 additions & 32 deletions R/parsePeakAbundanceMatrixQF.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,
cnames <- colnames(dataFrame)
dataFrame <- as.data.frame(dataFrame, check.names = FALSE)
colnames(dataFrame) <- cnames
datoldFormat <- ncol(colData(qfeatures))==3
oldFormat <- ncol(colData(qfeatures))==3
numRowDataCols <- ncol(rowData(qfeatures)[[1]])
dataColumnStartEndIndeces <- c(numRowDataCols+1,ncol(dataFrame))
numberOfPrecursors <- nrow(dataFrame)
Expand Down Expand Up @@ -180,8 +180,6 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,
return (returnObj)
}

#qfeatures <- readMSDial("inst/extdata/showcase/Metabolite_profile_showcase.txt")
#siriusFile <- "inst/extdata/testdata/canopus/canopusShort.txt"
#' Title
#'
#' @param qfeatures
Expand All @@ -195,39 +193,22 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,
#' @examples
addSiriusAnnotations <- function(qfeatures,
siriusFile,
rowData_col = NULL,
sirius_col =NULL) {

rowData_col = "Alignment ID",
sirius_col = "featureId") {
#TODO: specify more parameters in read delim
annotation <- read.delim(siriusFile)

rowData <- rowData(qfeatures[[1]])

if (is.null(sirius_col)) {
sirius_col <- grep("Alignment ID|featureId|ID", colnames(annotation), value = TRUE)
}

if (is.null(rowData_col)) {
rowData_col <- grep("featureId|Alignment ID|ID", colnames(rowData), value = TRUE)
}

# Check if the identified columns exist and are unique
if (length(sirius_col) != 1) {
stop("Could not uniquely identify the annotation column.")
}
if (length(rowData_col) != 1) {
stop("Could not uniquely identify the rowData column.")
}

# Print for debugging
print(paste("Merging by:", sirius_col, "and", rowData_col))

# Merge the data frames
annotatedRowData <- merge(annotation, rowData,
by.x = sirius_col, by.y = rowData_col, all.y = TRUE)
# annotatedRowData <- merge(annotation, rowData, by.x = "Alignment ID", by.y = "featureId", all.y = TRUE)
#annotatedRowData <- merge(annotation, rowData, by.x = siriusID, by.y =featureID, all.y = TRUE)

annotation_cols <- colnames(annotation)[colnames(annotation) != featureID]
annotatedRowData <- S4Vectors::merge( rowData, annotation,
by.x = rowData_col, by.y = sirius_col, all.x = TRUE)

#TODO: ? check for duplicate columns ?
annotation_cols <- colnames(annotation)[colnames(annotation) != rowData_col]
rowData_cols <- colnames(rowData)

for (col in colnames(annotatedRowData)) {
Expand All @@ -237,10 +218,6 @@ addSiriusAnnotations <- function(qfeatures,
attr(annotatedRowData[[col]], "source") <- "data"
}
}

# Update the row data in qfeatures
rowData(qfeatures[[1]]) <- annotatedRowData

return(qfeatures)
}
#annotation_cols <- names(which(sapply(rowData, function(x) attr(x, "source") == "sirius")))

0 comments on commit 2c72f7b

Please sign in to comment.