Skip to content

Commit e895d0b

Browse files
committed
add Gzip support to file selectors
Add Gzip support for the UI file selection classes. #91 - FileSelection: Specifically, the extension-based single file selection was adjusted to add support for ignoring or including files with a ".gz" extension. - ExtensionFileFilter: add a stripExtension method that works on String-type filenames - decorate both classes with JavaDocs
1 parent 53862ec commit e895d0b

2 files changed

Lines changed: 138 additions & 13 deletions

File tree

src/util/ExtensionFileFilter.java

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,48 @@
55

66
import javax.swing.filechooser.FileFilter;
77

8+
/**
9+
* Filters files such as BAI, BAM, BED, CDT, FASTA, FA, GFF, TAB for the FileSelection tool base on the extension of the filename. Also includes file extension parsing utilities used across all ScriptManager classes (especially for creating default filenames).
10+
*
11+
* @author William KM Lai
12+
* @see util.FileSelection
13+
*
14+
*/
815
public class ExtensionFileFilter extends FileFilter{
9-
//Filters files such as BAI, BAM, BED, CDT, FASTA, FA, GFF, TAB
10-
16+
1117
private String ext = "";
1218
private String ext2 = "";
1319
private String ext3 = "";
1420
private boolean includeGZ = false;
15-
21+
22+
/**
23+
* Create file filter for the given file extension.
24+
* If ExtensionFileFilter is instantiated with "fa", file extensions "fasta" and "fsa" are set to be equivalent.
25+
* If ExtensionFileFilter is instantiated with "gff", file extensions "gtf" and "gff3" are set to be equivalent.
26+
*
27+
* @param filter file extension to filter by (see notes on "fa" and "gff").
28+
*/
1629
public ExtensionFileFilter(String filter) {
1730
ext = filter;
1831
if(ext.equals("fa")) { ext2 = "fasta"; ext3 = "fsa"; }
1932
if(ext.equals("gff")) { ext2 = "gtf"; ext3 = "gff3"; }
2033
}
21-
34+
35+
/**
36+
* Create file filter for the given file extension with option to ignore or include ".gz" extensions.
37+
* If ExtensionFileFilter is instantiated with "fa", file extensions "fasta" and "fsa" are set to be equivalent.
38+
* If ExtensionFileFilter is instantiated with "gff", file extensions "gtf" and "gff3" are set to be equivalent.
39+
*
40+
* @param filter file extension to filter by (see notes on "fa" and "gff").
41+
* @param gz whether or not to ignore the ".gz" extension (e.g. if gz=true and filter="bed", then "XXX.bed.gz" will pass the filter, "XXX.bed" will pass the filter, but "XXX.gff" will NOT pass the filter).
42+
*/
2243
public ExtensionFileFilter(String filter, boolean gz) {
2344
ext = filter;
2445
if(ext.equals("fa")) { ext2 = "fasta"; ext3 = "fsa"; }
2546
if(ext.equals("gff")) { ext2 = "gtf"; ext3 = "gff3"; }
2647
includeGZ = gz;
2748
}
28-
49+
2950
public boolean accept(File f) {
3051
if (f.isDirectory()) return true;
3152
String extension = includeGZ ? getExtensionIgnoreGZ(f) : getExtension(f);
@@ -38,7 +59,13 @@ public boolean accept(File f) {
3859
}
3960
return false;
4061
}
41-
62+
63+
/**
64+
* Get the extension string from a File object.
65+
*
66+
* @param f the input file to get the extension for
67+
* @return the file extension string (without "." char)
68+
*/
4269
public static String getExtension(File f) {
4370
String ext = null;
4471
String s = f.getName();
@@ -48,7 +75,13 @@ public static String getExtension(File f) {
4875
}
4976
return ext;
5077
}
51-
78+
79+
/**
80+
* Get the extension string from a File object, ignoring ".gz" extension if present and searching for next extension.
81+
*
82+
* @param f the input file to get the extension for
83+
* @return the file extension string (without "." char)
84+
*/
5285
public static String getExtensionIgnoreGZ(File f) {
5386
String ext = null;
5487
String s = f.getName();
@@ -65,6 +98,15 @@ public static String getExtensionIgnoreGZ(File f) {
6598
return ext;
6699
}
67100

101+
/**
102+
* Get the filename without the last extension (tokenize filename by "." char and pull off last "." character and last token. If no "." characters, return the whole filename)
103+
* e.g. f="blahblah.fa" returns "blahblah"
104+
* e.g. f="oompaloompa" returns "oompaloompa"
105+
*
106+
* @param f the File to strip an extension from
107+
* @return string of filename with extension stripped away
108+
* @throws IOException
109+
*/
68110
public static String stripExtension(File f) throws IOException {
69111
String[] name = f.getName().split("\\.");
70112
String NEWNAME = name[0];
@@ -74,6 +116,17 @@ public static String stripExtension(File f) throws IOException {
74116
return(NEWNAME);
75117
}
76118

119+
/**
120+
* Get the string without the last extension, ignoring any ".gz" extensions (tokenize strings by "." char and pull off last "." character and last token. If no "." characters, return the whole filename)
121+
* e.g. f="blahblah.fa" returns "blahblah"
122+
* e.g. f="oompaloompa" returns "oompaloompa"
123+
* e.g. f="blahblah.fa.gz" returns "blahblah"
124+
* e.g. f="oompaloompa.gz" returns "oompaloompa"
125+
*
126+
* @param f the File to strip an extension from
127+
* @return string of filename with extension stripped away
128+
* @throws IOException
129+
*/
77130
public static String stripExtensionIgnoreGZ(File f) throws IOException {
78131
String NEWNAME = f.getName();
79132
if (NEWNAME.endsWith(".gz")) { NEWNAME = NEWNAME.substring(0, NEWNAME.length()-3); }
@@ -83,7 +136,31 @@ public static String stripExtensionIgnoreGZ(File f) throws IOException {
83136
}
84137
return(NEWNAME);
85138
}
86-
139+
140+
/**
141+
* Get the string without the last extension (tokenize strings by "." char and pull off last "." character and last token. If no "." characters, return the whole filename)
142+
* e.g. f="blahblah.fa" returns "blahblah"
143+
* e.g. f="oompaloompa" returns "oompaloompa"
144+
*
145+
* @param f the String to strip an extension from
146+
* @return string of filename with extension stripped away
147+
*/
148+
public static String stripExtension(String f) {
149+
String[] name = f.split("\\.");
150+
String NEWNAME = name[0];
151+
for(int x = 1; x < name.length-1; x++) {
152+
NEWNAME += ("." + name[x]);
153+
}
154+
return(NEWNAME);
155+
}
156+
157+
/**
158+
* Get the string of the cannonical filepath without the last extension. Return every character in the path until the last instance of "." character (not including ".").
159+
* e.g. f="/parent/directory/blahblah.fa" returns "/parent/directory/blahblah"
160+
*
161+
* @param f the String to strip an extension from
162+
* @return string of filename with extension stripped away
163+
*/
87164
public static String stripExtensionPath(File f) throws IOException {
88165
String NAME = f.getCanonicalPath();
89166
return(NAME.substring(0, NAME.lastIndexOf('.')));

src/util/FileSelection.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@
44

55
import javax.swing.JFileChooser;
66

7+
/**
8+
* Various kinds of file selectors used by the window_interface objects to restrict user input selection options.
9+
*
10+
* @author William KM Lai
11+
*
12+
*/
713
public class FileSelection {
8-
14+
15+
/**
16+
* Generic multi-file FileSelector without extension restrictions (commonly TAB-delimited file formats like matrix/table data).
17+
*
18+
* @param fc
19+
* @return array of File objects from selector UI
20+
*/
921
public static File[] getGenericFiles(JFileChooser fc){
1022
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
1123
fc.setMultiSelectionEnabled(true);
@@ -18,7 +30,13 @@ public static File[] getGenericFiles(JFileChooser fc){
1830
}
1931
return Files;
2032
}
21-
33+
34+
/**
35+
* Generic single-file FileSelector for retrieving directories.
36+
*
37+
* @param fc
38+
* @return a directory file
39+
*/
2240
public static File getOutputDir(JFileChooser fc) {
2341
fc.setDialogTitle("Output Directory");
2442
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
@@ -29,9 +47,26 @@ public static File getOutputDir(JFileChooser fc) {
2947
return null;
3048
}
3149
}
32-
50+
51+
/**
52+
* Extension-based single-file FileSelector for retrieving a single file with a specific extension.
53+
* @param fc
54+
* @param ext the extension to restrict file selection options by
55+
* @return a single file with a specific extension (see "ext")
56+
*/
3357
public static File getFile(JFileChooser fc, String ext){
34-
fc.setFileFilter(new ExtensionFileFilter(ext));
58+
return getFile(fc, ext, false);
59+
}
60+
61+
/**
62+
* Extension-based single-file FileSelector for retrieving a single file with a specific extension where ignoring ".gz" extensions can be toggled.
63+
* @param fc
64+
* @param ext the extension to restrict file selection options by
65+
* @param includeGZ true value means ignore the ".gz" if present when checking the extension
66+
* @return a single file with a specific extension (see "ext")
67+
*/
68+
public static File getFile(JFileChooser fc, String ext, boolean includeGZ){
69+
fc.setFileFilter(new ExtensionFileFilter(ext,includeGZ));
3570
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
3671
fc.setMultiSelectionEnabled(false);
3772
fc.setSelectedFile(new File(""));
@@ -43,11 +78,24 @@ public static File getFile(JFileChooser fc, String ext){
4378
}
4479
return Files;
4580
}
46-
81+
82+
/**
83+
* Extension-based multi-file FileSelector for retrieving a set of files with a specific extension.
84+
* @param fc
85+
* @param ext the extension to restrict file selection options by
86+
* @return a list of files with a specific extension (see "ext")
87+
*/
4788
public static File[] getFiles(JFileChooser fc, String ext){
4889
return getFiles(fc, ext, false);
4990
}
5091

92+
/**
93+
* Extension-based multi-file FileSelector for retrieving a set of files with a specific extension where ignoring ".gz" extensions can be toggled.
94+
* @param fc
95+
* @param ext the extension to restrict file selection options by
96+
* @param includeGZ true value means ignore the ".gz" if present when checking the extension
97+
* @return a list of files with a specific extension (see "ext")
98+
*/
5199
public static File[] getFiles(JFileChooser fc, String ext, boolean includeGZ){
52100
fc.setFileFilter(new ExtensionFileFilter(ext,includeGZ));
53101
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

0 commit comments

Comments
 (0)