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