Search in sources :

Example 1 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class TrackSetTest method canPrintFeaturesToFile.

@Test
public void canPrintFeaturesToFile() throws InvalidGenomicCoordsException, IOException, ClassNotFoundException, BamIndexNotFoundException, InvalidRecordException, SQLException, InvalidCommandLineException, InvalidColourException, ArgumentParserException {
    // --------------------------------------------------------------------
    // Prepare coords and trackSet
    GenomicCoords gc = new GenomicCoords("chr7:5565052-5571960", 80, null, null);
    TrackSet trackSet = new TrackSet();
    trackSet.addTrackFromSource("test_data/hg19_genes.gtf.gz", gc, null);
    trackSet.addTrackFromSource("test_data/hg19_genes.gtf.gz", gc, null);
    // --------------------------------------------------------------------
    // No redirection file: Nothing done
    List<String> cmdInput = Utils.tokenize("print #1 >", " ");
    boolean failed = false;
    try {
        trackSet.setPrintModeAndPrintFeaturesForRegex(cmdInput);
    } catch (InvalidCommandLineException e) {
        failed = true;
    }
    assertTrue(failed);
    // Invalid output: Non existent dir:
    cmdInput = Utils.tokenize("print #1 > test_data/foobar/delete.me", " ");
    failed = false;
    try {
        trackSet.setPrintModeAndPrintFeaturesForRegex(cmdInput);
    } catch (IOException e) {
        failed = true;
    }
    assertTrue(failed);
    // Now give an output file.
    File ff = new File("deleteme.gtf");
    ff.deleteOnExit();
    cmdInput = Utils.tokenize("print #1 > deleteme.gtf", " ");
    trackSet.setPrintModeAndPrintFeaturesForRegex(cmdInput);
    assertTrue(ff.exists());
    assertTrue(ff.length() > 200);
    assertEquals(13, FileUtils.readLines(ff).size());
    // Append to file
    cmdInput = Utils.tokenize("print #1 >> deleteme.gtf", " ");
    trackSet.setPrintModeAndPrintFeaturesForRegex(cmdInput);
    assertEquals(26, FileUtils.readLines(ff).size());
}
Also used : GenomicCoords(samTextViewer.GenomicCoords) InvalidCommandLineException(exceptions.InvalidCommandLineException) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 2 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class TrackSetTest method canReplaceOverloadedFunctionInAwk.

@Test
public void canReplaceOverloadedFunctionInAwk() throws Exception {
    TrackSet ts = new TrackSet();
    GenomicCoords gc = new GenomicCoords("chr1:1-100", 80, null, null);
    Track t1 = new TrackIntervalFeature("test_data/ALL.wgs.mergedSV.v8.20130502.svs.genotypes.vcf", gc);
    ts.addTrack(t1, "vcf");
    Track t2 = new TrackPileup("test_data/ds051.actb.bam", gc);
    ts.addTrack(t2, "bam");
    Track t3 = new TrackIntervalFeature("test_data/refSeq.bed", gc);
    ts.addTrack(t3, "bed");
    ts.setAwkForTrack(Utils.tokenize("awk 'get(AC) > 2 ||get(GT, 2, 1) && get(INFO/FOO) && get(FMT/BAR)' vcf", " "));
    assertTrue(ts.getTrack(t1).getAwk().endsWith("'"));
    assertTrue(ts.getTrack(t1).getAwk().contains("getInfoTag(\"AC\")"));
    assertTrue(ts.getTrack(t1).getAwk().contains("getFmtTag(\"GT\", 2, 1)"));
    assertTrue(ts.getTrack(t1).getAwk().contains("getInfoTag(\"INFO/FOO\")"));
    assertTrue(ts.getTrack(t1).getAwk().contains("getFmtTag(\"FMT/BAR\")"));
    boolean pass = false;
    try {
        // BAR not qualified by INFO/ or FMT/
        ts.setAwkForTrack(Utils.tokenize("awk 'get(BAR)' vcf", " "));
    } catch (InvalidCommandLineException e) {
        pass = true;
    }
    assertTrue(pass);
    ts.setAwkForTrack(Utils.tokenize("awk 'get( NM ) > 2' bam", " "));
    assertTrue(ts.getTrack(t2).getAwk().contains("getSamTag(\"NM\")"));
    pass = false;
    try {
        // Can't use get() on BED
        ts.setAwkForTrack(Utils.tokenize("awk 'get(NM)' bed", " "));
    } catch (InvalidCommandLineException e) {
        pass = true;
    }
    assertTrue(pass);
    // Nothing to replace
    String awk = "awk 'foo_get() get (bar)' vcf";
    ts.setAwkForTrack(Utils.tokenize(awk, " "));
    assertTrue(ts.getTrack(t1).getAwk().contains("foo_get() get (bar)"));
}
Also used : GenomicCoords(samTextViewer.GenomicCoords) InvalidCommandLineException(exceptions.InvalidCommandLineException) Test(org.junit.Test)

