How to use nanopub - 10 common examples

To help you get started, we’ve selected a few nanopub examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tetherless-world / whyis / importer.py View on Github external
def fetch(self, entity_name):
        u = self._get_access_url(entity_name)
        requests_session = requests.session()
        requests_session.mount('file://', LocalFileAdapter())
        requests_session.mount('file:///', LocalFileAdapter())
        r = requests_session.get(u, headers = self.headers, allow_redirects=True, stream=True)
        npub = nanopub.Nanopublication()
        if 'content-disposition' in r.headers:
            d = r.headers['content-disposition']
            fname = re.findall("filename=(.+)", d)
        else:
            fname = entity_name.split('/')[-1]
        content_type = r.headers.get('content-type')

        if self.file_types is not None:
            for file_type in self.file_types:
                npub.assertion.add((entity_name, rdflib.RDF.type, file_type))
        f = FileStorage(FileLikeFromIter(r.iter_content()), fname, content_type=content_type)
        old_nanopubs = self.app.add_file(f, entity_name, npub)
        npub.assertion.add((entity_name, self.app.NS.RDF.type, self.app.NS.pv.File))
        
        # old_np variable unused
        for _, old_np_assertion in old_nanopubs:
github tetherless-world / whyis / nanopub.py View on Github external
def retire(self, *nanopub_uris):
        self.db.store.nsBindings = {}
        graphs = []
        for nanopub_uri in nanopub_uris:
            for np_uri, assertion, provenance, pubinfo in self.db.query('''select ?np ?assertion ?provenance ?pubinfo where {
#    hint:Query hint:optimizer "Runtime" .
    ?np (np:hasAssertion/prov:wasDerivedFrom+/^np:hasAssertion)? ?retiree.
    ?np np:hasAssertion ?assertion;
        np:hasPublicationInfo ?pubinfo;
        np:hasProvenance ?provenance.
}''', initNs={"prov":prov, "np":np}, initBindings={"retiree":nanopub_uri}):
                print("Retiring", np_uri, "derived from", nanopub_uri)
                graphs.extend([np_uri, assertion, provenance, pubinfo])
                nanopub = Nanopublication(store=self.db.store, identifier=np_uri)
                self.db.remove((None,None,None,nanopub.assertion.identifier))
                self.db.remove((None,None,None,nanopub.provenance.identifier))
                self.db.remove((None,None,None,nanopub.pubinfo.identifier))
                self.db.remove((None,None,None,nanopub.identifier))
        self.db.commit()
        #data = [('c', c.n3()) for c in graphs]
        #session = requests.session()
        #session.keep_alive = False
        #session.delete(self.db.store.endpoint, data=[('c', c.n3()) for c in graphs])
        print("Retired %s nanopubs." % len(nanopub_uris))
github tetherless-world / whyis / whyis / autonomic / frir.py View on Github external
def process_nanopub(self, i, o, new_np):
        assertion = i.graph
        nanopub = Nanopublication(i.graph.store, i.identifier)
        quads = nanopub.serialize(format="nquads")
        i.identifier.split('/')[-1]
        fileid = self.app.nanopub_depot.create(quads, i.identifier.split('/')[-1]+'.nq', "application/n-quads")
        o.add(rdflib.RDF.type, whyis.ArchivedNanopublication)
        new_np.pubinfo.add((new_np.identifier, rdflib.RDF.type, whyis.FRIRNanopublication))
        expressions = dict([(part.identifier, self.expression_digest(part))
                            for part in [nanopub.assertion, nanopub.provenance, nanopub.pubinfo]])
        expressions[nanopub.identifier] = sum(expressions.values())
        nanopub_expression_uri = pexp[hex(expressions[nanopub.identifier])[2:]]
        for work, expression in expressions.items():
            exp = pexp[hex(expression)[2:]]
            o.graph.add((work, frbr.realization, exp))
            o.graph.add((work, rdflib.RDF.type, frbr.Work))
            o.graph.add((exp, rdflib.RDF.type, frbr.Expression))
        
        with self.app.nanopub_depot.get(fileid) as stored_file:
