Mercurial > repos > tomnl > create_sqlite_db
comparison backup_sqlite.R @ 20:74917d0e89cc draft
planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 04023134d8f28e85927ca293373c506484149ead-dirty
author | tomnl |
---|---|
date | Thu, 31 May 2018 11:29:47 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
19:906e8e2be944 | 20:74917d0e89cc |
---|---|
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, filter_Score=NA, filter_Rank=NA){ | |
142 if (is.null(pth)){ | |
143 return(0) | |
144 } | |
145 chunk=50 | |
146 c = 1 | |
147 header_s = FALSE | |
148 con = file(pth, "r") | |
149 while ( TRUE ) { | |
150 line = readLines(con, n = 1) | |
151 print(line) | |
152 if ( length(line) == 0 ) { | |
153 break | |
154 } | |
155 line_v <- unlist(strsplit(line, split="\t")) | |
156 if (!header_s){ | |
157 header = line_v | |
158 header_s = TRUE | |
159 df <- data.frame(ncol=length(header), nrow=0) | |
160 print(header) | |
161 colnames(df) <- header | |
162 df_blank <- df | |
163 print('CHECK') | |
164 write_to_table(df, db_con, name, FALSE, NA, NA) | |
165 } | |
166 print(line_v) | |
167 | |
168 if (!is.na(filter_Score)){ | |
169 if (line_v[header=='Score']<filter_Score){ | |
170 next | |
171 } | |
172 } | |
173 if (!is.na(filter_Rank)){ | |
174 | |
175 if (line_v[header=='Rank']>filter_Rank){ | |
176 next | |
177 } | |
178 } | |
179 | |
180 df <- rbind(df, line_v) | |
181 | |
182 | |
183 | |
184 if (c>chunk){ | |
185 write_to_table(df, db_con, name, TRUE, filter_Score, filter_Rank) | |
186 c=0 | |
187 df <- df_blanks | |
188 | |
189 } | |
190 c = c+1 | |
191 | |
192 print(line) | |
193 } | |
194 | |
195 | |
196 | |
197 close(con) | |
198 | |
199 | |
200 } | |
201 | |
202 write_to_table <- function(df, db_con, name, append){ | |
203 | |
204 df <- df[!df$UID=='UID',] | |
205 print(filter_Score) | |
206 print(filter_Rank) | |
207 print('filter rank and score') | |
208 | |
209 | |
210 # get peakid, an scan id | |
211 df_ids <- stringr::str_split_fixed(df$UID, '-', 3) | |
212 colnames(df_ids) <- c('grp_id', 'file_id', 'pid') | |
213 df <- cbind(df_ids, df) | |
214 DBI::dbWriteTable(db_con, name=name, value=df, row.names=FALSE, append=append) | |
215 } | |
216 | |
217 add_probmetab <- function(pth, con){ | |
218 if (!is.null(pth)){ | |
219 | |
220 df <- read.table(pth, header = TRUE, sep='\t', stringsAsFactors = FALSE, comment.char = "") | |
221 df$grp_id <- 1:nrow(df) | |
222 start <- T | |
223 for (i in 1:nrow(df)){ | |
224 | |
225 x <- df[i,] | |
226 | |
227 | |
228 if(is.na(x$proba) | x$proba =='NA'){ | |
229 | |
230 next | |
231 } | |
232 | |
233 mpc <- stringr::str_split(x$mpc, ';') | |
234 proba <- stringr::str_split(x$proba, ';') | |
235 | |
236 for (j in 1:length(mpc[[1]])){ | |
237 | |
238 row <- c(x$grp_id, x$propmz, mpc[[1]][j], proba[[1]][j]) | |
239 | |
240 if (start){ | |
241 df_out <- data.frame(t(row), stringsAsFactors=F) | |
242 start <- F | |
243 }else{ | |
244 df_out <- data.frame(rbind(df_out, row), stringsAsFactors=F) | |
245 } | |
246 print(df_out) | |
247 | |
248 } | |
249 | |
250 } | |
251 | |
252 colnames(df_out) <- c('grp_id', 'propmz', 'mpc', 'proba') | |
253 DBI::dbWriteTable(con, name='probmetab_results', value=df_out, row.names=FALSE) | |
254 | |
255 } | |
256 | |
257 | |
258 } | |
259 | |
260 add_extra_table_elucidation('metfrag_results', opt$metfrag_result, con, filter_Score=0.6, filter_Rank=NA) | |
261 add_extra_table_elucidation('sirius_csifingerid_results', opt$sirius_csifingerid_result, con, filter_Score=NA, filter_Rank=5) | |
262 add_probmetab(opt$probmetab_result, con) | |
263 | |
264 | |
265 | |
266 | |
267 cmd <- paste('SELECT cpg.grpid, cpg.mz, cpg.mzmin, cpg.mzmax, cpg.rt, cpg.rtmin, cpg.rtmax, c_peaks.cid, ', | |
268 'c_peaks.mzmin AS c_peak_mzmin, c_peaks.mzmax AS c_peak_mzmax, ', | |
269 'c_peaks.rtmin AS c_peak_rtmin, c_peaks.rtmax AS c_peak_rtmax, s_peak_meta.*, fileinfo.filename, fileinfo.nm_save ', | |
270 'FROM c_peak_groups AS cpg ', | |
271 'LEFT JOIN c_peak_X_c_peak_group AS cXg ON cXg.grpid=cpg.grpid ', | |
272 'LEFT JOIN c_peaks on c_peaks.cid=cXg.cid ', | |
273 'LEFT JOIN c_peak_X_s_peak_meta AS cXs ON cXs.cid=c_peaks.cid ', | |
274 'LEFT JOIN s_peak_meta ON cXs.pid=s_peak_meta.pid ', | |
275 'LEFT JOIN fileinfo ON s_peak_meta.fileid=fileinfo.fileid') | |
276 | |
277 print(cmd) | |
278 cpeakgroup_msms <- DBI::dbGetQuery(con, cmd) | |
279 | |
280 write.table(cpeakgroup_msms, file.path(opt$out_dir, 'cpeakgroup_msms.tsv'), row.names=FALSE, sep='\t') |