Example usage for org.apache.commons.configuration2.builder BasicConfigurationBuilder getConfiguration

List of usage examples for org.apache.commons.configuration2.builder BasicConfigurationBuilder getConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.configuration2.builder BasicConfigurationBuilder getConfiguration.

Prototype

@Override
public T getConfiguration() throws ConfigurationException 

Source Link

Document

This implementation creates the result configuration on first access.

Usage

From source file:Executable.Output.java

/**
 * Write the control file (for the final imputation step)
 * @param config The configuration to write
 * @throws OutputException If there is an IO problem
 *//* w ww  .  j  a va  2s.com*/
public void writeControl(List<ImmutableNode> config) throws OutputException {
    BasicConfigurationBuilder<ExtendedXMLConfiguration> builder = new BasicConfigurationBuilder<>(
            ExtendedXMLConfiguration.class).configure(new Parameters().xml());

    ExtendedXMLConfiguration c;
    try {
        c = builder.getConfiguration();
    } catch (ConfigurationException ex) {
        throw new ProgrammerException(ex);
    }

    c.setRootElementName("linkimputepro");

    c.addNodes(null, config);

    FileHandler handler = new FileHandler(c);
    try {
        handler.save(control);
    } catch (ConfigurationException ex) {
        if (ex.getCause() instanceof IOException) {
            IOException tex = (IOException) ex.getCause();
            throw new OutputException("Problem writing imputation control file", tex);
        }
        throw new ProgrammerException(ex);
    }
}

From source file:Executable.LinkImputeR.java

