comparison frag4feature.R @ 8:75b761fbacc0 draft

planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 7e1748612a9f9dce11a9e54ff36752b600e7aea3
author computational-metabolomics
date Wed, 12 Jun 2024 16:05:52 +0000
parents 090775983be7
children 548fed2ef792
comparison
equal deleted inserted replaced
7:2ce66d2f6a24 8:75b761fbacc0
1 library(optparse) 1 library(optparse)
2 library(msPurity) 2 library(msPurity)
3 library(xcms) 3 library(xcms)
4 print(sessionInfo()) 4 print(sessionInfo())
5 5
6 xset_pa_filename_fix <- function(opt, pa, xset=NULL) { 6 xset_pa_filename_fix <- function(opt, pa, xset = NULL) {
7 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)) {
8 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables
9 # needs to be done due to Galaxy moving the files around and screwing up any links to files
7 10
11 filepaths <- trimws(strsplit(opt$mzML_files, ",")[[1]]) # nolint
8 12
9 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)) { 13 filepaths <- filepaths[filepaths != ""]
10 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables
11 # needs to be done due to Galaxy moving the files around and screwing up any links to files
12 14
13 filepaths <- trimws(strsplit(opt$mzML_files, ",")[[1]]) # nolint 15 galaxy_names <- trimws(strsplit(opt$galaxy_names, ",")[[1]])
16 galaxy_names <- galaxy_names[galaxy_names != ""]
14 17
15 filepaths <- filepaths[filepaths != ""] 18 nsave <- names(pa@fileList)
19 old_filenames <- basename(pa@fileList)
16 20
17 galaxy_names <- trimws(strsplit(opt$galaxy_names, ",")[[1]]) 21 pa@fileList <- filepaths[match(names(pa@fileList), galaxy_names)]
18 galaxy_names <- galaxy_names[galaxy_names != ""] 22 names(pa@fileList) <- nsave
19 23
20 nsave <- names(pa@fileList) 24 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)])
21 old_filenames <- basename(pa@fileList) 25 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)])
26 }
27 print(pa@fileList)
22 28
23 pa@fileList <- filepaths[match(names(pa@fileList), galaxy_names)] 29 if (!is.null(xset)) {
24 names(pa@fileList) <- nsave 30 print(xset@filepaths)
25 31
26 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)]) 32 if (!all(basename(pa@fileList) == basename(xset@filepaths))) {
27 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)]) 33 if (!all(names(pa@fileList) == basename(xset@filepaths))) {
28 } 34 print("FILELISTS DO NOT MATCH")
29 print(pa@fileList) 35 message("FILELISTS DO NOT MATCH")
36 quit(status = 1)
37 } else {
38 xset@filepaths <- unname(pa@fileList)
39 }
40 }
41 }
30 42
31 if (!is.null(xset)) { 43 return(list(pa, xset))
32
33 print(xset@filepaths)
34
35 if (!all(basename(pa@fileList) == basename(xset@filepaths))) {
36 if (!all(names(pa@fileList) == basename(xset@filepaths))) {
37 print("FILELISTS DO NOT MATCH")
38 message("FILELISTS DO NOT MATCH")
39 quit(status = 1)
40 }else{
41 xset@filepaths <- unname(pa@fileList)
42 }
43 }
44 }
45
46 return(list(pa, xset))
47 } 44 }
48 45
49 46
50 option_list <- list( 47 option_list <- list(
51 make_option(c("-o", "--out_dir"), type = "character"), 48 make_option(c("-o", "--out_dir"), type = "character"),
52 make_option("--pa", type = "character"), 49 make_option("--pa", type = "character"),
53 make_option("--xset", type = "character"), 50 make_option("--xset", type = "character"),
54 make_option("--ppm", default = 10), 51 make_option("--ppm", default = 10),
55 make_option("--plim", default = 0.0), 52 make_option("--plim", default = 0.0),
56 make_option("--convert2RawRT", action = "store_true"), 53 make_option("--convert2RawRT", action = "store_true"),
57 make_option("--intense", action = "store_true"), 54 make_option("--intense", action = "store_true"),
58 make_option("--createDB", action = "store_true"), 55 make_option("--createDB", action = "store_true"),
59 make_option("--cores", default = 4), 56 make_option("--cores", default = 4),
60 make_option("--mzML_files", type = "character"), 57 make_option("--mzML_files", type = "character"),
61 make_option("--galaxy_names", type = "character"), 58 make_option("--galaxy_names", type = "character"),
62 make_option("--grp_peaklist", type = "character"), 59 make_option("--grp_peaklist", type = "character"),
63 make_option("--useGroup", action = "store_true") 60 make_option("--useGroup", action = "store_true")
64 ) 61 )
65 62
66 # store options 63 # store options
67 opt <- parse_args(OptionParser(option_list = option_list)) 64 opt <- parse_args(OptionParser(option_list = option_list))
68 print(opt) 65 print(opt)
69 66
70 loadRData <- function(rdata_path, name) { 67 loadRData <- function(rdata_path, name) {
71 #loads an RData file, and returns the named xset object if it is there 68 # loads an RData file, and returns the named xset object if it is there
72 load(rdata_path) 69 load(rdata_path)
73 return(get(ls()[ls() %in% name])) 70 return(get(ls()[ls() %in% name]))
74 } 71 }
75 72
76 # This function retrieve a xset like object 73 # This function retrieve a xset like object
77 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr 74 # @author Gildas Le Corguille lecorguille@sb-roscoff.fr
78 getxcmsSetObject <- function(xobject) { 75 getxcmsSetObject <- function(xobject) {
79 # XCMS 1.x 76 # XCMS 1.x
80 if (class(xobject) == "xcmsSet") 77 if (class(xobject) == "xcmsSet") {
81 return(xobject) 78 return(xobject)
79 }
82 # XCMS 3.x 80 # XCMS 3.x
83 if (class(xobject) == "XCMSnExp") { 81 if (class(xobject) == "XCMSnExp") {
84 # Get the legacy xcmsSet object 82 # Get the legacy xcmsSet object
85 suppressWarnings(xset <- as(xobject, "xcmsSet")) 83 suppressWarnings(xset <- as(xobject, "xcmsSet"))
86 sampclass(xset) <- xset@phenoData$sample_group 84 sampclass(xset) <- xset@phenoData$sample_group
97 95
98 print(pa@fileList) 96 print(pa@fileList)
99 print(xset@filepaths) 97 print(xset@filepaths)
100 98
101 if (is.null(opt$intense)) { 99 if (is.null(opt$intense)) {
102 intense <- FALSE 100 intense <- FALSE
103 }else{ 101 } else {
104 intense <- TRUE 102 intense <- TRUE
105 } 103 }
106 104
107 if (is.null(opt$convert2RawRT)) { 105 if (is.null(opt$convert2RawRT)) {
108 convert2RawRT <- FALSE 106 convert2RawRT <- FALSE
109 }else{ 107 } else {
110 convert2RawRT <- TRUE 108 convert2RawRT <- TRUE
111 } 109 }
112 110
113 if (is.null(opt$createDB)) { 111 if (is.null(opt$createDB)) {
114 createDB <- FALSE 112 createDB <- FALSE
115 }else{ 113 } else {
116 createDB <- TRUE 114 createDB <- TRUE
117 } 115 }
118 116
119 if (is.null(opt$useGroup)) { 117 if (is.null(opt$useGroup)) {
120 fix <- xset_pa_filename_fix(opt, pa, xset) 118 fix <- xset_pa_filename_fix(opt, pa, xset)
121 pa <- fix[[1]] 119 pa <- fix[[1]]
122 xset <- fix[[2]] 120 xset <- fix[[2]]
123 useGroup <- FALSE 121 useGroup <- FALSE
124 }else{ 122 } else {
125 # if are only aligning to the group not eah file we do not need to align the files between the xset and pa object 123 # if are only aligning to the group not eah file we do not need to align the files between the xset and pa object
126 print("useGroup") 124 print("useGroup")
127 fix <- xset_pa_filename_fix(opt, pa) 125 fix <- xset_pa_filename_fix(opt, pa)
128 pa <- fix[[1]] 126 pa <- fix[[1]]
129 useGroup <- TRUE 127 useGroup <- TRUE
130 } 128 }
131 129
132 130
133 if (is.null(opt$grp_peaklist)) { 131 if (is.null(opt$grp_peaklist)) {
134 grp_peaklist <- NA 132 grp_peaklist <- NA
135 }else{ 133 } else {
136 grp_peaklist <- opt$grp_peaklist 134 grp_peaklist <- opt$grp_peaklist
137 } 135 }
138 print(useGroup) 136 print(useGroup)
139 137
140 pa <- msPurity::frag4feature(pa = pa, 138 pa <- msPurity::frag4feature(
141 xset = xset, 139 pa = pa,
142 ppm = opt$ppm, 140 xset = xset,
143 plim = opt$plim, 141 ppm = opt$ppm,
144 intense = intense, 142 plim = opt$plim,
145 convert2RawRT = convert2RawRT, 143 intense = intense,
146 db_name = "alldata.sqlite", 144 convert2RawRT = convert2RawRT,
147 out_dir = opt$out_dir, 145 db_name = "alldata.sqlite",
148 grp_peaklist = grp_peaklist, 146 out_dir = opt$out_dir,
149 create_db = createDB, 147 grp_peaklist = grp_peaklist,
150 use_group = useGroup) 148 create_db = createDB,
149 use_group = useGroup
150 )
151 print(pa) 151 print(pa)
152 save(pa, file = file.path(opt$out_dir, "frag4feature_output.RData")) 152 save(pa, file = file.path(opt$out_dir, "frag4feature_output.RData"))
153 153
154 pa@grped_df$filename <- sapply(pa@grped_df$fileid, function(x) names(pa@fileList)[as.integer(x)]) 154 pa@grped_df$filename <- sapply(pa@grped_df$fileid, function(x) names(pa@fileList)[as.integer(x)])
155 155