List of usage examples for org.apache.commons.cli AlreadySelectedException getOption
public Option getOption()
From source file:edu.usc.scrc.PriorityPruner.CommandLineOptions.java
/** * Parses command line arguments, evaluates them, and stores values if * they're correct.//from w ww .j a v a 2 s. co m * * @param options * collection of Option-objects * @param args * command line arguments * @throws PriorityPrunerException * if error occurs during parsing */ private void parse(Options options, String[] args) throws PriorityPrunerException { try { CommandLineParser cmdLineParser = new GnuParser(); CommandLine commandLine = cmdLineParser.parse(options, args); setParsedOptions(commandLine.getOptions()); // gets set to true if -tfile option is entered through the command // line //boolean tFlag = false; // counts --tped & --tfam options entered through the command line //int tpedCount = 0; //int tfamCount = 0; // loops through all options to save the ones in effect as a String // for printing String tmp = "Options in effect:"; for (Option opt : commandLine.getOptions()) { tmp += ("\r\n -" + opt.getOpt()); if (opt.getValues() != null) { for (int i = 0; i < opt.getValues().length; i++) { tmp += (" " + opt.getValues()[i]); } } } // saves options in effect for printing this.setOptionsInEffect(tmp + "\r\n"); // parse max_distance if (commandLine.hasOption("max_distance")) { this.maxDistance(getLongArgument("max_distance", commandLine.getOptionValue("max_distance"), 0, Long.MAX_VALUE)); checkInput(1, "max_distance", commandLine); } // parse min_maf if (commandLine.hasOption("min_maf")) { this.setMinMaf(getDoubleArgument("min_maf", commandLine.getOptionValue("min_maf"), 0, 0.5)); checkInput(1, "min_maf", commandLine); } // parse min_hwe // if (commandLine.hasOption("min_hwe")) { // this.setMinHwe(getDoubleArgument("min_hwe", // commandLine.getOptionValue("min_hwe"), 0, 1)); // checkInput(1, "min_hwe", commandLine); // } // parses min_snp_callrate if (commandLine.hasOption("min_snp_callrate")) { this.setMinSnpCallRate(getDoubleArgument("min_snp_callrate", commandLine.getOptionValue("min_snp_callrate"), 0, 1)); checkInput(1, "min_snp_callrate", commandLine); } // parses min_design_score if (commandLine.hasOption("min_design_score")) { this.setMinDesignScore(getDoubleArgument("min_design_score", commandLine.getOptionValue("min_design_score"), 0, Double.MAX_VALUE)); checkInput(1, "min_design_score", commandLine); } // // parses option that sets the absolute minimum design score // // value, allowed arguments are doubles between 0.0 and // // Double.MAX_VALUE // if (commandLine.hasOption("amds")) { // this.setAbsoluteMinDesignScore(getDoubleArgument( // "absmindesignscore", // commandLine.getOptionValue("amds"), 0, Double.MAX_VALUE)); // checkInput(1, "amds", commandLine); // } // parse metric if (commandLine.hasOption("metric")) { String[] str = commandLine.getOptionValues("metric"); if (str.length % 2 == 1) { throw new PriorityPrunerException( "Only one argument specified for option \"metric\", a column name (text string) and a weight (decimal number) are required."); } else { for (int i = 0; i < str.length; i++) { if (str[i].equals("design_score")) { throw new PriorityPrunerException("Invalid metric: \"" + str[i] + "\". To filter on design score, simply add correct column in the SNP input file, no metric is needed."); } addMetric(str[i], this.getDoubleArgument("metric", str[i + 1], 0, Double.MAX_VALUE)); i++; } } } // parse st if (commandLine.hasOption("st")) { String[] str = commandLine.getOptionValues("st"); if (str.length % 2 == 1) { throw new PriorityPrunerException( "Only one argument specified for option: \"st\", a p-value (decimal number) and a number of surrogates (integer) are are required."); } else { for (int i = 0; i < str.length; i++) { addSurrogateThreshold(this.getDoubleArgument("st", str[i], 0, 1), this.getIntegerArgument("st", str[i + 1], 0, Integer.MAX_VALUE)); i++; } } } // parse r2t if (commandLine.hasOption("r2t")) { String[] str = commandLine.getOptionValues("r2t"); if (str.length % 2 == 1) { throw new PriorityPrunerException( "Only one argument specified for option: \"r2t\", a p-value and a threshold (decimal numbers between 0 and 1) are required."); } else { for (int i = 0; i < str.length; i++) { addR2Threshold(this.getDoubleArgument("r2t", str[i], 0, 1), this.getDoubleArgument("r2t", str[i + 1], 0, 1)); i++; } } } // parse r2 if (commandLine.hasOption("r2")) { String value = commandLine.getOptionValue("r2"); this.addR2Threshold(1, this.getDoubleArgument("r2", value, 0, 1)); checkInput(1, "r2", commandLine); } // parse chr if (commandLine.hasOption("chr")) { String value = commandLine.getOptionValue("chr"); // recoding chromosome X-representations to "23" if (value.toUpperCase().equals("X") || value.toUpperCase().equals("CHRX")) { //value = "23"; } // if chromosome Y or mitochondrial DNA is encountered, an // exception is thrown else if (value.toUpperCase().equals("M") || value.toUpperCase().equals("MT") || value.toUpperCase().equals("CHRM") || value.toUpperCase().equals("CHRMT") || value.toUpperCase().equals("Y") || value.toUpperCase().equals("CHRY") || value.toUpperCase().equals("24")) { throw new PriorityPrunerException("Chromosome \"" + value + "\" specified in the command line, is not supported. Please update input files and rerun program."); } this.setChr(value); checkInput(1, "chr", commandLine); } // parse out if (commandLine.hasOption("out")) { String value = commandLine.getOptionValue("out"); if (value.endsWith("/")) { value = new StringBuilder(value).append("prioritypruner").toString(); } this.setOutputPrefix(value); checkInput(1, "out", commandLine); } // // parse forceInclude // if (commandLine.hasOption("force_include")) { // String value = commandLine.getOptionValue("force_include"); // this.setForceIncludeFilePath(value); // checkInput(1, "force_include", commandLine); // } // parse do_not_pick_force_included_snps_first if (commandLine.hasOption("do_not_pick_force_included_snps_first")) { this.setSortByForceIncludeAndPValue(false); } // parse snp_table if (commandLine.hasOption("snp_table")) { String[] str = commandLine.getOptionValues("snp_table"); this.setSnpTablePath(str[0]); // counts number of metrics added to command line int metrics = 0; for (int i = 0; i < getParsedOptions().length; i++) { if (getParsedOptions()[i].getOpt().equals("metric")) { metrics++; } } this.setNumMetrics(metrics); checkInput(1, "snp_table", commandLine); } // parse tped if (commandLine.hasOption("tped")) { String value = commandLine.getOptionValue("tped"); checkInput(1, "tped", commandLine); this.setTped(value); } // parse tfam if (commandLine.hasOption("tfam")) { String value = commandLine.getOptionValue("tfam"); checkInput(1, "tfam", commandLine); this.setTfam(value); } // parse tfile if (commandLine.hasOption("tfile")) { String value = commandLine.getOptionValue("tfile"); checkInput(1, "tfile", commandLine); this.setTped(value + ".tped"); this.setTfam(value + ".tfam"); this.setTfile(value); } // parse use_surrogate_for_non_passing_index_snps // if (commandLine.hasOption("use_surrogate_for_non_passing_index_snps")) { // this.setUseSurrogateForNonPassingIndexSnp(true); // } // parses verbose if (commandLine.hasOption("verbose")) { this.setVerbose(true); } // parse outputLDTable if (commandLine.hasOption("ld")) { this.setOutputLDTable(true); } // parse remove if (commandLine.hasOption("remove")) { String value = commandLine.getOptionValue("remove"); checkInput(1, "remove", commandLine); this.setRemove(value); } // parse keep if (commandLine.hasOption("keep")) { String value = commandLine.getOptionValue("keep"); checkInput(1, "keep", commandLine); this.setKeep(value); } // parse keep_random if (commandLine.hasOption("keep_random")) { String value = commandLine.getOptionValue("keep_random"); this.setKeepPercentage(this.getDoubleArgument("keep_random", value, 0, 1)); checkInput(1, "keep_random", commandLine); } // parse no_surrogates_for_force_included_snps if (commandLine.hasOption("no_surrogates_for_force_included_snps")) { this.setAddSurrogatesForForceIncludedSnps(false); } // parse seed if (commandLine.hasOption("seed")) { String value = commandLine.getOptionValue("seed"); this.seed = new Long(this.getLongArgument("seed", value, Long.MIN_VALUE, Long.MAX_VALUE)); } // check that we have all required arguments checkRequiredArguments(commandLine); // checks if any unrecognized arguments been entered checkLeftArguments(commandLine); // if several options from the same options group been entered, // an AlreadySelectedException gets thrown. A custom message is // generated since we wanted another design than the one provided in // the library gave } catch (AlreadySelectedException e) { String message = ""; for (int i = 0; i < e.getOptionGroup().getNames().toArray().length; i++) { message += "\"" + e.getOptionGroup().getNames().toArray()[i] + "\" "; } throw new PriorityPrunerException( "The options: " + message + " may not both be defined. Type --help for help."); // if an undefined option is entered an UnrecognizedOptionException // gets thrown } catch (UnrecognizedOptionException e) { throw new PriorityPrunerException(e.getOption() + " is not a valid option. Type --help for help."); // if an option that is supposed to have arguments is missing, // a MissingArgumentException gets thrown } catch (MissingArgumentException e) { throw new PriorityPrunerException("Missing argument for option \"" + e.getOption().getOpt() + "\". Expected: " + e.getOption().getArgName() + ". Type --help for help."); // if a required option is missing, a MissingOptionException gets // thrown } catch (MissingOptionException e) { // if any other problem occurs while parsing, a general // ParseException gets thrown } catch (ParseException parseException) { throw new PriorityPrunerException("Invalid command line options. Type --help for help."); } }