|
| 1 | +package cli.Coordinate_Manipulation; |
| 2 | + |
| 3 | +import picocli.CommandLine.Command; |
| 4 | +import picocli.CommandLine.Option; |
| 5 | +import picocli.CommandLine.Parameters; |
| 6 | + |
| 7 | +import java.util.concurrent.Callable; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.FileNotFoundException; |
| 12 | + |
| 13 | +import objects.ToolDescriptions; |
| 14 | +import util.CDTUtilities; |
| 15 | +import util.ExtensionFileFilter; |
| 16 | +import scripts.Coordinate_Manipulation.ShiftCoord; |
| 17 | + |
| 18 | +/** |
| 19 | + Coordinate_ManipulationCLI/ShiftCoordCLI |
| 20 | +*/ |
| 21 | +@Command(name = "shift-coord", mixinStandardHelpOptions = true, |
| 22 | + description = ToolDescriptions.shift_coordinate_description, |
| 23 | + version = "ScriptManager "+ ToolDescriptions.VERSION, |
| 24 | + sortOptions = false, |
| 25 | + exitCodeOnInvalidInput = 1, |
| 26 | + exitCodeOnExecutionException = 1) |
| 27 | +public class ShiftCoordCLI implements Callable<Integer> { |
| 28 | + |
| 29 | + @Parameters( index = "0", description = "the coordinate file (BED/GFF format) to shift") |
| 30 | + private File input; |
| 31 | + |
| 32 | + @Option(names = {"-o", "--output"}, description = "specify output filepath (default input filename with _shiftXXXbp.bed/gff)") |
| 33 | + private String outputFilepath = null; |
| 34 | + @Option(names = {"-t", "--shift"}, description = "shift distance in bp, upstream < 0 and downstream > 0 (default=0)") |
| 35 | + private int shift = 0; |
| 36 | + @Option(names = {"-u", "--unstranded"}, description = "don't force strandedness (default=forced)") |
| 37 | + private boolean stranded = true; |
| 38 | + @Option(names = {"-z", "--gzip"}, description = "gzip output (default=false)") |
| 39 | + private boolean gzOutput = false; |
| 40 | + @Option(names = {"--gff"}, description = "input is GFF format (default=BED format)") |
| 41 | + private boolean isGFF = false; |
| 42 | + |
| 43 | + @Override |
| 44 | + public Integer call() throws Exception { |
| 45 | + System.err.println( ">ShiftCoord.call()" ); |
| 46 | + String validate = validateInput(); |
| 47 | + if(!validate.equals("")){ |
| 48 | + System.err.println( validate ); |
| 49 | + System.err.println("Invalid input. Check usage using '-h' or '--help'"); |
| 50 | + System.exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + if(isGFF) { |
| 54 | + ShiftCoord.shiftGFFInterval(new File(outputFilepath), input, shift, stranded, gzOutput); |
| 55 | + } else { |
| 56 | + ShiftCoord.shiftBEDInterval(new File(outputFilepath), input, shift, stranded, gzOutput); |
| 57 | + } |
| 58 | + |
| 59 | + System.err.println("Shift Complete"); |
| 60 | + return(0); |
| 61 | + } |
| 62 | + |
| 63 | + private String validateInput() throws IOException { |
| 64 | + String r = ""; |
| 65 | + |
| 66 | + //check inputs exist |
| 67 | + if(!input.exists()){ |
| 68 | + r += "(!)BED/GFF file does not exist: " + input.getName() + "\n"; |
| 69 | + } |
| 70 | + if(!"".equals(r)){ return(r); } |
| 71 | + |
| 72 | + //set default output filename |
| 73 | + if(outputFilepath==null){ |
| 74 | + String SUFFIX = shift < 0 ? "_shift" + shift + "bp." : "_shift+" + shift + "bp."; |
| 75 | + SUFFIX += isGFF ? "gff" : "bed"; |
| 76 | + SUFFIX += gzOutput ? ".gz" : ""; |
| 77 | + outputFilepath = ExtensionFileFilter.stripExtension(input) + SUFFIX; |
| 78 | + //check output filename is valid |
| 79 | + }else{ |
| 80 | + //no extension check |
| 81 | + //check directory |
| 82 | + File BASEFILE = new File(outputFilepath); |
| 83 | + if(BASEFILE.getParent()==null){ |
| 84 | +// System.err.println("default to current directory"); |
| 85 | + } else if(!new File(BASEFILE.getParent()).exists()){ |
| 86 | + r += "(!)Check output directory exists: " + BASEFILE.getParent() + "\n"; |
| 87 | + } |
| 88 | + } |
| 89 | + return(r); |
| 90 | + } |
| 91 | +} |
0 commit comments