| 1 | /* |
|---|
| 2 | * To change this license header, choose License Headers in Project Properties. |
|---|
| 3 | * To change this template file, choose Tools | Templates |
|---|
| 4 | * and open the template in the editor. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | package org.biohackathon.SPARQLBuilder.OWL; |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * |
|---|
| 12 | * @author atsuko |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | public class QueryPathGenerator { |
|---|
| 16 | private String sparqlEndpoint = null; |
|---|
| 17 | private RDFSchemaAnalyzerFactory factory = null; |
|---|
| 18 | private RDFSchemaAnalyzer analyzer = null; |
|---|
| 19 | //private OWLClassGraph graph; |
|---|
| 20 | //private PathMatrix matrix = null; |
|---|
| 21 | |
|---|
| 22 | private static final String CDIR = "cdata"; |
|---|
| 23 | |
|---|
| 24 | public static void main(String[] args){ |
|---|
| 25 | // For Test |
|---|
| 26 | //String sp = "http://data.allie.dbcls.jp/sparql"; |
|---|
| 27 | //String sc = "http://purl.org/allie/ontology/201108#ShortForm"; |
|---|
| 28 | //String ec = "http://purl.org/allie/ontology/201108#LongForm"; |
|---|
| 29 | |
|---|
| 30 | String sp = "http://www.ebi.ac.uk/rdf/services/chembl/sparql"; |
|---|
| 31 | String sc = "http://rdf.ebi.ac.uk/terms/chembl#Enzyme"; |
|---|
| 32 | String ec = "http://rdf.ebi.ac.uk/terms/chembl#Activity"; |
|---|
| 33 | |
|---|
| 34 | //String sp = "http://www.ebi.ac.uk/rdf/services/biosamples/sparql"; |
|---|
| 35 | //String sc = "http://rdf.ebi.ac.uk/terms/biosd/Sample"; |
|---|
| 36 | //String ec = "http://purl.obolibrary.org/obo/NCBITaxon_7955"; |
|---|
| 37 | //String sp = "http://www.ebi.ac.uk/rdf/services/biosamples/sparql"; |
|---|
| 38 | //String sc = "http://rdf.ebi.ac.uk/terms/biosd/Sample"; |
|---|
| 39 | //String ec = "http://purl.obolibrary.org/obo/NCBITaxon_7955"; |
|---|
| 40 | //String sp = "http://lsd.dbcls.jp/sparql"; |
|---|
| 41 | //String sc = "http://purl.jp/bio/10/lsd/ontology/201209#EnglishCode"; |
|---|
| 42 | //String ec = "http://purl.jp/bio/10/lsd/ontology/201209#JapaneseCode"; |
|---|
| 43 | //QueryPathGenerator qpg = new QueryPathGenerator(sp, "c:\\cdata"); |
|---|
| 44 | QueryPathGenerator qpg1 = new QueryPathGenerator(sp, "cdata/"); |
|---|
| 45 | //SClass[] cl = qpg.getClasses(null); |
|---|
| 46 | |
|---|
| 47 | long start1 = System.currentTimeMillis(); |
|---|
| 48 | Path[] path1 = qpg1.getPaths(sc, ec, false); |
|---|
| 49 | long end1 = System.currentTimeMillis(); |
|---|
| 50 | |
|---|
| 51 | QueryPathGenerator qpg2 = new QueryPathGenerator(sp, "ddata/"); |
|---|
| 52 | long start2 = System.currentTimeMillis(); |
|---|
| 53 | Path[] path2 = qpg2.getPaths(sc, ec, false); |
|---|
| 54 | long end2 = System.currentTimeMillis(); |
|---|
| 55 | System.out.println("TOTAL1:"); |
|---|
| 56 | System.out.println(end1 - start1); |
|---|
| 57 | System.out.println("TOTAL2:"); |
|---|
| 58 | System.out.println(end2 - start2); |
|---|
| 59 | |
|---|
| 60 | System.out.println(path1.length); |
|---|
| 61 | System.out.println(path2.length); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public QueryPathGenerator(){ |
|---|
| 65 | factory = new RDFSchemaAnalyzerFactory(CDIR); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public QueryPathGenerator(String sparqlEndpoint){ |
|---|
| 69 | factory = new RDFSchemaAnalyzerFactory(CDIR); |
|---|
| 70 | setSPARQLendpoint(sparqlEndpoint); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public QueryPathGenerator(String sparqlEndpoint, String crawlFileName){ |
|---|
| 74 | factory = new RDFSchemaAnalyzerFactory(crawlFileName); |
|---|
| 75 | setSPARQLendpoint(sparqlEndpoint); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public SClass[] getClasses(String keyword){ |
|---|
| 79 | String[] keywords = null; |
|---|
| 80 | if ( keyword != null ){ |
|---|
| 81 | if ( keyword.length() != 0 ){ |
|---|
| 82 | keywords = new String[1]; |
|---|
| 83 | keywords[0] = keyword; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | try { |
|---|
| 87 | return analyzer.getOWLClasses(null, keywords, null, true); |
|---|
| 88 | }catch(Exception e){ |
|---|
| 89 | System.err.println(e); |
|---|
| 90 | return null; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public Path[] getPaths(String startClass, String endClass, boolean countLink){ |
|---|
| 95 | if ( analyzer == null ){ |
|---|
| 96 | System.err.println("ERROR. SPARQL endpoint is not decided."); |
|---|
| 97 | } |
|---|
| 98 | OWLClassGraph graph = new OWLClassGraph(startClass, endClass); |
|---|
| 99 | return graph.getPaths(analyzer, countLink); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | public void setSPARQLendpoint(String sparqlEndpoint){ |
|---|
| 103 | this.sparqlEndpoint = sparqlEndpoint; |
|---|
| 104 | setAnalyzer(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public RDFSchemaAnalyzerFactory getFactory(){ |
|---|
| 108 | return factory; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | private void setAnalyzer(){ |
|---|
| 112 | //analyzer = new EndpointAnalyzer(sparqlEndpoint); //ForTest |
|---|
| 113 | |
|---|
| 114 | try { |
|---|
| 115 | analyzer = factory.create(sparqlEndpoint); |
|---|
| 116 | } catch (Exception e) { |
|---|
| 117 | System.err.println(e); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | public static String getClassLabel(String classURI, SClass[] classes){ |
|---|
| 122 | if ( classURI == null ){ |
|---|
| 123 | return ""; |
|---|
| 124 | } |
|---|
| 125 | for ( int i = 0 ; i < classes.length; i++ ){ |
|---|
| 126 | if ( classURI.equals(classes[i].getClassURI()) ){ |
|---|
| 127 | Label[] labels = classes[i].getLabels(); |
|---|
| 128 | for ( int j = 0 ; j < labels.length; j++ ){ |
|---|
| 129 | if ( labels[j].getLanguage() == null ){ |
|---|
| 130 | return labels[j].getLabel(); |
|---|
| 131 | }else if ( labels[j].getLanguage().equals("en") ){ |
|---|
| 132 | return labels[j].getLabel(); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | break; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | String[] url = classURI.split("/"); |
|---|
| 139 | String tmplabel = url[url.length-1]; |
|---|
| 140 | String[] tmplabel2 = tmplabel.split("#"); |
|---|
| 141 | String label = tmplabel2[tmplabel2.length-1]; |
|---|
| 142 | return label; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|