comparison MsDbOutputDataFrameStream.R @ 1:45e985cd8e9e draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit d4048accde6bdfd5b3e14f5394902d38991854f8-dirty
author prog
date Tue, 31 Jan 2017 05:27:24 -0500
parents 3afe41d3e9e7
children b8f70d8216b3
comparison
equal deleted inserted replaced
0:3afe41d3e9e7 1:45e985cd8e9e
1 if ( ! exists('MsDbOutputDataFrameStream')) { # Do not load again if already loaded 1 if ( ! exists('MsDbOutputDataFrameStream')) { # Do not load again if already loaded
2 2
3 library(methods) 3 library(methods)
4 library(plyr)
5 source('MsDbOutputStream.R') 4 source('MsDbOutputStream.R')
6 source('dfhlp.R', chdir = TRUE) 5 source('dfhlp.R', chdir = TRUE)
7 6
8 ##################### 7 #####################
9 # CLASS DECLARATION # 8 # CLASS DECLARATION #
24 23
25 ################## 24 ##################
26 # GET DATA FRAME # 25 # GET DATA FRAME #
27 ################## 26 ##################
28 27
29 MsDbOutputDataFrameStream$methods( getDataFrame = function(...) { 28 MsDbOutputDataFrameStream$methods( getDataFrame = function() {
30 29
31 # Put at least a column name if empty 30 # Put at least a column name if empty
32 if (nrow(.self$.df) == 0) 31 if (nrow(.self$.df) == 0)
33 .self$.df[[.self$.output.fields[[MSDB.TAG.MZ]]]] <- numeric() 32 .self$.df[[.self$.output.fields[[MSDB.TAG.MZ]]]] <- numeric()
34 33
35 return(.self$.df) 34 return(.self$.df)
36 }) 35 })
37 36
37 # Move columns to beginning {{{1
38
39 MsDbOutputDataFrameStream$methods( moveColumnsToBeginning = function(cols) {
40 all.cols <- colnames(.self$.df)
41 other.cols <- all.cols[ ! all.cols %in% cols]
42 cols <- cols[cols %in% all.cols]
43 .df <<- .self$.df[c(cols, other.cols)]
44 })
45
38 ################# 46 #################
39 # MATCHED PEAKS # 47 # MATCHED PEAKS #
40 ################# 48 #################
41 49
42 MsDbOutputDataFrameStream$methods( matchedPeaks = function(mz, rt = NULL, unused = NULL, peaks = NULL) { 50 MsDbOutputDataFrameStream$methods( matchedPeaks = function(mz, rt = NULL, unused = NULL, peaks = NULL) {
43 51
52 library(plyr)
53
44 # Set input values 54 # Set input values
45 x <- data.frame(mz = mz) 55 x <- data.frame(mz = mz)
46 if ( ! is.null(rt)) 56 colnames(x) <- MSDB.TAG.MZ
47 x <- cbind(x, data.frame(rt = rt)) 57 if ( ! is.null(rt)) {
58 x.rt <- data.frame(rt = rt)
59 colnames(x.rt) <- MSDB.TAG.RT
60 x <- cbind(x, x.rt)
61 }
48 62
49 # Merge input values with matched peaks 63 # Merge input values with matched peaks
50 if ( ! is.null(peaks)) { 64 if ( ! is.null(peaks)) {
51 65
52 # No rows 66 # No rows
71 } 85 }
72 86
73 # Concatenate results in one line 87 # Concatenate results in one line
74 if (.self$.one.line) { 88 if (.self$.one.line) {
75 # For each column, concatenate all values in one string. 89 # For each column, concatenate all values in one string.
76 for (c in seq(peaks)) 90 for (c in seq(peaks)) {
77 peaks[1, c] <- paste0(peaks[[c]], collapse = .self$.match.sep, FUN.VALUE = '') 91 v <- peaks[[c]]
92 v <- v[ ! is.na(v)] # remove NA values
93 v <- v[ ! duplicated(v)] # remove duplicates
94 peaks[1, c] <- paste0(v, collapse = .self$.match.sep, FUN.VALUE = '')
95 }
78 peaks <- peaks[1, ] # Keep only first line 96 peaks <- peaks[1, ] # Keep only first line
79 } 97 }
80 } 98 }
81 99
82 # Merge 100 # Merge