<commit_before>/*
 * The Exomiser - A tool to annotate and prioritize genomic variants
 *
 * Copyright (c) 2016-2018 Queen Mary University of London.
 * Copyright (c) 2012-2016 Charité Universitätsmedizin Berlin and Genome Research Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.monarchinitiative.exomiser.core.phenotype.service;


import org.monarchinitiative.exomiser.core.phenotype.PhenotypeTerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for checking input HPO identifiers against those from the hp.obo ontology file.
 *
 * @author <NAME> <<EMAIL>>
 * @since 12.0.0
 */
public class HpoIdChecker {

    private static final Logger logger = LoggerFactory.getLogger(HpoIdChecker.class);
    private static final Pattern HPO_ID_PATTERN = Pattern.compile("HP:[0-9]{7}");

    private final Map<String, PhenotypeTerm> termIdToTerms;

    /**
     *
     * @param termIdToTerms A map of all current and
     * @return a new instance of the HpoIdChecker
     */
    public static HpoIdChecker of(Map<String, PhenotypeTerm> termIdToTerms) {
        Objects.requireNonNull(termIdToTerms);
        return new HpoIdChecker(termIdToTerms);
    }

    private HpoIdChecker(Map<String, PhenotypeTerm> termIdToTerms) {
        this.termIdToTerms = termIdToTerms;
    }

    /**
     * Checks the input id and returns the current version of it. Invalid HPO ids will throw an exception, unrecognised
     * yet valid ids will be returned as is.
     * @param hpoId The hpoId for which the current version is required
     * @return the current hpoId for the input id
     * @throws IllegalArgumentException when fed an invalid HPO identifier.
     */
    public String getCurrentId(String hpoId) {
        PhenotypeTerm term = getCurrentTerm(hpoId);
        return term == null ? hpoId : term.getId();
    }

    /**
     * Checks the input id and returns the current {@link PhenotypeTerm} for it. Invalid HPO ids will throw an exception,
     * unrecognised yet valid ids will return null values.
     *
     * @param hpoId The hpoId for which the current version is required
     * @return the current {@link PhenotypeTerm} for the input id or null if invalid/unrecognised
     * @throws IllegalArgumentException when fed an invalid HPO identifier.
     */
    @Nullable
    public PhenotypeTerm getCurrentTerm(String hpoId) {
        logger.debug("Checking ID {} is not obsolete", hpoId);
        Matcher hpoIdMatcher = HPO_ID_PATTERN.matcher(hpoId);
        if (!hpoIdMatcher.matches()) {
            throw new IllegalArgumentException("Input '" + hpoId + "' not a valid HPO identifier!");
        }
        PhenotypeTerm term = termIdToTerms.get(hpoId);
        if (term == null) {
            logger.warn("Input {} - unable to find current id so returning input {}", hpoId, hpoId);
            return null;
        }
        if (hpoId.equals(term.getId())) {
            logger.debug("Input is current - {} ({})", hpoId, term.getLabel());
            return term;
        } else {
            logger.info("Input term {} is obsolete. Replaced by {} ({})", hpoId, term.getId(), term.getLabel());
            return term;
        }
    }
}<commit_msg>Update HpoIdChecker to make a copy of input map<commit_after>/*
 * The Exomiser - A tool to annotate and prioritize genomic variants
 *
 * Copyright (c) 2016-2018 Queen Mary University of London.
 * Copyright (c) 2012-2016 Charité Universitätsmedizin Berlin and Genome Research Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.monarchinitiative.exomiser.core.phenotype.service;


import com.google.common.collect.ImmutableMap;
import org.monarchinitiative.exomiser.core.phenotype.PhenotypeTerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for checking input HPO identifiers against those from the hp.obo ontology file.
 *
 * @author <NAME> <<EMAIL>>
 * @since 12.0.0
 */
public class HpoIdChecker {

    private static final Logger logger = LoggerFactory.getLogger(HpoIdChecker.class);
    private static final Pattern HPO_ID_PATTERN = Pattern.compile("HP:[0-9]{7}");

    private final Map<String, PhenotypeTerm> termIdToTerms;

    /**
     *
     * @param termIdToTerms A map of all current and
     * @return a new instance of the HpoIdChecker
     */
    public static HpoIdChecker of(Map<String, PhenotypeTerm> termIdToTerms) {
        Objects.requireNonNull(termIdToTerms);
        return new HpoIdChecker(termIdToTerms);
    }

    private HpoIdChecker(Map<String, PhenotypeTerm> termIdToTerms) {
        this.termIdToTerms = ImmutableMap.copyOf(termIdToTerms);
    }

    /**
     * Checks the input id and returns the current version of it. Invalid HPO ids will throw an exception, unrecognised
     * yet valid ids will be returned as is.
     * @param hpoId The hpoId for which the current version is required
     * @return the current hpoId for the input id
     * @throws IllegalArgumentException when fed an invalid HPO identifier.
     */
    public String getCurrentId(String hpoId) {
        PhenotypeTerm term = getCurrentTerm(hpoId);
        return term == null ? hpoId : term.getId();
    }

    /**
     * Checks the input id and returns the current {@link PhenotypeTerm} for it. Invalid HPO ids will throw an exception,
     * unrecognised yet valid ids will return null values.
     *
     * @param hpoId The hpoId for which the current version is required
     * @return the current {@link PhenotypeTerm} for the input id or null if invalid/unrecognised
     * @throws IllegalArgumentException when fed an invalid HPO identifier.
     */
    @Nullable
    public PhenotypeTerm getCurrentTerm(String hpoId) {
        logger.debug("Checking ID {} is not obsolete", hpoId);
        Matcher hpoIdMatcher = HPO_ID_PATTERN.matcher(hpoId);
        if (!hpoIdMatcher.matches()) {
            throw new IllegalArgumentException("Input '" + hpoId + "' not a valid HPO identifier!");
        }
        PhenotypeTerm term = termIdToTerms.get(hpoId);
        if (term == null) {
            logger.warn("Input {} - unable to find current id. Returning input {}", hpoId, hpoId);
            return null;
        }
        if (hpoId.equals(term.getId())) {
            logger.debug("Input is current - {} ({})", hpoId, term.getLabel());
            return term;
        } else {
            logger.info("Input term {} is obsolete. Replaced by {} ({})", hpoId, term.getId(), term.getLabel());
            return term;
        }
    }
}
{'fraction_non_alphanumeric': 0.08793532338308457, 'fraction_numerical': 0.006467661691542288, 'mean_word_length': 3.12782340862423, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1896440', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2265, 'n_words': 827}