-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
181 lines (147 loc) · 6.13 KB
/
Copy pathMain.java
File metadata and controls
181 lines (147 loc) · 6.13 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
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String args[]) throws FileNotFoundException {
File file = new File("Genes_relation.data");
Scanner scanner = new Scanner(file);
scanner.nextLine();
double totalExample = 0;
String[][] trainingData,testData;
Hashtable<String, String> geneIDTable;
Hashtable<String, Integer> localizationTable = new Hashtable<>();
while (scanner.hasNext()) {
totalExample++;
String[] splitLine = scanner.nextLine().split(",");
// addGeneIDToHashtable(geneIDTable, splitLine);
// addEssentialToHashtable(essentialTable,splitLine);
// addClassToHashtable(classTable,splitLine);
// addComplexToHashtable(complexTable,splitLine);
// addPhenotypeToHashtable(phenotypeTable,splitLine);
// addChromosomeToHashtable(chromosomeTable,splitLine);
// addMotifToHashtable(motifTable,splitLine);
addLocalizationToHashtable(localizationTable,splitLine);
}
int amountOfAttr = 8;
trainingData = new String[(int) totalExample][amountOfAttr];
scanner = new Scanner(file);
scanner.nextLine();
int i = 0;
while (scanner.hasNext()) {
String[] splitLine = scanner.nextLine().split(",");
for(int j = 0; j < amountOfAttr; j++){
if(j == 7){
trainingData[i][j] = splitLine[splitLine.length-1];
}
else {
trainingData[i][j] = splitLine[j];
}
}
i++;
}
//print2D(trainingData);
int totalTestExample = 0;
NaiveBayesian_Implementation naiveBayesian_implementation = new NaiveBayesian_Implementation(localizationTable,trainingData ,totalExample);
file = new File("Genes_relation.test");
scanner = new Scanner(file);
scanner.nextLine();
//Create an output file
File writeFileName = new File("output.txt");
try{
if (writeFileName.createNewFile()) {
System.out.println("File created: " + writeFileName.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
};
try {
FileWriter fileWriter = new FileWriter(writeFileName);
fileWriter.write("GeneID,Localization" + "\n");
int line = 0;
while(scanner.hasNext()) {
scanner.nextLine();
totalTestExample++;
}
testData = new String[totalTestExample][2];
scanner = new Scanner(file);
scanner.nextLine();
while(scanner.hasNext()){
String[] splitLine = scanner.nextLine().split(",");
//System.out.println(line + ": " + naiveBayesian_implementation.predictLocalization(splitLine));
testData[line][0] = splitLine[0];
testData[line][1] = naiveBayesian_implementation.predictLocalization(splitLine);
line++;
}
geneIDTable = naiveBayesian_implementation.findTheFinalLocalization(testData);
// print2D(testData);
// System.out.println(testData.length);
// System.out.println(geneIDTable.toString());
// System.out.println(geneIDTable.size());
Set<String> keys = geneIDTable.keySet();
String[] geneID = new String[keys.size()];
String[] localization = new String[keys.size()];
i = 0;
for(String key : keys){
geneID[i] = key;
localization[i] = geneIDTable.get(key);
i++;
}
for(i =0; i < geneID.length;i++){
fileWriter.write(geneID[i] + "," + localization[i] + "\n");
}
compareOutputVsKeys(geneID, localization);
fileWriter.close();
}catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private static void compareOutputVsKeys(String[] geneID, String[] localization) throws FileNotFoundException {
File file;
Scanner scanner;
int i;
double correct = 0;
file = new File("keys.txt");
scanner = new Scanner(file);
scanner.nextLine();
while(scanner.hasNext()){
String[] splitLine = scanner.nextLine().split(",");
for(i = 0; i < geneID.length;i++){
if(splitLine[0].equals(geneID[i])){
if(splitLine[1].equals(localization[i])){
correct++;
}
}
}
}
double accuracy = correct/geneID.length;
System.out.println("accuracy is: " +Math.round(accuracy * 100) + "%");
}
public static void print2D(String mat[][])
{
// Loop through all rows
for (int i = 0; i < mat.length; i++) {
System.out.println();
// Loop through all elements of current row
for (int j = 0; j < mat[i].length; j++) {
System.out.print(mat[i][j] + " ");
}
}
}
private static void addLocalizationToHashtable(Hashtable<String, Integer> localizationTable, String[] splitLine) {
int rightValue = splitLine.length-1;
if (localizationTable.containsKey(splitLine[rightValue])) {
localizationTable.put(splitLine[rightValue], localizationTable.get(splitLine[rightValue])+1);
} else {
localizationTable.put(splitLine[rightValue], 1);
}
}
}