Example usage for org.apache.commons.cli CommandLine getArgs

List of usage examples for org.apache.commons.cli CommandLine getArgs

Introduction

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

Prototype

public String[] getArgs() 

Source Link

Document

Retrieve any left-over non-recognized options and arguments

Usage

From source file:de.unisb.cs.st.javaslicer.traceResult.TraceResult.java

public static void main(final String[] args) {
    Options options = createOptions();// w ww . ja va  2 s  .c  o  m
    CommandLineParser parser = new GnuParser();
    CommandLine cmdLine;

    try {
        cmdLine = parser.parse(options, args, true);
    } catch (ParseException e) {
        System.err.println("Error parsing the command line arguments: " + e.getMessage());
        return;
    }

    if (cmdLine.hasOption('h')) {
        printHelp(options, System.out);
        System.exit(0);
    }

    InstanceFilter<InstructionInstance> filter;
    if (cmdLine.hasOption("filter")) {
        if ("labels".equals(cmdLine.getOptionValue("filter"))) {
            filter = InstanceFilter.LabelFilter.instance;
        } else if ("additionals".equals(cmdLine.getOptionValue("filter"))) {
            filter = InstanceFilter.AdditionalLabelFilter.instance;
        } else if ("none".equals(cmdLine.getOptionValue("filter"))) {
            filter = null;
        } else {
            System.err.println("Illegal argument for filter: " + cmdLine.getOptionValue("filter"));
            return;
        }
    } else {
        // default:
        filter = InstanceFilter.AdditionalLabelFilter.instance;
    }

    String[] additionalArgs = cmdLine.getArgs();
    if (additionalArgs.length != 1) {
        System.err.println("Error: No input file given.");
        printHelp(options, System.err);
        System.exit(-1);
    }

    final File traceFile = new File(additionalArgs[0]);
    Long threadToTrace = null;
    if (cmdLine.hasOption('t')) {
        try {
            threadToTrace = Long.parseLong(cmdLine.getOptionValue('t'));
        } catch (final NumberFormatException e) {
            System.err.println("Illegal thread id: " + cmdLine.getOptionValue('t'));
            System.exit(-1);
        }
    }

    System.out.println("Opening and reading trace file...");
    TraceResult tr = null;
    try {
        tr = readFrom(traceFile);
    } catch (final IOException e) {
        System.err.println("Error opening trace file: " + e);
        System.exit(-1);
        return;
    }

    final List<ThreadId> threads = tr.getThreads();
    if (threads.size() == 0) {
        System.err.println("The trace file contains no tracing information.");
        System.exit(-1);
    }

    System.out.println("The trace file contains traces for these threads:");
    ThreadId tracing = null;
    for (final ThreadId t : threads) {
        if (threadToTrace == null) {
            if ("main".equals(t.getThreadName())
                    && (tracing == null || t.getJavaThreadId() < tracing.getJavaThreadId()))
                tracing = t;
        } else if (t.getJavaThreadId() == threadToTrace.longValue()) {
            tracing = t;
        }
        System.out.format("%15d: %s%n", t.getJavaThreadId(), t.getThreadName());
    }
    System.out.println();

    if (tracing == null) {
        System.out.println(threadToTrace == null ? "Couldn't find a main thread."
                : "The thread you selected was not found.");
        System.exit(-1);
        return;
    }

    System.out.println(threadToTrace == null ? "Selected:" : "You selected:");
    System.out.format("%15d: %s%n", tracing.getJavaThreadId(), tracing.getThreadName());

    try {
        if (cmdLine.hasOption("length")) {
            final BackwardTraceIterator<AbstractInstructionInstance> it = tr.getBackwardIterator(tracing,
                    filter, new AbstractInstructionInstanceFactory());
            ProgressMonitor monitor = null;
            if (cmdLine.hasOption("--progress")) {
                monitor = new ConsoleProgressMonitor(System.out, "Computing trace length", true, 100, true,
                        true);
                monitor.start(it);
            }
            try {
                while (it.hasNext())
                    it.next();
            } finally {
                if (monitor != null)
                    monitor.end();
            }

            System.out.format(
                    "%nNumber of instructions: %d  (+ %d additional = %d total instructions)%nReady%n",
                    it.getNumInstructions(), it.getNumFilteredInstructions(),
                    it.getNumInstructions() + it.getNumFilteredInstructions());
        } else {
            System.out.println();
            System.out.println("The backward trace:");
            BackwardTraceIterator<AbstractInstructionInstance> it = tr.getBackwardIterator(tracing, filter,
                    new AbstractInstructionInstanceFactory());
            long nr = 0;
            String format = "%8d (%8d)  %-100s -> %3d %7d %s%n";
            System.out.format("%19s  %-100s    %3s %7s %s%n", "Nr (  intern)", "Location", "Dep", "OccNr",
                    "Instruction");
            while (it.hasNext()) {
                InstructionInstance inst = it.next();
                ReadMethod method = inst.getInstruction().getMethod();
                ReadClass class0 = method.getReadClass();
                System.out.format(format, nr++, inst.getInstanceNr(),
                        class0.getName() + "." + method.getName() + ":" + inst.getInstruction().getLineNumber(),
                        inst.getStackDepth(), inst.getOccurrenceNumber(), inst.toString());
            }

            System.out.format(
                    "%nNumber of instructions: %d  (+ %d additional = %d total instructions)%nReady%n",
                    it.getNumInstructions(), it.getNumFilteredInstructions(),
                    it.getNumInstructions() + it.getNumFilteredInstructions());
        }
    } catch (final TracerException e) {
        System.err.print("Error while tracing: ");
        e.printStackTrace(System.err);
        System.exit(-1);
    }
}

From source file:eu.irreality.age.SwingAetheriaGameLoaderInterface.java

