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