Mercurial > repos > computational-metabolomics > mspurity_spectralmatching
comparison frag4feature.R @ 6:fecfe8c80e25 draft
"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 2579c8746819670348c378f86116f83703c493eb"
author | computational-metabolomics |
---|---|
date | Thu, 04 Mar 2021 12:32:11 +0000 |
parents | 5ff9d40c7a42 |
children | 77706396e7bd |
comparison
equal
deleted
inserted
replaced
5:829af6fecd96 | 6:fecfe8c80e25 |
---|---|
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 | 7 |
8 | 8 |
9 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)){ | 9 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)) { |
10 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables | 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 | 11 # needs to be done due to Galaxy moving the files around and screwing up any links to files |
12 | 12 |
13 filepaths <- trimws(strsplit(opt$mzML_files, ',')[[1]]) | 13 filepaths <- trimws(strsplit(opt$mzML_files, ",")[[1]]) # nolint |
14 | |
14 filepaths <- filepaths[filepaths != ""] | 15 filepaths <- filepaths[filepaths != ""] |
15 new_names <- basename(filepaths) | |
16 | 16 |
17 galaxy_names <- trimws(strsplit(opt$galaxy_names, ',')[[1]]) | 17 galaxy_names <- trimws(strsplit(opt$galaxy_names, ",")[[1]]) |
18 galaxy_names <- galaxy_names[galaxy_names != ""] | 18 galaxy_names <- galaxy_names[galaxy_names != ""] |
19 | 19 |
20 nsave <- names(pa@fileList) | 20 nsave <- names(pa@fileList) |
21 old_filenames <- basename(pa@fileList) | 21 old_filenames <- basename(pa@fileList) |
22 | 22 |
26 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)]) | 26 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)]) |
27 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)]) | 27 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)]) |
28 } | 28 } |
29 print(pa@fileList) | 29 print(pa@fileList) |
30 | 30 |
31 if(!is.null(xset)){ | 31 if (!is.null(xset)) { |
32 | 32 |
33 print(xset@filepaths) | 33 print(xset@filepaths) |
34 | 34 |
35 if(!all(basename(pa@fileList)==basename(xset@filepaths))){ | 35 if (!all(basename(pa@fileList) == basename(xset@filepaths))) { |
36 if(!all(names(pa@fileList)==basename(xset@filepaths))){ | 36 if (!all(names(pa@fileList) == basename(xset@filepaths))) { |
37 print('FILELISTS DO NOT MATCH') | 37 print("FILELISTS DO NOT MATCH") |
38 message('FILELISTS DO NOT MATCH') | 38 message("FILELISTS DO NOT MATCH") |
39 quit(status = 1) | 39 quit(status = 1) |
40 }else{ | 40 }else{ |
41 xset@filepaths <- unname(pa@fileList) | 41 xset@filepaths <- unname(pa@fileList) |
42 } | 42 } |
43 } | 43 } |
46 return(list(pa, xset)) | 46 return(list(pa, xset)) |
47 } | 47 } |
48 | 48 |
49 | 49 |
50 option_list <- list( | 50 option_list <- list( |
51 make_option(c("-o", "--out_dir"), type="character"), | 51 make_option(c("-o", "--out_dir"), type = "character"), |
52 make_option("--pa", type="character"), | 52 make_option("--pa", type = "character"), |
53 make_option("--xset", type="character"), | 53 make_option("--xset", type = "character"), |
54 make_option("--ppm", default=10), | 54 make_option("--ppm", default = 10), |
55 make_option("--plim", default=0.0), | 55 make_option("--plim", default = 0.0), |
56 make_option("--convert2RawRT", action="store_true"), | 56 make_option("--convert2RawRT", action = "store_true"), |
57 make_option("--intense", action="store_true"), | 57 make_option("--intense", action = "store_true"), |
58 make_option("--createDB", action="store_true"), | 58 make_option("--createDB", action = "store_true"), |
59 make_option("--cores", default=4), | 59 make_option("--cores", default = 4), |
60 make_option("--mzML_files", type="character"), | 60 make_option("--mzML_files", type = "character"), |
61 make_option("--galaxy_names", type="character"), | 61 make_option("--galaxy_names", type = "character"), |
62 make_option("--grp_peaklist", type="character"), | 62 make_option("--grp_peaklist", type = "character"), |
63 make_option("--useGroup", action="store_true") | 63 make_option("--useGroup", action = "store_true") |
64 ) | 64 ) |
65 | 65 |
66 # store options | 66 # store options |
67 opt<- parse_args(OptionParser(option_list=option_list)) | 67 opt <- parse_args(OptionParser(option_list = option_list)) |
68 print(opt) | 68 print(opt) |
69 | 69 |
70 loadRData <- function(rdata_path, name){ | 70 loadRData <- function(rdata_path, name) { |
71 #loads an RData file, and returns the named xset object if it is there | 71 #loads an RData file, and returns the named xset object if it is there |
72 load(rdata_path) | 72 load(rdata_path) |
73 return(get(ls()[ls() %in% name])) | 73 return(get(ls()[ls() %in% name])) |
74 } | 74 } |
75 | 75 |
76 # This function retrieve a xset like object | 76 # This function retrieve a xset like object |
77 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr | 77 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr |
78 getxcmsSetObject <- function(xobject) { | 78 getxcmsSetObject <- function(xobject) { |
79 # XCMS 1.x | 79 # XCMS 1.x |
80 if (class(xobject) == "xcmsSet") | 80 if (class(xobject) == "xcmsSet") |
81 return (xobject) | 81 return(xobject) |
82 # XCMS 3.x | 82 # XCMS 3.x |
83 if (class(xobject) == "XCMSnExp") { | 83 if (class(xobject) == "XCMSnExp") { |
84 # Get the legacy xcmsSet object | 84 # Get the legacy xcmsSet object |
85 suppressWarnings(xset <- as(xobject, 'xcmsSet')) | 85 suppressWarnings(xset <- as(xobject, "xcmsSet")) |
86 sampclass(xset) <- xset@phenoData$sample_group | 86 sampclass(xset) <- xset@phenoData$sample_group |
87 return (xset) | 87 return(xset) |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 # Requires | 91 # Requires |
92 pa <- loadRData(opt$pa, 'pa') | 92 pa <- loadRData(opt$pa, "pa") |
93 xset <- loadRData(opt$xset, c('xset','xdata')) | 93 xset <- loadRData(opt$xset, c("xset", "xdata")) |
94 xset <- getxcmsSetObject(xset) | 94 xset <- getxcmsSetObject(xset) |
95 | 95 |
96 pa@cores <- opt$cores | 96 pa@cores <- opt$cores |
97 | 97 |
98 print(pa@fileList) | 98 print(pa@fileList) |
99 print(xset@filepaths) | 99 print(xset@filepaths) |
100 | 100 |
101 if(is.null(opt$intense)){ | 101 if (is.null(opt$intense)) { |
102 intense = FALSE | 102 intense <- FALSE |
103 }else{ | 103 }else{ |
104 intense = TRUE | 104 intense <- TRUE |
105 } | 105 } |
106 | 106 |
107 if(is.null(opt$convert2RawRT)){ | 107 if (is.null(opt$convert2RawRT)) { |
108 convert2RawRT = FALSE | 108 convert2RawRT <- FALSE |
109 }else{ | 109 }else{ |
110 convert2RawRT= TRUE | 110 convert2RawRT <- TRUE |
111 } | 111 } |
112 | 112 |
113 if(is.null(opt$createDB)){ | 113 if (is.null(opt$createDB)) { |
114 createDB = FALSE | 114 createDB <- FALSE |
115 }else{ | 115 }else{ |
116 createDB = TRUE | 116 createDB <- TRUE |
117 } | 117 } |
118 | 118 |
119 if(is.null(opt$useGroup)){ | 119 if (is.null(opt$useGroup)) { |
120 fix <- xset_pa_filename_fix(opt, pa, xset) | 120 fix <- xset_pa_filename_fix(opt, pa, xset) |
121 pa <- fix[[1]] | 121 pa <- fix[[1]] |
122 xset <- fix[[2]] | 122 xset <- fix[[2]] |
123 useGroup=FALSE | 123 useGroup <- FALSE |
124 }else{ | 124 }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 | 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 |
126 print('useGroup') | 126 print("useGroup") |
127 fix <- xset_pa_filename_fix(opt, pa) | 127 fix <- xset_pa_filename_fix(opt, pa) |
128 pa <- fix[[1]] | 128 pa <- fix[[1]] |
129 useGroup=TRUE | 129 useGroup <- TRUE |
130 } | 130 } |
131 | 131 |
132 | 132 |
133 if(is.null(opt$grp_peaklist)){ | 133 if (is.null(opt$grp_peaklist)) { |
134 grp_peaklist = NA | 134 grp_peaklist <- NA |
135 }else{ | 135 }else{ |
136 grp_peaklist = opt$grp_peaklist | 136 grp_peaklist <- opt$grp_peaklist |
137 } | 137 } |
138 print(useGroup) | 138 print(useGroup) |
139 | 139 |
140 | 140 pa <- msPurity::frag4feature(pa = pa, |
141 | 141 xset = xset, |
142 pa <- msPurity::frag4feature(pa=pa, | 142 ppm = opt$ppm, |
143 xset=xset, | 143 plim = opt$plim, |
144 ppm=opt$ppm, | 144 intense = intense, |
145 plim=opt$plim, | 145 convert2RawRT = convert2RawRT, |
146 intense=intense, | 146 db_name = "alldata.sqlite", |
147 convert2RawRT=convert2RawRT, | 147 out_dir = opt$out_dir, |
148 db_name='alldata.sqlite', | 148 grp_peaklist = grp_peaklist, |
149 out_dir=opt$out_dir, | 149 create_db = createDB, |
150 grp_peaklist=grp_peaklist, | 150 use_group = useGroup) |
151 create_db=createDB, | |
152 use_group=useGroup) | |
153 | |
154 print(pa) | 151 print(pa) |
155 save(pa, file=file.path(opt$out_dir, 'frag4feature_output.RData')) | 152 save(pa, file = file.path(opt$out_dir, "frag4feature_output.RData")) |
156 | 153 |
157 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)]) |
158 | 155 |
159 print(head(pa@grped_df)) | 156 print(head(pa@grped_df)) |
160 write.table(pa@grped_df, file.path(opt$out_dir, 'frag4feature_output.tsv'), row.names=FALSE, sep='\t') | 157 write.table(pa@grped_df, file.path(opt$out_dir, "frag4feature_output.tsv"), row.names = FALSE, sep = "\t") |