public static void main(String[] args) {

    if (args.length > 0) {
        //parse command line

        Option sdi = new Option("sdi", "use single-document interface");
        Option worldFile = OptionBuilder.withArgName("file").hasArg()
                .withDescription("The world file or URL to play").withLongOpt("worldfile").create("w");
        /*// w w w  .ja  v a2 s . c  o  m
        Option worldUrl = OptionBuilder.withArgName( "url" )
           .hasArg()
           .withDescription(  "The world URL to play" )
           .create( "worldurl" );   */
        Option logFile = OptionBuilder.withArgName("file").hasArg()
                .withDescription("Log file to load the game from (requires a world file)")
                .withLongOpt("logfile").create("l");
        Option stateFile = OptionBuilder.withArgName("file").hasArg()
                .withDescription("State file to load the game from (requires a world file)")
                .withLongOpt("statefile").create("s");
        Option errorLog = OptionBuilder.withArgName("errorlog").hasArg()
                .withDescription("A file to append the error output to").withLongOpt("errorlog").create("e");

        Option saveDir = OptionBuilder.withArgName("pathToDir").hasArg()
                .withDescription("Path to the directory where saves will be stored by default")
                .withLongOpt("savedir").create("d");

        Options options = new Options();

        options.addOption(sdi);
        options.addOption(worldFile);
        //options.addOption( worldUrl );
        options.addOption(logFile);
        options.addOption(stateFile);
        options.addOption(errorLog);
        options.addOption(saveDir);

        CommandLineParser parser = new GnuParser();
        try {
            // parse the command line arguments
            CommandLine line = parser.parse(options, args);

            String desiredWorldFile = null;
            //String desiredWorldUrl = null;
            String desiredLogFile = null;
            String desiredStateFile = null;

            String errorLogFile = null;

            String saveDirPath = null;

            if (line.hasOption("e"))
                errorLogFile = line.getOptionValue("e");

            if (line.hasOption("s"))
                desiredStateFile = line.getOptionValue("s");
            if (line.hasOption("l"))
                desiredLogFile = line.getOptionValue("l");
            if (line.hasOption("w"))
                desiredWorldFile = line.getOptionValue("w");

            if (line.hasOption("d"))
                saveDirPath = line.getOptionValue("sd");

            //first, redirect std. error if necessary
            if (errorLogFile != null)
                redirectStandardError(errorLogFile);

            //set save dir if requested
            if (saveDirPath != null) {
                Paths.setSaveDir(saveDirPath);
            }

            //if ( line.hasOption("worldurl") ) desiredWorldFile = line.getOptionValue("worldurl");
            if (desiredWorldFile == null /*&& desiredWorldUrl == null*/ && line.getArgs().length > 0)
                desiredWorldFile = line.getArgs()[0];
            boolean desiredSdi = line.hasOption("sdi"); //Boolean.valueOf( line.getOptionValue("sdi") ).booleanValue();       

            if (SwingAetheriaGUI.getInstance() != null && !desiredSdi) {
                //abrir un fichero en una instancia de AGE ya abierta
                System.out.println("Opening file in existing instance...");
                createLocalGameFromFile(desiredWorldFile, true, desiredLogFile != null, desiredLogFile,
                        desiredStateFile);
                return;
            } else {

                System.out.println("Working directory: " + Paths.getWorkingDirectory());
                setLookAndFeel();

                if (!desiredSdi) {
                    new SwingAetheriaGUI();
                    if (desiredWorldFile != null)
                        createLocalGameFromFile(desiredWorldFile, !desiredSdi, desiredLogFile != null,
                                desiredLogFile, desiredStateFile);
                } else {
                    if (desiredWorldFile != null)
                        createLocalGameFromFile(desiredWorldFile, !desiredSdi, desiredLogFile != null,
                                desiredLogFile, desiredStateFile);
                    else
                        SwingSDIInterface.main(args); //args does nothing in this case, really
                }
            }

        } catch (ParseException exp) {
            // oops, something went wrong
            System.err.println("Parsing failed.  Reason: " + exp.getMessage());
        }

    } else {
        setLookAndFeel();
        new SwingAetheriaGUI();
    }

    /*
    if ( args.length > 0 && SwingAetheriaGUI.getInstance() != null )
    {
       //abrir un fichero en una instancia de AGE ya abierta
       System.out.println("Opening file in existing instance...");
       createLocalGameFromFile(args[0]);
       return;
    }
    */

    /*
    if ( args.length > 0 )
    {
       System.out.println("Opening file in newly created instance...");
       createLocalGameFromFile(args[0]);
    }
    */

}

From source file:com.ricston.akka.matrix.Main.java

public static void main(String[] args) throws InterruptedException, NumberFormatException, IOException {

    CommandLineParser parser = new GnuParser();
    int numberOfActors = -1;

    Options options = new Options();
    // Set the command line options recognized by this program.
    setUpCLOptions(options);/* w  w  w .ja va  2s  . c  o m*/
    // Parse the command line arguments.
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        quitApp(options);
    }

    if (cmd.hasOption(ACTORS)) {
        // Set the number of actors to create if the user supplied this argument.
        numberOfActors = Integer.parseInt(cmd.getOptionValue(ACTORS));
    }
    if (cmd.hasOption(GENERATE)) {
        // Generate a file containing two randomly generated matrices.
        String[] genArgs = cmd.getOptionValues(GENERATE);
        if (genArgs.length != 5) {
            quitApp(options);
        }
        MatrixFile.writeMatrixFile(genArgs[0],
                generateMatrix(Integer.parseInt(genArgs[1]), Integer.parseInt(genArgs[2])),
                generateMatrix(Integer.parseInt(genArgs[3]), Integer.parseInt(genArgs[4])),
                new ArrayList<List<Double>>());

    }
    if (cmd.hasOption(COMPUTE)) {
        // Compute the matrix multiplication of the matrices in the files
        // given as argument.
        String[] compArgs = cmd.getOptionValues(COMPUTE);
        ActorRef managerRef = null;

        if (numberOfActors > 0) {
            final int actors = numberOfActors;
            managerRef = actorOf(new UntypedActorFactory() {
                public UntypedActor create() {
                    return new ManagerActor(actors);
                }
            });
        } else {
            managerRef = actorOf(ManagerActor.class);
        }
        // Start the manager actor.
        managerRef.start();
        // Create and send an AllJobsMsg.
        managerRef.tell(new AllJobsMsg(compArgs));
    }
    if (cmd.getArgs().length > 0 || args.length == 0) {
        // Handle unrecognized arguments.
        quitApp(options);
    }

}

From source file:com.cloudera.recordbreaker.schemadict.SchemaDictionary.java

public static void main(String argv[]) throws IOException {
    boolean shouldDump = false;
    boolean shouldAdd = false;
    File avroDataFile = null;//from w ww.  j  av a 2  s  .c  o  m
    String dictMessage = null;

    CommandLine cmd = null;
    Options options = new Options();
    options.addOption("?", false, "Help for command-line");
    options.addOption("d", false, "Dump contents of schema dictionary");
    options.addOption("a", true, "Add datafile to new schema dictionary element");
    options.addOption("m", true, "Add comment message as part of new schema dictionary element");

    try {
        CommandLineParser parser = new PosixParser();
        cmd = parser.parse(options, argv);
    } catch (ParseException e) {
        HelpFormatter fmt = new HelpFormatter();
        fmt.printHelp("SchemaDictionary", options, true);
        System.err.println("Required input: <schemadictionary>");
        System.exit(-1);
    }

    if (cmd.hasOption("?")) {
        HelpFormatter fmt = new HelpFormatter();
        fmt.printHelp("SchemaDictionary", options, true);
        System.err.println("Required input: <schemadictionary>");
        System.exit(0);
    }

    if (cmd.hasOption("d")) {
        shouldDump = true;
    }

    if (cmd.hasOption("a")) {
        avroDataFile = new File(cmd.getOptionValue("a")).getCanonicalFile();
    }
    if (cmd.hasOption("m")) {
        dictMessage = cmd.getOptionValue("m");
        if (cmd.hasOption("a")) {
            shouldAdd = true;
        }
    }
    if ((!shouldAdd) && (cmd.hasOption("a") || cmd.hasOption("m"))) {
        System.err.println("Must indicate -a AND -m to add new schema dictionary item");
        HelpFormatter fmt = new HelpFormatter();
        fmt.printHelp("SchemaDictionary", options, true);
        System.err.println("Required input: <schemadictionary>");
        System.exit(0);
    }

    String[] argArray = cmd.getArgs();
    if (argArray.length == 0) {
        System.err.println("No schema dictionary path provided.");
        HelpFormatter fmt = new HelpFormatter();
        fmt.printHelp("SchemaDictionary", options, true);
        System.err.println("Required input: <schemadictionary>");
        System.exit(0);
    }

    File dictionaryDir = new File(argArray[0]).getCanonicalFile();
    SchemaDictionary dict = new SchemaDictionary(dictionaryDir);

    if (shouldAdd) {
        dict.addDictionaryElt(avroDataFile, dictMessage);
    }

    if (shouldDump) {
        int counter = 1;
        for (SchemaDictionaryEntry entry : dict.contents()) {
            System.err.println("" + counter + ".  " + entry.getInfo());
            System.err.println(entry.getSchema());
            System.err.println();
            counter++;
        }
        int numItems = counter - 1;
        System.err.println(
                "Dictionary at " + dictionaryDir.getCanonicalPath() + " has " + numItems + " item(s).");
    }
}

