-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.R
146 lines (125 loc) · 2.8 KB
/
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
######################
#
# Grep name in x
#
#####################
specgrep <- function(x,name) {
x=x[,grep(name, names(x))]
return(x)
}
######################
#
# Remove name in x
#
#####################
removegrep <- function(x,name) {
x=x[,-grep(name, names(x))]
return(x)
}
######################
#
# BlankFilter - find features which are "highly" present in blank (based on a ratio cutoff)
# to.remove=AdvancedBlankFilter(Blanks,samples,0.01)
#
#####################
BlankFilter <- function(blanks, samples, cutoff) {
blanks[is.na(blanks)] <- 0
samples[is.na(samples)] <- 0
blanks <- apply(blanks,1,median,na.rm=TRUE)
samples <- apply(samples,1,max,na.rm=TRUE)
to.remove <- which(blanks/samples >= cutoff)
return(to.remove)
}
######################
#
# coverage - compute features with coverage of cutoff or above
# features=coverage(samples,1)
#
#####################
coverage <- function(intensities, coverage) {
amount = dim(intensities)
cutoff = amount[2]*coverage
features=rep(0,amount[1])
for (i in 1:amount[1]) {
temp=intensities[i,]
a=!is.na(temp)
a=which(a)
if (length(a)>=cutoff) {
features[i]=i
}
}
features=features[which(features>0)]
return(features)
}
######################
#
# extract.names - extract unique names
# names=extract.names(samples)
#
#####################
extract.names <- function(x){
names=gsub("intensity_","",names(x))
names=gsub("_Rep1","",names)
names=gsub("_Rep2","",names)
names=gsub("_Rep3","",names)
names=gsub("_2","",names)
names=unique(names)
return(names)
}
######################
#
# remove.narows - removes rows/features which are completely missing (NA)
# samples=remove.narows(samples)
#
#####################
remove.narows<-function(X) {
ind <- apply(X, 1, function(x) all(is.na(x)))
return(ind)
}
######################
#
# Outersect
#
#####################
outersect <- function(x, y) {
sort(c(x[!x%in%y],
y[!y%in%x]))
}
######################
#
# coverage - compute features with coverage of cutoff or above
# features=coverage(samples,0.75)
#
#####################
coverage <- function(intensities, coverage) {
amount = dim(intensities)
cutoff = amount[2]*coverage
features=rep(0,amount[1])
for (i in 1:amount[1]) {
temp=intensities[i,]
a=!is.na(temp)
a=which(a)
if (length(a)>=cutoff) {
features[i]=i
}
}
features=features[which(features>0)]
return(features)
}
#####################
#
# Normalize against references (spiked-ins)
#
#####################
norm2ref <- function(data,ref) {
m <- rowMeans(ref)
for (i in 1:ncol(data)) {
factor <- c()
for (j in 1:length(m)) {
factor[j]<-ref[j,i]-m[j]
}
factor <- mean(factor)
data[,i] <- as.matrix(data[,i])-rep(factor,nrow(data))
}
return(data)
}