Example usage for org.apache.commons.cli DefaultParser DefaultParser

List of usage examples for org.apache.commons.cli DefaultParser DefaultParser

Introduction

In this page you can find the example usage for org.apache.commons.cli DefaultParser DefaultParser.

Prototype

DefaultParser

Source Link

Usage

From source file:com.act.analysis.similarity.SimilarityAnalysis.java

public static void main(String[] args) throws Exception {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*from   w w w .  j a v  a 2  s. c o  m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        return;
    }

    LicenseManager.setLicenseFile(cl.getOptionValue(OPTION_LICENSE_FILE));

    if (cl.hasOption(OPTION_TARGET_INCHI) && cl.hasOption(OPTION_TARGET_FILE)) {
        System.err.format("Specify only one of -%s or -%s\n", OPTION_TARGET_INCHI, OPTION_TARGET_FILE);
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    List<SimilarityOperator> querySimilarityOperators = new ArrayList<>();
    List<String> header = new ArrayList<>();
    header.add("name");
    header.add("id");
    header.add("inchi");

    if (cl.hasOption(OPTION_QUERY_INCHI) && !cl.hasOption(OPTION_QUERY_FILE)) {
        SimilarityOperator so = makeSimilarityOperators("from inchi", cl.getOptionValue(OPTION_QUERY_INCHI));
        so.init();
        querySimilarityOperators.add(so);
        header.addAll(so.getResultFields());
    } else if (cl.hasOption(OPTION_QUERY_FILE) && !cl.hasOption(OPTION_QUERY_INCHI)) {
        TSVParser parser = new TSVParser();
        parser.parse(new File(cl.getOptionValue(OPTION_QUERY_FILE)));
        for (Map<String, String> row : parser.getResults()) {
            System.out.format("Compiling query for %s, %s\n", row.get("name"), row.get("inchi"));
            SimilarityOperator so = makeSimilarityOperators(row.get("name"), row.get("inchi"));
            so.init();
            querySimilarityOperators.add(so);
            header.addAll(so.getResultFields());
        }
    } else {
        System.err.format("Specify exactly one of -%s or -%s\n", OPTION_QUERY_INCHI, OPTION_QUERY_FILE);
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    List<Map<String, String>> targetChemicals = null;
    if (cl.hasOption(OPTION_TARGET_INCHI) && !cl.hasOption(OPTION_TARGET_FILE)) {
        String inchi = cl.getOptionValue(OPTION_TARGET_INCHI);
        targetChemicals = Collections.singletonList(new HashMap<String, String>() {
            {
                put("name", "direct-input");
                put("id", null);
                put("inchi", inchi);
            }
        });
    } else if (cl.hasOption(OPTION_TARGET_FILE) && !cl.hasOption(OPTION_TARGET_INCHI)) {
        TSVParser parser = new TSVParser();
        parser.parse(new File(cl.getOptionValue(OPTION_TARGET_FILE)));
        targetChemicals = parser.getResults();
    } else {
        System.err.format("Specify exactly one of -%s or -%s\n", OPTION_TARGET_INCHI, OPTION_TARGET_FILE);
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    AlignmentMoleculeFactory alignmentMoleculeFactory = new AlignmentMoleculeFactory();

    // TODO: add symmetric computations for target as query and each query as target.
    TSVWriter<String, String> writer = new TSVWriter<>(header);
    writer.open(new File(cl.getOptionValue(OPTION_OUTPUT_FILE)));
    try {
        for (Map<String, String> row : targetChemicals) {
            Molecule targetMol = MolImporter.importMol(row.get("inchi"));
            Cleaner.clean(targetMol, 3); // This will assign 3D atom coordinates to the MolAtoms in targetMol.
            Molecule targetFragment = findLargestFragment(targetMol.convertToFrags());
            AlignmentMolecule am = alignmentMoleculeFactory.create(targetFragment,
                    AlignmentProperties.DegreeOfFreedomType.TRANSLATE_ROTATE_FLEXIBLE);
            Map<String, String> outputRow = new HashMap<>(row);
            System.out.format("Processing target %s\n", row.get("name"));
            for (SimilarityOperator so : querySimilarityOperators) {
                System.out.format("  running query %s\n", so.getName());
                Map<String, Double> results = so.calculateSimilarity(am);
                outputRow.putAll(doubleMapToStringMap(results));
            }
            writer.append(outputRow);
            writer.flush();
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
    System.out.format("Done\n");
}

From source file:be.i8c.sag.documentationgenerator.cli.GeneratorCmd.java

public static void main(String[] args) throws Exception {
    Options CLI_OPTIONS = createOptions();

    if (args.length == 0) {
        logger.debug("Show arguments explanation if no arguments are provided.");
        printOptions(CLI_OPTIONS);/*w w w  . j a va2 s .c  o  m*/
    } else {
        Arguments arguments;
        try {
            logger.debug("Attempt to parse arguments.");
            arguments = new Arguments(new DefaultParser().parse(CLI_OPTIONS, args));
        } catch (ParseException e) {
            throw new ParseException(
                    "Error occurred parsing the arguments that were passed to the client: " + e.getMessage());
        }

        GeneratorFile documentationGenerator = new GeneratorFile(arguments.PACKAGE_DIR, arguments.OUTPUT_DIR,
                arguments.CONFIG_DIR);

        logger.debug("Setting package name regex to: {}.", packageNameRegex);
        documentationGenerator.setPackageNameRegex(arguments.PACKAGE_NAME_REGEX);
        if (arguments.PACKAGE_NAMES != null && arguments.PACKAGE_NAMES.length > 0) {
            documentationGenerator.setPackageNames(arguments.PACKAGE_NAMES);
        }

        logger.debug("Setting folder qualified name regex to: {}.", arguments.FOLDER_QUALIFIER_NAME_REGEX);
        documentationGenerator.setFolderQualifiedNameRegex(arguments.FOLDER_QUALIFIER_NAME_REGEX);

        if (!arguments.GENERATE_HTML && !arguments.GENERATE_MD && !arguments.GENERATE_TXT
                && !arguments.GENERATE_PDF && !arguments.GENERATE_XML && !arguments.INTERMEDIATE_XML) {
            logger.warn(
                    "None of the following arguments have been given. -{}, -{}, -{}, -{}, -{}, -{}. Generating PDF as default",
                    Arguments.Names.GENERATE_HTML, Arguments.Names.GENERATE_MD, Arguments.Names.GENERATE_PDF,
                    Arguments.Names.GENERATE_TXT, Arguments.Names.GENERATE_XML,
                    Arguments.Names.INTERMEDIATE_XML);
            documentationGenerator.addPdfOutput();
        } else {
            if (arguments.GENERATE_HTML) {
                if (arguments.XSLT_DIR != null)
                    documentationGenerator
                            .addHtmlOutput(new File(arguments.XSLT_DIR, FileNames.Input.INTERMEDIATE_HTML));
                else
                    documentationGenerator.addHtmlOutput();
            }

            if (arguments.GENERATE_MD) {
                if (arguments.XSLT_DIR != null)
                    documentationGenerator
                            .addMdOutput(new File(arguments.XSLT_DIR, FileNames.Input.INTERMEDIATE_MD));
                else
                    documentationGenerator.addMdOutput();
            }

            if (arguments.GENERATE_PDF) {
                if (arguments.XSLT_DIR != null)
                    documentationGenerator
                            .addPdfOutput(new File(arguments.XSLT_DIR, FileNames.Input.INTERMEDIATE_XML));
                else
                    documentationGenerator.addPdfOutput();
            }

            if (arguments.GENERATE_TXT) {
                if (arguments.XSLT_DIR != null)
                    documentationGenerator
                            .addTxtOutput(new File(arguments.XSLT_DIR, FileNames.Input.INTERMEDIATE_TEXT));
                else
                    documentationGenerator.addTxtOutput();
            }

            if (arguments.GENERATE_XML) {
                if (arguments.XSLT_DIR != null)
                    documentationGenerator
                            .addXmlOutput(new File(arguments.XSLT_DIR, FileNames.Input.INTERMEDIATE_XML));
                else
                    documentationGenerator.addXmlOutput();
            }

            if (arguments.INTERMEDIATE_XML)
                documentationGenerator.addIntermediateOutput();
        }

        documentationGenerator.generateDocumentation();
    }
}

From source file:act.installer.HMDBParser.java

public static void main(String[] args) throws Exception {
    // Parse the command line options
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*from  www. j av  a  2  s. c o m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(PubchemParser.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(PubchemParser.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        return;
    }

    File inputDir = new File(cl.getOptionValue(OPTION_INPUT_DIRECTORY));
    if (!inputDir.isDirectory()) {
        System.err.format("Input directory at %s is not a directory\n", inputDir.getAbsolutePath());
        System.exit(1);
    }

    String dbName = cl.getOptionValue(OPTION_DB_NAME, DEFAULT_DB_NAME);
    String dbHost = cl.getOptionValue(OPTION_DB_HOST, DEFAULT_DB_HOST);
    Integer dbPort = Integer.valueOf(cl.getOptionValue(OPTION_DB_PORT, DEFAULT_DB_PORT));

    LOGGER.info("Connecting to %s:%d/%s", dbHost, dbPort, dbName);
    MongoDB db = new MongoDB(dbHost, dbPort, dbName);
    HMDBParser parser = Factory.makeParser(db);

    LOGGER.info("Starting parser");
    parser.run(inputDir);
    LOGGER.info("Done");
}

From source file:com.atilika.kuromoji.benchmark.Benchmark.java

public static void main(String[] args) throws IOException {
    Options options = new Options();
    options.addOption("h", "help", false, "Display this help message and exit");
    options.addOption("t", "tokenizer", true, "Tokenizer class to use");
    options.addOption("u", "user-dictionary", true, "Optional user dictionary filename to use");
    options.addOption("c", "count", true, "Number of documents ot process (Default: 0, which means all");
    //        options.addOption("v", "validation-input", true, "Validation filename");
    options.addOption("o", "output", true,
            "Output filename.  If unset, segmentation is done, but the result is discarded");
    options.addOption("n", "n-best", true, "The number of tokenizations to get per input");
    options.addOption(null, "benchmark-output", true, "Benchmark metrics output filename filename");

    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = null;/*w w w. j a v  a  2s .  c om*/
    try {
        commandLine = parser.parse(options, args);

        args = commandLine.getArgs();

        if (args.length != 1) {
            throw new ParseException("A single input filename is required");
        }
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        usage(options);
    }

    String inputFilename = args[0];

    String className = commandLine.getOptionValue("t", "com.atilika.kuromoji.ipadic.Tokenizer");

    className += "$Builder";

    String userDictionaryFilename = commandLine.getOptionValue("u");

    TokenizerBase tokenizer = null;
    try {
        Class clazz = Class.forName(className);

        // Make builder
        Object builder = clazz.getDeclaredConstructor(null).newInstance();

        // Set user dictionary
        if (userDictionaryFilename != null) {
            builder.getClass().getMethod("userDictionary", String.class).invoke(builder,
                    userDictionaryFilename);
        }

        // Build tokenizer
        tokenizer = (TokenizerBase) builder.getClass().getMethod("build").invoke(builder);

    } catch (Exception e) {
        System.err.println("Could not create tokenizer. Got " + e);
        e.printStackTrace();
        System.exit(1);
    }

    File outputFile = null;
    String outputFilename = commandLine.getOptionValue("o");

    if (outputFilename != null) {
        outputFile = new File(outputFilename);
    }

    File statisticsFile = null;
    String statisticsFilename = commandLine.getOptionValue("benchmark-output");

    if (statisticsFilename != null) {
        statisticsFile = new File(statisticsFilename);
    }

    long count = Long.parseLong(commandLine.getOptionValue("c", "0"));

    int nbest = Integer.parseInt(commandLine.getOptionValue("n", "1"));

    Benchmark benchmark = new Builder().tokenizer(tokenizer).inputFile(new File(inputFilename))
            .outputFile(outputFile).outputStatisticsFile(statisticsFile).setOutputStatistiscs(true).count(count)
            .nbest(nbest).build();

    benchmark.benchmark();
}

From source file:com.example.dlp.DeIdentification.java

/**
 * Command line application to de-identify data using the Data Loss Prevention API.
 * Supported data format: strings//from  ww  w . j  av a2s .c om
 */
public static void main(String[] args) throws Exception {

    OptionGroup optionsGroup = new OptionGroup();
    optionsGroup.setRequired(true);

    Option deidentifyMaskingOption = new Option("m", "mask", true, "deid with character masking");
    optionsGroup.addOption(deidentifyMaskingOption);

    Option deidentifyFpeOption = new Option("f", "fpe", true, "deid with FFX FPE");
    optionsGroup.addOption(deidentifyFpeOption);

    Options commandLineOptions = new Options();
    commandLineOptions.addOptionGroup(optionsGroup);

    Option maskingCharacterOption = Option.builder("maskingCharacter").hasArg(true).required(false).build();
    commandLineOptions.addOption(maskingCharacterOption);

    Option numberToMaskOption = Option.builder("numberToMask").hasArg(true).required(false).build();
    commandLineOptions.addOption(numberToMaskOption);

    Option alphabetOption = Option.builder("commonAlphabet").hasArg(true).required(false).build();
    commandLineOptions.addOption(alphabetOption);

    Option wrappedKeyOption = Option.builder("wrappedKey").hasArg(true).required(false).build();
    commandLineOptions.addOption(wrappedKeyOption);

    Option keyNameOption = Option.builder("keyName").hasArg(true).required(false).build();
    commandLineOptions.addOption(keyNameOption);

    CommandLineParser parser = new DefaultParser();
    HelpFormatter formatter = new HelpFormatter();
    CommandLine cmd;

    try {
        cmd = parser.parse(commandLineOptions, args);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp(DeIdentification.class.getName(), commandLineOptions);
        System.exit(1);
        return;
    }

    if (cmd.hasOption("m")) {
        // deidentification with character masking
        int numberToMask = Integer.parseInt(cmd.getOptionValue(numberToMaskOption.getOpt(), "0"));
        char maskingCharacter = cmd.getOptionValue(maskingCharacterOption.getOpt(), "*").charAt(0);
        String val = cmd.getOptionValue(deidentifyMaskingOption.getOpt());
        deIdentifyWithMask(val, maskingCharacter, numberToMask);
    } else if (cmd.hasOption("f")) {
        // deidentification with FPE
        String wrappedKey = cmd.getOptionValue(wrappedKeyOption.getOpt());
        String keyName = cmd.getOptionValue(keyNameOption.getOpt());
        String val = cmd.getOptionValue(deidentifyFpeOption.getOpt());
        FfxCommonNativeAlphabet alphabet = FfxCommonNativeAlphabet.valueOf(
                cmd.getOptionValue(alphabetOption.getOpt(), FfxCommonNativeAlphabet.ALPHA_NUMERIC.name()));
        deIdentifyWithFpe(val, alphabet, keyName, wrappedKey);
    }
}

From source file:ctlogger.CTlogger.java

public static void main(String args[]) {

    /**//from ww  w  .j  a v a2s.c om
     * 
     * Original code for command line parsing
     * (This has been replaced by code using Apache Commons CLI, see below)
     *
    String helpMsg = "CTlogger -x -r -z -g -k <skiplines> -f <flush_sec> -p <poll_sec> -n <nanVal> -i <leadingID> -s <SourceName> -H <HeaderLine> <logger.dat> <CTfolder>";
      int dirArg = 0;
      while((dirArg<args.length) && args[dirArg].startsWith("-")) {      // arg parsing
         if(args[dirArg].equals("-h"))    { 
     System.err.println(helpMsg);
     System.exit(0);
         }
         if(args[dirArg].equals("-x"))    { debug = true;   }
         if(args[dirArg].equals("-b"))    { noBackwards = true;   }
         if(args[dirArg].equals("-g"))    { gzipmode = true;   }         // default false
         if(args[dirArg].equals("-a"))    { appendMode = false;   }      // default true
         if(args[dirArg].equals("-z"))     { zipmode = false; }          // default true
         if(args[dirArg].equals("-N"))    { newFileMode = true;   }      // default false
         if(args[dirArg].equals("-f"))   { autoflush = Long.parseLong(args[++dirArg]); }
         if(args[dirArg].equals("-p"))   { pollInterval = Long.parseLong(args[++dirArg]); }
         if(args[dirArg].equals("-k"))   { skipLines = Long.parseLong(args[++dirArg]); }
         if(args[dirArg].equals("-r"))   { repeatFetch = true; }   
         if(args[dirArg].equals("-B"))   { blockMode = true; }   
         if(args[dirArg].equals("-t"))   { storeTime = true; }   
         if(args[dirArg].equals("-T"))   { trimTime = Double.parseDouble(args[++dirArg]); }
         if(args[dirArg].equals("-n"))   { nanVal = args[++dirArg]; }
         if(args[dirArg].equals("-i"))   { leadingID = args[++dirArg]; }
         if(args[dirArg].equals("-s"))   { SourceName = args[++dirArg]; }
         if(args[dirArg].equals("-H"))   { HeaderLine = args[++dirArg]; }
         dirArg++;
      }
    if(args.length < (dirArg+2)) {
       System.err.println(helpMsg);
       System.exit(0);
    }
    loggerFileName = args[dirArg++];      // args[0]:  logger.dat file
    CTrootfolder = args[dirArg++];         // args[1]:  CT destination folder        
    */

    //
    // Parse command line arguments
    //
    // 1. Setup command line options
    //
    Options options = new Options();
    // Boolean options (only the flag, no argument)
    options.addOption("h", "help", false, "Print this message");
    options.addOption("x", "debug", false, "turn on debug output");
    options.addOption("b", "nobackwards", false, "no backwards-going time allowed");
    options.addOption("g", "gzipmode", false, "turn on gzip for extra compression");
    options.addOption("a", "noappend", false,
            "turn off append mode (i.e., do not append to end of existing CT data)");
    options.addOption("z", "nozip", false, "turn off zip mode (it is on by default)");
    options.addOption("N", "newfilemode", false, "re-parse entire logger file every time it is checked");
    options.addOption("r", "repeatFetch", false, "turn on repeat fetch (auto-fetch data loop)");
    options.addOption("B", "blockMode", false,
            "turn on CloudTurbine writer block mode (multiple points per output data file, packed data)");
    options.addOption("t", "storeTime", false,
            "store time string as a channel; time is the first data entry in each line; if this option is not specified, then the time channel is skipped/not saved to CloudTurbine");
    // Options with an argument
    Option outputFolderOption = Option.builder("f").argName("autoflush").hasArg()
            .desc("flush interval (sec); default = \"" + autoflush + "\"").build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("p").argName("pollInterval").hasArg().desc(
            "if repeatFetch option has been specified, recheck the logger data file at this polling interval (sec); default = \""
                    + pollInterval + "\"")
            .build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("k").argName("skipLines").hasArg().desc(
            "in logger file, the num lines to skip after the header line to get to the first line of data; default = \""
                    + skipLines + "\"")
            .build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("T").argName("trimTime").hasArg().desc(
            "trim (ring-buffer loop) time (sec) (trimTime=0 for indefinite); default = \"" + trimTime + "\"")
            .build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("n").argName("nanVal").hasArg()
            .desc("replace NAN with this; default = \"" + nanVal + "\"").build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("i").argName("leadingID").hasArg()
            .desc("leading ID string (IWG1 compliant)").build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("s").argName("sourceName").hasArg()
            .desc("CloudTurbine source name; default = \"" + SourceName + "\"").build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("H").argName("HeaderLine").hasArg().desc(
            "optional CSV list of channel names; if not supplied, this is read from the first line in the logger file")
            .build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("l").argName("loggerfilename").hasArg()
            .desc("name of the logger data file; required argument").build();
    options.addOption(outputFolderOption);
    outputFolderOption = Option.builder("o").longOpt("outputfolder").argName("folder").hasArg()
            .desc("Location of output files (source is created under this folder); default = " + CTrootfolder)
            .build();
    options.addOption(outputFolderOption);

    //
    // 2. Parse command line options
    //
    CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, args);
    } catch (org.apache.commons.cli.ParseException exp) {
        // oops, something went wrong
        System.err.println("Command line argument parsing failed: " + exp.getMessage());
        return;
    }

    //
    // 3. Retrieve the command line values
    //
    if (line.hasOption("help")) {
        // Display help message and quit
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("CTlogger", options);
        return;
    }
    debug = line.hasOption("x");
    noBackwards = line.hasOption("b");
    gzipmode = line.hasOption("g");
    appendMode = !line.hasOption("a");
    zipmode = !line.hasOption("z");
    newFileMode = line.hasOption("N");
    repeatFetch = line.hasOption("r");
    blockMode = line.hasOption("B");
    storeTime = line.hasOption("t");
    autoflush = Long.parseLong(line.getOptionValue("f", Long.toString(autoflush)));
    pollInterval = Long.parseLong(line.getOptionValue("p", Long.toString(pollInterval)));
    skipLines = Long.parseLong(line.getOptionValue("k", Long.toString(skipLines)));
    trimTime = Double.parseDouble(line.getOptionValue("T", Double.toString(trimTime)));
    nanVal = line.getOptionValue("n", nanVal);
    if (line.hasOption("i")) {
        leadingID = line.getOptionValue("i");
    }
    SourceName = line.getOptionValue("s", SourceName);
    if (line.hasOption("H")) {
        HeaderLine = line.getOptionValue("H");
    }
    if (line.hasOption("l")) {
        loggerFileName = line.getOptionValue("l");
    } else {
        System.err.println("ERROR: you must supply the logger file name.");
        return;
    }
    CTrootfolder = line.getOptionValue("o", CTrootfolder);

    if (!debug) {
        System.err.println("CTlogger: " + loggerFileName + ", CTrootfolder: " + CTrootfolder
                + ", pollInterval: " + pollInterval);
    } else {
        System.err.println("debug = " + debug);
        System.err.println("noBackwards = " + noBackwards);
        System.err.println("gzipmode = " + gzipmode);
        System.err.println("appendMode = " + appendMode);
        System.err.println("zipmode = " + zipmode);
        System.err.println("newFileMode = " + newFileMode);
        System.err.println("repeatFetch = " + repeatFetch);
        System.err.println("blockMode = " + blockMode);
        System.err.println("storeTime = " + storeTime);
        System.err.println("autoflush = " + autoflush);
        System.err.println("pollInterval = " + pollInterval);
        System.err.println("skipLines = " + skipLines);
        System.err.println("trimTime = " + trimTime);
        System.err.println("nanVal = " + nanVal);
        System.err.println("leadingID = " + leadingID);
        System.err.println("SourceName = " + SourceName);
        System.err.println("HeaderLine = " + HeaderLine);
        System.err.println("loggerFileName = " + loggerFileName);
        System.err.println("CTrootfolder = " + CTrootfolder);
    }

    //
    // Run CTlogger
    //
    if (!repeatFetch)
        getData(true); // run once
    else {
        Timer timer = new Timer();
        TimerTask fetchTask = new TimerTask() {
            @Override
            public void run() {
                if (newFileMode)
                    getData(true);
                else if (getData(false)) { // pick up from old data if you can
                    System.err.println("Failed to pick up from old data, refetch from start of file...");
                    boolean status = getData(true);
                    System.err.println("refetch status: " + status);
                }
                if (debug)
                    System.err.println("Waiting for data, pollInterval: " + pollInterval + " sec...");
            };
        };
        // repeatFetch@autoflush interval, convert to msec
        if ((autoflush > 0) && (pollInterval > autoflush))
            pollInterval = autoflush;
        timer.scheduleAtFixedRate(fetchTask, 0, pollInterval * 1000);
    }
}

From source file:act.installer.pubchem.PubchemMeshSynonyms.java

public static void main(final String[] args) {

    // Parse the command line options
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());//ww  w.jav a 2 s  .  c o  m
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(PubchemMeshSynonyms.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(PubchemMeshSynonyms.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        return;
    }

    String serviceHostIp = cl.getOptionValue(OPTION_SERVICE_HOST, DEFAULT_SERVICE_HOST);
    Integer servicePort = Integer.parseInt(cl.getOptionValue(OPTION_SERVICE_PORT, DEFAULT_SERVICE_PORT));
    String queryInchi = cl.getOptionValue(OPTION_QUERY_INCHI);

    PubchemMeshSynonyms pubchemMeshSynonyms = new PubchemMeshSynonyms(serviceHostIp, servicePort);
    String cid = pubchemMeshSynonyms.fetchCIDFromInchi(queryInchi);
    if (cid != null) {
        Map<PubchemSynonymType, Set<String>> pubchemSynonyms = pubchemMeshSynonyms
                .fetchPubchemSynonymsFromCID(cid);
        LOGGER.info("Resulting Pubchem synonyms for %s are: %s", queryInchi, pubchemSynonyms);
        Map<MeshTermType, Set<String>> meshTerms = pubchemMeshSynonyms.fetchMeshTermsFromCID(cid);
        LOGGER.info("Resulting MeSH term s for %s are: %s", queryInchi, meshTerms);
    } else {
        LOGGER.info("No PubChem compound ID was found for the input InChI.");
    }
}

From source file:com.act.lcms.db.analysis.IonSearchAnalysis.java

public static void main(String[] args) throws Exception {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());// w w  w  . ja v a 2  s .  c o  m
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        return;
    }

    File lcmsDir = new File(cl.getOptionValue(OPTION_DIRECTORY));
    if (!lcmsDir.isDirectory()) {
        System.err.format("File at %s is not a directory\n", lcmsDir.getAbsolutePath());
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    Double fontScale = null;
    if (cl.hasOption("font-scale")) {
        try {
            fontScale = Double.parseDouble(cl.getOptionValue("font-scale"));
        } catch (IllegalArgumentException e) {
            System.err.format("Argument for font-scale must be a floating point number.\n");
            System.exit(1);
        }
    }

    try (DB db = DB.openDBFromCLI(cl)) {
        Set<String> includeIons = null;
        if (cl.hasOption("include-ions")) {
            String[] ionNames = cl.getOptionValues("include-ions");
            includeIons = new HashSet<>(Arrays.asList(ionNames));
            System.out.format("Including ions in search: %s\n", StringUtils.join(includeIons, ", "));
        }
        Set<String> excludeIons = null;
        if (cl.hasOption("exclude-ions")) {
            String[] ionNames = cl.getOptionValues("exclude-ions");
            excludeIons = new HashSet<>(Arrays.asList(ionNames));
            System.out.format("Excluding ions from search: %s\n", StringUtils.join(excludeIons, ", "));
        }

        Set<Integer> takeSamplesFromPlateIds = null;
        if (cl.hasOption(OPTION_FILTER_BY_PLATE_BARCODE)) {
            String[] plateBarcodes = cl.getOptionValues(OPTION_FILTER_BY_PLATE_BARCODE);
            System.out.format("Considering only sample wells in plates: %s\n",
                    StringUtils.join(plateBarcodes, ", "));
            takeSamplesFromPlateIds = new HashSet<>(plateBarcodes.length);
            for (String plateBarcode : plateBarcodes) {
                Plate p = Plate.getPlateByBarcode(db, plateBarcode);
                if (p == null) {
                    System.err.format("WARNING: unable to find plate in DB with barcode %s\n", plateBarcode);
                } else {
                    takeSamplesFromPlateIds.add(p.getId());
                }
            }
            // Allow filtering on barcode even if we couldn't find any in the DB.
        }

        System.out.format("Loading/updating LCMS scan files into DB\n");
        ScanFile.insertOrUpdateScanFilesInDirectory(db, lcmsDir);

        System.out.format("Processing LCMS scans\n");
        Pair<List<LCMSWell>, Set<Integer>> positiveWellsAndPlateIds = Utils.extractWellsAndPlateIds(db,
                cl.getOptionValues(OPTION_STRAINS), cl.getOptionValues(OPTION_CONSTRUCTS),
                takeSamplesFromPlateIds, false);
        List<LCMSWell> positiveWells = positiveWellsAndPlateIds.getLeft();
        if (positiveWells.size() == 0) {
            throw new RuntimeException("Found no LCMS wells for specified strains/constructs");
        }
        // Only take negative samples from the plates where we found the positive samples.
        Pair<List<LCMSWell>, Set<Integer>> negativeWellsAndPlateIds = Utils.extractWellsAndPlateIds(db,
                cl.getOptionValues(OPTION_NEGATIVE_STRAINS), cl.getOptionValues(OPTION_NEGATIVE_CONSTRUCTS),
                positiveWellsAndPlateIds.getRight(), true);
        List<LCMSWell> negativeWells = negativeWellsAndPlateIds.getLeft();
        if (negativeWells == null || negativeWells.size() == 0) {
            System.err.format("WARNING: no valid negative samples found in same plates as positive samples\n");
        }

        // Extract the reference MZ that will be used in the LCMS trace processing.
        List<Pair<String, Double>> searchMZs = null;
        Set<CuratedChemical> standardChemicals = null;
        List<ChemicalAssociatedWithPathway> pathwayChems = null;
        if (cl.hasOption(OPTION_SEARCH_MZ)) {
            // Assume mz can be an FP number of a chemical name.
            String massStr = cl.getOptionValue(OPTION_SEARCH_MZ);
            Pair<String, Double> searchMZ = Utils.extractMassFromString(db, massStr);
            if (searchMZ != null) {
                searchMZs = Collections.singletonList(searchMZ);
            }
            standardChemicals = Utils.extractTargetsForWells(db, positiveWells);
        } else {
            CuratedChemical targetChemical = Utils.requireOneTarget(db, positiveWells);
            if (targetChemical == null) {
                throw new RuntimeException(
                        "Unable to find a curated chemical entry for specified strains'/constructs' targets.  "
                                + "Please specify a chemical name or m/z explicitly or update the curated chemicals list in the DB.");
            }
            System.out.format("Using reference M/Z for positive target %s (%f)\n", targetChemical.getName(),
                    targetChemical.getMass());
            searchMZs = Collections.singletonList(Pair.of(targetChemical.getName(), targetChemical.getMass()));
            standardChemicals = Collections.singleton(targetChemical);
        }

        // Look up the standard by name, or use the target if none is specified.
        List<StandardWell> standardWells = null;
        if (cl.hasOption(OPTION_NO_STANDARD)) {
            System.err.format("WARNING: skipping standard comparison (no-standard option specified)\n");
            standardWells = new ArrayList<>(0);
        } else if (cl.hasOption(OPTION_STANDARD_WELLS)) {
            String[] standardCoordinates = cl.getOptionValues(OPTION_STANDARD_WELLS);
            standardWells = new ArrayList<>(standardCoordinates.length);
            Plate standardPlate = Plate.getPlateByBarcode(db, cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE));
            List<String> foundCoordinates = new ArrayList<>(standardCoordinates.length);
            for (String stringCoords : standardCoordinates) {
                Pair<Integer, Integer> coords = Utils.parsePlateCoordinates(stringCoords);
                StandardWell well = StandardWell.getInstance().getStandardWellsByPlateIdAndCoordinates(db,
                        standardPlate.getId(), coords.getLeft(), coords.getRight());
                if (well == null) {
                    System.err.format("Unable to find standard well at %s [%s]\n", standardPlate.getBarcode(),
                            stringCoords);
                    continue;
                }
                standardWells.add(well);
                foundCoordinates.add(stringCoords);
            }
            System.out.format("Using explicitly specified standard wells %s [%s]\n", standardPlate.getBarcode(),
                    StringUtils.join(foundCoordinates, ", "));
        } else if (cl.hasOption(OPTION_STANDARD_NAME)) {
            String standardName = cl.getOptionValue(OPTION_STANDARD_NAME);
            System.out.format("Using explicitly specified standard %s\n", standardName);
            standardWells = Collections.singletonList(Utils.extractStandardWellFromPlate(db,
                    cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE), standardName));
        } else if (standardChemicals != null && standardChemicals.size() > 0) {
            // Default to using the target chemical(s) as a standard if none is specified.
            standardWells = new ArrayList<>(standardChemicals.size());
            for (CuratedChemical c : standardChemicals) {
                String standardName = c.getName();
                System.out.format("Searching for well containing standard %s\n", standardName);
                standardWells.add(Utils.extractStandardWellFromPlate(db,
                        cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE), standardName));
            }
        }

        boolean useFineGrainedMZ = cl.hasOption("fine-grained-mz");
        boolean useSNR = cl.hasOption(OPTION_USE_SNR);

        /* Process the standard, positive, and negative wells, producing ScanData containers that will allow them to be
         * iterated over for graph writing. */
        HashMap<Integer, Plate> plateCache = new HashMap<>();
        Pair<List<ScanData<StandardWell>>, Double> allStandardScans = AnalysisHelper.processScans(db, lcmsDir,
                searchMZs, ScanData.KIND.STANDARD, plateCache, standardWells, useFineGrainedMZ, includeIons,
                excludeIons, useSNR);
        Pair<List<ScanData<LCMSWell>>, Double> allPositiveScans = AnalysisHelper.processScans(db, lcmsDir,
                searchMZs, ScanData.KIND.POS_SAMPLE, plateCache, positiveWells, useFineGrainedMZ, includeIons,
                excludeIons, useSNR);
        Pair<List<ScanData<LCMSWell>>, Double> allNegativeScans = AnalysisHelper.processScans(db, lcmsDir,
                searchMZs, ScanData.KIND.NEG_CONTROL, plateCache, negativeWells, useFineGrainedMZ, includeIons,
                excludeIons, useSNR);

        String fmt = "pdf";
        String outImg = cl.getOptionValue(OPTION_OUTPUT_PREFIX) + "." + fmt;
        String outData = cl.getOptionValue(OPTION_OUTPUT_PREFIX) + ".data";
        System.err.format("Writing combined scan data to %s and graphs to %s\n", outData, outImg);

        produceLCMSSearchPlots(lcmsDir, outData, outImg, allStandardScans, allPositiveScans, allNegativeScans,
                fontScale, useFineGrainedMZ, cl.hasOption(OPTION_USE_HEATMAP), useSNR);
    }
}

