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