<commit_before>package org.ugent.caagt.genestacker.cli;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.ugent.caagt.genestacker.Haplotype;
import org.ugent.caagt.genestacker.Plant;
import org.ugent.caagt.genestacker.exceptions.GenestackerException;
import org.ugent.caagt.genestacker.io.*;
import org.ugent.caagt.genestacker.search.*;
import org.ugent.caagt.genestacker.search.bb.*;
import org.ugent.caagt.genestacker.search.bb.heuristics.*;
import org.ugent.caagt.genestacker.search.constraints.*;
import org.ugent.caagt.genestacker.util.GenestackerConstants;
import org.ugent.caagt.genestacker.util.TimeFormatting;

/**
 * Command line interface for Gene Stacker software.
 */
public class Main
{
    
    // logger
    private Logger logger = LogManager.getLogger(Main.class);
    
    // options to check before parsing other options
    private Options checkFirstOptions;
    // required parameters
    private Options requiredOptions;
    // constraints
    private Options constraintOptions;
    // heuristic options
    private Options heuristicPresetsOptions;
    private Options individualHeuristicOptions;
    // verbosity options
    private Options verbosityOptions;
    // general options
    private Options miscOptions;
    // groups all options
    private Options allOptions;
    
    // parameter values
    private double successProb;
    private String outputFile;
    private String inputFile;
    // constraints
    private List<Constraint> constraints;
    private NumberOfSeedsPerCrossing numSeeds;
    private boolean homozygousIdeotypeParents;
    // heuristics
    private boolean h0 = false;     // initial plant filtering
    private boolean h1a = false;    // weak improvement heuristic
    private boolean h1b = false;    // strong improvement heuristic
    private boolean h2a = false;    // seed lot filtering (w.r.t. weak improvement)
    private boolean h2b = false;    // seed lot filtering (w.r.t. strong improvement)
    private boolean h3 = false;     // Pareto optimal subschemes
    private boolean h3s1 = false;   // H3 seeded (version 1)
    private boolean h3s2 = false;   // H3 seeded (version 2 -- extra seed lot filtering in second run)
    private boolean h4 = false;     // Pareto optimal seed lots
    private boolean h5 = false;     // heuristic seed lot construction
    private boolean h5c = false;    // heuristic seed lot construction with consistency check
    private boolean h6 = false;     // heuristic population size bound
    // option for heuristic seed lot constructor
    private int maxNumCrossovers = GenestackerConstants.UNLIMITED_CROSSOVERS;
    // misc options
    private GraphFileFormat graphFileFormat;
    private boolean useKosambiMap;
    private boolean tree;
    private long runtimeLimit = GenestackerConstants.NO_RUNTIME_LIMIT;
    private boolean minimizePopSizeOnly;
    private int numThreads;
    private boolean writeIntermediateOutput;
    
    // total runtime (ms)
    private long totalRuntime;
    
    /**
     * Run Gene Stacker software from command line.
     * 
     * @param args
     */
    public static void main( String[] args ){
        Main cli = new Main();
        cli.run(args);
    }
    
    private void run(String args[]){
        // setup options
        setupOptions();
        // parse special options only
        try {
            // create command line parser that ignores unknown options
            CommandLineParser parser = new ExtendedPosixParser(true);
            // parse special options
            CommandLine cmd = parser.parse(checkFirstOptions, args, false);
            parseSpecialOptions(cmd);
        } catch (ParseException ex) {
            logger.error("Invalid usage (failed to parse options)", ex);
            printHelp();
            System.exit(1);
        }
        // go ahead and parse full options
        try {
            // create command line parser
            CommandLineParser parser = new PosixParser();
            // parse options
            CommandLine cmd = parser.parse(allOptions, args);
            parseOptions(cmd);
        } catch (ParseException ex){
            logger.error("Invalid usage (failed to parse options)", ex);
            printHelp();
            System.exit(1);
        }
        // run search engine
        try {
            // check config file
            checkConfigFile();
            // run engine
            search();
        } catch (GenestackerException ex){
            // report genestacker errors
            logger.error("Fatal error occurred while running Gene Stacker", ex);            
            System.exit(1);
        } catch (IOException | ArchiveException ex){
            // report IO errors
            logger.error("Input/output error", ex);            
            System.exit(1);
        } catch (Exception ex){
            // unexpected error (catch-all)
            logger.error("Unexpected error", ex);
            System.exit(1);
        }
    }
    
    private void checkConfigFile() throws GenestackerException {
        // try to read config file entries
        GenestackerResourceBundle.getConfig("dot.path");
        GenestackerResourceBundle.getConfig("crossingscheme.xml.schema");
        GenestackerResourceBundle.getConfig("genestackerinput.xml.schema");
    }
    
