Mercurial > repos > prog > lcmsmatching
comparison MassbankSpectrum.R @ 0:e66bb061af06 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
author | prog |
---|---|
date | Tue, 12 Jul 2016 12:02:37 -0400 |
parents | |
children | 253d531a0193 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e66bb061af06 |
---|---|
1 if ( ! exists('MassbankSpectrum')) { # Do not load again if already loaded | |
2 | |
3 source('BiodbEntry.R') | |
4 source('MassbankCompound.R') | |
5 | |
6 ########################### | |
7 # MASSBANK SPECTRUM CLASS # | |
8 ########################### | |
9 | |
10 MassbankSpectrum <- setRefClass("MassbankSpectrum", contains = "BiodbEntry") | |
11 | |
12 ########### | |
13 # FACTORY # | |
14 ########### | |
15 | |
16 createMassbankSpectrumFromTxt <- function(contents, drop = TRUE) { | |
17 | |
18 library(stringr) | |
19 | |
20 spectra <- list() | |
21 | |
22 # Define fields regex | |
23 regex <- character() | |
24 regex[[RBIODB.ACCESSION]] <- "^ACCESSION: (.+)$" | |
25 regex[[RBIODB.MSDEV]] <- "^AC\\$INSTRUMENT: (.+)$" | |
26 regex[[RBIODB.MSDEVTYPE]] <- "^AC\\$INSTRUMENT_TYPE: (.+)$" | |
27 regex[[RBIODB.MSTYPE]] <- "^AC\\$MASS_SPECTROMETRY: MS_TYPE (.+)$" | |
28 regex[[RBIODB.MSPRECMZ]] <- "^MS\\$FOCUSED_ION: PRECURSOR_M/Z (.+)$" | |
29 regex[[RBIODB.NB.PEAKS]] <- "^PK\\$NUM_PEAK: ([0-9]+)$" | |
30 regex[[RBIODB.MSPRECANNOT]] <- "^MS\\$FOCUSED_ION: PRECURSOR_TYPE (.+)$" | |
31 | |
32 for (text in contents) { | |
33 | |
34 # Create instance | |
35 spectrum <- MassbankSpectrum$new() | |
36 | |
37 # Read text | |
38 lines <- strsplit(text, "\n") | |
39 for (s in lines[[1]]) { | |
40 | |
41 # Test generic regex | |
42 parsed <- FALSE | |
43 for (field in names(regex)) { | |
44 g <- str_match(s, regex[[field]]) | |
45 if ( ! is.na(g[1,1])) { | |
46 spectrum$setField(field, g[1,2]) | |
47 parsed <- TRUE | |
48 break | |
49 } | |
50 } | |
51 if (parsed) | |
52 next | |
53 | |
54 # MS MODE | |
55 g <- str_match(s, "^AC\\$MASS_SPECTROMETRY: ION_MODE (.+)$") | |
56 if ( ! is.na(g[1,1])) { | |
57 spectrum$setField(RBIODB.MSMODE, if (g[1,2] == 'POSITIVE') RBIODB.MSMODE.POS else RBIODB.MSMODE.NEG) | |
58 next | |
59 } | |
60 | |
61 # PEAKS | |
62 if (.parse.peak.line(spectrum, s)) | |
63 next | |
64 } | |
65 | |
66 spectra <- c(spectra, spectrum) | |
67 } | |
68 | |
69 # Replace elements with no accession id by NULL | |
70 spectra <- lapply(spectra, function(x) if (is.na(x$getField(RBIODB.ACCESSION))) NULL else x) | |
71 | |
72 # Set associated compounds | |
73 compounds <- createMassbankCompoundFromTxt(contents) | |
74 for (i in seq(spectra)) | |
75 if ( ! is.null(spectra[[i]])) | |
76 spectra[[i]]$setField(RBIODB.COMPOUND, compounds[[i]]) | |
77 | |
78 # If the input was a single element, then output a single object | |
79 if (drop && length(contents) == 1) | |
80 spectra <- spectra[[1]] | |
81 | |
82 return(spectra) | |
83 } | |
84 | |
85 ################### | |
86 # PARSE PEAK LINE # | |
87 ################### | |
88 | |
89 .parse.peak.line <- function(spectrum, line) { | |
90 | |
91 peaks <- RBIODB.PEAK.DF.EXAMPLE | |
92 | |
93 # Annotation | |
94 g <- str_match(line, "^\\s+([0-9][0-9.]*) ([A-Z0-9+-]+) ([0-9]+) ([0-9][0-9.]*) ([0-9][0-9.]*)$") | |
95 if ( ! is.na(g[1,1])) | |
96 peaks[1, c(RBIODB.PEAK.MZ, RBIODB.PEAK.FORMULA, RBIODB.PEAK.FORMULA.COUNT, RBIODB.PEAK.MASS, RBIODB.PEAK.ERROR.PPM)] <- list(as.double(g[1,2]), g[1,3], as.integer(g[1,4]), as.double(g[1,5]), as.double(g[1,6])) | |
97 | |
98 # Peak | |
99 g <- str_match(line, "^\\s+([0-9][0-9.]*) ([0-9][0-9.]*) ([0-9]+)$") | |
100 if ( ! is.na(g[1,1])) | |
101 peaks[1, c(RBIODB.PEAK.MZ, RBIODB.PEAK.INTENSITY, RBIODB.PEAK.RELATIVE.INTENSITY)] <- list(as.double(g[1,2]), as.double(g[1,3]), as.integer(g[1,4])) | |
102 | |
103 if (nrow(peaks) > 0) { | |
104 | |
105 # Get curent peaks and merge with new peaks | |
106 current.peaks <- spectrum$getField(RBIODB.PEAKS) | |
107 if ( ! is.null(current.peaks)) | |
108 peaks <- rbind(current.peaks, peaks) | |
109 | |
110 spectrum$setField(RBIODB.PEAKS, peaks) | |
111 | |
112 return(TRUE) | |
113 } | |
114 | |
115 return(FALSE) | |
116 } | |
117 } |