From source file:com.twitter.heron.metricscachemgr.MetricsCacheManager.java

public static void main(String[] args) throws Exception {
    Options options = constructOptions();
    Options helpOptions = constructHelpOptions();

    CommandLineParser parser = new DefaultParser();

    // parse the help options first.
    CommandLine cmd = parser.parse(helpOptions, args, true);
    if (cmd.hasOption("h")) {
        usage(options);/*  w  w  w . ja v  a  2s .co m*/
        return;
    }

    try {
        // Now parse the required options
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        usage(options);
        throw new RuntimeException("Error parsing command line options: ", e);
    }

    Level logLevel = Level.INFO;
    if (cmd.hasOption("v")) {
        logLevel = Level.ALL;
    }

    String cluster = cmd.getOptionValue("cluster");
    String role = cmd.getOptionValue("role");
    String environ = cmd.getOptionValue("environment");
    int masterPort = Integer.valueOf(cmd.getOptionValue("master_port"));
    int statsPort = Integer.valueOf(cmd.getOptionValue("stats_port"));
    String systemConfigFilename = cmd.getOptionValue("system_config_file");
    String overrideConfigFilename = cmd.getOptionValue("override_config_file");
    String metricsSinksConfigFilename = cmd.getOptionValue("sink_config_file");
    String topologyName = cmd.getOptionValue("topology_name");
    String topologyId = cmd.getOptionValue("topology_id");
    String metricsCacheMgrId = cmd.getOptionValue("metricscache_id");

    // read heron internal config file
    SystemConfig systemConfig = SystemConfig.newBuilder(true).putAll(systemConfigFilename, true)
            .putAll(overrideConfigFilename, true).build();

    // Log to file and sink(exception)
    LoggingHelper.loggerInit(logLevel, true);
    LoggingHelper.addLoggingHandler(
            LoggingHelper.getFileHandler(metricsCacheMgrId, systemConfig.getHeronLoggingDirectory(), true,
                    systemConfig.getHeronLoggingMaximumSize(), systemConfig.getHeronLoggingMaximumFiles()));
    LoggingHelper.addLoggingHandler(new ErrorReportLoggingHandler());

    LOG.info(String.format(
            "Starting MetricsCache for topology %s with topologyId %s with "
                    + "MetricsCache Id %s, master port: %d.",
            topologyName, topologyId, metricsCacheMgrId, masterPort));

    LOG.info("System Config: " + systemConfig);

    // read sink config file
    MetricsSinksConfig sinksConfig = new MetricsSinksConfig(metricsSinksConfigFilename);
    LOG.info("Sinks Config: " + sinksConfig.toString());

    // build config from cli
    Config config = Config.toClusterMode(Config.newBuilder().putAll(ConfigLoader.loadClusterConfig())
            .putAll(Config.newBuilder().put(Key.CLUSTER, cluster).put(Key.ROLE, role).put(Key.ENVIRON, environ)
                    .build())
            .putAll(Config.newBuilder().put(Key.TOPOLOGY_NAME, topologyName).put(Key.TOPOLOGY_ID, topologyId)
                    .build())
            .build());
    LOG.info("Cli Config: " + config.toString());

    // build metricsCache location
    TopologyMaster.MetricsCacheLocation metricsCacheLocation = TopologyMaster.MetricsCacheLocation.newBuilder()
            .setTopologyName(topologyName).setTopologyId(topologyId)
            .setHost(InetAddress.getLocalHost().getHostName()).setControllerPort(-1) // not used for metricscache
            .setMasterPort(masterPort).setStatsPort(statsPort).build();

    MetricsCacheManager metricsCacheManager = new MetricsCacheManager(topologyName, METRICS_CACHE_HOST,
            masterPort, statsPort, systemConfig, sinksConfig, config, metricsCacheLocation);
    metricsCacheManager.start();

    LOG.info("Loops terminated. MetricsCache Manager exits.");
}