Example 3 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class InteractiveInput method setGenome.

private void setGenome(List<String> cmdTokens, TrackProcessor proc) throws InvalidGenomicCoordsException, IOException, InvalidCommandLineException {
    List<String> tokens = new ArrayList<String>(cmdTokens);
    tokens.remove(0);
    if (tokens.size() == 0) {
        // Try to read fasta from history file
        ASCIIGenomeHistory ag = new ASCIIGenomeHistory();
        try {
            tokens.add(ag.getReference().get(0));
            System.err.println("Using " + ag.getReference().get(0));
        } catch (Exception e) {
            System.err.println("A previous reference file was not found.");
            throw new InvalidCommandLineException();
        }
    }
    GenomicCoords testSeqDict = new GenomicCoords("default", Utils.getTerminalWidth(), null, null);
    testSeqDict.setGenome(tokens, true);
    if (testSeqDict.getSamSeqDict() != null) {
        proc.getGenomicCoordsHistory().setGenome(tokens);
    } else {
        System.err.println(Utils.padEndMultiLine("Cannot set genome from " + tokens, Utils.getTerminalWidth()));
        this.interactiveInputExitCode = ExitCode.ERROR;
    }
}
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 4 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class InteractiveInput method setConfigOpt.

private void setConfigOpt(List<String> cmdTokens) throws IOException, InvalidConfigException, InvalidCommandLineException, InvalidColourException {
    List<String> args = new ArrayList<String>(cmdTokens);
    args.remove(0);
    if (args.size() == 0) {
        throw new InvalidCommandLineException();
    }
    if (args.size() == 1) {
        ConfigKey key = ConfigKey.getConfigKeyFromShort(args.get(0));
        if (ConfigKey.booleanKeys().contains(key)) {
            // If configkey is a type boolean, just flip the boolean
            key = ConfigKey.valueOf(key.toString());
            boolean value = !Utils.asBoolean(Config.get(key));
            Config.set(key, String.valueOf(value));
        } else {
            // configKey is expected to be the name of a configuration file
            new Config(args.get(0));
        }
    } else {
        ConfigKey key = ConfigKey.getConfigKeyFromShort(args.get(0));
        String value = args.get(1);
        try {
            Config.set(key, value);
        } catch (Exception e) {
            throw new InvalidConfigException();
        }
    }
}
Also used : ConfigKey(coloring.ConfigKey) Config(coloring.Config) ArrayList(java.util.ArrayList) InvalidCommandLineException(exceptions.InvalidCommandLineException) InvalidConfigException(exceptions.InvalidConfigException) 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 5 with InvalidCommandLineException

use of exceptions.InvalidCommandLineException in project ASCIIGenome by dariober.

the class InteractiveInput method show.

/**
 *Edit visualization setting in TrackProcessor as appropriate.
 * @return
 * @throws InvalidCommandLineException
 * @throws IOException
 * @throws InvalidGenomicCoordsException
 */
private ExitCode show(List<String> cmdTokens, TrackProcessor proc) throws InvalidCommandLineException, InvalidGenomicCoordsException, IOException {
    List<String> args = new ArrayList<String>(cmdTokens);
    args.remove(0);
    if (args.size() == 0) {
        System.err.println("At least one argument is required.");
        throw new InvalidCommandLineException();
    }
    // recognize "genome".
    if ("genome".startsWith(args.get(0))) {
        this.showGenome(proc);
        return ExitCode.CLEAN_NO_FLUSH;
    } else if ("trackInfo".startsWith(args.get(0))) {
        String info = Utils.padEndMultiLine(proc.getTrackSet().showTrackInfo(), proc.getWindowSize());
        System.err.println(info);
        return ExitCode.CLEAN_NO_FLUSH;
    } else if ("gruler".startsWith(args.get(0))) {
        proc.setShowGruler(!proc.isShowGruler());
        return ExitCode.CLEAN;
    } else if ("pctRuler".startsWith(args.get(0))) {
        proc.setShowCruler(!proc.isShowCruler());
        return ExitCode.CLEAN;
    } else {
        System.err.println("Unrecognized option: " + args.get(0));
        throw new InvalidCommandLineException();
    }
}
Also used : ArrayList(java.util.ArrayList) InvalidCommandLineException(exceptions.InvalidCommandLineException)

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