Mercurial > repos > tomnl > create_msp
comparison create_sqlite_db.R @ 4:7ae48818a229 draft
planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 97c9122c9af9ddca3a2498185ce8c7919c0df03a
| author | tomnl |
|---|---|
| date | Tue, 08 May 2018 05:28:37 -0400 |
| parents | 4b417094bf71 |
| children | 278a7e7a84cf |
comparison
equal
deleted
inserted
replaced
| 3:3ecba7815437 | 4:7ae48818a229 |
|---|---|
| 55 make_option("--cores", default=4), | 55 make_option("--cores", default=4), |
| 56 make_option("--mzML_files", type="character"), | 56 make_option("--mzML_files", type="character"), |
| 57 make_option("--galaxy_names", type="character"), | 57 make_option("--galaxy_names", type="character"), |
| 58 make_option("--grp_peaklist", type="character"), | 58 make_option("--grp_peaklist", type="character"), |
| 59 make_option("--db_name", type="character", default='lcms_data.sqlite'), | 59 make_option("--db_name", type="character", default='lcms_data.sqlite'), |
| 60 make_option("--raw_rt_columns", action="store_true") | 60 make_option("--raw_rt_columns", action="store_true"), |
| 61 make_option("--metfrag_result", type="character"), | |
| 62 make_option("--sirius_csifingerid_result", type="character"), | |
| 63 make_option("--probmetab_result", type="character") | |
| 61 ) | 64 ) |
| 65 | |
| 62 | 66 |
| 63 # store options | 67 # store options |
| 64 opt<- parse_args(OptionParser(option_list=option_list)) | 68 opt<- parse_args(OptionParser(option_list=option_list)) |
| 65 | 69 |
| 66 loadRData <- function(rdata_path, name){ | 70 loadRData <- function(rdata_path, name){ |
| 73 print(opt$xset) | 77 print(opt$xset) |
| 74 print(opt$xcms_camera_option) | 78 print(opt$xcms_camera_option) |
| 75 # Requires | 79 # Requires |
| 76 pa <- loadRData(opt$pa, 'pa') | 80 pa <- loadRData(opt$pa, 'pa') |
| 77 | 81 |
| 78 print('TESTETSTESTETE') | 82 |
| 79 print(pa@fileList) | 83 print(pa@fileList) |
| 80 | 84 |
| 81 | 85 |
| 82 if (opt$xcms_camera_option=='xcms'){ | 86 if (opt$xcms_camera_option=='xcms'){ |
| 83 xset <- loadRData(opt$xset, 'xset') | 87 xset <- loadRData(opt$xset, 'xset') |
| 124 px <- msPurity::purityX(xset, saveEIC = TRUE, | 128 px <- msPurity::purityX(xset, saveEIC = TRUE, |
| 125 cores=opt$cores, sqlitePth=db_pth, | 129 cores=opt$cores, sqlitePth=db_pth, |
| 126 rtrawColumns = rtrawColumns) | 130 rtrawColumns = rtrawColumns) |
| 127 } | 131 } |
| 128 | 132 |
| 129 | |
| 130 | |
| 131 con <- DBI::dbConnect(RSQLite::SQLite(), db_pth) | 133 con <- DBI::dbConnect(RSQLite::SQLite(), db_pth) |
| 134 | |
| 135 add_extra_table_elucidation <- function(name, pth){ | |
| 136 | |
| 137 if (!is.null(pth)){ | |
| 138 | |
| 139 print(pth) | |
| 140 df <- read.table(pth, header = TRUE, sep='\t', stringsAsFactors = FALSE, comment.char = "") | |
| 141 # bug for repeating headers | |
| 142 df <- df[!df$UID=='UID',] | |
| 143 | |
| 144 # get peakid, an scan id | |
| 145 df_ids <- stringr::str_split_fixed(df$UID, '-', 3) | |
| 146 colnames(df_ids) <- c('grp_id', 'file_id', 'peak_id') | |
| 147 df <- cbind(df_ids, df) | |
| 148 # export to database | |
| 149 | |
| 150 | |
| 151 DBI::dbWriteTable(con, name=name, value=df, row.names=FALSE) | |
| 152 | |
| 153 } | |
| 154 | |
| 155 } | |
| 156 | |
| 157 | |
| 158 add_probmetab <- function(pth){ | |
| 159 if (!is.null(pth)){ | |
| 160 | |
| 161 df <- read.table(pth, header = TRUE, sep='\t', stringsAsFactors = FALSE, comment.char = "") | |
| 162 df$grp_id <- 1:nrow(df) | |
| 163 start <- T | |
| 164 for (i in 1:nrow(df)){ | |
| 165 | |
| 166 x <- df[i,] | |
| 167 | |
| 168 | |
| 169 if(is.na(x$proba) | x$proba =='NA'){ | |
| 170 | |
| 171 next | |
| 172 } | |
| 173 | |
| 174 mpc <- stringr::str_split(x$mpc, ';') | |
| 175 proba <- stringr::str_split(x$proba, ';') | |
| 176 | |
| 177 for (j in 1:length(mpc[[1]])){ | |
| 178 | |
| 179 row <- c(x$grp_id, x$propmz, mpc[[1]][j], proba[[1]][j]) | |
| 180 | |
| 181 if (start){ | |
| 182 df_out <- data.frame(t(row), stringsAsFactors=F) | |
| 183 start <- F | |
| 184 }else{ | |
| 185 df_out <- data.frame(rbind(df_out, row), stringsAsFactors=F) | |
| 186 } | |
| 187 print(df_out) | |
| 188 | |
| 189 } | |
| 190 | |
| 191 } | |
| 192 | |
| 193 colnames(df_out) <- c('grp_id', 'propmz', 'mpc', 'proba') | |
| 194 DBI::dbWriteTable(con, name='probmetab_results', value=df_out, row.names=FALSE) | |
| 195 | |
| 196 } | |
| 197 | |
| 198 | |
| 199 } | |
| 200 | |
| 201 add_extra_table_elucidation('metfrag_results', opt$metfrag_result) | |
| 202 add_extra_table_elucidation('sirius_csifingerid_results', opt$sirius_csifingerid_result) | |
| 203 add_probmetab(opt$probmetab_result) | |
| 204 | |
| 205 | |
| 206 | |
| 132 | 207 |
| 133 cmd <- paste('SELECT cpg.grpid, cpg.mz, cpg.mzmin, cpg.mzmax, cpg.rt, cpg.rtmin, cpg.rtmax, c_peaks.cid, ', | 208 cmd <- paste('SELECT cpg.grpid, cpg.mz, cpg.mzmin, cpg.mzmax, cpg.rt, cpg.rtmin, cpg.rtmax, c_peaks.cid, ', |
| 134 'c_peaks.mzmin AS c_peak_mzmin, c_peaks.mzmax AS c_peak_mzmax, ', | 209 'c_peaks.mzmin AS c_peak_mzmin, c_peaks.mzmax AS c_peak_mzmax, ', |
| 135 'c_peaks.rtmin AS c_peak_rtmin, c_peaks.rtmax AS c_peak_rtmax, s_peak_meta.*, fileinfo.filename, fileinfo.nm_save ', | 210 'c_peaks.rtmin AS c_peak_rtmin, c_peaks.rtmax AS c_peak_rtmax, s_peak_meta.*, fileinfo.filename, fileinfo.nm_save ', |
| 136 'FROM c_peak_groups AS cpg ', | 211 'FROM c_peak_groups AS cpg ', |
