comparison create_sqlite_db.R @ 12:cb8dce9812ff draft

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