Mercurial > repos > tomnl > create_msp
comparison create_sqlite_db.R @ 0:4b417094bf71 draft
planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 0aa10df0ec1ed71601f932cfb11d7d4d4f620d80-dirty
| author | tomnl |
|---|---|
| date | Wed, 02 May 2018 13:09:23 -0400 |
| parents | |
| children | 7ae48818a229 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4b417094bf71 |
|---|---|
| 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 option_list <- list( | |
| 50 make_option(c("-o", "--out_dir"), type="character"), | |
| 51 make_option("--pa", type="character"), | |
| 52 make_option("--xset_xa", type="character"), | |
| 53 make_option("--xcms_camera_option", type="character"), | |
| 54 make_option("--eic", action="store_true"), | |
| 55 make_option("--cores", default=4), | |
| 56 make_option("--mzML_files", type="character"), | |
| 57 make_option("--galaxy_names", type="character"), | |
| 58 make_option("--grp_peaklist", type="character"), | |
| 59 make_option("--db_name", type="character", default='lcms_data.sqlite'), | |
| 60 make_option("--raw_rt_columns", action="store_true") | |
| 61 ) | |
| 62 | |
| 63 # store options | |
| 64 opt<- parse_args(OptionParser(option_list=option_list)) | |
| 65 | |
| 66 loadRData <- function(rdata_path, name){ | |
| 67 #loads an RData file, and returns the named xset object if it is there | |
| 68 load(rdata_path) | |
| 69 return(get(ls()[ls() == name])) | |
| 70 } | |
| 71 | |
| 72 print(paste('pa', opt$pa)) | |
| 73 print(opt$xset) | |
| 74 print(opt$xcms_camera_option) | |
| 75 # Requires | |
| 76 pa <- loadRData(opt$pa, 'pa') | |
| 77 | |
| 78 print('TESTETSTESTETE') | |
| 79 print(pa@fileList) | |
| 80 | |
| 81 | |
| 82 if (opt$xcms_camera_option=='xcms'){ | |
| 83 xset <- loadRData(opt$xset, 'xset') | |
| 84 fix <- xset_pa_filename_fix(opt, pa, xset) | |
| 85 pa <- fix[[1]] | |
| 86 xset <- fix[[2]] | |
| 87 xa <- NULL | |
| 88 }else{ | |
| 89 | |
| 90 xa <- loadRData(opt$xset, 'xa') | |
| 91 fix <- xset_pa_filename_fix(opt, pa, xa@xcmsSet) | |
| 92 pa <- fix[[1]] | |
| 93 xa@xcmsSet <- fix[[2]] | |
| 94 xset <- NULL | |
| 95 } | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 if(is.null(opt$grp_peaklist)){ | |
| 102 grp_peaklist = NA | |
| 103 }else{ | |
| 104 grp_peaklist = opt$grp_peaklist | |
| 105 } | |
| 106 | |
| 107 | |
| 108 print(pa@fileList) | |
| 109 print(xset@filepaths) | |
| 110 | |
| 111 | |
| 112 db_pth <- msPurity::create_database(pa, xset=xset, xsa=xa, out_dir=opt$out_dir, | |
| 113 grp_peaklist=grp_peaklist, db_name=opt$db_name) | |
| 114 | |
| 115 | |
| 116 if (!is.null(opt$eic)){ | |
| 117 if (is.null(opt$raw_rt_columns)){ | |
| 118 rtrawColumns <- FALSE | |
| 119 }else{ | |
| 120 rtrawColumns <- TRUE | |
| 121 } | |
| 122 | |
| 123 # Saves the EICS into the previously created database | |
| 124 px <- msPurity::purityX(xset, saveEIC = TRUE, | |
| 125 cores=opt$cores, sqlitePth=db_pth, | |
| 126 rtrawColumns = rtrawColumns) | |
| 127 } | |
| 128 | |
| 129 | |
| 130 | |
| 131 con <- DBI::dbConnect(RSQLite::SQLite(), db_pth) | |
| 132 | |
| 133 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, ', | |
| 135 '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 ', | |
| 137 'LEFT JOIN c_peak_X_c_peak_group AS cXg ON cXg.grpid=cpg.grpid ', | |
| 138 'LEFT JOIN c_peaks on c_peaks.cid=cXg.cid ', | |
| 139 'LEFT JOIN c_peak_X_s_peak_meta AS cXs ON cXs.cid=c_peaks.cid ', | |
| 140 'LEFT JOIN s_peak_meta ON cXs.pid=s_peak_meta.pid ', | |
| 141 'LEFT JOIN fileinfo ON s_peak_meta.fileid=fileinfo.fileid') | |
| 142 | |
| 143 print(cmd) | |
| 144 cpeakgroup_msms <- DBI::dbGetQuery(con, cmd) | |
| 145 | |
| 146 write.table(cpeakgroup_msms, file.path(opt$out_dir, 'cpeakgroup_msms.tsv'), row.names=FALSE, sep='\t') |
