Skip to content

Commit 0fedaa3

Browse files
authored
Merge pull request #44 from CEGRcode/build-cli
Merge the Command Line version to master
2 parents 0aad216 + 3ab7ea0 commit 0fedaa3

128 files changed

Lines changed: 9542 additions & 3615 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply plugin: 'eclipse'
99

1010
sourceSets.main.java.srcDirs = ['src']
1111
mainClassName = "main.ScriptManager"
12-
version = 'v0.12-dev'
12+
version = 'v0.12-devCLI'
1313
sourceCompatibility = 1.8
1414
targetCompatibility = 1.8
1515

@@ -48,8 +48,12 @@ repositories {
4848

4949
// Declare the dependencies for your production and test code
5050
dependencies {
51-
compile 'org.slf4j:slf4j-api:1.7.13'
52-
testCompile 'junit:junit:4.12'
51+
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.13'
52+
// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
53+
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1'
54+
// https://mvnrepository.com/artifact/info.picocli/picocli #CommandLineInterface Parsing Args
55+
compile group: 'info.picocli', name: 'picocli', version: '4.2.0'
56+
testCompile group: 'junit', name: 'junit', version: '4.12'
5357
compile fileTree(dir: 'lib', include: ['*.jar'])
5458
}
5559

src/charts/CompositePlot.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,23 @@ public static Component createCompositePlot(double[] x, double[] y1, String name
5555
}
5656

5757
public static Component createCompositePlot(double[] x, double[] y1, String name){
58-
final XYSeriesCollection dataset = new XYSeriesCollection();
59-
final XYSeries seriesC = new XYSeries("Data");
60-
for(int i = 0; i < x.length; i++) {
61-
seriesC.add(x[i], y1[i]);
62-
}
63-
dataset.addSeries(seriesC);
64-
6558
ArrayList<Color> COLORS = new ArrayList<Color>();
6659
COLORS.add(Color.BLACK);
67-
final JFreeChart chart = createChart(dataset, name, COLORS);
68-
final ChartPanel chartPanel = new ChartPanel(chart);
69-
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
70-
return chartPanel;
60+
return(createCompositePlot(x, y1, name, COLORS));
7161
}
7262

73-
private static JFreeChart createChart(final XYDataset dataset, String TITLE, ArrayList<Color> COLORS) {
63+
public static JFreeChart createChart(final XYDataset dataset, String TITLE, ArrayList<Color> COLORS) {
64+
return(createChart(dataset, TITLE, COLORS, true));
65+
}
66+
67+
public static JFreeChart createChart(final XYDataset dataset, String TITLE, ArrayList<Color> COLORS, boolean legend) {
7468
//Call Chart
7569
final JFreeChart chart = ChartFactory.createXYLineChart(
7670
TITLE, // chart title
7771
"Distance from Feature (bp)", // x axis label
7872
"Score", // y axis label
7973
dataset, // data
80-
PlotOrientation.VERTICAL, true, // include legend
74+
PlotOrientation.VERTICAL, legend, // include legend
8175
true, // tooltips
8276
false // urls
8377
);
@@ -93,9 +87,10 @@ private static JFreeChart createChart(final XYDataset dataset, String TITLE, Arr
9387
renderer.setSeriesLinesVisible(x, true); //Spline visibility
9488
renderer.setSeriesShapesVisible(x, false); //Data point dot visibility
9589
renderer.setSeriesStroke(x, new BasicStroke(3));
90+
renderer.setSeriesPaint(x, COLORS.get(x));
9691
}
97-
renderer.setSeriesPaint(0, COLORS.get(0));
98-
if(COLORS.size() == 2) { renderer.setSeriesPaint(1, COLORS.get(1)); }
92+
// renderer.setSeriesPaint(0, COLORS.get(0));
93+
// if(COLORS.size() == 2) { renderer.setSeriesPaint(1, COLORS.get(1)); }
9994

10095
plot.setRenderer(renderer);
10196
// change the auto tick unit selection to integer units only...

src/charts/HeatMap.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package charts;
22

33
import java.awt.Color;
4+
import java.io.File;
5+
import java.io.IOException;
46
import org.jfree.chart.ChartPanel;
7+
import org.jfree.chart.ChartUtilities;
58
import org.jfree.chart.JFreeChart;
69
import org.jfree.chart.axis.AxisLocation;
710
import org.jfree.chart.axis.NumberAxis;
@@ -21,7 +24,7 @@ public HeatMap() {
2124

2225
}
2326

24-
public static ChartPanel createCorrelationHeatmap(String[] labels, double[][] MATRIX) {
27+
public static ChartPanel createCorrelationHeatmap(String[] labels, double[][] MATRIX, File output) {
2528
// create a paint-scale and a legend showing it
2629
LookupPaintScale paintScale = new LookupPaintScale(0, 1, Color.black);
2730
paintScale.add(0.0, new Color(0, 0, 255));
@@ -60,6 +63,14 @@ public static ChartPanel createCorrelationHeatmap(String[] labels, double[][] MA
6063

6164
chart.addSubtitle(legend);
6265
chart.setBackgroundPaint(Color.white);
66+
67+
if(output!=null){
68+
int width = 640;
69+
int height = 480;
70+
try{ ChartUtilities.saveChartAsPNG(output, chart, width, height); }
71+
catch( IOException e ){ e.printStackTrace(); }
72+
}
73+
6374
return new ChartPanel(chart);
6475
}
6576

src/charts/LineChart.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package charts;
22

33
import java.awt.Color;
4+
import java.io.File;
45
import java.io.IOException;
56
import java.util.ArrayList;
67

78
import org.jfree.chart.ChartFactory;
89
import org.jfree.chart.ChartPanel;
10+
import org.jfree.chart.ChartUtilities;
911
import org.jfree.chart.JFreeChart;
1012
import org.jfree.chart.axis.CategoryAxis;
1113
import org.jfree.chart.axis.CategoryLabelPositions;
@@ -45,6 +47,24 @@ public static ChartPanel createLineChart(ArrayList<Double> y1, ArrayList<Double>
4547
return chartPanel;
4648
}
4749

50+
public static ChartPanel createLineChart(ArrayList<Double> y, String[] x, File output) throws IOException {
51+
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
52+
for(int i = 0; i < x.length; i++) {
53+
dataset.addValue(y.get(i).doubleValue(), "Duplication Rate", x[i]);
54+
}
55+
56+
JFreeChart chart = createChart(dataset);
57+
final ChartPanel chartPanel = new ChartPanel(chart);
58+
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
59+
60+
if(output != null) {
61+
int width = 640;
62+
int height = 480;
63+
ChartUtilities.saveChartAsPNG(output, chart, width, height);
64+
}
65+
return chartPanel;
66+
}
67+
4868
private static JFreeChart createChart(CategoryDataset dataset) throws IOException {
4969
final JFreeChart chart = ChartFactory.createLineChart(
5070
"Paired-End Duplication Rate", // chart title
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package cli.BAM_Format_Converter;
2+
3+
import picocli.CommandLine;
4+
import picocli.CommandLine.ArgGroup;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Option;
7+
import picocli.CommandLine.Parameters;
8+
9+
import java.util.concurrent.Callable;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
14+
import objects.ToolDescriptions;
15+
import util.ExtensionFileFilter;
16+
import scripts.BAM_Format_Converter.BAMtoBED;
17+
18+
/**
19+
BAM_Format_ConverterCLI/SEStatsCLI
20+
*/
21+
@Command(name = "bam-to-bed", mixinStandardHelpOptions = true,
22+
description = ToolDescriptions.bam_to_bed_description,
23+
sortOptions = false,
24+
exitCodeOnInvalidInput = 1,
25+
exitCodeOnExecutionException = 1)
26+
public class BAMtoBEDCLI implements Callable<Integer> {
27+
28+
@Parameters( index = "0", description = "The BAM file from which we generate a new file.")
29+
private File bamFile;
30+
31+
@Option(names = {"-o", "--output"}, description = "specify output directory (name will be same as original with .bed ext)" )
32+
private File output = null;
33+
@Option(names = {"-s", "--stdout"}, description = "stream output file to STDOUT (cannot be used with \"-o\" flag)" )
34+
private boolean stdout = false;
35+
36+
//Read
37+
@ArgGroup(exclusive = true, multiplicity = "0..1", heading = "%nSelect Read to output:%n\t@|fg(red) (select no more than one of these options)|@%n")
38+
ReadType readType = new ReadType();
39+
static class ReadType {
40+
@Option(names = {"-1", "--read1"}, description = "output read 1 (default)")
41+
boolean read1 = false;
42+
@Option(names = {"-2", "--read2"}, description = "output read 2")
43+
boolean read2 = false;
44+
@Option(names = {"-a", "--all-reads"}, description = "output combined")
45+
boolean combined = false;
46+
@Option(names = {"-m", "--midpoint"}, description = "output midpoint (require PE)")
47+
boolean midpoint = false;
48+
@Option(names = {"-f", "--fragment"}, description = "output fragment (requires PE)")
49+
private boolean fragment = false;
50+
}
51+
52+
@Option(names = {"-p", "--mate-pair"}, description = "require proper mate pair (default not required)")
53+
private boolean matePair = false;
54+
@Option(names = {"-n", "--min-insert"}, description = "filter by min insert size in bp")
55+
private int MIN_INSERT = -9999;
56+
@Option(names = {"-x", "--max-insert"}, description = "filter by max insert size in bp")
57+
private int MAX_INSERT = -9999;
58+
59+
private int STRAND = -9999;
60+
private int PAIR;
61+
62+
@Override
63+
public Integer call() throws Exception {
64+
System.err.println( ">BAMtoBEDCLI.call()" );
65+
String validate = validateInput();
66+
if(!validate.equals("")){
67+
System.err.println( validate );
68+
System.err.println("Invalid input. Check usage using '-h' or '--help'");
69+
System.exit(1);
70+
}
71+
72+
BAMtoBED script_obj = new BAMtoBED(bamFile, output, STRAND, PAIR, MIN_INSERT, MAX_INSERT, null);
73+
script_obj.run();
74+
75+
System.err.println("Conversion Complete");
76+
return(0);
77+
}
78+
79+
private String validateInput() throws IOException {
80+
String r = "";
81+
82+
// set strand method
83+
if(readType.read1) { STRAND=0; }
84+
else if(readType.read2) { STRAND=1; }
85+
else if(readType.combined) { STRAND=2; }
86+
else if(readType.midpoint) { STRAND=3; }
87+
else if(readType.fragment) { STRAND=4; }
88+
else { STRAND=0; }
89+
90+
//check inputs exist
91+
if(!bamFile.exists()){
92+
r += "(!)BAM file does not exist: " + bamFile.getName() + "\n";
93+
return(r);
94+
}
95+
//check input extensions
96+
if(!"bam".equals(ExtensionFileFilter.getExtension(bamFile))){
97+
r += "(!)Is this a BAM file? Check extension: " + bamFile.getName() + "\n";
98+
}
99+
//check BAI exists
100+
File f = new File(bamFile+".bai");
101+
if(!f.exists() || f.isDirectory()){
102+
r += "(!)BAI Index File does not exist for: " + bamFile.getName() + "\n";
103+
}
104+
//set default output filename
105+
if(output==null && !stdout){
106+
if(STRAND==0){ output = new File( bamFile.getName().split("\\.")[0] + "_READ1.bed" ); }
107+
else if(STRAND==1){ output = new File( bamFile.getName().split("\\.")[0] + "_READ2.bed" ); }
108+
else if(STRAND==2){ output = new File( bamFile.getName().split("\\.")[0] + "_COMBINED.bed" ); }
109+
else if(STRAND==3){ output = new File( bamFile.getName().split("\\.")[0] + "_MIDPOINT.bed" ); }
110+
else if(STRAND==4){ output = new File( bamFile.getName().split("\\.")[0] + "_FRAGMENT.bed" ); }
111+
else { r += "(!)Somehow invalid STRAND!This error should never print. Check code if it does.\n"; }
112+
//check stdout and output not both selected
113+
}else if(stdout){
114+
if(output!=null){ r += "(!)Cannot use -s flag with -o.\n"; }
115+
//check output filename is valid
116+
}else{
117+
//check ext
118+
try{
119+
if(!"bed".equals(ExtensionFileFilter.getExtension(output))){
120+
r += "(!)Use BED extension for output filename. Try: " + ExtensionFileFilter.stripExtension(output) + ".bed\n";
121+
}
122+
} catch( NullPointerException e){ r += "(!)Output filename must have extension: use BED extension for output filename. Try: " + output + ".bed\n"; }
123+
//check directory
124+
if(output.getParent()==null){
125+
// System.err.println("default to current directory");
126+
} else if(!new File(output.getParent()).exists()){
127+
r += "(!)Check output directory exists: " + output.getParent() + "\n";
128+
}
129+
}
130+
131+
// validate insert sizes
132+
if( MIN_INSERT<0 && MIN_INSERT!=-9999 ){ r += "MIN_INSERT must be a positive integer value: " + MIN_INSERT + "\n"; }
133+
if( MAX_INSERT<0 && MAX_INSERT!=-9999 ){ r += "MAX_INSERT must be a positive integer value: " + MAX_INSERT + "\n"; }
134+
if( MAX_INSERT<MIN_INSERT ){ r += "MAX_INSERT must be larger/equal to MIN_INSERT: " + MIN_INSERT + "," + MAX_INSERT + "\n"; }
135+
// turn pair status boolean into int
136+
PAIR = matePair ? 1 : 0;
137+
138+
return(r);
139+
}
140+
}

0 commit comments

Comments
 (0)