Search in sources :

Example 6 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class InteractiveInput method posHistory.

private void posHistory(List<String> cmdInput, List<GenomicCoords> history, int userWindowSize) throws InvalidCommandLineException {
    List<String> args = new ArrayList<String>(cmdInput);
    // Remove cmd name.
    args.remove(0);
    int n = 10;
    if (args.contains("-n")) {
        try {
            n = Integer.parseInt(args.get(args.indexOf("-n") + 1));
            args.remove(args.get(args.indexOf("n") + 1));
            args.remove("-n");
        } catch (Exception e) {
            System.err.println(Utils.padEndMultiLine("Argument to -n parameter must be an integer", userWindowSize));
            throw new InvalidCommandLineException();
        }
    }
    // Start listing positions from this index
    int start = 0;
    if (history.size() > n && n > 0) {
        start = history.size() - n;
    }
    for (int i = start; i < history.size(); i++) {
        GenomicCoords xgc = history.get(i);
        System.err.println(Utils.padEndMultiLine(xgc.toString(), userWindowSize));
    }
}
Also used : ArrayList(java.util.ArrayList) InvalidCommandLineException(exceptions.InvalidCommandLineException) InvalidCommandLineException(exceptions.InvalidCommandLineException) InvalidColourException(exceptions.InvalidColourException) InvalidRecordException(exceptions.InvalidRecordException) SQLException(java.sql.SQLException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) InvalidConfigException(exceptions.InvalidConfigException) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) InvalidGenomicCoordsException(exceptions.InvalidGenomicCoordsException)

Example 7 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class TrackSet method setTrackHeightForRegex.

/**
 * From cmdInput extract regex and yMaxLines then iterate through the tracks list to set
 * the yMaxLines in the tracks whose filename matches the regex.
 * The input list is updated in place!
 * @throws SQLException
 * @throws InvalidRecordException
 * @throws InvalidGenomicCoordsException
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws MalformedURLException
 */
public void setTrackHeightForRegex(List<String> tokens) throws InvalidCommandLineException, MalformedURLException, ClassNotFoundException, IOException, InvalidGenomicCoordsException, InvalidRecordException, SQLException {
    // MEMO of subcommand syntax:
    // 0 trackHeight
    // 1 int    mandatory
    // 2 regex  optional
    boolean invertSelection = Utils.argListContainsFlag(tokens, "-v");
    if (tokens.size() < 2) {
        System.err.println("Error in trackHeight subcommand. Expected 2 args got: " + tokens);
        throw new InvalidCommandLineException();
    }
    // Get height
    try {
        this.trackHeightForRegex = Integer.parseInt(tokens.get(1));
        this.trackHeightForRegex = this.trackHeightForRegex < 0 ? 0 : this.trackHeightForRegex;
    } catch (NumberFormatException e) {
        System.err.println("Number format exception: " + this.trackHeightForRegex);
        throw new InvalidCommandLineException();
    }
    // Regex
    List<String> trackNameRegex = new ArrayList<String>();
    if (tokens.size() >= 3) {
        trackNameRegex = tokens.subList(2, tokens.size());
    } else {
        // Default: Capture everything
        trackNameRegex.add(".*");
    }
    // Update
    this.regexForTrackHeight.clear();
    for (String x : trackNameRegex) {
        try {
            this.regexForTrackHeight.add(Pattern.compile(x));
        } catch (PatternSyntaxException e) {
            System.err.println("Command: " + tokens);
            System.err.println("Invalid regex in: " + x);
            System.err.println(e.getDescription());
            throw new InvalidCommandLineException();
        }
    }
    // And set as required:
    List<Track> tracksToReset = this.matchTracks(trackNameRegex, true, invertSelection);
    for (Track tr : tracksToReset) {
        tr.setyMaxLines(this.trackHeightForRegex);
    }
}
Also used : InvalidCommandLineException(exceptions.InvalidCommandLineException) ArrayList(java.util.ArrayList) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 8 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class TrackSet method setFeatureColorForRegex.

