-
Notifications
You must be signed in to change notification settings - Fork 25
/
grouper-functions.R
126 lines (107 loc) · 3.31 KB
/
grouper-functions.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
## grouper functions called by .binPeaks
## .grouperStrict
## strict grouping function
## Don't allow peaks of one sample in the same bin.
##
## params:
## mass: double, sorted mass
## intensities: double, corresponding intensities
## samples: double, corresponding sample id numbers
## tolerance: double, maximal deviation of a peak position to be
## considered as same peak
##
## returns:
## NA if further splitting is needed
## meanMass (double) if all criteria are matched
##
.grouperStrict <- function(mass, intensities, samples, tolerance) {
## don't accept two or more peaks of the same sample
if (anyDuplicated(samples)) {
return(NA)
}
meanMass <- mean(mass)
## all peaks in range?
if (any(abs(mass - meanMass) / meanMass > tolerance)) {
return(NA)
}
meanMass
}
## .grouperRelaxed
## relaxed grouping function (more than one peak of one sample per bin possible)
## choose highest peak in range.
##
## params:
## mass: double, sorted mass
## intensities: double, corresponding intensities
## samples: double, corresponding sample id numbers
## tolerance: double, maximal deviation of a peak position to be
## considered as same peak
## meanMass: double, mean of mass (new peak position)
##
## returns:
## NA if further splitting is needed
## meanMass (double) if all criteria are matched
##
.grouperRelaxed <- function(mass, intensities, samples, tolerance) {
meanMass <- mean(mass)
## all peaks in range?
if (any(abs(mass - meanMass) / meanMass > tolerance)) {
return(NA)
}
## choose highest peak in duplicates
if (anyDuplicated(samples)) {
s <- sort.int(intensities, decreasing=TRUE, index.return=TRUE)
samples <- samples[s$ix]
noDup <- !duplicated(samples)
noDup[s$ix] <- noDup
## replace mass corresponding to highest intensity
mass[noDup] <- mean(mass[noDup])
return(mass)
}
meanMass
}
## .grouperRelaxedHighestAtReference
## relaxed grouping function (more than one peak of one sample per bin possible)
## Choose highest test sample peaks in range around a reference peak.
##
## params:
## mass: double, sorted mass
## intensities: double, corresponding intensities
## samples: double, corresponding sample id numbers (1==reference)
## tolerance: double, maximal deviation of a peak position to be
## considered as same peak
##
## returns:
## NA if further splitting is needed
## meanMass (double) if all criteria are matched else 0
##
.grouperRelaxedHighestAtReference <- function(mass, intensities, samples,
tolerance) {
## any reference peaks in current samples?
ref <- samples == 1L
nRef <- sum(ref)
if (nRef == 0L) {
## no reference peak
return(0L)
} else if (nRef > 1L) {
## too many reference peaks => further splitting needed
return(NA)
}
## only one mass should left as reference mass
meanMass <- mass[ref]
## all peaks in range?
if (any(abs(mass - meanMass) / meanMass > tolerance)) {
return(NA)
}
## choose highest peak in duplicates
if (anyDuplicated(samples)) {
s <- sort.int(intensities, decreasing=TRUE, index.return=TRUE)
sSamples <- samples[s$ix]
noDup <- !duplicated(sSamples)
sMass <- double(length(sSamples))
sMass[noDup] <- meanMass
mass[s$ix] <- sMass
return(mass)
}
meanMass
}