001package org.dllearner.examples;
002
003import org.apache.jena.rdf.model.Model;
004import org.apache.jena.rdf.model.ModelFactory;
005import org.apache.jena.riot.Lang;
006import org.apache.log4j.Level;
007import org.apache.log4j.Logger;
008import org.dllearner.algorithms.celoe.CELOE;
009import org.dllearner.algorithms.celoe.OEHeuristicRuntime;
010import org.dllearner.core.*;
011import org.dllearner.core.StringRenderer.Rendering;
012import org.dllearner.kb.OWLAPIOntology;
013import org.dllearner.learningproblems.PosNegLPStandard;
014import org.dllearner.reasoning.ClosedWorldReasoner;
015import org.dllearner.reasoning.OWLAPIReasoner;
016import org.dllearner.refinementoperators.RhoDRDown;
017import org.dllearner.scripts.MouseDiabetesCBD;
018import org.dllearner.utilities.OwlApiJenaUtils;
019import org.semanticweb.elk.owlapi.ElkReasoner;
020import org.semanticweb.owlapi.apibinding.OWLManager;
021import org.semanticweb.owlapi.model.*;
022import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;
023import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl;
024
025import java.io.*;
026import java.util.ArrayList;
027import java.util.HashSet;
028import java.util.Set;
029import java.util.TreeSet;
030
031public class MouseDiabetes {
032        
033    private static final Logger logger = Logger.getLogger(MouseDiabetes.class);
034    private static final String dir = "/tmp/smallis/";
035    private static final String kbFilePath = dir + "monarch_module_exp1.owl";
036    private static final String genoDiseaseFilePath = dir + "mgi_geno_disease.ttl";
037    private static final String genoNotDiseaseFilePath = dir + "mgi_geno_notdisease.ttl";
038    public static final String posExamplesFilePath = dir + "pos_uris.txt";
039    public static final String negExamplesFilePath = dir + "neg_uris.txt";
040    
041    static boolean useCBD = true;
042
043    public static void main(String[] args) throws OWLOntologyCreationException, IOException, ComponentInitException {
044        setUp();
045        logger.debug("starting...");
046        OWLOntology ontology;
047        if(useCBD){
048                ontology = readCBD();
049        } else {
050                ontology = readDumpFiles();
051        }
052       
053        logger.debug("reading positive and negative examples...");
054        Set<OWLIndividual> posExamples = readExamples(posExamplesFilePath);
055        Set<OWLIndividual> negExamples = readExamples(negExamplesFilePath);
056        if(useCBD){
057                posExamples = new HashSet<>(new ArrayList<>(posExamples).subList(0, MouseDiabetesCBD.nrOfPosExamples));
058                negExamples = new HashSet<>(new ArrayList<>(negExamples).subList(0, MouseDiabetesCBD.nrOfNegExamples));
059        }
060        logger.debug("finished reading examples");
061        
062        logger.debug("initializing knowledge source...");
063        KnowledgeSource ks = new OWLAPIOntology(ontology);
064        ks.init();
065        logger.debug("finished initializing knowledge source");
066        
067        logger.debug("initializing reasoner...");
068        OWLAPIReasoner baseReasoner = new OWLAPIReasoner(ks);
069        baseReasoner.setUseFallbackReasoner(true);
070        baseReasoner.init();
071        Logger.getLogger(ElkReasoner.class).setLevel(Level.OFF);
072        logger.debug("finished initializing reasoner");
073        logger.debug("initializing reasoner component...");
074        ClosedWorldReasoner rc = new ClosedWorldReasoner(ks);
075        rc.setReasonerComponent(baseReasoner);
076        rc.setHandlePunning(true);
077        rc.setMaterializeExistentialRestrictions(true);
078        rc.init();
079        logger.debug("finished initializing reasoner");
080        
081//        Reasoner myObject = (Reasoner) MonProxyFactory.monitor(rc);
082        
083        logger.debug("initializing learning problem...");
084        PosNegLPStandard lp = new PosNegLPStandard(rc);
085        lp.setPositiveExamples(posExamples);
086        lp.setNegativeExamples(negExamples);
087        lp.init();
088        logger.debug("finished initializing learning problem");
089        
090        logger.debug("initializing learning algorithm...");
091        AbstractCELA la;
092        OEHeuristicRuntime heuristic = new OEHeuristicRuntime();
093        heuristic.setExpansionPenaltyFactor(0.1);
094        la = new CELOE(lp, rc);
095        ((CELOE) la).setHeuristic(heuristic);
096        ((CELOE) la).setMaxExecutionTimeInSeconds(1800);
097        ((CELOE) la).setNoisePercentage(80);
098        ((CELOE) la).setMaxNrOfResults(50);
099        ((CELOE) la).setWriteSearchTree(false);
100        ((CELOE) la).setReplaceSearchTree(true);
101        ((CELOE) la).setSearchTreeFile("log/mouse-diabetis.log");
102        OWLClassExpression startClass = new OWLClassImpl(IRI.create("http://dl-learner.org/smallis/Allelic_info"));
103//        startClass = new Intersection(
104//                      new NamedClass("http://dl-learner.org/smallis/Allelic_info"),
105//                      new ObjectSomeRestriction(new ObjectProperty("http://dl-learner.org/smallis/has_phenotype"), Thing.instance));
106                ((CELOE) la).setStartClass(startClass);
107        logger.debug("finished initializing learning algorithm");
108        logger.debug("initializing operator...");
109        RhoDRDown op = new RhoDRDown();
110        op.setInstanceBasedDisjoints(true);
111        op.setUseNegation(false);
112        op.setUseHasValueConstructor(true);
113        op.setUseAllConstructor(false);
114        op.setStartClass(startClass);
115        op.setReasoner(rc);
116        op.setSubHierarchy(rc.getClassHierarchy());
117        op.setObjectPropertyHierarchy(rc.getObjectPropertyHierarchy());
118        op.setDataPropertyHierarchy(rc.getDatatypePropertyHierarchy());
119        op.init();
120        logger.debug("finished initializing operator");
121        ((CELOE) la).setOperator(op);
122        
123//        Set<Description> refinements = op.refine(d, d.getLength()+4);
124//        for (Description ref : refinements) {
125//                      System.out.println(ref + ":" + lp.getAccuracyOrTooWeak(ref, 1.0));
126//              }
127        
128//        SortedSet<Description> subClasses = rc.getSubClasses(Thing.instance);
129//        for (Description sub : subClasses) {
130//                      System.out.println(sub + ":" + rc.getIndividuals(sub).size() + " instances");
131//              }
132        
133//        
134//              OWLOntologyManager man = OWLManager.createOWLOntologyManager();
135//              OWLDataFactory dataFactory = man.getOWLDataFactory();
136//              OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
137//              OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ontology);
138//              reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
139//        NodeSet<OWLClass> subClasses2 = reasoner.getSuperClasses(
140//                      dataFactory.getOWLClass(IRI.create("http://purl.obolibrary.org/obo/MP_0001265")), false);
141//              for (OWLClass cls : subClasses2.getFlattened()) {
142//                      System.out.println(cls);
143//              }
144        
145        la.init();
146        la.start();
147    }
148    
149    public static Set<OWLIndividual> loadPosExamples() throws IOException {
150            return readExamples(posExamplesFilePath);
151    }
152    
153    public static Set<OWLIndividual> loadNegExamples() throws IOException {
154            return readExamples(negExamplesFilePath);
155    }
156    
157    public static Set<OWLIndividual> readExamples(String filePath) throws IOException {
158        Set<OWLIndividual> indivs = new TreeSet<>();
159        try(BufferedReader buffRead = new BufferedReader(new FileReader(new File(filePath)))){
160                String line;
161                while ((line = buffRead.readLine()) != null) {
162                    line = line.trim();
163                    line = line.substring(1, line.length()-1);  // strip off angle brackets
164                    indivs.add(new OWLNamedIndividualImpl(IRI.create(line)));
165                }
166        }
167        return indivs;
168    }
169    private static void setUp() {
170        logger.setLevel(Level.DEBUG);
171        Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF);
172        StringRenderer.setRenderer(Rendering.DL_SYNTAX);
173    }
174    
175    public static OWLOntology readCBD() {
176        logger.debug("reading CBD-based knowledge base (" + kbFilePath + ")...");
177        try(FileInputStream is = new FileInputStream(new File(MouseDiabetesCBD.cbdFilePath))){
178                OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(is);
179                logger.debug("finished reading files");
180                
181                if(containsErrorNamedClasses(ontology)){
182                        System.exit(0);
183                }
184                
185                return ontology;
186        } catch (IOException | OWLOntologyCreationException e) {
187                        e.printStackTrace();
188                }
189            return null;
190    }
191
192    public static OWLOntology readDumpFiles() throws IOException {
193        Model model = ModelFactory.createDefaultModel();
194
195                logger.debug("reading main knowledge base (" + kbFilePath + ")...");
196                try (InputStream is = new FileInputStream(new File(kbFilePath))) {
197                        model.read(is, null, Lang.RDFXML.getName());
198                }
199
200                logger.debug("reading positive examples data (" + genoDiseaseFilePath + ")...");
201                try (InputStream is = new FileInputStream(new File(genoDiseaseFilePath))) {
202                        model.read(is, null, Lang.TURTLE.getName());
203                }
204
205                logger.debug("reading negative examples data (" + genoNotDiseaseFilePath + ")...");
206                try (InputStream is = new FileInputStream(new File(genoNotDiseaseFilePath))) {
207                        model.read(is, null, Lang.TURTLE.getName());
208                }
209
210        logger.debug("finished reading files");
211
212        //convert JENA model to OWL API ontology
213        logger.debug("converting to OWLApi ontology...");
214        OWLOntology ontology = OwlApiJenaUtils.getOWLOntology(model);
215        logger.debug("finished conversion");
216        
217        // sanity check
218        if(containsErrorNamedClasses(ontology)){
219                System.exit(0);
220        }
221        return ontology;
222    }
223    
224    private static boolean containsErrorNamedClasses(OWLOntology ontology){
225        for (OWLClass cls : ontology.getClassesInSignature()) {
226                        if(cls.toStringID().startsWith("http://org.semanticweb.owlapi/error#")){
227                                return true;
228                        }
229                }
230        return false;
231    }
232}