Mercurial > repos > tomnl > create_sqlite_db
comparison create_sqlite_db.R @ 0:fe7d7cc95ca5 draft
planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 2e847122cf605951c334858455fc1d3ebdb189e9-dirty
author | tomnl |
---|---|
date | Tue, 27 Mar 2018 06:03:50 -0400 |
parents | |
children | 1a88758357ed |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fe7d7cc95ca5 |
---|---|
1 library(msPurity) | |
2 library(optparse) | |
3 library(xcms) | |
4 library(CAMERA) | |
5 print('CREATING DATABASE') | |
6 | |
7 | |
8 xset_pa_filename_fix <- function(opt, pa, xset){ | |
9 | |
10 | |
11 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)){ | |
12 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables | |
13 # needs to be done due to Galaxy moving the files around and screwing up any links to files | |
14 | |
15 filepaths <- trimws(strsplit(opt$mzML_files, ',')[[1]]) | |
16 filepaths <- filepaths[filepaths != ""] | |
17 new_names <- basename(filepaths) | |
18 | |
19 galaxy_names <- trimws(strsplit(opt$galaxy_names, ',')[[1]]) | |
20 galaxy_names <- galaxy_names[galaxy_names != ""] | |
21 | |
22 nsave <- names(pa@fileList) | |
23 old_filenames <- basename(pa@fileList) | |
24 pa@fileList <- filepaths[match(names(pa@fileList), galaxy_names)] | |
25 names(pa@fileList) <- nsave | |
26 | |
27 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)]) | |
28 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)]) | |
29 } | |
30 | |
31 | |
32 if(!all(basename(pa@fileList)==basename(xset@filepaths))){ | |
33 if(!all(names(pa@fileList)==basename(xset@filepaths))){ | |
34 print('FILELISTS DO NOT MATCH') | |
35 message('FILELISTS DO NOT MATCH') | |
36 quit(status = 1) | |
37 }else{ | |
38 xset@filepaths <- unname(pa@fileList) | |
39 } | |
40 } | |
41 | |
42 | |
43 return(list(pa, xset)) | |
44 } | |
45 | |
46 | |
47 | |
48 option_list <- list( | |
49 make_option(c("-o", "--out_dir"), type="character"), | |
50 make_option("--pa", type="character"), | |
51 make_option("--xset_xa", type="character"), | |
52 make_option("--xcms_camera_option", type="character"), | |
53 make_option("--eic", action="store_true"), | |
54 make_option("--cores", default=4), | |
55 make_option("--mzML_files", type="character"), | |
56 make_option("--galaxy_names", type="character"), | |
57 make_option("--grp_peaklist", type="character"), | |
58 make_option("--db_name", type="character", default='lcms_data.sqlite'), | |
59 make_option("--raw_rt_columns", action="store_true") | |
60 ) | |
61 | |
62 # store options | |
63 opt<- parse_args(OptionParser(option_list=option_list)) | |
64 | |
65 loadRData <- function(rdata_path, name){ | |
66 #loads an RData file, and returns the named xset object if it is there | |
67 load(rdata_path) | |
68 return(get(ls()[ls() == name])) | |
69 } | |
70 | |
71 print(paste('pa', opt$pa)) | |
72 print(opt$xset) | |
73 print(opt$xcms_camera_option) | |
74 # Requires | |
75 pa <- loadRData(opt$pa, 'pa') | |
76 | |
77 print('TESTETSTESTETE') | |
78 print(pa@fileList) | |
79 | |
80 | |
81 if (opt$xcms_camera_option=='xcms'){ | |
82 xset <- loadRData(opt$xset, 'xset') | |
83 fix <- xset_pa_filename_fix(opt, pa, xset) | |
84 pa <- fix[[1]] | |
85 xset <- fix[[2]] | |
86 xa <- NULL | |
87 }else{ | |
88 | |
89 xa <- loadRData(opt$xset, 'xa') | |
90 fix <- xset_pa_filename_fix(opt, pa, xa@xcmsSet) | |
91 pa <- fix[[1]] | |
92 xa@xcmsSet <- fix[[2]] | |
93 xset <- NULL | |
94 } | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 if(is.null(opt$grp_peaklist)){ | |
101 grp_peaklist = NA | |
102 }else{ | |
103 grp_peaklist = opt$grp_peaklist | |
104 } | |
105 | |
106 | |
107 print(pa@fileList) | |
108 print(xset@filepaths) | |
109 | |
110 | |
111 db_pth <- msPurity::create_database(pa, xset=xset, xsa=xa, out_dir=opt$out_dir, | |
112 grp_peaklist=grp_peaklist, db_name=opt$db_name) | |
113 | |
114 | |
115 if (!is.null(opt$eic)){ | |
116 if (is.null(opt$raw_rt_columns)){ | |
117 rtrawColumns <- FALSE | |
118 }else{ | |
119 rtrawColumns <- TRUE | |
120 } | |
121 | |
122 # Saves the EICS into the previously created database | |
123 px <- msPurity::purityX(xset, saveEIC = TRUE, | |
124 cores=opt$cores, sqlitePth=db_pth, | |
125 rtrawColumns = rtrawColumns) | |
126 } | |
127 | |
128 | |
129 | |
130 con <- DBI::dbConnect(RSQLite::SQLite(), db_pth) | |
131 | |
132 cmd <- paste('SELECT cpg.grpid, cpg.mz, cpg.mzmin, cpg.mzmax, cpg.rt, cpg.rtmin, cpg.rtmax, c_peaks.cid, ', | |
133 'c_peaks.mzmin AS c_peak_mzmin, c_peaks.mzmax AS c_peak_mzmax, ', | |
134 'c_peaks.rtmin AS c_peak_rtmin, c_peaks.rtmax AS c_peak_rtmax, s_peak_meta.*, fileinfo.filename, fileinfo.nm_save ', | |
135 'FROM c_peak_groups AS cpg ', | |
136 'LEFT JOIN c_peak_X_c_peak_group AS cXg ON cXg.grpid=cpg.grpid ', | |
137 'LEFT JOIN c_peaks on c_peaks.cid=cXg.cid ', | |
138 'LEFT JOIN c_peak_X_s_peak_meta AS cXs ON cXs.cid=c_peaks.cid ', | |
139 'LEFT JOIN s_peak_meta ON cXs.pid=s_peak_meta.pid ', | |
140 'LEFT JOIN fileinfo ON s_peak_meta.fileid=fileinfo.fileid') | |
141 | |
142 print(cmd) | |
143 cpeakgroup_msms <- DBI::dbGetQuery(con, cmd) | |
144 | |
145 write.table(cpeakgroup_msms, file.path(opt$out_dir, 'cpeakgroup_msms.tsv'), row.names=FALSE, sep='\t') |