-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathNmrNormalization_script.R
158 lines (138 loc) · 7.07 KB
/
NmrNormalization_script.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#################################################################################################################
# SPECTRA NORMALIZATION FROM SPECTRAL DATA #
# User : Galaxy #
# Original data : -- #
# Starting date : 20-10-2014 #
# Version 1 : 27-01-2015 #
# Version 2 : 27-02-2015 #
# #
# Input files: #
# - Data matrix containing bucketed and integrated spectra to normalize #
# - Sample metadata matrix containing at least biological factor of interest #
# - Scaling method: Total intensity/Probabilistic Quotient Normalization #
# - Control group: name of control to compute median reference spectra #
# - Graph: normalization result representation #
#################################################################################################################
NmrNormalization <- function(dataMatrix,scalingMethod=c("None","Total","PQN","BioFactor"),sampleMetadata=NULL,
bioFactor=NULL,ControlGroup=NULL,graph=c("None","Overlay","One_per_individual"),
nomFichier=NULL,savLog.txtC=NULL)
{
## Option
##---------------
strAsFacL <- options()$stringsAsFactors
options(stingsAsFactors = FALSE)
options(warn = -1)
## Constants
##---------------
topEnvC <- environment()
flgC <- "\n"
## Log file (in case of integration into Galaxy)
##----------------------------------------------
if(!is.null(savLog.txtC))
sink(savLog.txtC, append = TRUE)
## Functions definition
##---------------------
#################################################################################################################
# Total intensity normalization
# Input parameters
# - data : bucketed spectra (rows=buckets; columns=samples)
#################################################################################################################
NmrBrucker_total <- function(data)
{
# Total intensity normalization
data.total <- apply(data,2,sum)
data.normalized <- data[,1]/data.total[1]
for (i in 2:ncol(data))
data.normalized <- cbind(data.normalized,data[,i]/data.total[i])
colnames(data.normalized) <- colnames(data)
rownames(data.normalized) <- rownames(data)
return(data.normalized)
}
#################################################################################################################
# Biological factor normalization
# Input parameters
# - data : bucketed spectra (rows=buckets; columns=samples)
# - sampleMetadata : dataframe with biological factor of interest measured for each invidual
# - bioFactor : name of the column cotaining the biological factor of interest
#################################################################################################################
NmrBrucker_bioFact <- function(data,sampleMetadata,bioFactor)
{
# Total intensity normalization
data.normalized <- data[,1]/bioFactor[1]
for (i in 2:ncol(data))
data.normalized <- cbind(data.normalized,data[,i]/bioFactor[i])
colnames(data.normalized) <- colnames(data)
rownames(data.normalized) <- rownames(data)
return(data.normalized)
}
#################################################################################################################
# Probabilistic quotient normalization (PQN)
# Input parameters
# - data : bucketed spectra (rows=buckets; columns=samples)
# - sampleMetadata : dataframe with treatment group of inviduals
# - pqnFactor : number of the column cotaining the biological facor of interest
# - nomControl : name of the treatment group
#################################################################################################################
NmrBrucker_pqn <- function(data,sampleMetadata,pqnFactor,nomControl)
{
# Total intensity normalization
data.total <- apply(data,2,sum)
data.normalized <- data[,1]/data.total[1]
for (i in 2:ncol(data))
data.normalized <- cbind(data.normalized,data[,i]/data.total[i])
colnames(data.normalized) <- colnames(data)
rownames(data.normalized) <- rownames(data)
# Reference spectrum
# Recuperation spectres individus controle
control.spectra <- data.normalized[,sampleMetadata[,pqnFactor]==nomControl]
spectrum.ref <- apply(control.spectra,1,median)
for (j in 1:length(spectrum.ref))
{
if (spectrum.ref[j] == 0)
spectrum.ref[j] <- mean(control.spectra[j, ])
if (spectrum.ref[j] == 0)
spectrum.ref[j] <- 10^(-24)
}
# Ratio between normalized and reference spectra
data.normalized.ref <- data.normalized/spectrum.ref
# Median ratio
data.normalized.ref.median <- apply(data.normalized.ref,1,median)
for (j in 1:length(data.normalized.ref.median))
if (data.normalized.ref.median[j] == 0 | is.na(data.normalized.ref.median[j]) | data.normalized.ref.median == "NaN" | data.normalized.ref.median == "NA")
data.normalized.ref.median[j] <- mean(data.normalized.ref[j, ])
# Normalization
data.normalizedPQN <- data.normalized[,1]/data.normalized.ref.median
for (i in 2:ncol(data))
data.normalizedPQN <- cbind(data.normalizedPQN,data.normalized[,i]/data.normalized.ref.median)
colnames(data.normalizedPQN) <- colnames(data)
rownames(data.normalizedPQN) <- rownames(data)
return(data.normalizedPQN)
}
## Tests
if (scalingMethod=="QuantitativeVariable")
{
if(mode(sampleMetadata[,bioFactor]) == "character")
bioFact <- factor(sampleMetadata[,bioFactor])
else
bioFact <- sampleMetadata[,bioFactor]
}
## Spectra scaling depending on the user choice
if (scalingMethod == "None")
{
NormalizedBucketedSpectra <- dataMatrix
}
else if (scalingMethod == "Total")
{
NormalizedBucketedSpectra <- NmrBrucker_total(dataMatrix)
}
else if (scalingMethod == "PQN")
{
NormalizedBucketedSpectra <- NmrBrucker_pqn(dataMatrix,sampleMetadata,bioFactor,ControlGroup)
}
else if (scalingMethod == "QuantitativeVariable")
{
NormalizedBucketedSpectra <- NmrBrucker_bioFact(dataMatrix,sampleMetadata,bioFact)
}
## OUTPUTS
return(list(NormalizedBucketedSpectra))
}