-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproteinConcNormalization.R
More file actions
151 lines (130 loc) · 6.24 KB
/
proteinConcNormalization.R
File metadata and controls
151 lines (130 loc) · 6.24 KB
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
rppa.proteinConc.normalize <- function(slideA, slideB, normalize.with.median.first = T,
target.column="Slide", normalize.per.deposition=F, output.all=F)
{
#check if target column is free on both slides
if(output.all && !is.null(slideA[[target.column]]))
{
cat("The target column of slideA is not available.")
return(NA)
}
if(output.all && !is.null(slideB[[target.column]]))
{
cat("The target column of slideB is not available.")
return(NA)
}
slideA[[target.column]] <- attr(slideA, "title")
slideB[[target.column]] <- attr(slideB, "title")
sub.normalize <- function(slideA){
slideA$upper <- slideA$upper / median(slideA$concentrations, na.rm=T)
slideA$lower <- slideA$lower / median(slideA$concentrations, na.rm=T)
slideA$concentrations <- slideA$concentrations / median(slideA$concentrations, na.rm=T)
return(slideA)
}
if(normalize.with.median.first)
{
if(!normalize.per.deposition){
slideA <- sub.normalize(slideA)
slideB <- sub.normalize(slideB)
}
else{
slideA <- ddply(slideA, .(Deposition), sub.normalize)
slideB <- ddply(slideB, .(Deposition), sub.normalize)
}
}
result <- slideA
result$concentrations <- slideA$concentrations / slideB$concentrations
#calculate percentage error, build sum and calculate real error on the new value.
result$upper <- ((( slideA$upper - slideA$concentrations) /slideA$concentrations ) + (( slideB$upper - slideB$concentrations) / slideB$concentrations )) * result$concentrations
result$lower <- (( (slideA$concentrations - slideA$lower) / slideA$concentrations ) + ( (slideB$concentrations - slideB$lower) /slideB$concentrations )) * result$concentrations
result$upper <- result$upper + result$concentrations
result$lower <- result$concentrations - result$lower
result[[target.column]] <- paste(slideA[[target.column]], "normalized by", slideB[[target.column]])
if(output.all) result <- rbind(slideA, result, slideB)
return(result)
}
rppa.specific.dilution <- function(spots, dilution=0.25, deposition=4, ...)
{
#if(!is.null(spots$Inducer)) spots$Inducer <- gsub(" [0-9]+[.][0-9] mM", "", spots$Inducer )
spots.subset <- subset(spots, DilutionFactor == dilution & Deposition == deposition & !is.na(Signal))
spots.subset$x.weighted.mean <- spots.subset$Signal
spots.subset$x.err <- 0
spots.summarize <- rppa.serialDilution.summarize(spots.subset, ...)
spots.summarize$x.err <- spots.summarize$sem
if(length(spots.summarize$x.err[is.na(spots.summarize$x.err)])>0) cat("WARNING: some samples have only one valid value and no standard error could be computed!")
spots.summarize$concentrations <- spots.summarize$x.weighted.mean
spots.summarize$upper <- spots.summarize$x.weighted.mean + spots.summarize$x.err
spots.summarize$lower <- spots.summarize$x.weighted.mean - spots.summarize$x.err
spots.summarize <- spots.summarize[,!(colnames(spots.summarize) %in% c("sem", "x.weighted.mean", "x.err", "Deposition"))]
attr(spots.summarize, "title") <- attr(spots, "title")
attr(spots.summarize, "antibody") <- attr(spots, "antibody")
return(spots.summarize)
}
rppa.duplicate.nas <- function(data.protein.conc.copy)
{
foreach(property=c("A","B")) %do%{
data.protein.conc.copy <- foreach(i=1:nrow(data.protein.conc.copy), .combine=rbind) %do% {
if(is.na(data.protein.conc.copy[i,property])){
foreach(A=levels(data.protein.conc.copy[[property]]), .combine=rbind) %do% {
currentRow <- data.protein.conc.copy[i,]
currentRow[[property]] <- A
return(currentRow)
}
}
else return(data.protein.conc.copy[i,])
}
}
return(data.protein.conc.copy)
}
rppa.normalize.to.ref.sample <- function(data.protein.conc, sampleReference, each.A=F, each.B=F, specific.A=NULL, specific.B=NULL, each.fill=F, method="mean")
{
require(plyr)
toRefSample <- function(data.protein.conc){
if(is.null(specific.A) && is.null(specific.B)) my.subset <- subset(data.protein.conc, Sample == sampleReference)
else if(is.null(specific.B)){
if(!is.na(specific.A)) my.subset <- subset(data.protein.conc, Sample == sampleReference & A == specific.A)
else my.subset <- subset(data.protein.conc, Sample == sampleReference & is.na(A))
}
else if(is.null(specific.A)){
if(!is.na(specific.B)) my.subset <- subset(data.protein.conc, Sample == sampleReference & B == specific.B)
else my.subset <- subset(data.protein.conc, Sample == sampleReference & is.na(B))
}
else{
if(!is.na(specific.A) && !is.na(specific.B)) my.subset <- subset(data.protein.conc, Sample == sampleReference & A == specific.A & B == specific.B)
else if(is.na(specific.A) & !is.na(specific.B)) my.subset <- subset(data.protein.conc, Sample == sampleReference & B == specific.B & is.na(A))
else if(is.na(specific.B) & !is.na(specific.A)) my.subset <- subset(data.protein.conc, Sample == sampleReference & A == specific.A & is.na(B))
else my.subset <- subset(data.protein.conc, Sample == sampleReference & is.na(A) & is.na(B))
}
if(method == "mean") meanOfRefSample <- mean(my.subset$concentrations, na.rm=T)
else if(method == "median") meanOfRefSample <- median(my.subset$concentrations, na.rm=T)
data.protein.conc <- within(data.protein.conc, {
concentrations <- concentrations / meanOfRefSample
upper <- upper / meanOfRefSample
lower <- lower / meanOfRefSample
}, meanOfRefSample=meanOfRefSample)
}
if(each.fill)
{
data.protein.conc <- ddply(data.protein.conc, .(A, B, Slide), function(x, sampleRef){
within(x, {
reference <- concentrations[Sample==sampleRef]
concentrations <- concentrations / reference
upper <- upper / reference
lower <- lower / reference
})
}, sampleRef=sampleReference)
}
else if(each.A && each.B){
data.protein.conc <- ddply(data.protein.conc, .(A, B), toRefSample)
}
else if(each.A){
data.protein.conc <- ddply(data.protein.conc, .(A), toRefSample)
}
else if(each.B){
data.protein.conc <- ddply(data.protein.conc, .(B), toRefSample)
}
else
{
data.protein.conc <- toRefSample(data.protein.conc)
}
return(data.protein.conc)
}