comparison MS2snoop.R @ 3:c68c94865667 draft

planemo upload commit 09540e3a13b630b078a35afd3e129bc18c9953f0
author workflow4metabolomics
date Wed, 22 Jun 2022 13:07:01 +0000
parents df2672c37732
children 856001213966
comparison
equal deleted inserted replaced
2:a35fde23940e 3:c68c94865667
13 #' 13 #'
14 #' @import optparse 14 #' @import optparse
15 #' 15 #'
16 16
17 17
18 assign("MS2SNOOP_VERSION", "1.0.1") 18 assign("MS2SNOOP_VERSION", "1.1.0")
19 lockBinding("MS2SNOOP_VERSION", globalenv()) 19 lockBinding("MS2SNOOP_VERSION", globalenv())
20 20
21 assign("MISSING_PARAMETER_ERROR", 1) 21 assign("MISSING_PARAMETER_ERROR", 1)
22 lockBinding("MISSING_PARAMETER_ERROR", globalenv()) 22 lockBinding("MISSING_PARAMETER_ERROR", globalenv())
23 23
200 mzref, 200 mzref,
201 rtref, 201 rtref,
202 c_name, 202 c_name,
203 min_number_scan, 203 min_number_scan,
204 mzdecimal, 204 mzdecimal,
205 r_threshold=DEFAULT_EXTRACT_FRAGMENTS_R_THRESHOLD, 205 r_threshold = DEFAULT_EXTRACT_FRAGMENTS_R_THRESHOLD,
206 seuil_ra=DEFAULT_EXTRACT_FRAGMENTS_SEUIL_RA, 206 seuil_ra = DEFAULT_EXTRACT_FRAGMENTS_SEUIL_RA,
207 tolmz=DEFAULT_EXTRACT_FRAGMENTS_TOLMZ, 207 tolmz = DEFAULT_EXTRACT_FRAGMENTS_TOLMZ,
208 tolrt=DEFAULT_EXTRACT_FRAGMENTS_TOLRT 208 tolrt = DEFAULT_EXTRACT_FRAGMENTS_TOLRT
209 ) { 209 ) {
210 ## filter precursor in the precursors file based on mz and rt in the 210 ## filter precursor in the precursors file based on mz and rt in the
211 ## compound list 211 ## compound list
212 cat("processing ", c_name, "\n") 212 cat("processing ", c_name, "\n")
213 selected_precursors <- which( 213 selected_precursors <- which(
250 ## creation of cross table row=scan col=mz X=ra 250 ## creation of cross table row=scan col=mz X=ra
251 vmz <- levels(as.factor(sfrgtfil$mznominal)) 251 vmz <- levels(as.factor(sfrgtfil$mznominal))
252 252
253 if (global_verbose) { 253 if (global_verbose) {
254 cat(" fragments :", vmz) 254 cat(" fragments :", vmz)
255 cat("\n")
255 } 256 }
256 257
257 ## mz of precursor in data precursor to check correlation with 258 ## mz of precursor in data precursor to check correlation with
258 mz_prec <- paste0("mz", round(mean(sprec$mz), mzdecimal)) 259 mz_prec <- paste0("mz", round(mean(sprec$mz), mzdecimal))
259 260
370 cat(" non detected in fragments file \n") 371 cat(" non detected in fragments file \n")
371 } 372 }
372 if (!is.null(res_comp_by_file)) { 373 if (!is.null(res_comp_by_file)) {
373 res_comp <- rbind(res_comp, res_comp_by_file) 374 res_comp <- rbind(res_comp, res_comp_by_file)
374 } 375 }
375 if (global_verbose) {
376 cat("\n")
377 }
378 dev.off() 376 dev.off()
379 } 377 }
380 } else { 378 } else {
381 res_comp <- NULL 379 res_comp <- NULL
382 cat(" non detected in precursor file \n") 380 cat(" non detected in precursor file \n")
623 MISSING_INPUT_FILE_ERROR 621 MISSING_INPUT_FILE_ERROR
624 ) 622 )
625 } 623 }
626 } 624 }
627 625
626 get_csv_or_tsv <- function(
627 path,
628 sep_stack = c("\t", ",", ";"),
629 header = TRUE,
630 quote = "\""
631 ) {
632 sep <- sep_stack[1]
633 result <- tryCatch({
634 read.table(
635 file = path,
636 sep = sep,
637 header = header,
638 quote = quote
639 )
640 }, error = function(e) {
641 return(data.frame())
642 })
643 if (length(sep_stack) == 1) {
644 return(result)
645 }
646 # if (
647 # ncol(result) == 0 || ## failed
648 # ncol(result) == 1 ## only one row, suspicious, possible fail # nolint
649 # ) {
650 new_result <- get_csv_or_tsv(
651 path,
652 sep_stack = sep_stack[-1],
653 header = header,
654 quote = quote
655 )
656 if (ncol(new_result) > ncol(result)) {
657 return(new_result)
658 }
659 # }
660 return(result)
661 }
662
628 main <- function(args) { 663 main <- function(args) {
629 if (args$version) { 664 if (args$version) {
630 cat(sprintf("%s\n", MS2SNOOP_VERSION)) 665 cat(sprintf("%s\n", MS2SNOOP_VERSION))
631 base::quit(status = 0) 666 base::quit(status = 0)
632 } 667 }
637 } 672 }
638 if (args$verbose) { 673 if (args$verbose) {
639 set_verbose() 674 set_verbose()
640 } 675 }
641 ## MSpurity precursors file 676 ## MSpurity precursors file
642 precursors <- read.table( 677 precursors <- get_csv_or_tsv(args$precursors)
643 file = args$precursors,
644 header = TRUE,
645 sep = "\t",
646 quote = "\""
647 )
648 ## MSpurity fragments file 678 ## MSpurity fragments file
649 fragments <- read.table( 679 fragments <- get_csv_or_tsv(args$fragments)
650 file = args$fragments,
651 header = TRUE,
652 sep = "\t",
653 quote = "\""
654 )
655 ## list of compounds : col1=Name of molecule, col2=m/z, col3=retention time 680 ## list of compounds : col1=Name of molecule, col2=m/z, col3=retention time
656 compounds <- read.table( 681 compounds <- get_csv_or_tsv(args$compounds)
657 file = args$compounds,
658 sep = "\t",
659 quote = "\"",
660 header = TRUE
661 )
662 682
663 res_all <- NULL 683 res_all <- NULL
664 for (i in seq_len(nrow(compounds))) { 684 for (i in seq_len(nrow(compounds))) {
665 ## loop execution for all compounds in the compounds file 685 ## loop execution for all compounds in the compounds file
666 res_cor <- NULL 686 res_cor <- NULL