1111import java .util .regex .Matcher ;
1212import java .util .regex .Pattern ;
1313
14+ import objects .CustomExceptions .FASTAException ;
1415import objects .ToolDescriptions ;
1516import util .FASTAUtilities ;
1617import util .ExtensionFileFilter ;
1718import scripts .BAM_Manipulation .FilterforPIPseq ;
1819
1920/**
20- BAM_ManipulatioCLIn/FilterforPIPseqCLI
21- */
22- @ Command (name = "filter-pip-seq" , mixinStandardHelpOptions = true ,
23- description = ToolDescriptions .filter_pip_seq_description + "\n " +
24- "Note this program does not index the resulting BAM file and user must use appropriate samtools command to generate BAI." ,
25- sortOptions = false ,
26- exitCodeOnInvalidInput = 1 ,
27- exitCodeOnExecutionException = 1 )
21+ * BAM_ManipulatioCLIn/FilterforPIPseqCLI
22+ */
23+ @ Command (name = "filter-pip-seq" , mixinStandardHelpOptions = true , description = ToolDescriptions .filter_pip_seq_description
24+ + "\n "
25+ + "Note this program does not index the resulting BAM file and user must use appropriate samtools command to generate BAI." , version = "ScriptManager "
26+ + ToolDescriptions .VERSION , sortOptions = false , exitCodeOnInvalidInput = 1 , exitCodeOnExecutionException = 1 )
2827public class FilterforPIPseqCLI implements Callable <Integer > {
29-
30- @ Parameters ( index = "0" , description = "The reference genome FASTA file." )
28+
29+ @ Parameters (index = "0" , description = "The reference genome FASTA file." )
3130 private File genomeFASTA ;
32- @ Parameters ( index = "1" , description = "The BAM file from which we filter." )
31+ @ Parameters (index = "1" , description = "The BAM file from which we filter." )
3332 private File bamFile ;
34-
35- @ Option (names = {"-o" , "--output" }, description = "specify output file (default=<bamFileNoExt>_PSfilter.bam)" )
33+
34+ @ Option (names = { "-o" , "--output" }, description = "specify output file (default=<bamFileNoExt>_PSfilter.bam)" )
3635 private File output = null ;
37- @ Option (names = {"-f" , "--filter" }, description = "filter by upstream sequence, works only for single-nucleotide A,T,C, or G. (default seq ='T')" )
36+ @ Option (names = { "-f" ,
37+ "--filter" }, description = "filter by upstream sequence, works only for single-nucleotide A,T,C, or G. (default seq ='T')" )
3838 private String filterString = "T" ;
39-
39+
4040 @ Override
4141 public Integer call () throws Exception {
42- System .err .println ( ">FilterforPIPseqCLI.call()" );
42+ System .err .println (">FilterforPIPseqCLI.call()" );
4343 String validate = validateInput ();
44- if (!validate .equals ("" )){
45- System .err .println ( validate );
44+ if (!validate .equals ("" )) {
45+ System .err .println (validate );
4646 System .err .println ("Invalid input. Check usage using '-h' or '--help'" );
4747 System .exit (1 );
4848 }
49-
50- FilterforPIPseq script_obj = new FilterforPIPseq (bamFile , genomeFASTA , output , filterString , null );
51- script_obj .run ();
52-
53- System .err .println ( "BAM Generated." );
54- return (0 );
49+
50+ try {
51+ FilterforPIPseq script_obj = new FilterforPIPseq (bamFile , genomeFASTA , output , filterString , null );
52+ script_obj .run ();
53+ } catch (FASTAException e ) {
54+ System .err .println (e .getMessage () + "\n " );
55+ }
56+
57+ System .err .println ("BAM Generated." );
58+ return (0 );
5559 }
56-
57- //validateInput outline
60+
61+ // validateInput outline
5862 private String validateInput () throws IOException {
5963 String r = "" ;
60-
61- //check inputs exist
62- if (!bamFile .exists ()){
64+
65+ // check inputs exist
66+ if (!bamFile .exists ()) {
6367 r += "(!)BAM file does not exist: " + bamFile .getName () + "\n " ;
6468 }
65- if (!genomeFASTA .exists ()){
69+ if (!genomeFASTA .exists ()) {
6670 r += "(!)FASTA file does not exist: " + genomeFASTA .getName () + "\n " ;
6771 }
68- if (!r .equals ("" )){ return (r ); }
69- //check input extensions
72+ if (!r .equals ("" )) {
73+ return (r );
74+ }
75+ // check input extensions
7076 ExtensionFileFilter faFilter = new ExtensionFileFilter ("fa" );
71- if (!faFilter .accept (genomeFASTA )){
77+ if (!faFilter .accept (genomeFASTA )) {
7278 r += "(!)Is this a FASTA file? Check extension: " + genomeFASTA .getName () + "\n " ;
7379 }
74- if (!"bam" .equals (ExtensionFileFilter .getExtension (bamFile ))){
80+ if (!"bam" .equals (ExtensionFileFilter .getExtension (bamFile ))) {
7581 r += "(!)Is this a BAM file? Check extension: " + bamFile .getName () + "\n " ;
7682 }
77- //check BAI exists
78- File f = new File (bamFile + ".bai" );
79- if (!f .exists () || f .isDirectory ()){
83+ // check BAI exists
84+ File f = new File (bamFile + ".bai" );
85+ if (!f .exists () || f .isDirectory ()) {
8086 r += "(!)BAI Index File does not exist for: " + bamFile .getName () + "\n " ;
8187 }
82- //check FAI exists (generate if not)
88+ // check FAI exists (generate if not)
8389 File FAI = new File (genomeFASTA + ".fai" );
84- if (!FAI .exists () || FAI .isDirectory ()) {
90+ if (!FAI .exists () || FAI .isDirectory ()) {
8591 System .err .println ("FASTA Index file not found.\n Generating new one...\n " );
86- boolean FASTA_INDEX = FASTAUtilities .buildFASTAIndex (genomeFASTA );
87- if (!FASTA_INDEX ){ r += "FASTA Index failed to build." ; }
92+ try {
93+ FASTAUtilities .buildFASTAIndex (genomeFASTA );
94+ } catch (FASTAException e ) {
95+ r += e .getMessage ();
96+ }
97+
8898 }
89- //set default output filename
90- if (output == null ){
99+ // set default output filename
100+ if (output == null ) {
91101 output = new File (ExtensionFileFilter .stripExtension (bamFile ) + "_PSfilter.bam" );
92- //check output filename is valid
93- }else {
94- //check ext
95- try {
96- if (!"bam" .equals (ExtensionFileFilter .getExtension (output ))){
97- r += "(!)Use BAM extension for output filename. Try: " + ExtensionFileFilter .stripExtension (output ) + ".bam\n " ;
102+ // check output filename is valid
103+ } else {
104+ // check ext
105+ try {
106+ if (!"bam" .equals (ExtensionFileFilter .getExtension (output ))) {
107+ r += "(!)Use BAM extension for output filename. Try: " + ExtensionFileFilter .stripExtension (output )
108+ + ".bam\n " ;
98109 }
99- } catch ( NullPointerException e ){ r += "(!)Output filename must have extension: use BAM extension for output filename. Try: " + ExtensionFileFilter .stripExtension (output ) + ".bam\n " ; }
100- //check directory
101- if (output .getParent ()==null ){
110+ } catch (NullPointerException e ) {
111+ r += "(!)Output filename must have extension: use BAM extension for output filename. Try: "
112+ + ExtensionFileFilter .stripExtension (output ) + ".bam\n " ;
113+ }
114+ // check directory
115+ if (output .getParent () == null ) {
102116// System.err.println("default to current directory");
103- } else if (!new File (output .getParent ()).exists ()){
104- r += "(!)Check output directory exists: " + output .getParent () + "\n " ;
105- }
117+ } else if (!new File (output .getParent ()).exists ()) {
118+ r += "(!)Check output directory exists: " + output .getParent () + "\n " ;
119+ }
106120 }
107-
108- //check filter string is valid ATCG
121+
122+ // check filter string is valid ATCG
109123 Pattern seqPat = Pattern .compile ("[ATCG]+" );
110- Matcher m = seqPat .matcher ( filterString );
111- if ( !m .matches () ) {
112- r += "(!)Filter string must be formatted as a nucleotide sequence.\n " + filterString +
113- " is not a valid nucleotide sequence.\n Expected input string format: \" [ATCG]\" " ;
124+ Matcher m = seqPat .matcher (filterString );
125+ if ( !m .matches ()) {
126+ r += "(!)Filter string must be formatted as a nucleotide sequence.\n " + filterString
127+ + " is not a valid nucleotide sequence.\n Expected input string format: \" [ATCG]\" " ;
114128 }
115-
116- return (r );
129+
130+ return (r );
117131 }
118132}
0 commit comments