-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget.R
107 lines (82 loc) · 2.26 KB
/
get.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' @importFrom stats aggregate
#' @importFrom dplyr bind_rows group_by summarise
#' @importFrom magrittr %>%
sampProcess <- function(file,scans,dp){
pl <- getFile(file,scans) %>%
mutate(mz = round(mz,dp)) %>%
group_by(polarity,mz) %>%
summarise(intensity = sum(intensity)/length(scans))
return(pl)
}
#' @importFrom mzR openMSfile peaks close
#' @importFrom purrr map
#' @importFrom dplyr bind_rows
#' @importFrom tibble as_tibble
#' @importFrom magrittr set_colnames set_names
getFile <- function(file,scans){
ms <- openMSfile(file,backend = 'pwiz')
hd <- header(ms)
headerTemp(file,hd)
hd <- hd %>%
select(seqNum,polarity,filterString) %>%
group_by(polarity,filterString) %>%
mutate(scan = 1:dplyr::n()) %>%
filter(scan %in% scans)
hd$polarity[hd$polarity == 0] <- 'n'
hd$polarity[hd$polarity == 1] <- 'p'
file_peaks <- ms %>%
peaks() %>%
.[hd$seqNum] %>%
map(~{
d <- .
d %>%
set_colnames(c('mz','intensity')) %>%
as_tibble() %>%
filter(intensity > 0)
}) %>%
set_names(hd$seqNum) %>%
bind_rows(.id = 'seqNum') %>%
mutate(seqNum = as.numeric(seqNum)) %>%
left_join(hd, by = "seqNum") %>%
select(-filterString,-seqNum)
return(file_peaks)
}
#' @importFrom parallel makeCluster stopCluster parLapply clusterExport
#' @importFrom dplyr mutate
getPeaks <- function(files,scans){
idx <- tibble(
fileName = files
) %>%
rowid_to_column(var = 'idx')
dp <- binnerDP()
pks <- future_map(files,getFile,scans = scans) %>%
set_names(idx$idx) %>%
bind_rows(.id = 'idx') %>%
mutate(idx = as.numeric(idx)) %>%
left_join(idx,
by = 'idx') %>%
mutate(fileName = basename(fileName),
mz = round(mz,5),bin = round(mz,dp))
return(pks)
}
#' @importFrom mzR header
getHeaders <- function(files){
idx <- tibble(
FileName = files
) %>%
rowid_to_column(var = 'idx')
available_header_temps <- availableHeaderTemps(files)
file_headers <- available_header_temps %>%
future_map(readRDS)
file_headers <- file_headers %>%
set_names(idx$idx) %>%
bind_rows(.id = 'idx') %>%
mutate(
idx = as.numeric(idx)
) %>%
left_join(idx,
by = 'idx') %>%
select(idx,FileName,acquisitionNum,totIonCurrent,polarity,filterString) %>%
as_tibble()
return(file_headers)
}