Skip to content

Commit 8776b45

Browse files
committed
create ExpandBED command line version
This tool did not implement JFrame elements in the script class so the CLI implementation did not need to create an src/window_interface/*/ ExpandBEDOutput class. As a result, fewer files were affected: window_interface/*Window -adjust output argument when initializing script class to be full filename and not just directory script/* -use output as filename rather than directory name -add capability to print to STDOUT cli/* -call appropriate script class -write up picocli parsing objects -write up validateInput() method to check inputs and write messages for the user
1 parent 932a3bf commit 8776b45

3 files changed

Lines changed: 89 additions & 13 deletions

File tree

src/cli/Coordinate_Manipulation/BED_Manipulation/ExpandBEDCLI.java

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import objects.ToolDescriptions;
1515
import util.ExtensionFileFilter;
16-
//import scripts.Coordinate_Manipulation.BED_Manipulation.ExpandBED;
16+
import scripts.Coordinate_Manipulation.BED_Manipulation.ExpandBED;
1717

1818
/**
1919
Coordinate_ManipulationCLI/ExpandBEDCLI
@@ -25,6 +25,26 @@
2525
exitCodeOnExecutionException = 1)
2626
public class ExpandBEDCLI implements Callable<Integer> {
2727

28+
@Parameters( index = "0", description = "the BED file to expand on")
29+
private File bedFile;
30+
31+
@Option(names = {"-o", "--output"}, description = "specify output directory (name will be same as original with coordinate info appended)")
32+
private File output = null;
33+
@Option(names = {"-s", "--stdout"}, description = "output bed to STDOUT")
34+
private boolean stdout = false;
35+
36+
@ArgGroup(validate = false, heading = "%nType of Expansion%n")
37+
ExpandType expandType = new ExpandType();
38+
static class ExpandType {
39+
@Option(names = {"-c", "--center"}, description = "expand from center (default, at 250bp)")
40+
private int center = -999;
41+
@Option(names = {"-b", "--border"}, description = "add to border")
42+
private int border = -999;
43+
}
44+
45+
private boolean byCenter = true;
46+
private int SIZE = 250;
47+
2848
@Override
2949
public Integer call() throws Exception {
3050
System.err.println( ">ExpandBEDCLI.call()" );
@@ -35,16 +55,66 @@ public Integer call() throws Exception {
3555
System.exit(1);
3656
}
3757

38-
//SEStats.getSEStats( output, bamFile, null );
58+
ExpandBED.expandBEDBorders(output, bedFile, SIZE, byCenter);
3959

40-
//System.err.println("Calculations Complete");
60+
System.err.println("Expansion Complete");
4161
return(0);
4262
}
4363

4464
private String validateInput() throws IOException {
4565
String r = "";
46-
//validate input here
47-
//append messages to the user to `r`
66+
67+
//check inputs exist
68+
if(!bedFile.exists()){
69+
r += "(!)BED file does not exist: " + bedFile.getName() + "\n";
70+
return(r);
71+
}
72+
//check input extensions
73+
if(!"bed".equals(ExtensionFileFilter.getExtension(bedFile))){
74+
r += "(!)Is this a BED file? Check extension: " + bedFile.getName() + "\n";
75+
}
76+
77+
// Define default behavior
78+
if(expandType.center==-999 && expandType.border==-999){
79+
SIZE = 250;
80+
byCenter = true;
81+
}else if(expandType.border==-999){
82+
SIZE = expandType.center;
83+
byCenter = true;
84+
}else if(expandType.center==-999){
85+
SIZE = expandType.border;
86+
byCenter = false;
87+
}else{
88+
r += "(!) Both center and border are flagged. This should have been caught by Picocli.";
89+
}
90+
//check size of expansion is valid
91+
if(SIZE<=0){
92+
r += "(!) Invalid size input. Must be a positive integer greater than 0.";
93+
}
94+
95+
//set default output filename
96+
if(output==null && !stdout){
97+
if(byCenter){ output = new File(ExtensionFileFilter.stripExtension(bedFile) + "_" + Integer.toString(SIZE) + "bp.bed"); }
98+
else{ output = new File(ExtensionFileFilter.stripExtension(bedFile) + "_border_" + Integer.toString(SIZE) + "bp.bed"); }
99+
//check stdout and output not both selected
100+
}else if(stdout){
101+
if(output!=null){ r += "(!)Cannot use -s flag with -o.\n"; }
102+
//check output filename is valid
103+
}else{
104+
//check ext
105+
try{
106+
if(!"bed".equals(ExtensionFileFilter.getExtension(output))){
107+
r += "(!)Use BED extension for output filename. Try: " + ExtensionFileFilter.stripExtension(output) + ".bed\n";
108+
}
109+
} catch( NullPointerException e){ r += "(!)Output filename must have extension: use BED extension for output filename. Try: " + output + ".bed\n"; }
110+
//check directory
111+
if(output.getParent()==null){
112+
// System.err.println("default to current directory");
113+
} else if(!new File(output.getParent()).exists()){
114+
r += "(!)Check output directory exists: " + output.getParent() + "\n";
115+
}
116+
}
117+
48118
return(r);
49119
}
50-
}
120+
}

src/scripts/Coordinate_Manipulation/BED_Manipulation/ExpandBED.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import java.util.Scanner;
88

99
public class ExpandBED {
10-
public static void expandBEDBorders(File out_path, File input, int SIZE, boolean ExCenter) throws IOException {
11-
String newName = (input.getName()).substring(0,input.getName().length() - 4) + "_" + Integer.toString(SIZE) +"bp.bed";
12-
Scanner scan = new Scanner(input);
13-
PrintStream OUT = null;
14-
if(out_path == null) OUT = new PrintStream(newName);
15-
else OUT = new PrintStream(out_path + File.separator + newName);
10+
public static void expandBEDBorders(File out_filepath, File input, int SIZE, boolean ExCenter) throws IOException {
11+
12+
Scanner scan = new Scanner(input);
13+
PrintStream OUT = System.out;
14+
if( out_filepath != null ) OUT = new PrintStream(out_filepath);
1615

1716
while (scan.hasNextLine()) {
1817
String[] temp = scan.nextLine().split("\t");

src/window_interface/Coordinate_Manipulation/BED_Manipulation/ExpandBEDWindow.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ public Void doInBackground() throws IOException {
7070
} else {
7171
setProgress(0);
7272
for(int x = 0; x < BEDFiles.size(); x++) {
73-
ExpandBED.expandBEDBorders(OUTPUT_PATH, BEDFiles.get(x), SIZE, rdbtnExpandFromCenter.isSelected());
73+
File XBED = BEDFiles.get(x);
74+
// Set outfilepath
75+
String newName = (XBED.getName()).substring(0,XBED.getName().length() - 4) + "_" + Integer.toString(SIZE) +"bp.bed";
76+
File OUT_FILE = null;
77+
if(OUTPUT_PATH == null) OUT_FILE = new File( newName );
78+
else OUT_FILE = new File( OUTPUT_PATH + File.separator + newName );
79+
// Execute expansion and update progress
80+
ExpandBED.expandBEDBorders(OUT_FILE, XBED, SIZE, rdbtnExpandFromCenter.isSelected());
7481
int percentComplete = (int)(((double)(x + 1) / BEDFiles.size()) * 100);
7582
setProgress(percentComplete);
7683
}

0 commit comments

Comments
 (0)