-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSVCodonFileManager.java
More file actions
393 lines (353 loc) · 12.8 KB
/
Copy pathCSVCodonFileManager.java
File metadata and controls
393 lines (353 loc) · 12.8 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package ca.virology.baseByBase.gui.CodeHop;
import ca.virology.lib.util.gui.UITools;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
Class is used when user tries to import custom codon table from csv file.
File should be in the following:
GCT, 0.36
GCC, 0.13
...
*/
public class CSVCodonFileManager {
double codonProb[] = new double[21];
ArrayList<String> codonsInFile = new ArrayList<String>(64);
String filePath;
String fileName;
boolean cancelled = false;
double slack;
public CSVCodonFileManager(JPanel parent) {
FileFilter filter = new FileNameExtensionFilter("CSV files", "csv");
JFileChooser fileChooser = new JFileChooser(new File(System.getProperty("user.home")));
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(parent.getParent());
if (result != JFileChooser.APPROVE_OPTION) {
CodeHopSelectPanel.fileNameDisplay.setText("");
cancelled = true;
return;
}
slack = CustomCodonTable.slack;
filePath = fileChooser.getSelectedFile().getAbsolutePath();
fileName = fileChooser.getSelectedFile().getName();
CodeHopSelectPanel.fileNameDisplay.setText(fileName);
}
/*
Responsible for ensuring file is in the right format (codon csv).
*/
public boolean isCorrectCSV() {
Scanner scanner; //scanner
try {
scanner = new Scanner(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
scanner.useDelimiter(",|\\n");
boolean onProbability = false; //variable to keep track of alternating pattern codon/probability/codon/prob...
String codon = "";
double pct;
while (scanner.hasNext()) {
if (!onProbability) {//if we are reading the codon
codon = scanner.next().replace("\n", "").replace("\r", "");
onProbability = true;
} else {//if we are reading the probability
onProbability = false;
try {
pct = Double.parseDouble(scanner.next().replace("\n", "").replace("\r", ""));
} catch (Exception e) {
//e.printStackTrace();
UITools.showError("Invalid csv formatting. Please refer to the help section for formatting.", CodeHopWizard.mainFrame);
System.out.println("csv read error, there should be a number there!");
scanner.close();
return false;
}
if (!readCodons(codon)) {
scanner.close();
return false;
}
if (!readProbabilities(getAAfromCodon(codon), pct)) {
UITools.showError("File content error: The sum of each probability for an AA must be close to 1!", CodeHopWizard.mainFrame);
scanner.close();
return false;
}
}
}
scanner.close();
return true;
}
/*
return a CodonTable made from the input file. simple.
structure similar to isCorrectCSV()
*/
public CodonTable getCodonTableFromCSV() {
CodonTable newTable = new CodonTable();
Scanner scanner; //scanner
boolean onProbability = false;
String codon = "";
double pct;
try {
scanner = new Scanner(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
scanner.useDelimiter(",|\\n");
while (scanner.hasNext()) {
if (!onProbability) {
codon = scanner.next().replace("\n", "").replace("\r", "");
onProbability = true;
} else {
onProbability = false;
pct = Double.parseDouble(scanner.next().replace("\n", "").replace("\r", ""));
newTable.modify(codon, pct);
}
}
scanner.close();
return newTable;
}
/*
as the file is read, the codons are passed to this function to keep track of which has been read.
Returns true if the codon that is being read has not alrady been previously read.
*/
private boolean readCodons(String codon) {
if (!isCodon(codon)) {//check if codon is codon
UITools.showError("File content error: \"" + codon + "\" not a codon!", CodeHopWizard.mainFrame);
return false;
}
if (codonsInFile.size() == 0) {// add the first one
codonsInFile.add(codon);
return true;
}
for (int i = 0; i < codonsInFile.size(); i++) {//check if its already in the list
if (codonsInFile.get(i).equals(codon)) {
UITools.showError("File content error: Duplicate codon (" + codon + ") in CSV file!", CodeHopWizard.mainFrame);
return false;
}
}
codonsInFile.add(codon);
return true;
}
/*
As codons are read, their probabilities are added in codonProb
*/
private boolean readProbabilities(String aa, double prob) {
switch (aa) {
case "A":
codonProb[0] += prob;
return checkCurrentProb(0, false);
case "C":
codonProb[1] += prob;
return checkCurrentProb(1, false);
case "D":
codonProb[2] += prob;
return checkCurrentProb(2, false);
case "E":
codonProb[3] += prob;
return checkCurrentProb(3, false);
case "F":
codonProb[4] += prob;
return checkCurrentProb(4, false);
case "G":
codonProb[5] += prob;
return checkCurrentProb(5, false);
case "H":
codonProb[6] += prob;
return checkCurrentProb(6, false);
case "I":
codonProb[7] += prob;
return checkCurrentProb(7, false);
case "K":
codonProb[8] += prob;
return checkCurrentProb(8, false);
case "L":
codonProb[9] += prob;
return checkCurrentProb(9, false);
case "M":
codonProb[10] += prob;
return checkCurrentProb(10, false);
case "N":
codonProb[11] += prob;
return checkCurrentProb(11, false);
case "P":
codonProb[12] += prob;
return checkCurrentProb(12, false);
case "Q":
codonProb[13] += prob;
return checkCurrentProb(13, false);
case "R":
codonProb[14] += prob;
return checkCurrentProb(14, false);
case "S":
codonProb[15] += prob;
return checkCurrentProb(15, false);
case "T":
codonProb[16] += prob;
return checkCurrentProb(16, false);
case "V":
codonProb[17] += prob;
return checkCurrentProb(17, false);
case "W":
codonProb[18] += prob;
return checkCurrentProb(18, false);
case "Y":
codonProb[19] += prob;
return checkCurrentProb(19, false);
case "*":
codonProb[20] += prob;
return checkCurrentProb(20, false);
}
return true;
}
/*
Given an index for the codonProb array list, this method checks whether the probability contained is valid (between 0.95 and 1.05)
finalCheck flag indicates whether or not this is the final check of the input (ie all values have been put in)
true = all values have been read in
false = still reading from file and adding
*/
private boolean checkCurrentProb(int index, boolean finalCheck) {
if (codonProb[index] > 1 + slack) {
return false;
} else if (finalCheck == true && codonProb[index] < 1 - slack) {
return false;
} else {
return true;
}
}
/*
Determines whether input string is codon. Returns boolean.
*/
private boolean isCodon(String codon) {
if (codon.length() != 3) {
return false;
}
char c1 = codon.charAt(0);
char c2 = codon.charAt(1);
char c3 = codon.charAt(2);
if (c1 != 'A' && c1 != 'T' && c1 != 'C' && c1 != 'G') {
return false;
}
if (c2 != 'A' && c2 != 'T' && c2 != 'C' && c2 != 'G') {
return false;
}
if (c3 != 'A' && c3 != 'T' && c3 != 'C' && c3 != 'G') {
return false;
}
return true;
}
/*
returns aa from a codon. Made static so can be accessed from elsewhere. useful.
*/
public static String getAAfromCodon(String codon) {
Map<String, String> map = new HashMap<String, String>();
map.put("TTT", "F");
map.put("TTC", "F");
map.put("TTA", "L");
map.put("TTG", "L");
map.put("CTT", "L");
map.put("CTC", "L");
map.put("CTA", "L");
map.put("CTG", "L");
map.put("ATT", "I");
map.put("ATC", "I");
map.put("ATA", "I");
map.put("ATG", "M");
map.put("GTT", "V");
map.put("GTC", "V");
map.put("GTA", "V");
map.put("GTG", "V");
map.put("TCT", "S");
map.put("TCC", "S");
map.put("TCA", "S");
map.put("TCG", "S");
map.put("AGT", "S");
map.put("AGC", "S");
map.put("CCT", "P");
map.put("CCC", "P");
map.put("CCA", "P");
map.put("CCG", "P");
map.put("ACT", "T");
map.put("ACC", "T");
map.put("ACA", "T");
map.put("ACG", "T");
map.put("GCT", "A");
map.put("GCC", "A");
map.put("GCA", "A");
map.put("GCG", "A");
map.put("TAT", "Y");
map.put("TAC", "Y");
map.put("TAA", "*");
map.put("TAG", "*");
map.put("CAT", "H");
map.put("CAC", "H");
map.put("CAA", "Q");
map.put("CAG", "Q");
map.put("AAT", "N");
map.put("AAC", "N");
map.put("AAA", "K");
map.put("AAG", "K");
map.put("GAT", "D");
map.put("GAC", "D");
map.put("GAA", "E");
map.put("GAG", "E");
map.put("TGT", "C");
map.put("TGC", "C");
map.put("TGA", "*");
map.put("TGG", "W");
map.put("CGT", "R");
map.put("CGC", "R");
map.put("CGA", "R");
map.put("CGG", "R");
map.put("AGA", "R");
map.put("AGG", "R");
map.put("GGT", "G");
map.put("GGC", "G");
map.put("GGA", "G");
map.put("GGG", "G");
return map.get(codon);
}
/*
Exports the UI table to csv format. Method is called when "Export table" is clicked from the UI -> static
*/
public static void exportCurrentTableToCSV(String[][] table) {
JFileChooser fc = new JFileChooser(new File(System.getProperty("user.home")));
fc.setDialogTitle("Save Codon Table");
int returnVal = fc.showSaveDialog(fc.getParent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
String fullPathFileName = f.getAbsolutePath();
String filename = f.getName();
if (!fullPathFileName.endsWith(".csv")) {
fullPathFileName += ".csv";
f = new File(fullPathFileName);
filename += ".csv";
}
if (f.exists()) {
int ret = JOptionPane.showConfirmDialog(CodeHopWizard.mainFrame, "File \"" + filename + "\" already exists. Overwrite?", "Save codon table", JOptionPane.YES_NO_OPTION);
if (ret == 1) {
return;
}
}
PrintWriter writer;
try {
writer = new PrintWriter(fullPathFileName, "UTF-8");
} catch (Exception e) {
//catch this
UITools.showError("Could not write/create file", CodeHopWizard.mainFrame);
return;
}
for (int i = 0; i < table.length; i++) {
writer.println(table[i][1] + ", " + table[i][2]);
}
writer.close();
UITools.showInfoMessage("File successfully written. Custom codon table will be used.", CodeHopWizard.mainFrame);
}
}
}