From source file:edu.msu.cme.rdp.abundstats.cli.AbundMain.java

public static void main(String[] args) throws IOException {
    File inputFile;//from w w  w  .  j a v a 2 s  .c  o  m
    File resultDir = new File(".");
    RPlotter plotter = null;
    boolean isClusterFile = true;
    List<AbundStatsCalculator> statCalcs = new ArrayList();
    double clustCutoffFrom = Double.MIN_VALUE, clustCutoffTo = Double.MAX_VALUE;

    String usage = "Main [options] <cluster file>";
    try {
        CommandLine line = new PosixParser().parse(options, args);

        if (line.hasOption("result-dir")) {
            resultDir = new File(line.getOptionValue("result-dir"));
            if (!resultDir.exists() && !resultDir.mkdirs()) {
                throw new Exception(
                        "Result directory " + resultDir + " does not exist and could not be created");
            }
        }

        if (line.hasOption("R-location")) {
            plotter = new RPlotter();
            plotter.setCommandTemplate(rplotterTemplate);
            plotter.setRPath(line.getOptionValue("R-location"));
            plotter.setOutFileExt(".png");

            if (!new File(plotter.getRPath()).canExecute()) {
                throw new Exception(plotter.getRPath() + " does not exist or is not exectuable");
            }
        }

        if (line.hasOption("lower-cutoff")) {
            clustCutoffFrom = Double.valueOf(line.getOptionValue("lower-cutoff"));
        }

        if (line.hasOption("upper-cutoff")) {
            clustCutoffTo = Double.valueOf(line.getOptionValue("upper-cutoff"));
        }

        if (line.hasOption("jaccard")) {
            statCalcs.add(new Jaccard(true));
        }

        if (line.hasOption("sorensen")) {
            statCalcs.add(new Sorensen(true));
        }

        if (line.hasOption("otu-table")) {
            isClusterFile = false;
        }

        if (statCalcs.isEmpty()) {
            throw new Exception("Must specify at least one stat to compute (jaccard, sorensen)");
        }

        args = line.getArgs();
        if (args.length != 1) {
            throw new Exception("Unexpected number of command line arguments");
        }

        inputFile = new File(args[0]);

    } catch (Exception e) {
        new HelpFormatter().printHelp(usage, options);
        System.err.println("Error: " + e.getMessage());
        return;
    }

    if (isClusterFile) {
        RDPClustParser parser;
        parser = new RDPClustParser(inputFile);

        try {
            if (parser.getClusterSamples().size() == 1) {
                throw new IOException("Cluster file must have more than one sample");
            }

            List<Cutoff> cutoffs = parser.getCutoffs(clustCutoffFrom, clustCutoffTo);
            if (cutoffs.isEmpty()) {
                throw new IOException(
                        "No cutoffs in cluster file in range [" + clustCutoffFrom + "-" + clustCutoffTo + "]");
            }

            for (Cutoff cutoff : cutoffs) {
                List<Sample> samples = new ArrayList();

                for (ClusterSample clustSample : parser.getClusterSamples()) {
                    Sample s = new Sample(clustSample.getName());
                    for (Cluster clust : cutoff.getClusters().get(clustSample.getName())) {
                        s.addSpecies(clust.getNumberOfSeqs());
                    }
                    samples.add(s);
                }

                processSamples(samples, statCalcs, resultDir, cutoff.getCutoff() + "_", plotter);
            }

        } finally {
            parser.close();
        }
    } else {
        List<Sample> samples = new ArrayList();
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        String line = reader.readLine();

        if (line == null || line.split("\\s+").length < 2) {
            throw new IOException("Must be 2 or more samples for abundance statistic calculations!");
        }
        int numSamples = line.split("\\s+").length;

        boolean header = true;
        try {
            Integer.valueOf(line.split("\\s+")[0]);
            header = false;
        } catch (Exception e) {
        }

        if (header) {
            for (String s : line.split("\\s+")) {
                samples.add(new Sample(s));
            }
        } else {
            int sample = 0;
            for (String s : line.split("\\s+")) {
                samples.add(new Sample("" + sample));
                samples.get(sample).addSpecies(Integer.valueOf(s));
                sample++;
            }
        }

        int lineno = 2;
        while ((line = reader.readLine()) != null) {
            if (line.trim().equals("")) {
                continue;
            }
            int sample = 0;
            if (line.split("\\s+").length != numSamples) {
                System.err.println(
                        "Line number " + lineno + " didn't have the expected number of samples (contained "
                                + line.split("\\s+").length + ", expected " + numSamples + ")");
            }

            for (String s : line.split("\\s+")) {
                samples.get(sample).addSpecies(Integer.valueOf(s));
                sample++;
            }

            lineno++;
        }

        processSamples(samples, statCalcs, resultDir, inputFile.getName(), plotter);
    }
}

From source file:edu.msu.cme.rdp.probematch.cli.SliceToPrimer.java