    private void setupOptions(){
        
        // setup required params
        
        Option successProbOption = OptionBuilder.withLongOpt("success-prob")<commit_msg>updated error messages<commit_after>package org.ugent.caagt.genestacker.cli;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.ugent.caagt.genestacker.Haplotype;
import org.ugent.caagt.genestacker.Plant;
import org.ugent.caagt.genestacker.exceptions.GenestackerException;
import org.ugent.caagt.genestacker.io.*;
import org.ugent.caagt.genestacker.search.*;
import org.ugent.caagt.genestacker.search.bb.*;
import org.ugent.caagt.genestacker.search.bb.heuristics.*;
import org.ugent.caagt.genestacker.search.constraints.*;
import org.ugent.caagt.genestacker.util.GenestackerConstants;
import org.ugent.caagt.genestacker.util.TimeFormatting;

/**
 * Command line interface for Gene Stacker software.
 */
public class Main
{
    
    // logger
    private Logger logger = LogManager.getLogger(Main.class);
    
    // options to check before parsing other options
    private Options checkFirstOptions;
    // required parameters
    private Options requiredOptions;
    // constraints
    private Options constraintOptions;
    // heuristic options
    private Options heuristicPresetsOptions;
    private Options individualHeuristicOptions;
    // verbosity options
    private Options verbosityOptions;
    // general options
    private Options miscOptions;
    // groups all options
    private Options allOptions;
    
    // parameter values
    private double successProb;
    private String outputFile;
    private String inputFile;
    // constraints
    private List<Constraint> constraints;
    private NumberOfSeedsPerCrossing numSeeds;
    private boolean homozygousIdeotypeParents;
    // heuristics
    private boolean h0 = false;     // initial plant filtering
    private boolean h1a = false;    // weak improvement heuristic
    private boolean h1b = false;    // strong improvement heuristic
    private boolean h2a = false;    // seed lot filtering (w.r.t. weak improvement)
    private boolean h2b = false;    // seed lot filtering (w.r.t. strong improvement)
    private boolean h3 = false;     // Pareto optimal subschemes
    private boolean h3s1 = false;   // H3 seeded (version 1)
    private boolean h3s2 = false;   // H3 seeded (version 2 -- extra seed lot filtering in second run)
    private boolean h4 = false;     // Pareto optimal seed lots
    private boolean h5 = false;     // heuristic seed lot construction
    private boolean h5c = false;    // heuristic seed lot construction with consistency check
    private boolean h6 = false;     // heuristic population size bound
    // option for heuristic seed lot constructor
    private int maxNumCrossovers = GenestackerConstants.UNLIMITED_CROSSOVERS;
    // misc options
    private GraphFileFormat graphFileFormat;
    private boolean useKosambiMap;
    private boolean tree;
    private long runtimeLimit = GenestackerConstants.NO_RUNTIME_LIMIT;
    private boolean minimizePopSizeOnly;
    private int numThreads;
    private boolean writeIntermediateOutput;
    
    // total runtime (ms)
    private long totalRuntime;
    
    /**
     * Run Gene Stacker software from command line.
     * 
     * @param args
     */
    public static void main( String[] args ){
        Main cli = new Main();
        cli.run(args);
    }
    
    private void run(String args[]){
        // setup options
        setupOptions();
        // parse special options only
        try {
            // create command line parser that ignores unknown options
            CommandLineParser parser = new ExtendedPosixParser(true);
            // parse special options
            CommandLine cmd = parser.parse(checkFirstOptions, args, false);
            parseSpecialOptions(cmd);
        } catch (ParseException ex) {
            logger.error("Invalid usage: {}", ex.getMessage());
            printHelp();
            System.exit(1);
        }
        // go ahead and parse full options
        try {
            // create command line parser
            CommandLineParser parser = new PosixParser();
            // parse options
            CommandLine cmd = parser.parse(allOptions, args);
            parseOptions(cmd);
        } catch (ParseException ex){
            logger.error("Invalid usage: {}", ex.getMessage());
            printHelp();
            System.exit(1);
        }
        // run search engine
        try {
            // check config file
            checkConfigFile();
            // run engine
            search();
        } catch (GenestackerException ex){
            // report genestacker errors
            logger.error("Fatal error occurred while running Gene Stacker", ex);            
            System.exit(1);
        } catch (IOException | ArchiveException ex){
            // report IO errors
            logger.error("Input/output error: {}", ex.getMessage());            
            System.exit(1);
        } catch (Exception ex){
            // unexpected error (catch-all)
            logger.error("Unexpected error", ex);
            System.exit(1);
        }
    }
    
    private void checkConfigFile() throws GenestackerException {
        // try to read config file entries
        GenestackerResourceBundle.getConfig("dot.path");
        GenestackerResourceBundle.getConfig("crossingscheme.xml.schema");
        GenestackerResourceBundle.getConfig("genestackerinput.xml.schema");
    }
    
    private void setupOptions(){
        
        // setup required params
        
        Option successProbOption = OptionBuilder.withLongOpt("success-prob")
{'fraction_non_alphanumeric': 0.08698402656966629, 'fraction_numerical': 0.004111972165111498, 'mean_word_length': 2.652035807103667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6349718', 'n_tokens_mistral': 3386, 'n_tokens_neox': 3279, 'n_words': 923}