Python client for Nanopub
Project description
nanopub
The nanopub
library provides a high-level, user-friendly python interface for searching, publishing and retracting nanopublications.
Setup
Install using pip:
pip install nanopub
To publish to the nanopub server you need to setup your profile. This allows the nanopub server to identify you. Run the following interactive command:
setup_profile
It will add and store RSA keys to sign your nanopublications, publish a nanopublication with your name and ORCID iD to declare that you are using using these RSA keys, and store your ORCID iD to automatically add as author to the provenance of any nanopublication you will publish using this library.
Quick Start
Publishing nanopublications
from nanopub import Publication, NanopubClient
from rdflib import Graph, URIRef, RDF, FOAF
# Create the client, that allows searching, fetching and publishing nanopubs
client = NanopubClient()
# Either quickly publish a statement to the server
client.claim('All cats are gray')
# Or: 1. construct a desired assertion (a graph of RDF triples)
my_assertion = Graph()
my_assertion.add( (URIRef('www.example.org/timbernerslee'), RDF.type, FOAF.Person) )
# 2. Make a Publication object with this assertion
publication = Publication.from_assertion(assertion_rdf=my_assertion)
# 3. Publish the Publication object. The URI at which it is published is returned.
publication_info = client.publish(publication)
print(publication_info)
Searching for nanopublications
from nanopub import NanopubClient
# Search for all nanopublications containing the text 'fair'
results = client.find_nanopubs_with_text('fair')
print(results)
# Search for nanopublications whose assertions contain triples that are ```rdf:Statement```s.
# Return only the first three results.
results = client.find_nanopubs_with_pattern(
pred='http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
obj='http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement',
max_num_results=3)
print(results)
# Search for nanopublications that introduce a concept that is a ```p-plan:Step```.
# Return only one result.
results = client.find_things('http://purl.org/net/p-plan#Step', max_num_results=1)
print(results)
Fetching nanopublications and inspecting them
# Fetch the nanopublication at the specified URI
np = client.fetch('http://purl.org/np/RApJG4fwj0szOMBMiYGmYvd5MCtRle6VbwkMJUb1SxxDM')
# Print the RDF contents of the nanopublication
print(np)
# Iterate through all triples in the assertion graph
for s, p, o in np.assertion:
print(s, p, o)
# Iterate through the publication info
for s, p, o in np.pubinfo:
print(s, p, o)
# Iterate through the provenance graph
for s, p, o in np.provenance:
print(s,p,o)
# See the concept that is introduced by this nanopublication (if any)
print(np.introduces_concept)
Specifying concepts relative to the nanopublication namespace
You can optionally specify that the Publication
introduces a particular concept using blank nodes.
The pubinfo graph will note that this nanopub npx:introduces the concept. The concept should be a blank node
(rdflib.term.BNode), and is converted to a URI derived from the nanopub's URI with a fragment (#) made from the blank
node's name.
from rdflib.term import BNode
publication = Publication.from_assertion(assertion_rdf=my_assertion,
introduces_concept=BNode('timbernerslee'))
Upon publication, any blank nodes in the rdf graph are replaced with the nanopub's URI, with the blank node name as a
fragment. For example, if the blank node is called 'step', that would result in a URI composed of the nanopub's (base)
URI, followed by #step. In case you are basing your publication on rdf that has a lot of concepts specific to this
nanopublication that are not blank nodes you could use replace_in_rdf
to easily replace them with blank nodes:
from nanopub import replace_in_rdf
replace_in_rdf(rdf=my_assertion, oldvalue=URIRef('www.example.org/timbernerslee'), newvalue=BNode('timbernerslee'))
Specifying derived_from
You can specify that the nanopub's assertion is derived from another URI (such as an existing nanopublication):
publication = Publication.from_assertion(assertion_rdf=my_assertion,
derived_from=rdflib.URIRef('www.example.org/another-nanopublication'))
Note that derived_from
may also be passed a list of URIs.
Dependencies
The nanopub
library currently uses the nanopub-java
tool for signing and publishing new nanopublications. This is automatically installed by the library.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file nanopub-0.2.9-py3-none-any.whl
.
File metadata
- Download URL: nanopub-0.2.9-py3-none-any.whl
- Upload date:
- Size: 20.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc49f4bdbe6208e1186501aaf54a0738799bd3e36eb03bce92808e94db4a1d25 |
|
MD5 | 5a92b677abf82031b878c6fd6c16ee86 |
|
BLAKE2b-256 | e7582ec0efd378f9acfc31553aea6d65cf5727c4d8308ad38ba5377d9d40ce32 |