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