private static XMLConfiguration convert(File ini) throws INIException, VCFInputException {
    FileBasedConfigurationBuilder<INIConfiguration> inibuilder = new FileBasedConfigurationBuilder<>(
            INIConfiguration.class).configure(new Parameters().fileBased().setFile(ini));

    INIConfiguration config;//  www  . j a  v a 2 s  .  c  o  m
    try {
        config = inibuilder.getConfiguration();
    } catch (ConfigurationException ex) {
        throw new INIException(
                "There's a problem reading the ini file.  " + "Does it exist? Is it formatted correctly?", ex);
    }

    List<ImmutableNode> xml = new ArrayList<>();

    xml.add(new ImmutableNode.Builder().name("mode").value("accuracy").create());

    String[] sdepths = config.getString("Global.depth").split(",");
    int[] depths = new int[sdepths.length];
    for (int i = 0; i < sdepths.length; i++) {
        try {
            depths[i] = Integer.parseInt(sdepths[i]);
        } catch (NumberFormatException ex) {
            throw new INIException("Values for the depth option must be integers");
        }

        if (depths[i] <= 0) {
            throw new INIException("Values for the depth option must be positive");
        }

    }

    int maxDepth = NumberUtils.max(depths);
    int minDepth = NumberUtils.min(depths);
    double error;
    try {
        error = config.getDouble("Global.error", 0.01);
    } catch (ConversionException ex) {
        throw new INIException("Values for the error option must be a number");
    }

    if ((error <= 0.0) || (error > 1.0)) {
        throw new INIException("Values for error must be between 0 and 1");
    }

    File input = new File(config.getString("Input.filename"));
    List<PositionFilter> inputfilters = new ArrayList<>();

    int numSnps = VCF.numberPositionsFromFile(input);
    for (HierarchicalConfiguration<ImmutableNode> i : config.childConfigurationsAt("InputFilters")) {
        switch (i.getRootElementName()) {
        //ADD FILTERS
        case "maf":
            double maf;
            try {
                maf = i.getDouble(null);
            } catch (ConversionException ex) {
                throw new INIException("Parameter for the MAF Filter must be a number");
            }
            if ((maf <= 0) | (maf >= 0.5)) {
                throw new INIException("Parameter for the MAF filter must be between 0 and 0.5");
            }
            inputfilters.add(new MAFFilter(maf, 8, 100, error));
            break;
        case "hw":
            double sig;
            try {
                sig = i.getDouble(null);
            } catch (ConversionException ex) {
                throw new INIException("Parameter for the HW Filter must be a number");
            }
            if ((sig <= 0) | (sig >= 1.0)) {
                throw new INIException("Parameter for the HWS filter must be between 0 and 1");
            }
            inputfilters.add(new ParalogHWFilter(sig / numSnps, error));
            break;
        case "positionmissing":
            double missing;
            try {
                missing = i.getDouble(null);
            } catch (ConversionException ex) {
                throw new INIException("Parameter for the Position Missing filter must be a number");
            }
            if ((missing <= 0) | (missing >= 1.0)) {
                throw new INIException("Parameter for the Position Missing filter must be between 0 and 1");
            }
            inputfilters.add(new PositionMissing(i.getDouble(null), minDepth));
            break;
        }
    }

    String saveString = config.getString("Input.save", null);
    File save = (saveString == null) ? null : new File(saveString);

    String readsformat = config.getString("Input.readsformat", null);
    if ((readsformat != null) && (readsformat.split(",").length > 2)) {
        throw new INIException("readsformat must be either a single format or two"
                + "formats comma separated (LinkImputeR currently only works on" + "biallelic SNPs)");
    }

    int maxInDepth;
    try {
        maxInDepth = config.getInt("Input.maxdepth", 100);
    } catch (ConversionException ex) {
        throw new INIException("Parameter values for the maxdepth option must be an integer.");
    }

    Input o = new Input(input, inputfilters, save, maxInDepth, readsformat);
    xml.add(o.getConfig());

    String sampleMethod = config.getString("Accuracy.maskmethod", "all");
    Method sm;
    switch (sampleMethod) {
    case "bysnp":
        sm = Method.BYSNP;
        break;
    case "bysample":
        sm = Method.BYSAMPLE;
        break;
    case "all":
        sm = Method.ALL;
        break;
    default:
        throw new INIException("maskmethod must be either \"bysnp\", \"bysample\" or \"all\".");
    }
    int numberMasked;
    try {
        numberMasked = config.getInt("Accuracy.numbermasked", 10000);
    } catch (ConversionException ex) {
        throw new INIException("Parameter values for the numbermasked option must be an integer.");
    }
    int minMaskDepth;
    try {
        minMaskDepth = config.getInt("Accuracy.mindepth", 30);
    } catch (ConversionException ex) {
        throw new INIException("Parameter values for the maxdepth option must be an integer");
    }
    DepthMaskFactory dmf = new DepthMaskFactory(numberMasked, minMaskDepth, maxDepth, sm);
    xml.add(dmf.getConfig());

    String accuracyMethod = config.getString("Accuracy.accuracymethod", "correct");
    AccuracyMethod am;
    switch (accuracyMethod) {
    case "correlation":
        am = AccuracyMethod.CORRELATION;
        break;
    case "correct":
        am = AccuracyMethod.CORRECT;
        break;
    default:
        throw new INIException("accuractymethod must be either \"correlation\" or \"correct\".");
    }

    Caller caller = new BinomialCaller(error);
    String statsRoot = config.getString("Stats.root");
    boolean partial;
    try {
        partial = config.getBoolean("Stats.partial", false);
    } catch (ConversionException ex) {
        throw new INIException("Stats partial must be convertible to a boolean.  Try \"yes\" or \"no\".");
    }

    boolean eachMasked;
    try {
        eachMasked = config.getBoolean("Stats.eachmasked", false);
    } catch (ConversionException ex) {
        throw new INIException("Stats eachmasked must be convertible to a boolean.  Try \"yes\" or \"no\".");
    }

    int casenum = 1;

    for (int depth : depths) {
        List<List<VCFFilter>> cases = new ArrayList<>();
        cases.add(new ArrayList<>());

        for (HierarchicalConfiguration<ImmutableNode> i : config.childConfigurationsAt("CaseFilters")) {
            List<List<VCFFilter>> newcases = new ArrayList<>();
            String[] options = i.getString(null).split(",");
            for (String opt : options) {
                VCFFilter f;
                switch (i.getRootElementName()) {
                case "maf":
                    double maf;
                    try {
                        maf = Double.parseDouble(opt);
                    } catch (NumberFormatException ex) {
                        throw new INIException("Parameter for the MAF Filter must be a number");
                    }
                    if ((maf <= 0) | (maf >= 0.5)) {
                        throw new INIException("Parameter for the MAF filter must be between 0 and 0.5");
                    }
                    f = new MAFFilter(maf, 8, 100, error);
                    for (List<VCFFilter> c : cases) {
                        List<VCFFilter> nc = new ArrayList<>(c);
                        nc.add(f);
                        newcases.add(nc);
                    }
                    break;
                case "missing":
                    double missing;
                    try {
                        missing = Double.parseDouble(opt);
                    } catch (NumberFormatException ex) {
                        throw new INIException("Parameter for the Missing filter must be a number");
                    }
                    if ((missing <= 0) | (missing >= 1.0)) {
                        throw new INIException("Parameter for the Missing filter must be between 0 and 1");
                    }

                    VCFFilter fp = new PositionMissing(missing, depth);
                    VCFFilter fs = new SampleMissing(missing, depth);
                    for (List<VCFFilter> c : cases) {
                        List<VCFFilter> nc = new ArrayList<>(c);
                        nc.add(fp);
                        nc.add(fs);
                        newcases.add(nc);
                    }
                    break;
                case "samplemissing":
                    double smissing;
                    try {
                        smissing = Double.parseDouble(opt);
                    } catch (NumberFormatException ex) {
                        throw new INIException("Parameter for the Missing filter must be a number");
                    }
                    if ((smissing <= 0) | (smissing >= 1.0)) {
                        throw new INIException("Parameter for the Missing filter must be between 0 and 1");
                    }
                    f = new SampleMissing(smissing, depth);
                    for (List<VCFFilter> c : cases) {
                        List<VCFFilter> nc = new ArrayList<>(c);
                        nc.add(f);
                        newcases.add(nc);
                    }
                    break;
                case "positionmissing":
                    double pmissing;
                    try {
                        pmissing = Double.parseDouble(opt);
                    } catch (NumberFormatException ex) {
                        throw new INIException("Parameter for the Missing filter must be a number");
                    }
                    if ((pmissing <= 0) | (pmissing >= 1.0)) {
                        throw new INIException("Parameter for the Missing filter must be between 0 and 1");
                    }
                    f = new PositionMissing(pmissing, depth);
                    for (List<VCFFilter> c : cases) {
                        List<VCFFilter> nc = new ArrayList<>(c);
                        nc.add(f);
                        newcases.add(nc);
                    }
                    break;
                //NEED TO THINK ABOUT WHAT TO DO WITH SIGNIFICANCE!
                //POSSIBLY CHANGES IN CASE????
                /*case "hw":
                    f = new ParalogHWFilter(Double.parseDouble(opt),error);
                    for (List<VCFFilter> c: cases)
                    {
                        List<VCFFilter> nc = new ArrayList<>(c);
                        nc.add(f);
                        newcases.add(nc);
                    }
                    break;
                        */
                }
            }
            cases = newcases;
        }

        ImputationOption imputer = new ImputationOption(new KnniLDProbOptimizedCalls(depth, am));
        CombinerOption combiner = new CombinerOption(new MaxDepthCombinerOptimizedCalls(depth, am));

        for (List<VCFFilter> filters : cases) {
            String name = "Case " + casenum;

            File prettyStats = null;
            File genoStats = null;
            File depthStats = null;
            File dgStats = null;
            File eachMaskedFile = null;

            switch (config.getString("Stats.level", "sum")) {
            case "sum":
                break;
            case "pretty":
                prettyStats = new File(statsRoot + "pretty_" + casenum + ".dat");
                break;
            case "table":
                prettyStats = new File(statsRoot + "pretty_" + casenum + ".dat");
                genoStats = new File(statsRoot + "geno_" + casenum + ".dat");
                depthStats = new File(statsRoot + "depth_" + casenum + ".dat");
                dgStats = new File(statsRoot + "dg_" + casenum + ".dat");
                break;
            default:
                throw new INIException("Stats level must be either \"sum\", \"pretty\" or \"table\".");
            }

            if (eachMasked) {
                eachMaskedFile = new File(statsRoot + "each_" + casenum + ".dat");
            }

            PrintStats print = new PrintStats(prettyStats, genoStats, depthStats, dgStats, eachMaskedFile,
                    partial);

            Case cas = new Case(name, filters, caller, imputer, combiner, print, "Depth(" + depth + ")");
            xml.add(cas.getConfig());

            casenum++;
        }
    }

    File sum = new File(statsRoot + "sum.dat");
    File control = new File(config.getString("Output.control"));
    File table;
    if (config.getString("Stats.level").equals("table")) {
        table = new File(statsRoot + "table.dat");
    } else {
        table = null;
    }

    Output out = new Output(sum, table, control, partial);
    xml.add(out.getConfig());

    File log;
    String ls = config.getString("Log.file", null);
    if (ls != null) {
        log = new File(ls);
    } else {
        log = null;
    }
    Level level;
    switch (config.getString("Log.level", "critical").toLowerCase()) {
    case "brief":
        level = Log.Level.BRIEF;
        break;
    case "detail":
        level = Log.Level.DETAIL;
        break;
    case "debug":
        level = Log.Level.DEBUG;
        break;
    case "critical":
        level = Log.Level.CRITICAL;
        break;
    default:
        throw new INIException("log level must be either \"critical\", \"brief\", \"details\" or \"debug\".");
    }

    xml.add(Log.getConfig(level, log));

    BasicConfigurationBuilder<ExtendedXMLConfiguration> xmlbuilder = new BasicConfigurationBuilder<>(
            ExtendedXMLConfiguration.class).configure(new Parameters().xml());

    ExtendedXMLConfiguration c;
    try {
        c = xmlbuilder.getConfiguration();
    } catch (ConfigurationException ex) {
        throw new ProgrammerException(ex);
    }

    c.setRootElementName("linkimputer");

    c.addNodes(null, xml);

    return c;
}

From source file:org.demoiselle.internal.implementation.ConfigurationLoader.java

private void loadConfiguration() {
    Configuration config;//from w  w  w.j  ava 2s  .  c  o m
    BasicConfigurationBuilder<? extends Configuration> builder = createConfiguration();

    if (builder instanceof FileBasedConfigurationBuilder) {
        Parameters params = new Parameters();

        ((FileBasedConfigurationBuilder) builder)
                .configure(params.fileBased().setURL(Reflections.getResourceAsURL(this.resource)));
    }

    try {
        config = builder.getConfiguration();
    } catch (org.apache.commons.configuration2.ex.ConfigurationException e) {
        getLogger().warning(getBundle().getString("file-not-found", this.resource));
        config = null;
    }

    this.configuration = config;
}