public static void main(String[] args) throws Exception {
    //args = "--fedit-dist 4 --redit-dist=4 -k --max-length=400 --min-length=280 -o java_sliced_edit4.fasta TGCGAYCCSAARGCBGACTC ATSGCCATCATYTCRCCGGA /scratch/fishjord/tae_kwon_primer_match/all_genomes.fasta".split(" ");
    PatternBitMask64[] fprimers;//from   w ww.j av  a2s. c  om
    String[] fprimerStrs, rprimerStrs;
    PatternBitMask64[] rprimers;

    FastaWriter seqOut;
    PrintStream statsOut;

    int fEdit = 3;
    int rEdit = 3;
    int minLength = Integer.MIN_VALUE;
    int maxLength = Integer.MAX_VALUE;
    boolean allowAmbiguities = true;
    boolean keepPrimers = false;

    SequenceReader inSeqs;

    try {

        CommandLine line = new PosixParser().parse(options, args);

        if (line.hasOption("edit-dist")) {
            fEdit = rEdit = Integer.parseInt(line.getOptionValue("edit-dist"));

            if (line.hasOption("redit-dist") || line.hasOption("fedit-dist")) {
                throw new Exception("edit-dist, [fedit-dist, redit-dist] are mutually exclusive");
            }
        }

        if (line.hasOption("fedit-dist")) {
            fEdit = Integer.parseInt(line.getOptionValue("fedit-dist"));
        }

        if (line.hasOption("no-ambiguities")) {
            allowAmbiguities = false;
        }

        if (line.hasOption("keep-primers")) {
            keepPrimers = true;
        }

        if (line.hasOption("redit-dist")) {
            rEdit = Integer.parseInt(line.getOptionValue("redit-dist"));
        }

        if (line.hasOption("seq-out")) {
            seqOut = new FastaWriter(new File(line.getOptionValue("seq-out")));
        } else {
            throw new Exception("Must specify seq-out");
        }

        if (line.hasOption("stats-out")) {
            statsOut = new PrintStream(new File(line.getOptionValue("stats-out")));
        } else {
            statsOut = System.out;
        }

        if (line.hasOption("min-length")) {
            minLength = Integer.parseInt(line.getOptionValue("min-length"));
        }

        if (line.hasOption("max-length")) {
            maxLength = Integer.parseInt(line.getOptionValue("max-length"));
        }

        args = line.getArgs();

        if (args.length != 3) {
            throw new Exception("Unexpected number of command line arguments");
        }

        fprimers = translateStringPrimers(args[0].split(","), allowAmbiguities, false);
        fprimerStrs = args[0].split(",");
        rprimers = translateStringPrimers(args[1].split(","), allowAmbiguities, true);
        rprimerStrs = args[1].split(",");
        inSeqs = new SequenceReader(new File(args[2]));

    } catch (Exception e) {
        new HelpFormatter().printHelp("SliceToPrimer [options] <f,p,r,i,m,e,r> <r,p,r,i,m,e,r> <in_seq_file>",
                options);
        System.err.println("ERROR: " + e.getMessage());
        return;
    }

    Sequence seq;

    statsOut.println(
            "orig_seqid\tsliced_seqid\tfprimer\tstart\tend\tscore\trprimer\tstart\tend\tscore\tlength");

    ScoringMatrix sccoringMatrix = ScoringMatrix.getDefaultNuclMatrix();

    DPMAligner[] faligners = new DPMAligner[fprimers.length];
    for (int index = 0; index < faligners.length; index++) {
        faligners[index] = new DPMAligner(fprimerStrs[index], Integer.MAX_VALUE);
    }

    try {
        while ((seq = inSeqs.readNextSequence()) != null) {
            Set<PrimerMatch> fprimerMatches = new HashSet();
            Set<PrimerMatch> rprimerMatches = new HashSet();

            for (int index = 0; index < fprimers.length; index++) {
                PatternBitMask64 primer = fprimers[index];

                for (BitVector64Match r : BitVector64.process(seq.getSeqString().toCharArray(), primer, fEdit)
                        .getResults()) {
                    PrimerMatch match = new PrimerMatch();
                    match.start = r.getPosition() - (primer.getPatternLength() + r.getScore());
                    match.end = r.getPosition();
                    match.score = r.getScore();
                    match.primerIndex = index;
                    fprimerMatches.add(match);
                }
            }

            for (int index = 0; index < rprimers.length; index++) {
                PatternBitMask64 primer = rprimers[index];

                for (BitVector64Match r : BitVector64.process(seq.getSeqString().toCharArray(), primer, rEdit)
                        .getResults()) {
                    PrimerMatch match = new PrimerMatch();
                    match.start = r.getPosition() - (primer.getPatternLength() + r.getScore());
                    match.end = r.getPosition();
                    match.score = r.getScore();
                    match.primerIndex = index;
                    rprimerMatches.add(match);
                }
            }

            if (fprimerMatches.isEmpty() || rprimerMatches.isEmpty()) {
                statsOut.println(seq.getSeqName() + "\tEither/or no forward/reverse primer hits");
                continue;
            }
            for (PrimerMatch fmatch : fprimerMatches) {
                PrimerMatch bestReverse = null;
                int bestScore = Integer.MAX_VALUE;
                for (PrimerMatch rmatch : rprimerMatches) {
                    if (rmatch.start > fmatch.end && rmatch.start - fmatch.end < bestScore) {
                        bestReverse = rmatch;
                        bestScore = rmatch.start - fmatch.end;
                    }
                }

                if (bestReverse == null) {
                    statsOut.println(seq.getSeqName() + "\tNo reverse primer before " + fmatch.end);
                    continue;
                }

                String slicedSeq = null;
                if (keepPrimers) {
                    slicedSeq = seq.getSeqString().substring(fmatch.start, bestReverse.end);
                } else {
                    slicedSeq = seq.getSeqString().substring(fmatch.end, bestReverse.start);
                }

                String seqid = seq.getSeqName() + "_" + fmatch.primerIndex + "_" + fmatch.start;
                if (slicedSeq.length() > minLength && slicedSeq.length() < maxLength) {
                    seqOut.writeSeq(seqid, "", slicedSeq);
                }

                DPMAlignment seqs = faligners[fmatch.primerIndex]
                        .align(seq.getSeqString().substring(fmatch.start, fmatch.end));
                System.err.println(">" + seqid);
                System.err.println(fprimerStrs[fmatch.primerIndex]);
                System.err.println(seq.getSeqString().substring(fmatch.start, fmatch.end));
                System.err.println();
                System.err.println(seqs.getAlignedMatchFragment());
                System.err.println(seqs.getAlignedProbe());

                System.err.println();

                statsOut.println(seq.getSeqName() + "\t" + seqid + "\t" + fmatch.primerIndex + "\t"
                        + fmatch.start + "\t" + fmatch.end + "\t" + fmatch.score + "\t"
                        + bestReverse.primerIndex + "\t" + bestReverse.start + "\t" + bestReverse.end + "\t"
                        + bestReverse.score + "\t" + slicedSeq.length());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        statsOut.close();
        seqOut.close();
    }
}

From source file:ComputeNode.java

public static void main(String[] argv) {

    String fileservername = "localhost";
    String id = null;//from w  w w . jav  a 2 s. com
    Double underLoad = null;
    Double overLoad = null;
    Double failProb = null;
    Double constantload = null;
    Pair<Double, Double> gaussian = null;
    String fileName = null;

    ArgumentHandler cli = new ArgumentHandler(
            "FileServer [-h] [collector address] [-u underload] "
                    + "[-o overload] [-c constant_load|-g mean variance] " + "[-p fail_prob] [-f configfile]",
            "Bala Subrahmanyam Kambala, Daniel William DaCosta - "
                    + "GPLv3 (http://www.gnu.org/copyleft/gpl.html)",
            "");
    cli.addOption("h", "help", false, "Print this usage information.");
    cli.addOption("u", "underLoad", true, "Under load threshold");
    cli.addOption("o", "overLoad", true, "Over load threshold");
    cli.addOption("c", "constant", true, "Generate constant load");
    cli.addOption("p", "probability", true, "Fail Probability(0-100)");
    cli.addOption("f", "configfile", true,
            "The configuration file to read parameters from. " + "The default is " + defaultconf + ". "
                    + "Command line arguments will override config file " + "arguments.");
    cli.addOption(OptionBuilder.withLongOpt("gaussian").hasArgs(2)
            .withDescription("Generate a gaussian probability model for load "
                    + "simulation. The first parameter is the mean "
                    + "and the second parameter is the variance.")
            .create('g'));

    // parse command line
    CommandLine commandLine = cli.parse(argv);
    if (commandLine.hasOption('h')) {
        cli.usage("");
        System.exit(0);
    }

    if (commandLine.hasOption('u')) {
        // TODO : Ensure the number is within range
        underLoad = Double.parseDouble(commandLine.getOptionValue('u'));
    }

    if (commandLine.hasOption('o')) {
        // TODO : Ensure the number is within range
        overLoad = Double.parseDouble(commandLine.getOptionValue('o'));
    }

    if (commandLine.hasOption('p')) {
        // TODO : Ensure the number is within range
        failProb = Double.parseDouble(commandLine.getOptionValue('p'));
    }

    if (commandLine.hasOption('c')) {
        // TODO : Ensure the number is within range
        constantload = Double.parseDouble(commandLine.getOptionValue('c'));
    }

    if (commandLine.hasOption('g')) {
        // TODO : Ensure the number is within range
        gaussian = new Pair<Double, Double>(Double.parseDouble(commandLine.getOptionValues('g')[0]),
                Double.parseDouble(commandLine.getOptionValues('g')[1]));
    }

    // TODO: If these flags are no longer mutually exclusive this
    // code should be adjusted to account for whatever constraint are
    // needed.
    if ((constantload != null) && (gaussian != null)) {
        cli.usage("-g -c switches are mutually exclusive!\n");
        System.exit(1);
    }

    if (commandLine.hasOption('f')) {
        fileName = commandLine.getOptionValue('f');
    }

    if (commandLine.getArgs().length != 0)
        fileservername = commandLine.getArgs()[0];
    System.out.println(argv);
    try {
        ComputeNode node = new ComputeNode(fileservername, underLoad, overLoad, failProb, constantload,
                gaussian, fileName);

        Naming.rebind("ComputeNode" + Integer.toString(node.getID()), node);

        // Scheduling heart beat message handler
        Timer t = new Timer();
        HeartBeatHandler h = node.new HeartBeatHandler();
        t.schedule(h, 0, 1 * 1000);

    } catch (ConnectException ce) {
        //lg.log(Level.SEVERE, "Server is not alive");
        ce.printStackTrace();
    } catch (Exception e) {
        //lg.log(Level.SEVERE, "Exception in file server");
        e.printStackTrace();
    }
}

From source file:HLA.java

public static void main(String[] args) throws IOException {

    if (!isVersionOrHigher()) {
        System.err.println("JRE of 1.8+ is required to run Kourami. Exiting.");
        System.exit(1);//from   w ww.  j a  v  a  2s  . c om
    }

    CommandLineParser parser = new DefaultParser();
    Options options = HLA.createOption();
    Options helponlyOpts = HLA.createHelpOption();
    String[] bams = null;
    CommandLine line = null;
    boolean exitRun = false;
    try {
        CommandLine helpcheck = new DefaultParser().parse(helponlyOpts, args, true);
        if (helpcheck.getOptions().length > 0)
            HLA.help(options);
        else {
            line = parser.parse(options, args);
            if (line.hasOption("h"))//help"))
                HLA.help(options);
            else {
                if (line.hasOption("a"))
                    HLA.TYPEADDITIONAL = true;

                HLA.OUTPREFIX = line.getOptionValue("o");//outfilePrefix");
                String tmploc = line.getOptionValue("d");//msaDirectory");
                HLA.MSAFILELOC = tmploc;
                if (tmploc.endsWith(File.separator))
                    HLA.MSAFILELOC = tmploc.substring(0, tmploc.length() - 1);
                if (!new File(HLA.MSAFILELOC).exists() || !new File(HLA.MSAFILELOC).isDirectory()) {
                    System.err.println("Given msaDirectory: " + HLA.MSAFILELOC
                            + "\t does NOT exist or is NOT a directory.");
                    exitRun = true;
                } else if (!new File(HLA.MSAFILELOC + File.separator + "hla_nom_g.txt").exists()) {
                    System.err.println("hla_nom_g.txt NOT FOUND in " + HLA.MSAFILELOC);
                    System.err
                            .println("Please download hla_nom_g.txt from the same IMGT Release as msa files.");
                    exitRun = true;
                }
            }
            bams = line.getArgs();

            if (bams.length < 1 || (bams.length == 1 && bams[bams.length - 1].equals("DEBUG1228")))
                throw new ParseException("At least 1 bam file is required. See Usage:");
            else {
                if (bams.length > 1 && bams[bams.length - 1].equals("DEBUG1228")) {
                    String[] tmpbams = new String[bams.length - 1];
                    for (int i = 0; i < bams.length - 1; i++)
                        tmpbams[i] = bams[i];
                    bams = tmpbams;
                    HLA.DEBUG = true;
                }

                for (String b : bams)
                    if (!new File(b).exists()) {
                        System.err
                                .println("Input bam : " + b + " DOES NOT exist. Please check the bam exists.");
                        exitRun = true;
                    }
            }

        }
        if (exitRun)
            throw new ParseException("Exitting . . .");
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        //System.err.println("Failed to parse command line args. Check usage.");
        HLA.help(options);
    }

    String[] list = { "A", "B", "C", "DQA1", "DQB1", "DRB1" };

    String[] extList = { "A", "B", "C", "DQA1", "DQB1", "DRB1", "DOA", "DMA", "DMB", "DPA1", "DPB1", "DRA",
            "DRB3", "DRB5", "F", "G", "H", "J", "L" };
    //,"DPA1", "DPB1", "DRA",  "DRB4", "F", "G" , "H", "J" ,"K", "L", "V"};
    //,"DPA1", "DPB1", "DRA", "DRB3", "DRB4", "F", "G" , "H", "J" ,"K", "L", "V"};

    if (HLA.TYPEADDITIONAL)
        list = extList;

    File[] bamfiles = new File[bams.length];

    for (int i = 0; i < bams.length; i++)
        bamfiles[i] = new File(bams[i]);

    //check if <HLA.OUTPREFIX>.result is writable
    //if not exit.
    BufferedWriter resultWriter = null;
    try {
        resultWriter = new BufferedWriter(new FileWriter(HLA.OUTPREFIX + ".result"));
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.err.println("\n\n>>> CANNOT open output file: " + HLA.OUTPREFIX + ".result <<<\n\n");
        HLA.help(options);
    }

    HLA.log = new LogHandler();
    for (int i = 0; i < args.length; i++)
        HLA.log.append(" " + args[i]);
    HLA.log.appendln();

    try {
        System.err.println("----------------REF GRAPH CONSTRUCTION--------------");

        HLA.log.appendln("----------------REF GRAPH CONSTRUCTION--------------");
        HLA hla = new HLA(list, HLA.MSAFILELOC + File.separator + "hla_nom_g.txt");

        //1. bubble counting before loading reads.
        //System.err.println("----------------BUBBLE COUNTING: REF GRAPH--------------");
        //HLA.log.appendln("----------------BUBBLE COUNTING: REF GRAPH--------------");
        //hla.countStems();

        System.err.println("----------------     READ LOADING     --------------");

        HLA.log.appendln("----------------     READ LOADING     --------------");

        hla.loadReads(bamfiles);

        System.err.println("----------------    GRAPH CLEANING    --------------");
        HLA.log.appendln("----------------    GRAPH CLEANING    --------------");

        hla.flattenInsertionNodes(list);
        hla.removeUnused(list);
        hla.removeStems(list);

        /*updating error prob*/
        hla.updateErrorProb();

        hla.log.flush();

        StringBuffer resultBuffer = new StringBuffer();

        HLA.DEBUG3 = HLA.DEBUG;

        hla.countBubblesAndMerge(list, resultBuffer);

        hla.writeResults(resultBuffer, resultWriter);
    } catch (Exception e) {
        e.printStackTrace();
        HLA.log.outToFile();
        System.exit(-1);
    }
    /*printingWeights*/
    //hla.printWeights();
    HLA.log.outToFile();
    HLA.log.appendln("NEW_NODE_ADDED:\t" + HLA.NEW_NODE_ADDED);
    HLA.log.appendln("HOPPPING:\t" + HLA.HOPPING);
    HLA.log.appendln("INSERTION_NODE_ADDED:\t" + HLA.INSERTION_NODE_ADDED);
    HLA.log.appendln("INSERTION_WITH_NO_NEW_NODE:\t" + HLA.INSERTION_WITH_NO_NEW_NODE);
    HLA.log.appendln("INSERTION_COUNTS:\t" + HLA.INSERTION);
}

From source file:com.zimbra.cs.lmtpserver.utils.LmtpInject.java

public static void main(String[] args) {
    CliUtil.toolSetup();//w  w w. j  a  v  a 2 s  .  co  m
    CommandLine cl = parseArgs(args);

    if (cl.hasOption("h")) {
        usage(null);
    }
    boolean quietMode = cl.hasOption("q");
    int threads = 1;
    if (cl.hasOption("t")) {
        threads = Integer.valueOf(cl.getOptionValue("t")).intValue();
    }

    String host = null;
    if (cl.hasOption("a")) {
        host = cl.getOptionValue("a");
    } else {
        host = "localhost";
    }

    int port;
    Protocol proto = null;
    if (cl.hasOption("smtp")) {
        proto = Protocol.SMTP;
        port = 25;
    } else
        port = 7025;
    if (cl.hasOption("p"))
        port = Integer.valueOf(cl.getOptionValue("p")).intValue();

    String[] recipients = cl.getOptionValues("r");
    String sender = cl.getOptionValue("s");
    boolean tracingEnabled = cl.hasOption("T");

    int everyN;
    if (cl.hasOption("N")) {
        everyN = Integer.valueOf(cl.getOptionValue("N")).intValue();
    } else {
        everyN = 100;
    }

    // Process files from the -d option.
    List<File> files = new ArrayList<File>();
    if (cl.hasOption("d")) {
        File dir = new File(cl.getOptionValue("d"));
        if (!dir.isDirectory()) {
            System.err.format("%s is not a directory.\n", dir.getPath());
            System.exit(1);
        }
        File[] fileArray = dir.listFiles();
        if (fileArray == null || fileArray.length == 0) {
            System.err.format("No files found in directory %s.\n", dir.getPath());
        }
        Collections.addAll(files, fileArray);
    }

    // Process files specified as arguments.
    for (String arg : cl.getArgs()) {
        files.add(new File(arg));
    }

    // Validate file content.
    if (!cl.hasOption("noValidation")) {
        Iterator<File> i = files.iterator();
        while (i.hasNext()) {
            InputStream in = null;
            File file = i.next();
            boolean valid = false;
            try {
                in = new FileInputStream(file);
                if (FileUtil.isGzipped(file)) {
                    in = new GZIPInputStream(in);
                }
                in = new BufferedInputStream(in); // Required for RFC 822 check
                if (!EmailUtil.isRfc822Message(in)) {
                    System.err.format("%s does not contain a valid RFC 822 message.\n", file.getPath());
                } else {
                    valid = true;
                }
            } catch (IOException e) {
                System.err.format("Unable to validate %s: %s.\n", file.getPath(), e.toString());
            } finally {
                ByteUtil.closeStream(in);
            }
            if (!valid) {
                i.remove();
            }
        }
    }

    if (files.size() == 0) {
        System.err.println("No files to inject.");
        System.exit(1);
    }

    if (!quietMode) {
        System.out.format(
                "Injecting %d message(s) to %d recipient(s).  Server %s, port %d, using %d thread(s).\n",
                files.size(), recipients.length, host, port, threads);
    }

    int totalFailed = 0;
    int totalSucceeded = 0;
    long startTime = System.currentTimeMillis();
    boolean verbose = cl.hasOption("v");
    boolean skipTLSCertValidation = cl.hasOption("skipTLSCertValidation");

    LmtpInject injector = null;
    try {
        injector = new LmtpInject(threads, sender, recipients, files, host, port, proto, quietMode,
                tracingEnabled, verbose, skipTLSCertValidation);
    } catch (Exception e) {
        mLog.error("Unable to initialize LmtpInject", e);
        System.exit(1);
    }

    injector.setReportEvery(everyN);
    injector.markStartTime();
    try {
        injector.run();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    int succeeded = injector.getSuccessCount();
    int failedThisTime = injector.getFailureCount();
    long elapsedMS = System.currentTimeMillis() - startTime;
    double elapsed = elapsedMS / 1000.0;
    double msPerMsg = 0.0;
    double msgSizeKB = 0.0;
    if (succeeded > 0) {
        msPerMsg = elapsedMS;
        msPerMsg /= succeeded;
        msgSizeKB = injector.mFileSizeTotal / 1024.0;
        msgSizeKB /= succeeded;
    }
    double msgPerSec = ((double) succeeded / (double) elapsedMS) * 1000;
    if (!quietMode) {
        System.out.println();
        System.out.printf(
                "LmtpInject Finished\n" + "submitted=%d failed=%d\n" + "%.2fs, %.2fms/msg, %.2fmsg/s\n"
                        + "average message size = %.2fKB\n",
                succeeded, failedThisTime, elapsed, msPerMsg, msgPerSec, msgSizeKB);
    }

    totalFailed += failedThisTime;
    totalSucceeded += succeeded;

    if (totalFailed != 0)
        System.exit(1);
}

From source file:com.yahoo.labs.yamall.local.Yamall.java

public static void main(String[] args) {
    String[] remainingArgs = null;
    String inputFile = null;/*from  www .  j  av a2 s.c  om*/
    String predsFile = null;
    String saveModelFile = null;
    String initialModelFile = null;
    String lossName = null;
    String parserName = null;
    String linkName = null;
    String invertHashName = null;
    double learningRate = 1;
    String minPredictionString = null;
    String maxPredictionString = null;
    String fmNumberFactorsString = null;
    int bitsHash;
    int numberPasses;
    int holdoutPeriod = 10;

    boolean testOnly = false;
    boolean exponentialProgress;
    double progressInterval;

    options.addOption("h", "help", false, "displays this help");
    options.addOption("t", false, "ignore label information and just test");
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("binary")
            .desc("reports loss as binary classification with -1,1 labels").build());
    options.addOption(
            Option.builder().hasArg(false).required(false).longOpt("solo").desc("uses SOLO optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pcsolo")
            .desc("uses Per Coordinate SOLO optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pistol")
            .desc("uses PiSTOL optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("kt")
            .desc("(EXPERIMENTAL) uses KT optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pckt")
            .desc("(EXPERIMENTAL) uses Per Coordinate KT optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pccocob")
            .desc("(EXPERIMENTAL) uses Per Coordinate COCOB optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("cocob")
            .desc("(EXPERIMENTAL) uses COCOB optimizer").build());
    options.addOption(
            Option.builder().hasArg(false).required(false).longOpt("fm").desc("Factorization Machine").build());
    options.addOption(Option.builder("f").hasArg(true).required(false).desc("final regressor to save")
            .type(String.class).longOpt("final_regressor").build());
    options.addOption(Option.builder("p").hasArg(true).required(false).desc("file to output predictions to")
            .longOpt("predictions").type(String.class).build());
    options.addOption(
            Option.builder("i").hasArg(true).required(false).desc("initial regressor(s) to load into memory")
                    .longOpt("initial_regressor").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc(
            "specify the loss function to be used. Currently available ones are: absolute, squared (default), hinge, logistic")
            .longOpt("loss_function").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc(
            "specify the link function used in the output of the predictions. Currently available ones are: identity (default), logistic")
            .longOpt("link").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("output human-readable final regressor with feature names").longOpt("invert_hash")
            .type(String.class).build());
    options.addOption(
            Option.builder("l").hasArg(true).required(false).desc("set (initial) learning Rate, default = 1.0")
                    .longOpt("learning_rate").type(String.class).build());
    options.addOption(Option.builder("b").hasArg(true).required(false)
            .desc("number of bits in the feature table, default = 18").longOpt("bit_precision")
            .type(String.class).build());
    options.addOption(Option.builder("P").hasArg(true).required(false)
            .desc("progress update frequency, integer: additive; float: multiplicative, default = 2.0")
            .longOpt("progress").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("smallest prediction to output, before the link function, default = -50")
            .longOpt("min_prediction").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("smallest prediction to output, before the link function, default = 50")
            .longOpt("max_prediction").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("ignore namespaces beginning with the characters in <arg>").longOpt("ignore")
            .type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc("number of training passes")
            .longOpt("passes").type(String.class).build());
    options.addOption(
            Option.builder().hasArg(true).required(false).desc("holdout period for test only, default = 10")
                    .longOpt("holdout_period").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("number of factors for Factorization Machines default = 8").longOpt("fmNumberFactors")
            .type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("specify the parser to use. Currently available ones are: vw (default), libsvm, tsv")
            .longOpt("parser").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc("schema file for the TSV input")
            .longOpt("schema").type(String.class).build());

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Unrecognized option");
        help();
    }
    if (cmd.hasOption("h"))
        help();
    if (cmd.hasOption("t"))
        testOnly = true;
    if (cmd.hasOption("binary")) {
        binary = true;
        System.out.println("Reporting binary loss");
    }
    initialModelFile = cmd.getOptionValue("i");
    predsFile = cmd.getOptionValue("p");
    lossName = cmd.getOptionValue("loss_function", "squared");
    linkName = cmd.getOptionValue("link", "identity");
    saveModelFile = cmd.getOptionValue("f");
    learningRate = Double.parseDouble(cmd.getOptionValue("l", "1.0"));
    bitsHash = Integer.parseInt(cmd.getOptionValue("b", "18"));
    invertHashName = cmd.getOptionValue("invert_hash");
    minPredictionString = cmd.getOptionValue("min_prediction", "-50");
    maxPredictionString = cmd.getOptionValue("max_prediction", "50");
    fmNumberFactorsString = cmd.getOptionValue("fmNumberFactors", "8");
    parserName = cmd.getOptionValue("parser", "vw");

    numberPasses = Integer.parseInt(cmd.getOptionValue("passes", "1"));
    System.out.println("Number of passes = " + numberPasses);
    if (numberPasses > 1) {
        holdoutPeriod = Integer.parseInt(cmd.getOptionValue("holdout_period", "10"));
        System.out.println("Holdout period = " + holdoutPeriod);
    }

    remainingArgs = cmd.getArgs();
    if (remainingArgs.length == 1)
        inputFile = remainingArgs[0];

    InstanceParser instanceParser = null;
    if (parserName.equals("vw"))
        instanceParser = new VWParser(bitsHash, cmd.getOptionValue("ignore"), (invertHashName != null));
    else if (parserName.equals("libsvm"))
        instanceParser = new LIBSVMParser(bitsHash, (invertHashName != null));
    else if (parserName.equals("tsv")) {
        String schema = cmd.getOptionValue("schema");
        if (schema == null) {
            System.out.println("TSV parser requires a schema file.");
            System.exit(0);
        } else {
            String spec = null;
            try {
                spec = new String(Files.readAllBytes(Paths.get(schema)));
            } catch (IOException e) {
                System.out.println("Error reading the TSV schema file.");
                e.printStackTrace();
                System.exit(0);
            }
            instanceParser = new TSVParser(bitsHash, cmd.getOptionValue("ignore"), (invertHashName != null),
                    spec);
        }
    } else {
        System.out.println("Unknown parser.");
        System.exit(0);
    }
    System.out.println("Num weight bits = " + bitsHash);

    // setup progress
    String progress = cmd.getOptionValue("P", "2.0");
    if (progress.indexOf('.') >= 0) {
        exponentialProgress = true;
        progressInterval = (double) Double.parseDouble(progress);
    } else {
        exponentialProgress = false;
        progressInterval = (double) Integer.parseInt(progress);
    }

    // min and max predictions
    minPrediction = (double) Double.parseDouble(minPredictionString);
    maxPrediction = (double) Double.parseDouble(maxPredictionString);

    // number of factors for Factorization Machines
    fmNumberFactors = (int) Integer.parseInt(fmNumberFactorsString);

    // configure the learner
    Loss lossFnc = null;
    LinkFunction link = null;
    if (initialModelFile == null) {
        if (cmd.hasOption("kt")) {
            learner = new KT(bitsHash);
        } else if (cmd.hasOption("pckt")) {
            learner = new PerCoordinateKT(bitsHash);
        } else if (cmd.hasOption("pcsolo")) {
            learner = new PerCoordinateSOLO(bitsHash);
        } else if (cmd.hasOption("solo")) {
            learner = new SOLO(bitsHash);
        } else if (cmd.hasOption("pccocob")) {
            learner = new PerCoordinateCOCOB(bitsHash);
        } else if (cmd.hasOption("cocob")) {
            learner = new COCOB(bitsHash);
        } else if (cmd.hasOption("pistol")) {
            learner = new PerCoordinatePiSTOL(bitsHash);
        } else if (cmd.hasOption("fm")) {
            learner = new SGD_FM(bitsHash, fmNumberFactors);
        } else
            learner = new SGD_VW(bitsHash);
    } else {
        learner = IOLearner.loadLearner(initialModelFile);
    }

    // setup link function
    if (linkName.equals("identity")) {
        link = new IdentityLinkFunction();
    } else if (linkName.equals("logistic")) {
        link = new LogisticLinkFunction();
    } else {
        System.out.println("Unknown link function.");
        System.exit(0);
    }

    // setup loss function
    if (lossName.equals("squared")) {
        lossFnc = new SquareLoss();
    } else if (lossName.equals("hinge")) {
        lossFnc = new HingeLoss();
    } else if (lossName.equals("logistic")) {
        lossFnc = new LogisticLoss();
    } else if (lossName.equals("absolute")) {
        lossFnc = new AbsLoss();
    } else {
        System.out.println("Unknown loss function.");
        System.exit(0);
    }

    learner.setLoss(lossFnc);
    learner.setLearningRate(learningRate);

    // maximum range predictions
    System.out.println("Max prediction = " + maxPrediction + ", Min Prediction = " + minPrediction);
    // print information about the learner
    System.out.println(learner.toString());
    // print information about the link function
    System.out.println(link.toString());
    // print information about the parser
    System.out.println(instanceParser.toString());
    // print information about ignored namespaces
    System.out.println("Ignored namespaces = " + cmd.getOptionValue("ignore", ""));

    long start = System.nanoTime();
    FileInputStream fstream;
    try {
        BufferedReader br = null;
        if (inputFile != null) {
            fstream = new FileInputStream(inputFile);
            System.out.println("Reading datafile = " + inputFile);
            br = new BufferedReader(new InputStreamReader(fstream));
        } else {
            System.out.println("Reading from console");
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        File fout = null;
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        if (predsFile != null) {
            fout = new File(predsFile);
            fos = new FileOutputStream(fout);
            bw = new BufferedWriter(new OutputStreamWriter(fos));
        }

        try {
            System.out.println("average       example  current  current  current");
            System.out.println("loss          counter    label  predict  features");
            int iter = 0;
            double cumLoss = 0;
            double weightedSampleSum = 0;
            double sPlus = 0;
            double sMinus = 0;
            Instance sample = null;
            boolean justPrinted = false;
            int pass = 0;
            ObjectOutputStream ooutTr = null;
            ObjectOutputStream ooutHO = null;
            ObjectInputStream oinTr = null;
            double pred = 0;
            int limit = 1;
            double hError = Double.MAX_VALUE;
            double lastHError = Double.MAX_VALUE;
            int numTestSample = 0;
            int numTrainingSample = 0;
            int idx = 0;

            if (numberPasses > 1) {
                ooutTr = new ObjectOutputStream(new FileOutputStream("cache_training.bin"));
                ooutHO = new ObjectOutputStream(new FileOutputStream("cache_holdout.bin"));
                oinTr = new ObjectInputStream(new FileInputStream("cache_training.bin"));
            }

            do {
                while (true) {
                    double score;

                    if (pass > 0 && numberPasses > 1) {
                        Instance tmp = (Instance) oinTr.readObject();
                        if (tmp != null)
                            sample = tmp;
                        else
                            break;
                    } else {
                        String strLine = br.readLine();
                        if (strLine != null)
                            sample = instanceParser.parse(strLine);
                        else
                            break;
                    }

                    justPrinted = false;
                    idx++;

                    if (numberPasses > 1 && pass == 0 && idx % holdoutPeriod == 0) {
                        // store the current sample for the holdout set
                        ooutHO.writeObject(sample);
                        ooutHO.reset();
                        numTestSample++;
                    } else {
                        if (numberPasses > 1 && pass == 0) {
                            ooutTr.writeObject(sample);
                            ooutTr.reset();
                            numTrainingSample++;
                        }

                        iter++;
                        if (testOnly) {
                            // predict the sample
                            score = learner.predict(sample);
                        } else {
                            // predict the sample and update the classifier using the sample
                            score = learner.update(sample);
                        }
                        score = Math.min(Math.max(score, minPrediction), maxPrediction);
                        pred = link.apply(score);
                        if (!binary)
                            cumLoss += learner.getLoss().lossValue(score, sample.getLabel())
                                    * sample.getWeight();
                        else if (Math.signum(score) != sample.getLabel())
                            cumLoss += sample.getWeight();

                        weightedSampleSum += sample.getWeight();
                        if (sample.getLabel() > 0)
                            sPlus = sPlus + sample.getWeight();
                        else
                            sMinus = sMinus + sample.getWeight();

                        // output predictions to file
                        if (predsFile != null) {
                            bw.write(String.format("%.6f %s", pred, sample.getTag()));
                            bw.newLine();
                        }

                        // print statistics to screen
                        if (iter == limit) {
                            justPrinted = true;
                            System.out.printf("%.6f %12d  % .4f  % .4f  %d\n", cumLoss / weightedSampleSum,
                                    iter, sample.getLabel(), pred, sample.getVector().size());
                            if (exponentialProgress)
                                limit *= progressInterval;
                            else
                                limit += progressInterval;
                        }
                    }
                }
                if (numberPasses > 1) {
                    if (pass == 0) { // finished first pass of many
                        // write a null at the end of the files
                        ooutTr.writeObject(null);
                        ooutHO.writeObject(null);
                        ooutTr.flush();
                        ooutHO.flush();
                        ooutTr.close();
                        ooutHO.close();

                        System.out.println("finished first epoch");
                        System.out.println(numTrainingSample + " training samples");
                        System.out.println(numTestSample + " holdout samples saved");
                    }
                    lastHError = hError;
                    hError = evalHoldoutError();
                }
                if (numberPasses > 1) {
                    System.out.printf("Weighted loss on holdout on epoch %d = %.6f\n", pass + 1, hError);

                    oinTr.close();
                    oinTr = new ObjectInputStream(new FileInputStream("cache_training.bin"));

                    if (hError > lastHError) {
                        System.out.println("Early stopping");
                        break;
                    }
                }
                pass++;
            } while (pass < numberPasses);

            if (justPrinted == false) {
                System.out.printf("%.6f %12d  % .4f  % .4f  %d\n", cumLoss / weightedSampleSum, iter,
                        sample.getLabel(), pred, sample.getVector().size());
            }
            System.out.println("finished run");

            System.out.println(String.format("average loss best constant predictor: %.6f",
                    lossFnc.lossConstantBinaryLabels(sPlus, sMinus)));

            if (saveModelFile != null)
                IOLearner.saveLearner(learner, saveModelFile);
            if (invertHashName != null)
                IOLearner.saveInvertHash(learner.getWeights(), instanceParser.getInvertHashMap(),
                        invertHashName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // close the input stream
        try {
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // close the output stream
        if (predsFile != null) {
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        long millis = System.nanoTime() - start;
        System.out.printf("Elapsed time: %d min, %d sec\n", TimeUnit.NANOSECONDS.toMinutes(millis),
                TimeUnit.NANOSECONDS.toSeconds(millis) - 60 * TimeUnit.NANOSECONDS.toMinutes(millis));
    } catch (

    FileNotFoundException e) {
        System.out.println("Error opening the input file");
        e.printStackTrace();
    }

}