comparison create_sqlite_db.R @ 0:58ee3687f776 draft

planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 0c9e55ffdc126b76e52d083d24c8f18ac1a901de
author tomnl
date Tue, 05 Jun 2018 06:44:50 -0400
parents
children ac12e2f9df86
comparison
equal deleted inserted replaced
-1:000000000000 0:58ee3687f776
1 library(msPurity)
2 library(optparse)
3 library(xcms)
4 library(CAMERA)
5 print(sessionInfo())
6 print('CREATING DATABASE')
7
8
9 xset_pa_filename_fix <- function(opt, pa, xset){
10
11
12 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)){
13 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables
14 # needs to be done due to Galaxy moving the files around and screwing up any links to files
15
16 filepaths <- trimws(strsplit(opt$mzML_files, ',')[[1]])
17 filepaths <- filepaths[filepaths != ""]
18 new_names <- basename(filepaths)
19
20 galaxy_names <- trimws(strsplit(opt$galaxy_names, ',')[[1]])
21 galaxy_names <- galaxy_names[galaxy_names != ""]
22
23 nsave <- names(pa@fileList)
24 old_filenames <- basename(pa@fileList)
25 pa@fileList <- filepaths[match(names(pa@fileList), galaxy_names)]
26 names(pa@fileList) <- nsave
27
28 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)])
29 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)])
30 }
31
32
33 if(!all(basename(pa@fileList)==basename(xset@filepaths))){
34 if(!all(names(pa@fileList)==basename(xset@filepaths))){
35 print('FILELISTS DO NOT MATCH')
36 message('FILELISTS DO NOT MATCH')
37 quit(status = 1)
38 }else{
39 xset@filepaths <- unname(pa@fileList)
40 }
41 }
42
43
44 return(list(pa, xset))
45 }
46
47
48
49
50 option_list <- list(
51 make_option(c("-o", "--out_dir"), type="character"),
52 make_option("--pa", type="character"),
53 make_option("--xset_xa", type="character"),
54 make_option("--xcms_camera_option", type="character"),
55 make_option("--eic", action="store_true"),
56 make_option("--cores", default=4),
57 make_option("--mzML_files", type="character"),
58 make_option("--galaxy_names", type="character"),
59 make_option("--grp_peaklist", type="character"),
60 make_option("--db_name", type="character", default='lcms_data.sqlite'),
61 make_option("--raw_rt_columns", action="store_true"),
62 make_option("--metfrag_result", type="character"),
63 make_option("--sirius_csifingerid_result", type="character"),
64 make_option("--probmetab_result", type="character")
65 )
66
67
68 # store options
69 opt<- parse_args(OptionParser(option_list=option_list))
70
71 loadRData <- function(rdata_path, name){
72 #loads an RData file, and returns the named xset object if it is there
73 load(rdata_path)
74 return(get(ls()[ls() == name]))
75 }
76
77 print(paste('pa', opt$pa))
78 print(opt$xset)
79 print(opt$xcms_camera_option)
80 # Requires
81 pa <- loadRData(opt$pa, 'pa')
82
83
84 print(pa@fileList)
85
86
87 if (opt$xcms_camera_option=='xcms'){
88 xset <- loadRData(opt$xset, 'xset')
89 fix <- xset_pa_filename_fix(opt, pa, xset)
90 pa <- fix[[1]]
91 xset <- fix[[2]]
92 xa <- NULL
93 }else{
94
95 xa <- loadRData(opt$xset, 'xa')
96 fix <- xset_pa_filename_fix(opt, pa, xa@xcmsSet)
97 pa <- fix[[1]]
98 xa@xcmsSet <- fix[[2]]
99 xset <- NULL
100 }
101
102
103
104
105
106 if(is.null(opt$grp_peaklist)){
107 grp_peaklist = NA
108 }else{
109 grp_peaklist = opt$grp_peaklist
110 }
111
112
113
114
115
116 db_pth <- msPurity::create_database(pa, xset=xset, xsa=xa, out_dir=opt$out_dir,
117 grp_peaklist=grp_peaklist, db_name=opt$db_name)
118
119 print(db_pth)
120
121 if (!is.null(opt$eic)){
122 if (is.null(opt$raw_rt_columns)){
123 rtrawColumns <- FALSE
124 }else{
125 rtrawColumns <- TRUE
126 }
127 if (is.null(xset)){
128 xset <- xa@xcmsSet
129 }
130 # previous check should have matched filelists together
131 xset@filepaths <- unname(pa@fileList)
132
133 # Saves the EICS into the previously created database
134 px <- msPurity::purityX(xset, saveEIC = TRUE,
135 cores=1, sqlitePth=db_pth,
136 rtrawColumns = rtrawColumns)
137 }
138
139 con <- DBI::dbConnect(RSQLite::SQLite(), db_pth)
140
141 add_extra_table_elucidation <- function(name, pth, db_con){
142 if (is.null(pth)){
143 return(0)
144 }
145
146
147 DBI::dbWriteTable(conn=db_con, name=name, value=pth, sep='\t', header=T)
148
149
150 }
151
152 write_to_table <- function(df, db_con, name, append){
153
154 df <- df[!df$UID=='UID',]
155 print(filter_Score)
156 print(filter_Rank)
157 print('filter rank and score')
158
159
160 # get peakid, an scan id
161 df_ids <- stringr::str_split_fixed(df$UID, '-', 3)
162 colnames(df_ids) <- c('grp_id', 'file_id', 'pid')
163 df <- cbind(df_ids, df)
164 DBI::dbWriteTable(db_con, name=name, value=df, row.names=FALSE, append=append)
165 }
166
167 add_probmetab <- function(pth, xset, con){
168 if (!is.null(pth)){
169
170 df <- read.table(pth, header = TRUE, sep='\t', stringsAsFactors = FALSE, comment.char = "")
171 df$grp_id <- match(df$name, xcms::groupnames(xset))
172 start <- T
173 for (i in 1:nrow(df)){
174
175 x <- df[i,]
176
177
178 if(is.na(x$proba) | x$proba =='NA'){
179
180 next
181 }
182
183 mpc <- stringr::str_split(x$mpc, ';')
184 proba <- stringr::str_split(x$proba, ';')
185
186 for (j in 1:length(mpc[[1]])){
187
188 row <- c(x$grp_id, x$propmz, mpc[[1]][j], proba[[1]][j])
189
190 if (start){
191 df_out <- data.frame(t(row), stringsAsFactors=F)
192 start <- F
193 }else{
194 df_out <- data.frame(rbind(df_out, row), stringsAsFactors=F)
195 }
196 print(df_out)
197
198 }
199
200 }
201
202 colnames(df_out) <- c('grp_id', 'propmz', 'mpc', 'proba')
203 DBI::dbWriteTable(con, name='probmetab_results', value=df_out, row.names=FALSE)
204
205 }
206
207
208 }
209
210 add_extra_table_elucidation('metfrag_results', opt$metfrag_result, con)
211 add_extra_table_elucidation('sirius_csifingerid_results', opt$sirius_csifingerid_result, con)
212
213
214 if (is.null(xset)){
215 DBI::dbWriteTable(con, name='xset_classes', value=xa@xcmsSet@phenoData, row.names=TRUE)
216 add_probmetab(opt$probmetab_result, xa@xcmsSet, con)
217 }else{
218
219 DBI::dbWriteTable(con, name='xset_classes', value=xset@phenoData, row.names=TRUE)
220 add_probmetab(opt$probmetab_result, xset, con)
221
222 }
223
224 cmd <- paste('SELECT cpg.grpid, cpg.mz, cpg.mzmin, cpg.mzmax, cpg.rt, cpg.rtmin, cpg.rtmax, c_peaks.cid, ',
225 'c_peaks.mzmin AS c_peak_mzmin, c_peaks.mzmax AS c_peak_mzmax, ',
226 'c_peaks.rtmin AS c_peak_rtmin, c_peaks.rtmax AS c_peak_rtmax, s_peak_meta.*, fileinfo.filename, fileinfo.nm_save ',
227 'FROM c_peak_groups AS cpg ',
228 'LEFT JOIN c_peak_X_c_peak_group AS cXg ON cXg.grpid=cpg.grpid ',
229 'LEFT JOIN c_peaks on c_peaks.cid=cXg.cid ',
230 'LEFT JOIN c_peak_X_s_peak_meta AS cXs ON cXs.cid=c_peaks.cid ',
231 'LEFT JOIN s_peak_meta ON cXs.pid=s_peak_meta.pid ',
232 'LEFT JOIN fileinfo ON s_peak_meta.fileid=fileinfo.fileid')
233
234 print(cmd)
235 cpeakgroup_msms <- DBI::dbGetQuery(con, cmd)
236
237 write.table(cpeakgroup_msms, file.path(opt$out_dir, 'cpeakgroup_msms.tsv'), row.names=FALSE, sep='\t')