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 | /* |
---|
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 | //qpg1.testOWLClassGraph(); |
---|
46 | //SClass[] cl = qpg.getClasses(null); |
---|
47 | }*/ |
---|
48 | |
---|
49 | public QueryPathGenerator(){ |
---|
50 | factory = new RDFSchemaAnalyzerFactory(CDIR); |
---|
51 | } |
---|
52 | |
---|
53 | public QueryPathGenerator(String sparqlEndpoint){ |
---|
54 | factory = new RDFSchemaAnalyzerFactory(CDIR); |
---|
55 | setSPARQLendpoint(sparqlEndpoint); |
---|
56 | setOWLClassGraph(); |
---|
57 | } |
---|
58 | |
---|
59 | public QueryPathGenerator(String sparqlEndpoint, String crawlFileName){ |
---|
60 | factory = new RDFSchemaAnalyzerFactory(crawlFileName); |
---|
61 | setSPARQLendpoint(sparqlEndpoint); |
---|
62 | setOWLClassGraph(); |
---|
63 | } |
---|
64 | |
---|
65 | public SClass[] getClasses(String keyword){ |
---|
66 | String[] keywords = null; |
---|
67 | if ( keyword != null ){ |
---|
68 | if ( keyword.length() != 0 ){ |
---|
69 | keywords = new String[1]; |
---|
70 | keywords[0] = keyword; |
---|
71 | } |
---|
72 | } |
---|
73 | try { |
---|
74 | return analyzer.getOWLClasses(null, keywords, null, true); |
---|
75 | }catch(Exception e){ |
---|
76 | System.err.println(e); |
---|
77 | return null; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public Path[] getPaths(String startClass, String endClass, boolean countLink){ |
---|
82 | if ( graph == null ){ |
---|
83 | setOWLClassGraph(); |
---|
84 | } |
---|
85 | return graph.getPaths(startClass, endClass); |
---|
86 | //return graph.getPaths_old(analyzer, true, startClass, endClass); |
---|
87 | } |
---|
88 | |
---|
89 | public void setSPARQLendpoint(String sparqlEndpoint){ |
---|
90 | this.sparqlEndpoint = sparqlEndpoint; |
---|
91 | setAnalyzer(); |
---|
92 | } |
---|
93 | |
---|
94 | public RDFSchemaAnalyzerFactory getFactory(){ |
---|
95 | return factory; |
---|
96 | } |
---|
97 | |
---|
98 | private void setAnalyzer(){ |
---|
99 | try { |
---|
100 | analyzer = factory.create(sparqlEndpoint); |
---|
101 | } catch (Exception e) { |
---|
102 | System.err.println(e); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | public static String getClassLabelfromList(String classURI, SClass[] classes){ |
---|
107 | if ( classURI == null ){ |
---|
108 | return ""; |
---|
109 | } |
---|
110 | SClass sclass = null; |
---|
111 | for ( int i = 0 ; i < classes.length; i++ ){ |
---|
112 | if ( classURI.equals(classes[i].getClassURI()) ){ |
---|
113 | return getClassLabelfromClass(classes[i]); |
---|
114 | } |
---|
115 | } |
---|
116 | return ""; |
---|
117 | } |
---|
118 | |
---|
119 | public static String getClassLabelfromClass(SClass sclass){ |
---|
120 | Label[] labels = sclass.getLabels(); |
---|
121 | for ( int i = 0 ; i < labels.length; i++ ){ |
---|
122 | if ( labels[i].getLanguage() == null ){ |
---|
123 | return labels[i].getLabel(); |
---|
124 | }else if ( labels[i].getLanguage().equals("en") ){ |
---|
125 | return labels[i].getLabel(); |
---|
126 | } |
---|
127 | } |
---|
128 | String[] url = sclass.getClassURI().split("/"); |
---|
129 | String tmplabel = url[url.length-1]; |
---|
130 | String[] tmplabel2 = tmplabel.split("#"); |
---|
131 | String label = tmplabel2[tmplabel2.length-1]; |
---|
132 | return label; |
---|
133 | } |
---|
134 | |
---|
135 | private void setOWLClassGraph(){ |
---|
136 | graph = new OWLClassGraph(analyzer); |
---|
137 | } |
---|
138 | |
---|
139 | public OWLClassGraph getOWLClassGraph(){ |
---|
140 | if ( graph == null ){ |
---|
141 | graph = new OWLClassGraph(analyzer); |
---|
142 | } |
---|
143 | return graph; |
---|
144 | } |
---|
145 | } |
---|