public void setFeatureColorForRegex(List<String> cmdTokens) throws InvalidCommandLineException, InvalidColourException {
    List<String> argList = new ArrayList<String>(cmdTokens);
    // Remove cmd name
    argList.remove(0);
    // Collect all regex/color pairs from input. We move left to right along the command
    // arguments and collect -r/-R and set the regex inversion accordingly.
    List<Argument> colorForRegex = new ArrayList<Argument>();
    new Xterm256();
    while (argList.contains("-r") || argList.contains("-R")) {
        int r = argList.indexOf("-r") >= 0 ? argList.indexOf("-r") : Integer.MAX_VALUE;
        int R = argList.indexOf("-R") >= 0 ? argList.indexOf("-R") : Integer.MAX_VALUE;
        List<String> pair;
        boolean invert = false;
        if (r < R) {
            pair = Utils.getNArgsForParam(argList, "-r", 2);
        } else {
            pair = Utils.getNArgsForParam(argList, "-R", 2);
            invert = true;
        }
        String pattern = pair.get(0);
        try {
            // Check valid regex
            Pattern.compile(pattern);
        } catch (PatternSyntaxException e) {
            System.err.println("Invalid regex: " + pattern);
            throw new InvalidCommandLineException();
        }
        Argument xcolor = new Argument(pair.get(0), pair.get(1), invert);
        // Check this is a valid colour
        Xterm256.colorNameToXterm256(xcolor.getArg());
        colorForRegex.add(xcolor);
    }
    if (colorForRegex.size() == 0) {
        colorForRegex = null;
    }
    boolean invertSelection = Utils.argListContainsFlag(argList, "-v");
    // Regex to capture tracks: All positional args left
    List<String> trackNameRegex = new ArrayList<String>();
    if (argList.size() > 0) {
        trackNameRegex = argList;
    } else {
        // Default: Capture everything
        trackNameRegex.add(".*");
    }
    // Set as appropriate
    List<Track> tracksToReset = this.matchTracks(trackNameRegex, true, invertSelection);
    for (Track tr : tracksToReset) {
        tr.setColorForRegex(colorForRegex);
    }
}
Also used : Xterm256(coloring.Xterm256) ArrayList(java.util.ArrayList) InvalidCommandLineException(exceptions.InvalidCommandLineException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 9 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class TrackSet method setFilterForTrackIntervalFeature.

/**
 * Set filter for IntervalFeature tracks.
 * @throws SQLException
 * @throws InvalidRecordException
 * @throws InvalidGenomicCoordsException
 * @throws IOException
 * @throws ClassNotFoundException
 */
public void setFilterForTrackIntervalFeature(List<String> cmdInput) throws InvalidCommandLineException, ClassNotFoundException, IOException, InvalidGenomicCoordsException, InvalidRecordException, SQLException {
    List<String> args = new ArrayList<String>();
    for (String x : cmdInput) {
        args.add(x);
    }
    // Remove command name
    args.remove(0);
    // Default
    Pattern showRegex = Pattern.compile(Filter.DEFAULT_SHOW_REGEX.getValue());
    Pattern hideRegex = Pattern.compile(Filter.DEFAULT_HIDE_REGEX.getValue());
    // Get args:
    boolean invertSelection = Utils.argListContainsFlag(args, "-v");
    int flag = 0;
    if (Utils.argListContainsFlag(args, "-F")) {
        flag |= Pattern.LITERAL;
    }
    if (!Utils.argListContainsFlag(args, "-c")) {
        flag |= Pattern.CASE_INSENSITIVE;
    }
    try {
        if (args.contains("-i")) {
            int idx = args.indexOf("-i") + 1;
            showRegex = Pattern.compile(args.get(idx), flag);
            args.remove(idx);
            args.remove("-i");
        }
        if (args.contains("-e")) {
            int idx = args.indexOf("-e") + 1;
            hideRegex = Pattern.compile(args.get(idx), flag);
            args.remove(idx);
            args.remove("-e");
        }
    } catch (PatternSyntaxException e) {
        System.err.println("Invalid regex");
        throw new InvalidCommandLineException();
    }
    // What is left is positional args of regexes
    List<String> trackNameRegex = new ArrayList<String>();
    trackNameRegex.addAll(args);
    if (trackNameRegex.size() == 0) {
        // Default regex for matching tracks
        trackNameRegex.add(".*");
    }
    // TRACK REGEXES
    // Regex
    // And set as required:
    List<Track> tracksToReset = this.matchTracks(trackNameRegex, true, invertSelection);
    for (Track tr : tracksToReset) {
        tr.setShowHideRegex(showRegex, hideRegex);
    }
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) InvalidCommandLineException(exceptions.InvalidCommandLineException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 10 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class TrackSet method setPrintModeAndPrintFeaturesForRegex.

public void setPrintModeAndPrintFeaturesForRegex(List<String> cmdInput) throws InvalidCommandLineException, IOException, InvalidGenomicCoordsException, InvalidColourException, ArgumentParserException {
    // --------------------------------------------------------------------
    // PARSE ARGUMENTS
    List<String> args = new ArrayList<String>(cmdInput);
    // Remove cmd name.
    args.remove(0);
    // Defaults if no args given
    PrintRawLine printMode = null;
    // opts.getInt("nlines"); // Default number of lines to print
    int count = 10;
    // opts.getList("track_regex");
    List<String> trackNameRegex = new ArrayList<String>();
    trackNameRegex.add(".*");
    String printToFile = null;
    boolean append = false;
    // PARSE ARGS
    // --------------------------------------------------------------------
    boolean invertSelection = Utils.argListContainsFlag(args, "-v");
    boolean esf = Utils.argListContainsFlag(args, "-esf");
    if (esf) {
        printMode = PrintRawLine.CLIP;
    }
    Pattern highlightPattern = Pattern.compile("");
    if (args.contains("-hl")) {
        printMode = PrintRawLine.CLIP;
        String p = Utils.getArgForParam(args, "-hl", "");
        highlightPattern = Pattern.compile(p);
    }
    if (args.contains("-clip")) {
        printMode = PrintRawLine.CLIP;
        args.remove("-clip");
    }
    if (args.contains("-full")) {
        printMode = PrintRawLine.FULL;
        args.remove("-full");
    }
    if (args.contains("-off")) {
        printMode = PrintRawLine.OFF;
        args.remove("-off");
    }
    if (args.contains("-n")) {
        printMode = PrintRawLine.NO_ACTION;
        int idx = args.indexOf("-n") + 1;
        try {
            count = Integer.parseInt(args.get(idx));
            args.remove(idx);
            args.remove("-n");
        } catch (NumberFormatException e) {
            System.err.println("Invalid argument to -n. Expected INT. Got '" + args.get(idx) + "'");
            throw new InvalidCommandLineException();
        }
    }
    String sys = Utils.getArgForParam(args, "-sys", "");
    if (sys != "") {
        printMode = PrintRawLine.NO_ACTION;
    }
    String round = Utils.getArgForParam(args, "-round", "");
    Integer printNumDecimals = null;
    if (!round.isEmpty()) {
        printMode = PrintRawLine.NO_ACTION;
        try {
            printNumDecimals = Integer.valueOf(round);
        } catch (NumberFormatException e) {
            System.err.println("Invalid argument to -round. An integer is expected.");
            throw new InvalidCommandLineException();
        }
    }
    // Capture the redirection operator and remove operator and filename.
    // Position of the redirection operator, -1 if not present.
    int idx = -1;
    if (args.contains(">")) {
        idx = args.indexOf(">");
    } else if (args.contains(">>")) {
        idx = args.indexOf(">>");
        append = true;
    }
    if (idx >= 0) {
        // Redirection found, write to file, unless file is not given.
        try {
            printToFile = args.get(idx + 1);
            args.remove(idx);
            args.remove(printToFile);
            printToFile = Utils.tildeToHomeDir(printToFile);
        } catch (IndexOutOfBoundsException e) {
            System.err.println("No file found to write to.");
            throw new InvalidCommandLineException();
        }
    }
    // Everything left in arg list is positional args of regex for tracks
    if (args.size() >= 1) {
        trackNameRegex = args;
    }
    // END PARSING ARGS.
    // --------------------------------------------------------------------
    // Tracks affected by this command:
    List<Track> tracksToReset = this.matchTracks(trackNameRegex, true, invertSelection);
    // If we print to file just do that.
    if (printToFile != null && !printToFile.isEmpty()) {
        if (!append) {
            new File(printToFile).delete();
        }
        for (Track tr : tracksToReset) {
            tr.setExportFile(printToFile);
            tr.setHighlightPattern(highlightPattern);
            tr.printLines();
            // This is not ideal: redirecting to file also set mode to off
            tr.setPrintMode(PrintRawLine.OFF);
            // Reset to null so we don't keep writing to this file once we go to another position.
            tr.setExportFile(null);
        }
        return;
    }
    // Process as required
    for (Track tr : tracksToReset) {
        tr.setExplainSamFlag(esf);
        tr.setHighlightPattern(highlightPattern);
        tr.setPrintRawLineCount(count);
        tr.setSystemCommandForPrint(sys);
        if (printNumDecimals != null)
            tr.setPrintNumDecimals(printNumDecimals);
        if (printMode != null && printMode.equals(PrintRawLine.NO_ACTION)) {
            if (tr.getPrintMode().equals(PrintRawLine.OFF) && (cmdInput.contains("-n") || cmdInput.contains("-sys") || cmdInput.contains("-round"))) {
                // Make -n or -sys switch ON the printing mode
                // This happens if you exec `print -n INT` with the track set to OFF.
                tr.setPrintMode(PrintRawLine.CLIP);
            }
        // 
        } else if (printMode != null) {
            tr.setPrintMode(printMode);
        } else if (tr.getPrintMode().equals(PrintRawLine.OFF)) {
            // Toggle
            tr.setPrintMode(PrintRawLine.CLIP);
        } else {
            tr.setPrintMode(PrintRawLine.OFF);
        }
    // tr.setCutScriptForPrinting(cutScript);
    }
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) InvalidCommandLineException(exceptions.InvalidCommandLineException) File(java.io.File)

Aggregations

InvalidCommandLineException (exceptions.InvalidCommandLineException)18 ArrayList (java.util.ArrayList)13 PatternSyntaxException (java.util.regex.PatternSyntaxException)8 IOException (java.io.IOException)6 InvalidColourException (exceptions.InvalidColourException)5 InvalidConfigException (exceptions.InvalidConfigException)5 InvalidGenomicCoordsException (exceptions.InvalidGenomicCoordsException)5 InvalidRecordException (exceptions.InvalidRecordException)5 SQLException (java.sql.SQLException)5 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)5 Pattern (java.util.regex.Pattern)4 File (java.io.File)3 Test (org.junit.Test)3 GenomicCoords (samTextViewer.GenomicCoords)3 Config (coloring.Config)1 ConfigKey (coloring.ConfigKey)1 Xterm256 (coloring.Xterm256)1 CommandHelp (commandHelp.CommandHelp)1 CommandList (commandHelp.CommandList)1 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1