-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-functions.R
65 lines (52 loc) · 1.87 KB
/
data-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
library("MALDIquant")
toConsensusSpectraData <- function(data,
tolerance = 0.002,
POP = 0.5) {
consensusSpectra <- list()
sampleNames <- data$sampleNames
firstReplicateIndex <- 1
for (i in 1:length(sampleNames)) {
currentSample <- sampleNames[i]
currentSampleIndexes <-
which(data$spectraSampleNames == currentSample)
numSamples <- length(currentSampleIndexes)
sampleSpectra <- data$spectra[currentSampleIndexes]
binnedPeaks <- binPeaks(sampleSpectra, tolerance = tolerance)
binnedPeaksMatrix <- intensityMatrix(binnedPeaks)
consensusMasses <- vector()
consensusIntensities <- vector()
for (j in 1:ncol(binnedPeaksMatrix)) {
consensusPeaks <- which(binnedPeaksMatrix[, j] != 0)
currentPresence <- length(consensusPeaks) / numSamples
if (currentPresence >= POP) {
consensusIntensities <-
c(consensusIntensities, mean(binnedPeaksMatrix[consensusPeaks, j]))
consensusMasses <-
c(consensusMasses, colnames(binnedPeaksMatrix)[j])
}
}
currentConsensusSpectrum <- createMassPeaks(
mass = as.numeric(consensusMasses),
intensity = as.numeric(consensusIntensities),
metaData = list(name = currentSample)
)
consensusSpectra <-
c(consensusSpectra, currentConsensusSpectrum)
}
list(
spectraNames = sampleNames,
spectra = consensusSpectra,
spectraColors = data$samplesColors,
spectraConditions = data$samplesConditions,
datasetConditions = data$datasetConditions,
datasetConditionsColors = data$datasetConditionsColors
)
}
asPresenceMatrix <- function(data,
present = 1,
absent = 0) {
toret <- data
toret[data > 0] <- present
toret[is.na(data)] <- absent
toret
}