Skip to content

Commit

Permalink
Merge pull request #81 from NorStorz/feature/qfeature
Browse files Browse the repository at this point in the history
Feature/qfeature
  • Loading branch information
sneumann authored Aug 29, 2024
2 parents 06638e5 + c8142d2 commit 9f78385
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
Binary file added IntMedDf.Rdata
Binary file not shown.
17 changes: 17 additions & 0 deletions MetFamily.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
4 changes: 4 additions & 0 deletions R/FragmentMatrixFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,10 @@ convertToProjectFile2 <- function(filePeakMatrixQF,
metaboliteFamilies <- metaboliteFamilies[orderMS1features]
furtherProperties <- lapply(X = furtherProperties, FUN = function(props){props[orderMS1features]})

#TODO: resolve ?
#temporary fix
#filePeakMatrix <- NULL

if(!is.null(filePeakMatrix)){
## 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)}))
Expand Down
40 changes: 23 additions & 17 deletions R/parsePeakAbundanceMatrixQF.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @export
#'
#' @examples

parsePeakAbundanceMatrixQF <- function(qfeatures,
doPrecursorDeisotoping,
mzDeviationInPPM_precursorDeisotoping,
Expand All @@ -19,33 +20,36 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,
progress=FALSE)
{
## read file

if(!is.na(progress)) {
if(progress) {
incProgress(amount = 0.1, detail = paste("Parsing MS1 file content...", sep = ""))
} else {
print(paste("Parsing MS1 file content...", sep = ""))
}
}




cols_to_exclude <- c("Reference RT","Reference m/z","Comment",
"Manually modified for quantification",
"Total score","RT similarity","Average","Stdev")

cols_to_keep <- which(!colnames(rowData(qfeatures))[[1]] %in% cols_to_exclude)

dataFrame <- cbind(rowData(qfeatures)[[1]][,cols_to_keep] ,assay(qfeatures))

ncol(rowData(qfeatures)[[1]])
#workaround for avoiding change in colnames during coercion
cnames <- colnames(dataFrame)
dataFrame <- as.data.frame(dataFrame)
colnames(dataFrame) <- cnames
oldFormat <- ncol(colData(qfeatures))==3
numRowDataCols <- ncol(rowData(qfeatures)[[1]])
dataColumnStartEndIndeces <- c(numRowDataCols+1,ncol(dataFrame))
numberOfPrecursors <- nrow(dataFrame)
numberOfPrecursorsPrior <- numberOfPrecursors



if(nrow(colData(qfeatures))>0){

dataColumnStartEndIndeces <- 1
numberOfDataColumns <- nrow(colData(qfeatures))
if(ncol(assay(qfeatures))>0){
numberOfDataColumns <- ncol(assay(qfeatures))
sampleClass <- colData(qfeatures)$Class
sampleType <- colData(qfeatures)$Type
sampleInjectionOrder <- colData(qfeatures)$"Injection order"
Expand Down Expand Up @@ -74,7 +78,7 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,

## replace -1 by 0
if(numberOfDataColumns > 0) {
for(colIdx in dataColumnStartEndIndeces[[1]]:dataColumnStartEndIndeces[[2]]){
for(colIdx in (numRowDataCols+1):ncol(dataFrame)){
dataFrame[ , colIdx] <- gsub(x = gsub(x = dataFrame[ , colIdx], pattern = "\\.", replacement = ""), pattern = ",", replacement = ".")
}
}
Expand All @@ -97,13 +101,13 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,

## replace -1 by 0
if(numberOfDataColumns > 0){
for(colIdx in dataColumnStartEndIndeces[[1]]:dataColumnStartEndIndeces[[2]]){
for(colIdx in (numRowDataCols+1):ncol(dataFrame)){
dataFrame[ , colIdx] <- as.numeric(dataFrame[ , colIdx])
if(!is.na(sum(dataFrame[,colIdx] == -1)))
dataFrame[(dataFrame[,colIdx] == -1),colIdx] <- 0
}
}

vals <- NULL
## deisotoping
numberOfRemovedIsotopePeaks <- 0
if(doPrecursorDeisotoping & !is.null(dataFrame$"Average Mz")){
Expand All @@ -114,7 +118,7 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,
precursorsToRemove <- vector(mode = "logical", length = numberOfPrecursors)

if(numberOfDataColumns > 0){
intensities <- dataFrame[ , dataColumnStartEndIndeces[[1]]:dataColumnStartEndIndeces[[2]]]
intensities <- dataFrame[ , (numRowDataCols+1):ncol(dataFrame)]
medians <- apply(X = as.matrix(intensities), MARGIN = 1, FUN = median)
}

Expand All @@ -139,21 +143,23 @@ parsePeakAbundanceMatrixQF <- function(qfeatures,
} else {
validPrecursorsInIntensity <- TRUE
}

if(any(validPrecursorsInRt & validPrecursorsInMz & validPrecursorsInIntensity))
precursorsToRemove[[precursorIdx]] <- TRUE
}

}

## remove isotopes
dataFrame <- dataFrame[!precursorsToRemove, ]

numberOfRemovedIsotopePeaks <- sum(precursorsToRemove)
numberOfPrecursors <- nrow(dataFrame)
}

if(!is.na(progress)) if(progress) incProgress(amount = 0, detail = paste("Boxing...", sep = "")) else print(paste("Boxing...", sep = ""))
returnObj <- list()
returnObj$dataFrame <- dataFrame
returnObj$vals <- vals

## meta
returnObj$oldFormat <- oldFormat
Expand Down
4 changes: 2 additions & 2 deletions R/readMSDial.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ readMSDial <- function(file, version){
rownames(counts) <- ids

# Ensure row names of colData match counts column names
colData <- DataFrame(t(colDataRaw[-nrow(colDataRaw), -1]))
colData <- data.frame(t(colDataRaw[-nrow(colDataRaw), -1]))
rownames(colData) <- as.character(colDataRaw[nrow(colDataRaw), -1])
colnames(colData) <- as.character(colDataRaw[-nrow(colDataRaw), 1])

# Ensure row names of rowData match counts row names
rowData <- DataFrame(rowDataRaw[-1, ], row.names = ids)
rowData <- data.frame(rowDataRaw[-1, ], row.names = ids)
colnames(rowData) <- as.character(rowDataRaw[1,])

# Create SummarizedExperiment object
Expand Down
Binary file added medint.Rdata
Binary file not shown.
Binary file added tests/testthat/IntMedDf.Rdata
Binary file not shown.
10 changes: 6 additions & 4 deletions tests/testthat/test-readMSDial.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ test_that("exampledata", {
maximumRtDifference=0.05,
progress=FALSE)


## Test dimensions
expect_equal(nrow(data$dataFrame1), 5403)
expect_equal(ncol(data$dataFrame1), 20)
expect_equal(nrow(data$dataFrame), 5403)
expect_equal(ncol(data$dataFrame), 20)

## Test some values
expect_true(all(summary(t(data$dataFrame1[1,c("TRI03", "TRI02", "TRI01", "LVS03", "LVS02", "LVS01")])) == c("Min. : 236.0 ", "1st Qu.: 306.2 ", "Median : 357.5 ", "Mean : 501.0 ", "3rd Qu.: 554.2 ", "Max. :1146.0 ")))
expect_true(all(round(summary(data$dataFrame1[, "TRI01"])) == c(0, 809, 1537, 9818, 3207, 4407926)))
expect_true(all(summary(t(data$dataFrame[1,c("TRI03", "TRI02", "TRI01", "LVS03", "LVS02", "LVS01")])) == c("Min. : 236.0 ", "1st Qu.: 306.2 ", "Median : 357.5 ", "Mean : 501.0 ", "3rd Qu.: 554.2 ", "Max. :1146.0 ")))
expect_true(all(round(summary(data$dataFrame[, "TRI01"])) == c(0, 809, 1537, 9818, 3207, 4407926)))
})


Expand All @@ -37,3 +38,4 @@ test_that("Number of Rows and Columns are correct", {
})



0 comments on commit 9f78385

Please sign in to comment.