<commit_before>package uk.ac.ebi.arrayexpress.utils.efo;

import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Copyright 2009-2011 European Molecular Biology Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

public class EFOLoader
{
    private final static IRI IRI_AE_LABEL = IRI.create("http://www.ebi.ac.uk/efo/ArrayExpress_label");
    private final static IRI IRI_EFO_URI = IRI.create("http://www.ebi.ac.uk/efo/ArrayExpress_label");
    private final static IRI IRI_ALT_TERM = IRI.create("http://www.ebi.ac.uk/efo/alternative_term");
    private final static IRI IRI_PART_OF = IRI.create("http://www.obofoundry.org/ro/ro.owl#part_of");


    private Map<String, Set<String>> reverseSubClassOfMap = new HashMap<String, Set<String>>();
    private Map<String, Set<String>> reversePartOfMap = new HashMap<String, Set<String>>();

    public EFOLoader()
    {
    }

    public IEFO load( InputStream ontologyStream )
    {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology;
        OWLReasoner reasoner = null;

        EFOImpl efo = new EFOImpl();

        try {
            // to prevernt RDFXMLParser to fail on some machines
            // with SAXParseException: The parser has encountered more than "64,000" entity expansions
            System.setProperty("entityExpansionLimit", "100000000");
            ontology = manager.loadOntologyFromOntologyDocument(ontologyStream);

            OWLReasonerFactory reasonerFactory = new Reasoner.ReasonerFactory();
            reasoner = reasonerFactory.createReasoner(ontology);

            reasoner.precomputeInferences();
            if (reasoner.isConsistent()) {

                Set<OWLClass> classes = ontology.getClassesInSignature();
                for (OWLClass cls : classes) {
                    loadClass(ontology, reasoner, cls, efo);
                }

                // now, complete missing bits in parent-children relationships
                for (String id : reverseSubClassOfMap.keySet()) {
                    EFONode node = efo.getMap().get(id);
                    for (String parentId : reverseSubClassOfMap.get(id)) {
                        EFONode parentNode = efo.getMap().get(parentId);
                        if (null != parentNode) { // most likely parent is owl thing
                            node.getParents().add(parentNode);
                            parentNode.getChildren().add(node);
                        }
                    }
                }

                // and finally work out part_of relationships
                for (String partOfId : reversePartOfMap.keySet()) {
                    for (String id : reversePartOfMap.get(partOfId)) {
                        if (!efo.getPartOfIdMap().containsKey(id)) {
                            efo.getPartOfIdMap().put(id, new HashSet<String>());
                        }
                        efo.getPartOfIdMap().get(id).add(partOfId);
                    }
                }
            }
        } catch (OWLOntologyCreationException e) {
            throw new RuntimeException("Unable to read ontology from a stream", e);
        } catch (UnsupportedOperationException e) {
            throw new RuntimeException("Unable to reason the ontology", e);
        } finally {
            if (null != reasoner) {<commit_msg>Safeguarding the ontology relationships<commit_after>package uk.ac.ebi.arrayexpress.utils.efo;

import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Copyright 2009-2011 European Molecular Biology Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

public class EFOLoader
{
    // logging machinery
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final static IRI IRI_AE_LABEL = IRI.create("http://www.ebi.ac.uk/efo/ArrayExpress_label");
    private final static IRI IRI_EFO_URI = IRI.create("http://www.ebi.ac.uk/efo/ArrayExpress_label");
    private final static IRI IRI_ALT_TERM = IRI.create("http://www.ebi.ac.uk/efo/alternative_term");
    private final static IRI IRI_PART_OF = IRI.create("http://www.obofoundry.org/ro/ro.owl#part_of");

    private Map<String, Set<String>> reverseSubClassOfMap = new HashMap<String, Set<String>>();
    private Map<String, Set<String>> reversePartOfMap = new HashMap<String, Set<String>>();

    public EFOLoader()
    {
    }

    public IEFO load( InputStream ontologyStream )
    {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology;
        OWLReasoner reasoner = null;

        EFOImpl efo = new EFOImpl();

        try {
            // to prevernt RDFXMLParser to fail on some machines
            // with SAXParseException: The parser has encountered more than "64,000" entity expansions
            System.setProperty("entityExpansionLimit", "100000000");
            ontology = manager.loadOntologyFromOntologyDocument(ontologyStream);

            OWLReasonerFactory reasonerFactory = new Reasoner.ReasonerFactory();
            reasoner = reasonerFactory.createReasoner(ontology);

            reasoner.precomputeInferences();
            if (reasoner.isConsistent()) {

                Set<OWLClass> classes = ontology.getClassesInSignature();
                for (OWLClass cls : classes) {
                    loadClass(ontology, reasoner, cls, efo);
                }

                // now, complete missing bits in parent-children relationships
                for (String id : reverseSubClassOfMap.keySet()) {
                    EFONode node = efo.getMap().get(id);
                    if (null != node) {
                        for (String parentId : reverseSubClassOfMap.get(id)) {
                            EFONode parentNode = efo.getMap().get(parentId);
                            if (null != parentNode) { // most likely parent is owl thing
                                node.getParents().add(parentNode);
                                parentNode.getChildren().add(node);
                            } else {
                                logger.warn("Parent [{}] of [{}] is not loaded from the ontology", parentId, id);
                            }
                        }
                    } else {
                        logger.error("Node [{}] is not loaded from the ontology", id);
                    }
                }

                // and finally work out part_of relationships
                for (String partOfId : reversePartOfMap.keySet()) {
                    for (String id : reversePartOfMap.get(partOfId)) {
                        if (!efo.getPartOfIdMap().containsKey(id)) {
                            efo.getPartOfIdMap().put(id, new HashSet<String>());
                        }
                        efo.getPartOfIdMap().get(id).add(partOfId);
                    }
                }
            }
        } catch (OWLOntologyCreationException e) {
            throw new RuntimeException("Unable to read ontology from a stream", e);
        } catch (UnsupportedOperationException e) {
            throw new RuntimeException("Unable to reason the ontology", e);
        } finally {
            if (null != reasoner) {
{'fraction_non_alphanumeric': 0.09595070422535211, 'fraction_numerical': 0.005941901408450704, 'mean_word_length': 2.275315315315315, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7250611', 'n_tokens_mistral': 2589, 'n_tokens_neox': 2442, 'n_words': 625}