-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortbyRules.r
221 lines (186 loc) · 5.21 KB
/
SortbyRules.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
## Step.2
## To confirm whether the source data need to sort by the association rules,
## if not, skip to the third step.
##
## Annotations
## Input : G100.csv
## Output : G100_after.csv
fin_1 <- TEST_DATA
fin_2 <- "EX/rules/sum_data.csv"
fout_1 <- "EX/rules/genNode.csv"
fout_2 <- "EX/rules/express100_after.csv"
ruleBack <- read.csv(fin_1)
ruleBack <- data.frame(ruleBack)
row.names(ruleBack) <- ruleBack[,1]
ruleBack <- t(ruleBack)
ruleBack <- ruleBack[-1,]
gen <- ruleBack
gen <- data.frame(t(gen))
rownum <- nrow(gen)
gen <- data.frame(c('s'),gen,stringsAsFactors = F)
gen <- data.frame(gen[,1:2])
## give the name
for(s in 1:rownum){
gen[s,1] <- paste(as.character(rownames(gen)[s]),sep = "")
}
gen[,1] <- as.character(gen[,1])
order <- c(nrow(gen))
gen <- data.frame(order,gen)
gen <- data.frame(gen[,1:2])
## Read the result of the Aprori.
hc <- read.csv(fin_2,header = T,stringsAsFactors = F)
hc <- hc[,-2]
hc <- hc[,-3]
hc <- hc[,-3]
order <- c(nrow(hc))
from = hc[,1]
to = hc[,2]
row_names <- c(row.names(hc))
hc40ft <- data.frame(row_names,from,to,order,stringsAsFactors = F)
tt = 0;
count = 0;
gencount = 0;
mylist <- c()
mycount <- c();
mygencount <- c();
hc40ft <- data.frame(hc40ft,tag = c(0))
for(ix in 1:nrow(hc40ft)){
for(iy in 1:nrow(hc40ft)){
if( ix == 79 && iy == 98 ){
print(hc40ft[79,2])
print(hc40ft[98,3])
if(hc40ft[ix,2] == hc40ft[iy,3]){
print("mark...")
print(hc40ft[ix,5])
}
}
if(hc40ft[ix,2] == hc40ft[iy,3]&&hc40ft[ix,5] == 0){
hc40ft[ix,5] = 1
mylist <- c(mylist,hc40ft[ix,1])
}
}
}
mylist2 <- c()
for(xi in 1:nrow(hc40ft)){
t = 0
for(yi in 1:length(mylist)){
if(hc40ft[xi,1] == mylist[yi]){
t = 1
break
}
}
if(t == 0){
mylist2 <- c(mylist2,hc40ft[xi,1])
}
}
if(length(mylist2) == 0){
print("----- There is no head node but ring -----")
stop();
}
## Number for the head node of hc40ft_1
hc40ft_1 <- hc40ft[mylist2,]
for(xi in 1:nrow(hc40ft_1)){
for(yi in 1:nrow(hc40ft)){
if(hc40ft_1[xi,1] == hc40ft[yi,1]&&hc40ft[yi,4] > count){
count = count+1;
hc40ft_1[xi,4] = count;
hc40ft[yi,4] = count;
break;
}
}
}
## Number for the head node of gen
for(xi in 1:nrow(hc40ft_1)){
for(yi in 1:nrow(gen)){
if(hc40ft_1[xi,2] == gen[yi,2]&&gen[yi,1]>gencount){
gencount = gencount+1;
gen[yi,1] = gencount;
break;
}
}
}
mycount <- c(mycount,count);
mygencount <- c(mygencount,gencount);
## Number for the next node of the head node.(di er ci)
for(zz in 1:1000){
print(zz)
for(xi in 1:nrow(hc40ft_1)){
for(yi in 1:nrow(gen)){
if(hc40ft_1[xi,3] == gen[yi,2]&&gen[yi,1]>gencount){
gencount = gencount+1;
gen[yi,1] = gencount;
break;
}
}
}
## Select the next node of the head node
mygencount <- c(mygencount,gencount);
tt = count;
mylist3 <- c()
mylist4 <- c()
for(xi in 1:nrow(hc40ft_1)){
for(yi in 1:nrow(hc40ft)){
if(hc40ft_1[xi,3] == hc40ft[yi,2]&&hc40ft[yi,4]>count){
mylist3 <- c(mylist3,hc40ft_1[xi,4])
mylist4 <- c(mylist4,hc40ft[yi,1])
}
}
}
if(length(mylist3) == 0&&length(mylist4) == 0){ break;}
mydata_f_t <- data.frame(mylist3,mylist4)
## hc40ft, excute the second number
for(xi in 1:nrow(mydata_f_t)){
for(yi in 1:nrow(hc40ft)){
if(hc40ft[yi,1] == mydata_f_t[xi,2]&&hc40ft[yi,4]>count){
count = count+1;
hc40ft[yi,4] = count;
break;
}
}
}
## remove duplicates
p = 0
mycount <- c(mycount,count);
if(tt == count||gencount == (nrow(gen)-1)){break;}
hc40ft_2 <- hc40ft[mylist4,]
for(xi in 1:nrow(hc40ft_2)){
q = 0
ll <- c(strsplit(row.names(hc40ft_2)[xi-p],""))
for(yi in 1:length(ll[[1]])){
if(ll[[1]][yi] == "."){q = 1}
}
if(q == 1){
hc40ft_2 <- hc40ft_2[-(xi-p),]
p = p+1
}
if((xi-p) >= nrow(hc40ft_2)){break;}
}
hc40ft_1 <- data.frame();
mydata_f_t <- data.frame();
hc40ft_1 <- hc40ft_2;
}
## Now the gene table structure for the two columns,
## left is the node name, the right is the node order, and then write the file.
cc = 0
for(ix in 1:nrow(gen)){
if(gen[ix,1] == nrow(gen))
print(paste(gen[ix,1]," ",nrow(gen)))
cc = cc+1;
}
names(gen) <- c('order','gene')
gen <- cbind(gen$gene, gen$order)
write.csv(gen,fout_1,row.names = F)
if(length(mylist3) == 0&&length(mylist4) == 0){
print("----- There has noob gene -----")
}
## The last sort
## 2016-6-24 changed:
## Now,the output file is the result of the sequence by adjusting the source data.
sx <- read.csv(fout_1,header = T)
zero <- ruleBack
zero <- data.frame(t(zero))
one <- data.frame(sx,zero)
two <- one[order(one[,2],decreasing = T),] #Order [ descending 100...1 ]
two <- two[,-2]
write.csv(two,fout_2,row.names = F)
print("----- Sort by aprori completed! -----")