Mercurial > repos > prog > lcmsmatching
comparison massdb-helper.R @ 1:45e985cd8e9e draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit d4048accde6bdfd5b3e14f5394902d38991854f8-dirty
| author | prog |
|---|---|
| date | Tue, 31 Jan 2017 05:27:24 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:3afe41d3e9e7 | 1:45e985cd8e9e |
|---|---|
| 1 simplifySpectrum <- function(spec) { | |
| 2 if(length(spec) == 0){ | |
| 3 return(NA_real_) | |
| 4 } | |
| 5 #print(spec) | |
| 6 if (nrow(spec) == 0) | |
| 7 return(NA_real_) | |
| 8 if (ncol(spec) != 2) { | |
| 9 spec[, BIODB.PEAK.MZ] | |
| 10 mint <- BIODB.GROUP.INTENSITY %in% colnames(spec) | |
| 11 pint <- which(mint[1]) | |
| 12 if (length(pint) == 0) | |
| 13 stop( | |
| 14 "No intensity column founds, if there is more than 2 column, columns should be named", | |
| 15 paste0(BIODB.GROUP.INTENSITY, collapse = ", ") | |
| 16 ) | |
| 17 spec <- spec[, c(BIODB.PEAK.MZ, BIODB.GROUP.INTENSITY[pint[1]])] | |
| 18 ###Normalizing the intenities. | |
| 19 } | |
| 20 spec[, 2] <- as.numeric(spec[, 2]) * 100 / max(as.numeric(spec[, 2])) | |
| 21 colnames(spec) <- c(BIODB.PEAK.MZ, BIODB.PEAK.RELATIVE.INTENSITY) | |
| 22 spec | |
| 23 } | |
| 24 | |
| 25 | |
| 26 | |
| 27 calcDistance <- | |
| 28 function(spec1 , | |
| 29 spec2, | |
| 30 npmin = 2, | |
| 31 fun = c("wcosine"), | |
| 32 params = list()) { | |
| 33 #fun <- match.arg(fun) | |
| 34 | |
| 35 #SPec are always notmlized in pourcentage toa voir issues; | |
| 36 spec1 <- simplifySpectrum(spec1) | |
| 37 spec2 <- simplifySpectrum(spec2) | |
| 38 if(is.na(spec1)||is.na(spec2)) return(list(matched=numeric(0),similarity=0)) | |
| 39 params$mz1 <- as.numeric(spec1[, BIODB.PEAK.MZ]) | |
| 40 params$mz2 <- as.numeric(spec2[, BIODB.PEAK.MZ]) | |
| 41 params$int1 <- as.numeric(spec1[, BIODB.PEAK.RELATIVE.INTENSITY]) | |
| 42 params$int2 <- as.numeric(spec2[, BIODB.PEAK.RELATIVE.INTENSITY]) | |
| 43 res <- do.call(fun, args = params) | |
| 44 if (sum(res$matched != -1) < npmin) | |
| 45 return(list(matched = res$matched, similarity = 0)) | |
| 46 list(matched = res$matched, | |
| 47 similarity = res$measure) | |
| 48 } | |
| 49 | |
| 50 | |
| 51 | |
| 52 ###The returned sim list is not ordered | |
| 53 compareSpectra <- | |
| 54 function(spec, | |
| 55 libspec, | |
| 56 npmin = 2, | |
| 57 fun = BIODB.MSMS.DIST.WCOSINE, | |
| 58 params = list(), | |
| 59 decreasing = TRUE) { | |
| 60 #fun <- match.arg(fun) | |
| 61 if (length(libspec) == 0) { | |
| 62 return(NULL) | |
| 63 } | |
| 64 if (nrow(spec) == 0) { | |
| 65 return(NULL) | |
| 66 } | |
| 67 | |
| 68 ####spec is directly normalized. | |
| 69 vall <- | |
| 70 sapply( | |
| 71 libspec, | |
| 72 calcDistance, | |
| 73 spec1 = spec, | |
| 74 params = params, | |
| 75 fun = fun, | |
| 76 simplify = FALSE | |
| 77 ) | |
| 78 ####the list is ordered with the chosen metric. | |
| 79 sim <- | |
| 80 vapply(vall, | |
| 81 '[[', | |
| 82 i = "similarity", | |
| 83 FUN.VALUE = ifelse(decreasing, 0, 1)) | |
| 84 osim <- order(sim, decreasing = decreasing) | |
| 85 matched <- sapply(vall, '[[', i = "matched", simplify = FALSE) | |
| 86 | |
| 87 return(list( | |
| 88 ord = osim, | |
| 89 matched = matched, | |
| 90 similarity = sim | |
| 91 )) | |
| 92 } |
