|
| 1 | +package scriptmanager.cli.BAM_Statistics; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.concurrent.Callable; |
| 6 | + |
| 7 | +import picocli.CommandLine.ArgGroup; |
| 8 | +import picocli.CommandLine.Command; |
| 9 | +import picocli.CommandLine.Option; |
| 10 | +import picocli.CommandLine.Parameters; |
| 11 | +import scriptmanager.objects.ToolDescriptions; |
| 12 | +import scriptmanager.objects.ArchTEx.CorrParameter; |
| 13 | +import scriptmanager.scripts.BAM_Statistics.CrossCorrelation; |
| 14 | +import scriptmanager.util.ExtensionFileFilter; |
| 15 | + |
| 16 | +/** |
| 17 | + * Command line interface class for performing the ArchTEX cross correlation |
| 18 | + * analysis by calling a method implemented in the scripts package. |
| 19 | + * |
| 20 | + * @author Olivia Lang |
| 21 | + * @see scriptmanager.objects.ArchTEx.CorrParameter |
| 22 | + * @see scriptmanager.scripts.BAM_Statistics.CrossCorrelation |
| 23 | + */ |
| 24 | +@Command(name = "cross-corr", mixinStandardHelpOptions = true, |
| 25 | + description = ToolDescriptions.archtex_crosscorrelation_description, |
| 26 | + version = "ScriptManager "+ ToolDescriptions.VERSION, |
| 27 | + sortOptions = false, |
| 28 | + exitCodeOnInvalidInput = 1, |
| 29 | + exitCodeOnExecutionException = 1) |
| 30 | +public class CrossCorrelationCLI implements Callable<Integer> { |
| 31 | + |
| 32 | + @Parameters( index = "0", description = "The BAM file to perform the cross-correlation on") |
| 33 | + private File bamFile; |
| 34 | + |
| 35 | + @Option(names = {"-o", "--output"}, description = "specify output basename, default is the BAM input filename without extension") |
| 36 | + private File outputBasename = null; |
| 37 | + |
| 38 | + @ArgGroup(exclusive = true, multiplicity = "0..1", heading = "%nSelect correlation strategy:%n\t@|fg(red) (select no more than one of these options)|@%n") |
| 39 | + CorrType corrType = new CorrType(); |
| 40 | + static class CorrType { |
| 41 | + @Option(names = {"-g", "--genome"}, description = "Use the full genome correlation method") |
| 42 | + private boolean corrGenome = false; |
| 43 | + @Option(names = {"-r", "--random"}, description = "Use the random sampling correlation method (default)") |
| 44 | + private boolean corrRandom = false; |
| 45 | + } |
| 46 | + |
| 47 | + @Option(names = {"-t", "--cpu"}, description = "set number of threads for performance tuning (default=1)") |
| 48 | + private int cpu = 1; |
| 49 | + |
| 50 | + @ArgGroup(exclusive = true, multiplicity = "0..1", heading = "%nRandom Sampling Options:%n\t@|fg(red) (ignored if full genome correlation method selected)|@%n") |
| 51 | + SamplingParams samplingParams = new SamplingParams(); |
| 52 | + static class SamplingParams { |
| 53 | + @Option(names = {"-w", "--window"}, description = "set window frame size for each extraction (default=50kb)") |
| 54 | + private int windowSize = 50000; |
| 55 | + @Option(names = {"-i", "--iterations"}, description = "set number of random iterations per chromosome (default=10)") |
| 56 | + private int iterations = 10; |
| 57 | + } |
| 58 | + |
| 59 | + CorrParameter param = new CorrParameter(); |
| 60 | + |
| 61 | + @Override |
| 62 | + public Integer call() throws Exception { |
| 63 | + System.err.println( ">CrossCorrelationCLI.call()" ); |
| 64 | + String validate = validateInput(); |
| 65 | + if(!validate.equals("")){ |
| 66 | + System.err.println( validate ); |
| 67 | + System.err.println("Invalid input. Check usage using '-h' or '--help'"); |
| 68 | + System.exit(1); |
| 69 | + } |
| 70 | + |
| 71 | + CrossCorrelation.correlate( outputBasename, bamFile, param, null); |
| 72 | + |
| 73 | + System.err.println("Calculations Complete"); |
| 74 | + return(0); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Validate the input values before executing the script. |
| 79 | + * |
| 80 | + * @return a multi-line string describing input validation issues |
| 81 | + * @throws IOException |
| 82 | + */ |
| 83 | + private String validateInput() throws IOException { |
| 84 | + String r = ""; |
| 85 | + |
| 86 | + //check inputs exist |
| 87 | + if(!bamFile.exists()){ |
| 88 | + r += "(!)BAM file does not exist: " + bamFile.getName() + "\n"; |
| 89 | + return(r); |
| 90 | + } |
| 91 | + //check BAI exists |
| 92 | + File f = new File(bamFile+".bai"); |
| 93 | + if(!f.exists() || f.isDirectory()){ |
| 94 | + r += "(!)BAI Index File does not exist for: " + bamFile.getName() + "\n"; |
| 95 | + } |
| 96 | + //set default output filename |
| 97 | + if(outputBasename==null){ |
| 98 | +// output = new File("output_bam_stats.txt"); //this default name mimics the gui |
| 99 | + outputBasename = new File(ExtensionFileFilter.stripExtension(bamFile) + "_CrossCorrelation.txt"); |
| 100 | + //check output filename is valid |
| 101 | + }else{ |
| 102 | + //no check ext |
| 103 | + //check directory |
| 104 | + if(outputBasename.getParent()==null){ |
| 105 | +// System.err.println("default to current directory"); |
| 106 | + } else if(!new File(outputBasename.getParent()).exists()){ |
| 107 | + r += "(!)Check output directory exists: " + outputBasename.getParent() + "\n"; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + //validate random sampling params |
| 112 | + if (corrType.corrGenome) { |
| 113 | + param.setCorrType(true); |
| 114 | + } else { |
| 115 | + param.setCorrType(false); |
| 116 | + if (samplingParams.windowSize<1) { r += "(!)Window size must be at least 1\n"; } |
| 117 | + if (samplingParams.iterations<1) { r += "(!)Num iterations must be at least 1\n"; } |
| 118 | + param.setWindow(samplingParams.windowSize); |
| 119 | + } |
| 120 | + // valiate CPU |
| 121 | + if (cpu<1) { r += "(!)CPU count must be at least 1\n"; } |
| 122 | + param.setThreads(cpu); |
| 123 | + |
| 124 | + return(r); |
| 125 | + } |
| 126 | +} |
0 commit comments