Mercurial > repos > prog > lcmsmatching
comparison PeakforestConn.R @ 2:20d69a062da3 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit d4048accde6bdfd5b3e14f5394902d38991854f8
author | prog |
---|---|
date | Thu, 02 Mar 2017 08:55:00 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:253d531a0193 | 2:20d69a062da3 |
---|---|
1 ##################### | |
2 # CLASS DECLARATION # | |
3 ##################### | |
4 #'A class to connect to peakforest | |
5 #'@export | |
6 #'@field .url An urel to the database | |
7 PeakforestConn <- methods::setRefClass("PeakforestConn", contains = c("RemotedbConn","MassdbConn"), fields = list( .url = "character" )) # TODO Inherits also from MassdbConn | |
8 | |
9 ########################## | |
10 # GET ENTRY CONTENT TYPE # | |
11 ########################## | |
12 | |
13 PeakforestConn$methods( getEntryContentType = function(type) { | |
14 return(BIODB.JSON) | |
15 }) | |
16 | |
17 ##################### | |
18 # GET ENTRY CONTENT # | |
19 ##################### | |
20 | |
21 PeakforestConn$methods( getEntryContent = function(id) { | |
22 | |
23 | |
24 # Initialize return values | |
25 content <- rep(NA_character_, length(id)) | |
26 # Request | |
27 | |
28 url <- get.entry.url(BIODB.PEAKFOREST, id[i], BIODB.JSON,token = .self$.token) | |
29 jsonstr <- .self$.get.url(url) | |
30 if(startsWith("<html>", jsonstr) ){ | |
31 next | |
32 } | |
33 | |
34 return(content) | |
35 }) | |
36 | |
37 | |
38 ########################################## | |
39 # SEARCH FOR SPECTRA IN GIVEN MASS RANGE # | |
40 ########################################## | |
41 | |
42 PeakforestConn$methods( searchMzRange = function(mzmin, mzmax, rtype = c("object","spec","peak")){ | |
43 | |
44 rtype <- match.arg(rtype) | |
45 if(mzmin>mzmax){ | |
46 stop("mzmin shloud be inferior to mzmax in searchMzRange.") | |
47 } | |
48 | |
49 url <- paste0("https://rest.peakforest.org/spectra/lcms/peaks/get-range/",mzmin,"/",mzmax) | |
50 | |
51 contents <- .self$.get.url(url) | |
52 | |
53 jsontree <- fromJSON(contents) | |
54 | |
55 ###No match form the output. | |
56 if( length(jsontree)==0 ) return(NULL) | |
57 | |
58 # Getting a list of all the id. | |
59 lid <- sapply(jsontree,function(x){ | |
60 x$source$id | |
61 }) | |
62 | |
63 # Returning the content for all the spectra | |
64 contents <- .self$getEntryContent(lid) | |
65 | |
66 entries <- .self$createEntry(contents) | |
67 | |
68 # Checking the return type | |
69 if( rtype=="object" ){ | |
70 return( entries ) | |
71 } | |
72 | |
73 ### XXXX See if we don't want to reduce the output and factorize this shit. | |
74 toreturn <- NULL | |
75 if( rtype=="spec" ){ | |
76 toreturn <- sapply(entries,function(x){ | |
77 x$getFieldsAsDataFrame() | |
78 }) | |
79 } | |
80 if( rtype=="peak" ){ | |
81 toreturn <- lapply(entries,function(x){ | |
82 temp <- as.data.frame( x$getFieldValue( BIODB.PEAKS )) | |
83 temp$accession = x$getFieldValue( BIODB.ACCESSION) | |
84 return(temp) | |
85 | |
86 }) | |
87 } | |
88 ###Trying to convert in data.frame | |
89 if(!is.data.frame(toreturn)){ | |
90 temp <- colnames(toreturn[[1]]) | |
91 toreturn <- do.call("rbind.fill",toreturn) | |
92 colnames(toreturn) <- temp | |
93 } | |
94 | |
95 return(toreturn) | |
96 }) | |
97 | |
98 | |
99 ################################################# | |
100 # SEARCH FOR SPECTRA IN A TOLERANCE AROUND A MZ # | |
101 ################################################# | |
102 | |
103 PeakforestConn$methods( searchMzTol = function(mz, tol, tolunit=BIODB.MZTOLUNIT.VALS, | |
104 rtype = c("object","spec","peak")){ | |
105 | |
106 rtype <- match.arg(rtype) | |
107 tolunit <- match.arg(tolunit) | |
108 | |
109 if( tolunit == BIODB.MZTOLUNIT.PPM){ | |
110 tol <- tol * mz * 10^-6 | |
111 } | |
112 | |
113 mzmin <- mz - tol | |
114 mzmax <- mz + tol | |
115 | |
116 return(.self$searchMzRange(mzmin, mzmax, rtype = rtype)) | |
117 | |
118 }) | |
119 | |
120 ################################################## | |
121 # SEARCH FOR MSMS SPECTRA PRECUSOR AROUND A MASS # | |
122 ################################################## | |
123 | |
124 | |
125 PeakforestConn$methods( | |
126 searchSpecPrecTol = function(mz, | |
127 tol, | |
128 tolunit = "plain", | |
129 mode = NULL) { | |
130 #TODO handle the units | |
131 #tolunit <- match.arg(tolunit) | |
132 | |
133 strmode <- '' | |
134 | |
135 if (!is.null(mode)) { | |
136 if (mode %in% c(BIODB.MSMODE.NEG, BIODB.MSMODE.POS)) { | |
137 strmode <- paste0('?polarity=', mode) | |
138 } | |
139 | |
140 } | |
141 | |
142 if (tolunit == BIODB.MZTOLUNIT.PPM) { | |
143 tol <- tol * mz * 10 ^ -6 | |
144 } | |
145 | |
146 ##Request which return peak and not spectra. | |
147 url <- | |
148 paste0( | |
149 "https://rest.peakforest.org/spectra/lcms/search-naive/", | |
150 mz, | |
151 "/", | |
152 tol, | |
153 strmode | |
154 ) | |
155 contents <- .self$.get.url(url) | |
156 entries <- .self$createReducedEntry(contents, drop = TRUE) | |
157 return(entries) | |
158 } | |
159 ) | |
160 | |
161 | |
162 ################ | |
163 # CREATE ENTRY # | |
164 ################ | |
165 | |
166 # Creates a Spectrum instance from file content. | |
167 # content A file content, downloaded from the public database. | |
168 # RETURN A spectrum instance. | |
169 PeakforestConn$methods( createEntry = function(content, drop = TRUE) { | |
170 return(createPeakforestSpectraFromJSON(content, drop = drop)) | |
171 }) | |
172 | |
173 PeakforestConn$methods( createReducedEntry = function(content , drop = TRUE){ | |
174 entries <- createReducedSpectraFromJSON(content, drop = drop) | |
175 return(entries) | |
176 }) |