github tetherless-world / whyis / nanopub.py View on Github external
filename = secure_filename(localpart)
                    f = DataURLStorage(np_graph.value(entity, self.app.NS.whyis.hasContent), filename=filename)
                    print('adding file', filename)
                    self.app.add_file(f, entity, np_graph)
                    np_graph.remove((entity, self.app.NS.whyis.hasContent, None))
                
                
                for context in np_graph.contexts():
                    if (context.identifier,rdflib.RDF.type, np.Nanopublication) in context:
                        nanopub_uri = context.identifier
                    else:
                        continue
                    fileid = nanopub_uri.replace(self.prefix, "")
                    r = False
                    now = rdflib.Literal(datetime.utcnow())
                    nanopub = Nanopublication(identifier=nanopub_uri, store=np_graph.store)
                    for revised in nanopub.pubinfo.objects(nanopub.assertion.identifier, prov.wasRevisionOf):
                        for nanopub_uri in self.db.subjects(predicate=np.hasAssertion, object=revised):
                            nanopub.pubinfo.set((nanopub_uri, prov.invalidatedAtTime, now))
                            to_retire.append(nanopub_uri)
                            r = True
                            print("Retiring", nanopub_uri)
                    if r:
                        nanopub.pubinfo.set((nanopub.assertion.identifier, dc.modified, now))
                    else:
                        nanopub.pubinfo.set((nanopub.assertion.identifier, dc.created, now))
                    g = rdflib.ConjunctiveGraph()
                    for graph_part in [rdflib.Graph(identifier=x.identifier, store=x.store) for x in (nanopub, nanopub.assertion, nanopub.provenance, nanopub.pubinfo)]:
                        g_part = rdflib.Graph(identifier=graph_part.identifier, store=g.store)
                        for s,p,o in graph_part:
                            g_part.add((s,p,o))
github tetherless-world / whyis / whyis / commands / load_nanopub.py View on Github external
print("Could not find active nanopublication to revise:", was_revision_of)
                return
            was_revision_of = wasRevisionOf
        g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize(), store=temp_store)
        if temp_store == "Sleepycat":
            g_store_tempdir = tempfile.mkdtemp()
            g.store.open(g_store_tempdir, True)
        else:
            g_store_tempdir = None
        # g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize())

        try:
            g1 = g.parse(location=input_file, format=file_format, publicID=flask.current_app.NS.local)
            if len(list(g.subjects(rdflib.RDF.type, np.Nanopublication))) == 0:
                print("Could not find existing nanopublications.", len(g1), len(g))
                new_np = Nanopublication(store=g1.store)
                new_np.add((new_np.identifier, rdflib.RDF.type, np.Nanopublication))
                new_np.add((new_np.identifier, np.hasAssertion, g1.identifier))
                new_np.add((g1.identifier, rdflib.RDF.type, np.Assertion))

            nanopub_prepare_graph = rdflib.ConjunctiveGraph(store=temp_store)
            if temp_store == "Sleepycat":
                nanopub_prepare_graph_store_tempdir = tempfile.mkdtemp()
                nanopub_prepare_graph.store.open(nanopub_prepare_graph_store_tempdir, True)
            else:
                nanopub_prepare_graph_store_tempdir = None

            try:
                nanopubs = []
                for npub in flask.current_app.nanopub_manager.prepare(g, store=nanopub_prepare_graph.store):
                    if was_revision_of is not None:
                        for r in was_revision_of:
github tetherless-world / whyis / nanopub.py View on Github external
def prepare(self, graph, mappings = None, store=None):
        if mappings is None:
            mappings = {}
        new_nps = list(graph.subjects(rdflib.RDF.type, np.Nanopublication))
        if len(new_nps) == 0:
            for context in list(graph.contexts()):
                new_np = Nanopublication(store=context.store)
                new_np.add((new_np.identifier, rdflib.RDF.type, np.Nanopublication))
                new_np.add((new_np.identifier, np.hasAssertion, context.identifier))
                new_np.add((graph.identifier, rdflib.RDF.type, np.Assertion))
                new_nps.append(new_np.identifier)
        for nanopub in new_nps:
            if self.prefix not in nanopub:
                fileid = self._reserve_id()
                new_uri = self.prefix[fileid]
                mappings[nanopub] = new_uri
                for assertion in graph.objects(nanopub, np.hasAssertion):
                    mappings[assertion] = rdflib.URIRef(new_uri+"_assertion")
                for pubinfo in graph.objects(nanopub, np.hasPublicationInfo):
                    mappings[pubinfo] = rdflib.URIRef(new_uri+"_pubinfo")
                for provenance in graph.objects(nanopub, np.hasProvenance):
                    mappings[provenance] = rdflib.URIRef(new_uri+"_provenance")
