<commit_before>/*
 * Copyright (C) 2002-2009 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * BEAST is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.app.beauti.options;

import dr.app.beauti.traitspanel.GuessTraitException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class TraitGuesser {

    public enum GuessType {
        SUFFIX,
        PREFIX,
        REGEX
    }

    public static enum TraitType {
        DISCRETE,
        INTEGER,
        CONTINUOUS
    }

    public enum Traits {

        TRAIT_SPECIES("species");

        Traits(String name) {
            this.name = name;
        }

        public String toString() {
            return name;
        }

        private final String name;
    }

    public boolean guessTrait = false; // no use ??
    public GuessType guessType = GuessType.SUFFIX;
    public Traits traitAnalysisType = Traits.TRAIT_SPECIES;
    public TraitType traitType = TraitType.DISCRETE;

    public int index = 0;
    public String separator;
    public String regex;

    public void guessTrait(BeautiOptions options) {

        for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {

            String value = null;

            try {
                switch (guessType) {
                    case SUFFIX:
                        value = guessTraitFromSuffix(options.taxonList.getTaxonId(i), separator);
                        break;
                    case PREFIX:
                        value = guessTraitFromPrefix(options.taxonList.getTaxonId(i), separator);
                        break;
                    case REGEX:
                        value = guessTraitFromRegex(options.taxonList.getTaxonId(i), regex);
                        break;
                    default:
                        throw new IllegalArgumentException("unknown GuessType");
                }

            } catch (GuessTraitException gfe) {
                //
            }

            options.taxonList.getTaxon(i).setAttribute(Traits.TRAIT_SPECIES.toString(), value);
        }
    }

    public String guessTraitFromSuffix(String label, String seperator) throws GuessTraitException {
        if (seperator.length() < 1 || index < 0) {
            throw new IllegalArgumentException("Invalid seperator");
        }

        int id = -1;
        String t = label;
        for (int i = 0; i <= index; i++) { // i <= index
            id = t.lastIndexOf(seperator);

            if (id < 0) {
                throw new IllegalArgumentException("Can not find seperator in taxon label or invalid seperator index");
            }

            t = t.substring(0, id);
        }

        return label.substring(id + 1); // exclude ith separator
    }

    public String guessTraitFromPrefix(String label, String seperator) throws GuessTraitException {
        if (seperator.length() < 1 || index < 0) {
            throw new IllegalArgumentException("Invalid seperator");
        }

        int id;
        int idSum = 0;
        String t = label;
        for (int i = 0; i <= index; i++) { // i <= index
            id = t.indexOf(seperator);

            if (id < 0) {
            	if (i == 0) {
            		throw new IllegalArgumentException("Can not find seperator in taxon label or invalid seperator index");
            	} else {
            		return label + seperator + traitAnalysisType.toString();
            	}
            }

            t = t.substring(id + 1);<commit_msg>BEAUTi: improve error message of guess trait.<commit_after>/*
 * Copyright (C) 2002-2009 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * BEAST is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.app.beauti.options;

import dr.app.beauti.traitspanel.GuessTraitException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class TraitGuesser {

    public enum GuessType {
        SUFFIX,
        PREFIX,
        REGEX
    }

    public static enum TraitType {
        DISCRETE,
        INTEGER,
        CONTINUOUS
    }

    public enum Traits {

        TRAIT_SPECIES("species");

        Traits(String name) {
            this.name = name;
        }

        public String toString() {
            return name;
        }

        private final String name;
    }

    public boolean guessTrait = false; // no use ??
    public GuessType guessType = GuessType.SUFFIX;
    public Traits traitAnalysisType = Traits.TRAIT_SPECIES;
    public TraitType traitType = TraitType.DISCRETE;

    public int index = 0;
    public String separator;
    public String regex;

    public void guessTrait(BeautiOptions options) {

        for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {

            String value = null;

            try {
                switch (guessType) {
                    case SUFFIX:
                        value = guessTraitFromSuffix(options.taxonList.getTaxonId(i), separator);
                        break;
                    case PREFIX:
                        value = guessTraitFromPrefix(options.taxonList.getTaxonId(i), separator);
                        break;
                    case REGEX:
                        value = guessTraitFromRegex(options.taxonList.getTaxonId(i), regex);
                        break;
                    default:
                        throw new IllegalArgumentException("unknown GuessType");
                }

            } catch (GuessTraitException gfe) {
                //
            }

            options.taxonList.getTaxon(i).setAttribute(Traits.TRAIT_SPECIES.toString(), value);
        }
    }

    public String guessTraitFromSuffix(String label, String seperator) throws GuessTraitException {
        if (seperator.length() < 1 || index < 0) {
            throw new IllegalArgumentException("No seperator or wrong seperator index !");
        }

        int id = -1;
        String t = label;
        for (int i = 0; i <= index; i++) { // i <= index
            id = t.lastIndexOf(seperator);

            if (id < 0) {
                throw new IllegalArgumentException("Can not find seperator in taxon label (" + label + ")\n or invalid seperator (" + seperator + ") !");
            }

            t = t.substring(0, id);
        }

        return label.substring(id + 1); // exclude ith separator
    }

    public String guessTraitFromPrefix(String label, String seperator) throws GuessTraitException {
        if (seperator.length() < 1 || index < 0) {
            throw new IllegalArgumentException("No seperator or wrong seperator index !");
        }

        int id;
        int idSum = 0;
        String t = label;
        for (int i = 0; i <= index; i++) { // i <= index
            id = t.indexOf(seperator);

            if (id < 0) {
            	if (i == 0) {
            		throw new IllegalArgumentException("Can not find seperator in taxon label (" + label + ")\n or invalid seperator (" + seperator + ") !");
            	} else {
            		return label + seperator + traitAnalysisType.toString();
            	}
            }

            t = t.substring(id + 1);
{'fraction_non_alphanumeric': 0.08617142857142857, 'fraction_numerical': 0.008228571428571429, 'mean_word_length': 1.920894526034713, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3007008', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2327, 'n_words': 781}