Last active
December 18, 2015 06:59
-
-
Save DannyArends/5742912 to your computer and use it in GitHub Desktop.
Some code to test 4store. Store r/qtl cross object as N3 formatted file. We then use 2 shell commands (printed by R): (1) converts n3 to rdf, (2) upload to the 4store database using curl. After this we can use: 'rdmd tripleStore.d' to query the triple store.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| *~ | |
| *.rdf | |
| *.n3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # CrossObject to N3 format | |
| crossToNTriplet <- function(cross, dataset = "default", file = ""){ | |
| cat("@prefix : <http://www.rqtl.org/ns/#> .\n", file=file) | |
| cat("@prefix individual: <http://www.rqtl.org/ns/individual#> .\n", file=file, append=TRUE) | |
| cat("@prefix marker: <http://www.rqtl.org/ns/marker#> .\n", file=file, append=TRUE) | |
| cat("@prefix location: <http://www.rqtl.org/ns/location#> .\n", file=file, append=TRUE) | |
| cat("@prefix phenotype: <http://www.rqtl.org/ns/phenotype#> .\n", file=file, append=TRUE) | |
| map <- pull.map(cross) | |
| for(chr in names(map)){ | |
| for(mar in names(map[[chr]])){ | |
| mname <- sub("/","_",mar) | |
| cat(paste0("marker:", mname," location:chr ", chr," .\n"), file=file, append=TRUE) | |
| cat(paste0("marker:", mname," location:cm ", map[[chr]][mar]," .\n"), file=file, append=TRUE) | |
| geno <- pull.geno(cross)[,mar] | |
| for(ind in 1:length(geno)){ | |
| cat(paste0("individual:", ind," marker:", mname," \"", geno[ind],"\" .\n"), file=file, append=TRUE) | |
| } | |
| } | |
| } | |
| for(phe in names(pull.pheno(multitrait))){ | |
| pheno <- pull.pheno(cross)[,phe] | |
| for(ind in 1:length(pheno)){ | |
| cat(paste0("individual:", ind," phenotype:", phe," \"", pheno[ind],"\" .\n"), file=file, append=TRUE) | |
| } | |
| } | |
| } | |
| # CrossObject to N3->RDF and upload to 4 store tripleStore | |
| crossTotripleStore <- function(cross, file="default", toRDF=TRUE, to4Store=TRUE, host = "localhost", port=9000){ | |
| rdfname <- paste0(file, ".rdf") | |
| crossToNTriplet(cross, dataset=file, file=paste0(file,".n3")) | |
| if(file != ""){ | |
| cat("For RDF and 4Store upload execute:\n") | |
| cat(paste0("cd ", getwd(), "\n")) | |
| if(toRDF) cat(paste0("rdfcat -in N3 -out RDF/XML -n ", file, ".n3 > ", rdfname,"\n")) | |
| if(to4Store) cat(paste0("curl -T '", rdfname, "' http://",host,":",port,"/data/", rdfname,"\n")) | |
| } | |
| } | |
| library(qtl) | |
| data(multitrait) | |
| crossTotripleStore(multitrait,"multitrait") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio, std.math, std.conv, std.socket, std.string; | |
| /** Triple store class object */ | |
| class TripleStore{ | |
| public: | |
| /** Constructor for the TripleStore object */ | |
| this(string h = "localhost", ushort p = 9000){ | |
| host = h; port = p; | |
| } | |
| /** Query the triple store with query string query, supported formats: text, json, sparql */ | |
| string query(string query, string format="text", bool noHeader = true, uint buffersize = 1024){ | |
| connect(); | |
| scope(exit) disconnect(); | |
| write("GET "~url~"?query="~query~"&output="~format~" "~protocol~"\r\nHost: "~host~"\r\n\r\n"); | |
| return readOutput(noHeader, buffersize); | |
| } | |
| /** Connect to a triple store */ | |
| bool connect(){ | |
| try{ | |
| if(handle is null) handle = new TcpSocket(); | |
| handle.connect(new InternetAddress(host, cast(int)port)); | |
| handle.blocking(true); | |
| }catch(SocketException ex){ | |
| writefln("Failed to connect to server (%s:%d)", host, port); | |
| handle = null; | |
| return false; | |
| } | |
| return true; | |
| } | |
| /** Disconnect from a triple store */ | |
| void disconnect(){ | |
| if(isAlive()){ handle.close; delete handle; } | |
| } | |
| private: | |
| bool write(string msg){ // Write a message to server | |
| if(!isAlive()) return false; | |
| auto ret = handle.send(msg); | |
| return (ret < 0); | |
| } | |
| bool isAlive(){ // Is our handle still alive | |
| if(handle !is null) return handle.isAlive; | |
| return false; | |
| } | |
| string readOutput(bool noHeader = true, uint buffersize = 1024){ // Read the output from the server | |
| if(!isAlive()) return null; | |
| string response; | |
| char[] buf = new char[buffersize]; | |
| int ret; | |
| while((ret = handle.receive(buf)) > 0){ | |
| response ~= to!string(buf[0 .. ret].dup); | |
| } | |
| if(noHeader) response = response[(response.indexOf("\r\n\r\n")+4) .. $]; | |
| return response; | |
| } | |
| string host = "localhost"; // Host to connect to | |
| string url = "/sparql/"; // Url prefix on host for sparql queries | |
| string protocol = "HTTP/1.0"; // HTTP version we want to use | |
| ushort port = 9000; // Port to connect on | |
| Socket handle = null; // Socket to server (called handle) | |
| } | |
| void main(string[] args){ | |
| TripleStore store = new TripleStore("localhost", 9000); | |
| string query = "SELECT * WHERE {?s ?p ?o} LIMIT 3"; // Default query | |
| string format = "text"; // Default format | |
| if(args.length > 1) query = args[1]; | |
| if(args.length > 2) format = args[2]; | |
| writefln("%s", store.query(query, format)); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment