-
Notifications
You must be signed in to change notification settings - Fork 0
/
furTreatment.java
200 lines (174 loc) · 6.16 KB
/
furTreatment.java
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;
/*
* This program is the re-processing of the Apriori algorithm's results.
*/
public class furTreatment {
static String di = ",";
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String args[]) throws IOException{
int r = Integer.valueOf(args[0])+1; //the number of rules after Aprori
String f = args[1]; //the path of input file
String fout = args[2]; //the path of output file
File file = new File(fout);
//If file doesn't exists,create it.
if (!file.exists()) {
file.createNewFile();
}
else{
try {
FileWriter fw = new FileWriter(fout,false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("");
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
try{
BufferedReader b = new BufferedReader(new FileReader(f));
FileWriter fw = new FileWriter(fout,true);
BufferedWriter bw = new BufferedWriter(fw);
String line;
int j = 0;
Vector Data[] = new Vector[r-1];
System.out.println("Start");
b.readLine();
//Read all data from the file
while((line = b.readLine()) != null){
line = line.replace("\"", "");
line = line.replace("{", "");
line = line.replace("}", "");
line = line.replace(" ", "");
// All rules use "=>" on both sides to divide into two parts
String rules[] = line.split("=>");
String front[] = rules[0].split(di);
//Remove the 'many-to-one' rules
//System.out.println(rules[0]);
if(!(front[1].length() > 20)){
if(Data[j] == null){
Data[j] = new Vector<String>();
}
String head[] = front[1].split("[*]");//Split again,split the number.
Data[j].addAll(Arrays.asList(head[0]));//0 gene name
Data[j].addAll(Arrays.asList(head[1]));//1 gene part number
//Do with laster part
String item[] = rules[1].split(",");
//Remove the 'one-to-many' rules
if(!(item.length > 4)){
String tail[] = item[0].split("[*]");//Split again,split the number.
Data[j].addAll(Arrays.asList(tail[0]));//2 laster gene name
Data[j].addAll(Arrays.asList(tail[1]));//3 laster gene part number
Data[j].addAll(Arrays.asList(item[1]));//4 support value
j++;
}
else{
Data[j].clear();
j--;
}
}
}
//Change the form 'B->A' to 'A->B'
for(int i = 0;i < j-1;i++){
//System.out.println(i);
String t;
if((Data[i].get(0).toString()).compareTo(Data[i].get(2).toString()) > 0){
t = Data[i].get(0).toString();//gene
Data[i].set(0,Data[i].get(2).toString());
Data[i].set(2,t);
t = Data[i].get(1).toString();//number
Data[i].set(1,Data[i].get(3).toString());
Data[i].set(3,t);
}
}
//Delete duplicate
for(int k = 0;k < j-1;k++){
for(int i = k+1;i < j;i++){
//System.out.println(i);
if((Data[k].get(0).toString()).equals(Data[i].get(0).toString())
&& (Data[k].get(2).toString()).equals(Data[i].get(2).toString())
&&(Data[k].get(1).toString()).equals(Data[i].get(1).toString())
&& (Data[k].get(3).toString()).equals(Data[i].get(3).toString())){
Data[k].set(0,"$");
Data[k].set(2,"$");
}
}
}
//The first step of sort : insert the sort by the front.
Vector<String> t1 = new Vector<String>();
int temp1;
for(int k1 = 0 ;k1 <= j-1;k1++){
temp1 = k1;
for(int k2 = k1;k2 <= j-1;k2++){
if(((Data[k2].get(0)).toString()).compareTo((Data[temp1].get(0)).toString()) > 0){
temp1 = k2;
}
}
if(temp1 != k1){
t1 = Data[k1];
Data[k1] = Data[temp1];
Data[temp1] = t1;
}
}
//The first step of sort : When the front is equal, insert the sort by the rear.
int old = 0;
for(int k3 = 0;k3 <= j-1;k3++){
if(!(Data[old].get(0).toString()).equals(Data[k3].get(0).toString()) || (k3 == j-1)){//Make sure the end is right.
Vector<String> t2 = new Vector<String>();
int temp2;
for(int k1 = old ;k1 < k3;k1++){
temp2 = k1;
for(int k2 = k1;k2 < k3;k2++){
if(((Data[k2].get(2)).toString()).compareTo((Data[temp2].get(2)).toString()) > 0){
temp2 = k2;
}
}
if(temp2 != k1){
t2 = Data[k1];
Data[k1] = Data[temp2];
Data[temp2] = t2;
}
}
old = k3;
}
}
//merge data
int anchor = 0;
for(int k2 = 0;k2 <= j-1;k2++){
if(!(Data[anchor].get(0).toString()+Data[anchor].get(2).toString()).equals(
Data[k2].get(0).toString()+Data[k2].get(2).toString()) || (k2 == j-1)){
double refer = 0;
for(int k1 = anchor ;k1 < k2;k1++){
refer = refer + Double.parseDouble(Data[k1].get(4).toString());
if(k1 > anchor){
Data[k1].clear();
Data[k1].addAll(Arrays.asList("$"));
}
}
Data[anchor].set(4,refer);
Data[anchor].set(1,"sum");
Data[anchor].set(3,"sum");
anchor = k2;
}
}
//output results
bw.write("h_rule,h_no,t_rule,t_no,reference" + "\r\n");
for(int i = 0;i < j;i++){
if(!(Data[i].get(0).toString()).equals("$")){
bw.write(Data[i].toString().replace("[", "").replace("]", "").replace(" ", "") + "\r\n");
}
}
System.out.println("End");
bw.close();
b.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}