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