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