annotate create_msp.R @ 22:c8b34df46dae draft

planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 04023134d8f28e85927ca293373c506484149ead-dirty
author tomnl
date Fri, 01 Jun 2018 13:41:15 -0400
parents 079bdef733e8
children aa0cdd943aa7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
1 suppressWarnings(suppressPackageStartupMessages(require(optparse,quietly = T)))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
2 suppressWarnings(suppressPackageStartupMessages(require(msPurity)))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
3 print(sessionInfo())
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
4 # Get the parameter
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
5 option_list <- list(
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
6 make_option(c("-i","--purity"),type="character"),
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
7 make_option(c("-p","--ppm"),type="numeric"),
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
8 make_option(c("-m","--mode"),type="character")
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
9 )
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
10 opt <- parse_args(OptionParser(option_list=option_list))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
11
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
12 print(opt)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
13
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
14 load(opt$purity)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
15
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
16 grped_df <- pa@grped_df
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
17
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
18 msms <- pa@grped_ms2
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
19
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
20 puritydf <- pa@puritydf
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
21
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
22
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
23
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
24
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
25
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
26 grped_df$fileid <- sapply(grped_df$filename, function(x) which(basename(pa@fileList)==x))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
27
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
28 puritydf$fileid <- sapply(puritydf$filename, function(x) which(basename(pa@fileList)==x))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
29
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
30 selfrag <- as.numeric(unique(grped_df$grpid))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
31
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
32 of <- file(description = "outfile.msp", open = "w+a")
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
33
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
34 write.msp <- function(name,precmz,prectype,spectra,ofile){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
35
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
36 cat(paste("NAME: ", name, "\r\n", sep = ""), file = ofile)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
37
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
38 cat(paste("PRECURSORMZ: ", precmz , "\r\n", sep = ""), file = ofile)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
39
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
40 #cat(paste("PRECURSORTYPE: ", prectype, "\r\n", sep = ""), file = ofile) # No adducts? Annotation
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
41
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
42 cat("Comment:\r\n", file = ofile)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
43
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
44 cat(paste("Num Peaks: ", nrow(spectra), "\r\n", sep = ""), file = ofile)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
45
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
46 cat(paste(paste(spectra[,1], spectra[,2], sep = "\t"), sep = "\r\n"), sep = "\r\n", file = ofile)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
47
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
48 cat("\r\n", file = ofile)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
49 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
50
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
51 for(i in selfrag){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
52
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
53 j <- which(grped_df$grpid==i)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
54
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
55 spec <- msms[[as.character(i)]]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
56
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
57 if (length(j)>1){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
58
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
59 grpd <- grped_df[j,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
60
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
61 if (opt$mode=="all"){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
62
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
63 for(jj in 1:length(j)){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
64
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
65 idj <- paste(i,jj,sep=".")
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
66
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
67 specj <- spec[[jj]]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
68
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
69 grpdj <- grpd[jj,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
70
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
71 name = paste(i, grpdj$sample, grpdj$pid, sep='-')
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
72
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
73 write.msp(name,grpdj$precurMtchMZ,"",specj,of)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
74 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
75
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
76 }else{
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
77
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
78 prec_int <- sapply(grpd$precurMtchID, function(x) puritydf[ which(puritydf$seqNum==x & puritydf$fileid==grpd$fileid[1]), "precursorIntensity"] )
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
79
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
80 if (opt$mode=="max"){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
81
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
82 idx <- which(prec_int==max(prec_int))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
83
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
84 grpd <- grpd[idx,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
85 name = paste(i, grpd$sample, grpd$pid, sep='-')
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
86 write.msp(name,grpd$precurMtchMZ,"",specj[[idx]], of)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
87 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
88
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
89 if (opt$mode=="average"){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
90
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
91 file_weights <- prec_int/prec_int[which(prec_int==min(prec_int))] # spectra of the most intense precursor, largest weight
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
92
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
93 merged_msms <- do.call("rbind", spec)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
94
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
95 colnames(merged_msms) <- c("mz","int")
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
96
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
97 file_weights <-rep(file_weights,sapply(spec,nrow))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
98
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
99 merged_msms <- data.frame(cbind(merged_msms,file_weights))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
100
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
101 umzs <- sort(merged_msms$mz,index.return=T)$ix
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
102
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
103 merged_msms <- merged_msms[umzs,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
104
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
105 umzs <- merged_msms$mz
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
106
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
107 mz_groups <- list() # mz windows to bind
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
108
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
109 mz_grouped <- c() # used mzs
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
110
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
111 ppm <- opt$ppm # ppm level to bind mzs
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
112
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
113 for(y in 1:length(umzs)){ # spectra averaging
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
114
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
115 z <- umzs[y]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
116
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
117 if(!(z %in% mz_grouped)){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
118
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
119 mz_range <- z*(ppm/1e6)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
120
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
121 mz_range <- c(z-mz_range,z+mz_range)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
122
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
123 mz_group <- which(umzs>mz_range[1] & umzs<mz_range[2])
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
124
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
125 if(length(mz_group)>1){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
126
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
127 zz <- umzs[max(mz_group)]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
128
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
129 mz_range <- zz*(ppm/1e6)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
130
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
131 mz_range <- c(zz-mz_range,zz+mz_range)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
132
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
133 mz_group2 <- which(umzs>mz_range[1] & umzs<mz_range[2])
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
134
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
135 mz_group <- append(mz_group,mz_group2)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
136 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
137
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
138 mz_group <- unique(mz_group)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
139
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
140 mz_grouped <- append(mz_grouped,umzs[mz_group])
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
141
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
142 mz_group <- list(mz_group)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
143
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
144 mz_groups <- append(mz_groups,mz_group)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
145 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
146 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
147
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
148 averaged_spec <- t(sapply(mz_groups,function(x){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
149
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
150 if(length(x)==1){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
151
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
152 mz <- merged_msms$mz[x]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
153
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
154 nint <- merged_msms[x,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
155
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
156 nint <- nint$int*nint$file_weights
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
157
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
158 }else{
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
159
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
160 mz <- mean(merged_msms$mz[x])
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
161
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
162 nint <- sum(sapply(x,function(y){
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
163 nint <- merged_msms[y,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
164 nint <- nint$int*nint$file_weights
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
165 }))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
166 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
167 return(c(mz,nint))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
168 }))
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
169
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
170 write.msp(i,mean(grpd$precurMtchMZ),"",averaged_spec,of)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
171
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
172 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
173 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
174 }else{
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
175 spec <- spec[[1]]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
176
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
177 grpd <- grped_df[j,]
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
178 name = paste(i, grpd$sample, grpd$pid, sep='-')
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
179 write.msp(name,grpd$precurMtchMZ,"",spec,of)
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
180 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
181 }
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
182
079bdef733e8 planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit afee93a926072b534bb3541e1524f1ad6a7decb9
tomnl
parents:
diff changeset
183