<commit_before>package uk.ac.ebi.spot.goci.pussycat.service;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.exception.OntologyIndexingException;
import uk.ac.ebi.spot.goci.ontology.owl.ReasonedOntologyLoader;

import javax.validation.constraints.NotNull;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by dwelter on 30/06/15.
 */


@Service
public class OntologyService {

    @NotNull
    @Value("${efo.location}")
    private Resource efoResource;


    private ReasonedOntologyLoader ontologyLoader;

    private Map<String, Set<OWLClass>> labelToClassMap;
    private Map<OWLClass, List<String>> classToLabelMap;
    private Map<IRI, OWLClass> iriToClassMap;
    private OWLClass obsoleteClass;


    private Logger log = LoggerFactory.getLogger("rendering");

    protected Logger getLog() {
        return log;
    }

    @Autowired
    public OntologyService(ReasonedOntologyLoader ontologyLoader){
//    public OntologyService(){
//        this.ontologyLoader = new ReasonedOntologyLoader();
        this.ontologyLoader = ontologyLoader;
        ontologyLoader.setOntologyName("efo");
        ontologyLoader.setOntologyURI(URI.create("http://www.ebi.ac.uk/efo"));
        ontologyLoader.setOntologyResource(efoResource);
        ontologyLoader.setExclusionClassURI(URI.create("http://www.geneontology.org/formats/oboInOwl#ObsoleteClass"));
        ontologyLoader.setExclusionAnnotationURI(URI.create("http://www.ebi.ac.uk/efo/organizational_class"));
        ontologyLoader.setSynonymURIs(Collections.singleton(URI.create("http://www.ebi.ac.uk/efo/alternative_term")));
        System.out.println("Initialising the loader");
        ontologyLoader.init();
        System.out.println("Loader ready");
        setObsoleteClass();
        System.out.println("Obsolete class set... ");
        System.out.println("... and it is " + obsoleteClass.getIRI().toString());
        this.labelToClassMap = new HashMap<String, Set<OWLClass>>();
        this.classToLabelMap = new HashMap<OWLClass, List<String>>();
        this.iriToClassMap = new HashMap<IRI, OWLClass>();
        populateClassMaps();
        System.out.println("All done");

    }

    private void populateClassMaps(){
        System.out.println("About to build the class maps");

        OWLOntology ontology = ontologyLoader.getOntology();
        for (OWLClass owlClass : ontology.getClassesInSignature()) {
            // check this isn't an obsolete class
            if (!isObsolete(ontology, obsoleteClass, owlClass)) {
                // get class names, and enter them in the maps
                List<String> classNames = getClassNames(owlClass);

                classToLabelMap.put(owlClass, classNames);
                for (String name : getClassNames(owlClass)) {
                    if(name != null) {
                        name = normalizeSearchString(name);
                        if (!labelToClassMap.containsKey(name)) {
                            labelToClassMap.put(name, new HashSet<OWLClass>());
                        }
                        labelToClassMap.get(name).add(owlClass);
                    }
                }

                // get IRI, and enter it into the map
                iriToClassMap.put(owlClass.getIRI(), owlClass);
            }
        }
    }

    private void setObsoleteClass(){
        for (OWLClass nextClass : ontologyLoader.getOntology().getClassesInSignature()) {
            if (nextClass.getIRI().toURI().toString().equals(ontologyLoader.getExclusionClassURI().toString())) {
                obsoleteClass = nextClass;
                break;<commit_msg>Removed a few system.out statements<commit_after>package uk.ac.ebi.spot.goci.pussycat.service;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.exception.OntologyIndexingException;
import uk.ac.ebi.spot.goci.ontology.owl.ReasonedOntologyLoader;

import javax.validation.constraints.NotNull;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by dwelter on 30/06/15.
 */


@Service
public class OntologyService {

    @NotNull
    @Value("${efo.location}")
    private Resource efoResource;


    private ReasonedOntologyLoader ontologyLoader;

    private Map<String, Set<OWLClass>> labelToClassMap;
    private Map<OWLClass, List<String>> classToLabelMap;
    private Map<IRI, OWLClass> iriToClassMap;
    private OWLClass obsoleteClass;


    private Logger log = LoggerFactory.getLogger("rendering");

    protected Logger getLog() {
        return log;
    }

    @Autowired
    public OntologyService(ReasonedOntologyLoader ontologyLoader){
//    public OntologyService(){
//        this.ontologyLoader = new ReasonedOntologyLoader();
        this.ontologyLoader = ontologyLoader;
        ontologyLoader.setOntologyName("efo");
        ontologyLoader.setOntologyURI(URI.create("http://www.ebi.ac.uk/efo"));
        ontologyLoader.setOntologyResource(efoResource);
        ontologyLoader.setExclusionClassURI(URI.create("http://www.geneontology.org/formats/oboInOwl#ObsoleteClass"));
        ontologyLoader.setExclusionAnnotationURI(URI.create("http://www.ebi.ac.uk/efo/organizational_class"));
        ontologyLoader.setSynonymURIs(Collections.singleton(URI.create("http://www.ebi.ac.uk/efo/alternative_term")));
        ontologyLoader.init();
        setObsoleteClass();
        this.labelToClassMap = new HashMap<String, Set<OWLClass>>();
        this.classToLabelMap = new HashMap<OWLClass, List<String>>();
        this.iriToClassMap = new HashMap<IRI, OWLClass>();
        populateClassMaps();

    }

    private void populateClassMaps(){
        OWLOntology ontology = ontologyLoader.getOntology();
        for (OWLClass owlClass : ontology.getClassesInSignature()) {
            // check this isn't an obsolete class
            if (!isObsolete(ontology, obsoleteClass, owlClass)) {
                // get class names, and enter them in the maps
                List<String> classNames = getClassNames(owlClass);

                classToLabelMap.put(owlClass, classNames);
                for (String name : getClassNames(owlClass)) {
                    if(name != null) {
                        name = normalizeSearchString(name);
                        if (!labelToClassMap.containsKey(name)) {
                            labelToClassMap.put(name, new HashSet<OWLClass>());
                        }
                        labelToClassMap.get(name).add(owlClass);
                    }
                }

                // get IRI, and enter it into the map
                iriToClassMap.put(owlClass.getIRI(), owlClass);
            }
        }
    }

    private void setObsoleteClass(){
        for (OWLClass nextClass : ontologyLoader.getOntology().getClassesInSignature()) {
            if (nextClass.getIRI().toURI().toString().equals(ontologyLoader.getExclusionClassURI().toString())) {
                obsoleteClass = nextClass;
                break;
{'fraction_non_alphanumeric': 0.11126120594375537, 'fraction_numerical': 0.0019648778091612426, 'mean_word_length': 2.9591638308215846, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '7397025', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2339, 'n_words': 402}