github tetherless-world / whyis / nanopub.py View on Github external
def _get_nanopubs(self, graph):
        for nanopub_uri in graph.subjects(rdflib.RDF.type, np.Nanopublication):
            i += 1
            nanopub = Nanopublication(identifier=nanopub_uri)
            nanopub += graph.get_context(nanopub.identifier)
            #print "Nanopub", len(nanopub), len(output_graph.get_context(identifier=nanopub_uri))
            for assertion in graph.objects(nanopub.identifier, np.hasAssertion):
                a = nanopub.assertion
                a += graph.get_context(identifier=assertion)
                #print "Assertion", len(a), len(output_graph.get_context(identifier=assertion))
            for pubinfo in output_graph.objects(nanopub.identifier, np.hasPublicationInfo):
                p = nanopub.pubinfo
                p += graph.get_context(identifier=pubinfo)
                #print "PubInfo", len(p), len(output_graph.get_context(identifier=pubinfo))
            for provenance in output_graph.objects(nanopub.identifier, np.hasProvenance):
                p = nanopub.provenance
                p += graph.get_context(identifier=provenance)
                #print "Provenance", len(p), len(output_graph.get_context(identifier=provenance))
            yield nanopub
github tetherless-world / whyis / agents / nlp.py View on Github external
def process(self, i, o):
        document_count = float(self.document_count)
        
        query = """select distinct ?concept (count(distinct ?othernode) as ?count) ?assertion where {
  ?node sio:hasPart [ a sio:Term; prov:specializationOf ?concept].
  ?othernode sio:hasPart [ a sio:Term; prov:specializationOf ?concept].
  optional {
    graph ?assertion {
        ?concept sio:InverseDocumentFrequency ?idf.
    }
  }
} group by ?concept ?assertion"""
        for concept, count, assertion in self.app.db.query(query, initBindings=dict(node=i.identifier)):
            idf = log10(old_div(document_count,count.value))
            npub = nanopub.Nanopublication(store=o.graph.store)
            if assertion is not None:
                npub.pubinfo.add((npub.assertion.identifier, prov.wasRevisionOf, assertion))
            npub.assertion.add((concept, sio.InverseDocumentFrequency, Literal(idf)))
github tetherless-world / whyis / whyis / autonomic / deductor.py View on Github external
def process(self, i, o):
        npub = Nanopublication(store=o.graph.store)
        triples = self.app.db.query(
            '''CONSTRUCT {\n%s\n} WHERE {\n%s \nFILTER NOT EXISTS {\n%s\n\t}\nFILTER (regex(str(%s), "^(%s)")) .\n}''' % (
            self.construct, self.where, self.construct, self.resource, i.identifier), initNs=self.prefixes)
        for s, p, o, c in triples:
            print("Deductor Adding ", s, p, o)
            npub.assertion.add((s, p, o))
        npub.provenance.add((npub.assertion.identifier, prov.value,
                             rdflib.Literal(flask.render_template_string(self.explanation, **self.get_context(i)))))
github tetherless-world / whyis / nanopub.py View on Github external
f = self.depot.get(self._idmap[nanopub_uri])
        else:
            #try:
                fileid = nanopub_uri.replace(self.prefix, "", 1)
                f = self.depot.get(fileid)
            #except:
            #    try:
            #        fileid = self.db.value(nanopub_uri, dc.identifier)
            #        if fileid is not None:
            #            self._idmap[nanopub_uri] = fileid
            #        f = self.depot.get(fileid)
            #    except Exception as e:
            #        return None
        if graph is None:
            graph = rdflib.ConjunctiveGraph()
        nanopub = Nanopublication(store=graph.store, identifier=nanopub_uri)
        nanopub.parse(f, format="trig")
        try:
            f.close()
        except:
            pass
        return nanopub

nanopub

Python client for Nanopublications

Apache-2.0
Latest version published 10 months ago

Package Health Score

58 / 100
Full package analysis