From source file:com.act.biointerpretation.mechanisminspection.ReactionRenderer.java

public static void main(String[] args) throws IOException {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*from  w ww. ja  va2 s  . c  o m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(ReactionRenderer.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(ReactionRenderer.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        return;
    }

    Integer height = Integer.parseInt(cl.getOptionValue(OPTION_HEIGHT, "1000"));
    Integer width = Integer.parseInt(cl.getOptionValue(OPTION_WIDTH, "1000"));
    Boolean representCofactors = cl.hasOption(OPTION_COFACTOR)
            && Boolean.parseBoolean(cl.getOptionValue(OPTION_COFACTOR));

    NoSQLAPI api = new NoSQLAPI(cl.getOptionValue(OPTION_READ_DB), cl.getOptionValue(OPTION_READ_DB));

    for (String val : cl.getOptionValues(OPTION_RXN_IDS)) {
        Long reactionId = Long.parseLong(val);
        ReactionRenderer renderer = new ReactionRenderer(cl.getOptionValue(OPTION_FILE_FORMAT), width, height);
        renderer.drawReaction(api.getReadDB(), reactionId, cl.getOptionValue(OPTION_DIR_PATH),
                representCofactors);
        LOGGER.info(renderer.getSmartsForReaction(api.getReadDB(), reactionId, representCofactors));
    }
}