-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsamplesByExpAndColor.R
52 lines (51 loc) · 1.58 KB
/
samplesByExpAndColor.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
#This function must recieve a Vector character "VC" and a list "L" of two lists
#"L[1] and L[2]" where each line connects respectively a sample with his experiment.
#The output, is a list sorted in the same order as VC and each row connects
#the sample with his experiment, and assign a color to each experiment.
# IMPORTAT: The names in VC and L[,2] must match EXACTLY.
samplesByExpAndColor<-function(VC=vector,L){
ExpOrd<-matrix(rep(0,3*length(VC)),length(VC),3)
L[,1]<-as.vector(L[,1])
L[,2]<-as.vector(L[,2])
for(i in 1:length(VC)){
for(j in 1:length(L[,1])){
if(VC[i]==L[j,2]){
ExpOrd[i,1]<-L[j,1]
ExpOrd[i,2]<-L[j,2]
}
}
}
Coloreado<-groupsByColors(ExpOrd[,1])
ExpOrd[,3]<-Coloreado[,2]
#ExpOrd[,4]<-Coloreado[,1]
return(ExpOrd)
}
groupsByColors<-function(V,mode="character"){
colorEs<-vector(mode="character",length=0)
LevelsOfV<-theLevels(V)
COLORES<-GiveMeTheColors(LevelsOfV)
PutTheColors<-substituteByColors(V,LevelsOfV,COLORES)
return(PutTheColors)
}
theLevels<-function(NV){
LevelsNV<-vector()
FactorNV<-as.factor(NV)
LevelsNV<-attributes(FactorNV)$levels
return(LevelsNV)
}
GiveMeTheColors<-function(LevelsNV){
Colorines<-rainbow(length(LevelsNV))
return(Colorines)
}
substituteByColors <-function(ForSub,LevelsNV,COLORES){
Substituted<-matrix(rep(0,length(ForSub)*2),length(ForSub),2)
Substituted[,1]<-ForSub
for(i in 1:length(LevelsNV)){
for(j in 1:length(ForSub)){
if(LevelsNV[i]==ForSub[j]){
Substituted[j,2]<-(COLORES[i])
}
}
}
return(Substituted)
}