Ejemplo n.º 1
0
 def fetch(self, entity_name):
     u = self._get_access_url(entity_name)
     print(u)
     r = requests.get(u, headers=self.headers, allow_redirects=True)
     g = rdflib.Dataset()
     local = g.graph(rdflib.BNode())
     text = r.text
     for pattern, repl in self.replace:
         text = re.sub(pattern, repl, text)
     print(text)
     local.parse(data=text, format=self.format)
     # print self.postprocess_update
     if self.postprocess_update is not None:
         commands = self.postprocess_update
         if isinstance(commands, str):
             commands = [commands]
         for command in commands:
             # print "update postprocess query."
             g.update(command)
     if self.postprocess is not None:
         print("postprocessing", entity_name)
         p = self.postprocess
         p(g)
     # print g.serialize(format="trig")
     return rdflib.ConjunctiveGraph(identifier=local.identifier,
                                    store=g.store)
Ejemplo n.º 2
0
 def process_graph(self, inputGraph):
     instances = self.getInstances(inputGraph)
     results = []
     for i in instances:
         print("Processing", i.identifier, self)
         output_nanopub = Nanopublication()
         o = output_nanopub.assertion.resource(
             i.identifier)  # OutputClass(i.identifier)
         error = False
         try:
             result = self.process_nanopub(i, o, output_nanopub)
         except Exception as e:
             output_nanopub.add(
                 (output_nanopub.assertion.identifier,
                  self.app.NS.sioc.content, rdflib.Literal(str(e))))
             logging.exception(
                 "Error processing resource %s in nanopub %s" %
                 (i.identifier, inputGraph.identifier))
             error = True
         print("Output Graph", output_nanopub.identifier,
               len(output_nanopub))
         for new_np in self.app.nanopub_manager.prepare(
                 rdflib.ConjunctiveGraph(store=output_nanopub.store)):
             if len(new_np.assertion) == 0 and not error:
                 continue
             self.explain(new_np, i, o)
             new_np.add((new_np.identifier, sio.isAbout, i.identifier))
             # print new_np.serialize(format="trig")
             if not self.dry_run:
                 self.app.nanopub_manager.publish(new_np)
             results.append(new_np)
     return results
Ejemplo n.º 3
0
 def __init__(self, store, prefix, app, update_listener=None):
     self.db = rdflib.ConjunctiveGraph(store)
     self.store = store
     self.app = app
     self.depot = DepotManager.get('nanopublications')
     self.prefix = rdflib.Namespace(prefix)
     self.update_listener = update_listener
Ejemplo n.º 4
0
def create_annotation_graph_ressource_version(path_to_annot_graphs_dir, version, ressource_name, desc, title, sources):
    """
    This function is used to create the ressource_info file associated to the version of the created annotation_graph.
    - path_to_annot_graphs_dir: a path to annotation graphs directory
    - version: the version of the annotation graph
    - ressource_name: the name of the associated ressource for the annotation graph
    - desc: a description of the graph
    - title: a title for the graph
    - sources: a list of uris which vere used to build the annotation graph
    """
    ressource_version = Database_ressource_version(ressource = ressource_name, version = version)
    n_triples = 0
    subjects = set()
    for annot_graph in os.listdir(path_to_annot_graphs_dir):
        if not annot_graph.endswith(".ttl"):
            continue
        if annot_graph == "void.ttl":
            continue
        sub_g = rdflib.ConjunctiveGraph()
        sub_g.parse(path_to_annot_graphs_dir + annot_graph, format = 'turtle')
        n_triples += len(sub_g)
        subjects = subjects.union(set([s for s in sub_g.subjects()]))
        ressource_version.add_DataDump(annot_graph)
    for source in sources:
        ressource_version.add_version_attribute(DCTERMS["source"], rdflib.URIRef(source))
    ressource_version.add_version_attribute(DCTERMS["description"], rdflib.Literal(desc))
    ressource_version.add_version_attribute(DCTERMS["title"], rdflib.Literal(title))
    ressource_version.add_version_attribute(VOID["triples"], rdflib.Literal(n_triples, datatype=XSD.long ))
    ressource_version.add_version_attribute(VOID["distinctSubjects"], rdflib.Literal(len(subjects), datatype=XSD.long ))
    ressource_version.version_graph.serialize(destination=path_to_annot_graphs_dir + "void.ttl", format = 'turtle')
Ejemplo n.º 5
0
def catalyst_graph_for(file):
    if file.startswith('/'):
        file = 'file://'+file
    logging.info("InferenceStore catalyst_graph_for started")
        
    # quads = jsonld.to_rdf(file, {'format': 'application/nquads'})
    logging.info("InferenceStore JSON-LD loaded")

    g = ConjunctiveGraph()
    g.namespace_manager = namespace_manager
    # g.parse(data=quads, format='nquads')
    g.load(file, format="json-ld")
    logging.info("InferenceStore base graph loaded")

    f = FuXiInferenceStore.get_instance()

    # get the inference engine
    cl = f.get_inference(g)
    logging.info("InferenceStore inference graph loaded")

    union_g = rdflib.ConjunctiveGraph()

    for s,p,o in g.triples( (None, None, None) ):
       union_g.add( (s,p,o) )

    for s,p,o in cl.triples( (None, None, None) ):
       union_g.add( (s,p,o) )

    logging.info("InferenceStore union graph prepared")

    return union_g
Ejemplo n.º 6
0
 def get(self, nanopub_uri, graph=None):
     nanopub_uri = rdflib.URIRef(nanopub_uri)
     f = None
     if nanopub_uri in self._idmap:
         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
Ejemplo n.º 7
0
 def __init__(self, store, archive_path, prefix):
     self.db = rdflib.ConjunctiveGraph(store)
     self.store = store
     self.archive_path = archive_path
     self.prefix = rdflib.Namespace(prefix)
     if not os.path.exists(self.archive_path):
         os.makedirs(self.archive_path)
Ejemplo n.º 8
0
def write_ttl_news(entries, out_file, template=None, subject_uri=None):
    """Write NEWS in Turtle format"""
    import rdflib
    import rdflib.namespace
    import rdflib.resource
    import datetime

    # Set up namespaces and make a graph for the output
    doap = rdflib.Namespace("http://usefulinc.com/ns/doap#")
    dcs = rdflib.Namespace("http://ontologi.es/doap-changeset#")
    rdfs = rdflib.Namespace("http://www.w3.org/2000/01/rdf-schema#")
    rdf = rdflib.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
    xsd = rdflib.Namespace("http://www.w3.org/2001/XMLSchema#")
    g = rdflib.ConjunctiveGraph()
    ns = rdflib.namespace.NamespaceManager(g)
    ns.bind("doap", doap)
    ns.bind("dcs", dcs)

    # Load given template file
    if template is not None:
        g.load(template, format="turtle")

    if subject_uri is not None:
        # Use given subject uri
        subject = rdflib.URIRef(subject_uri)
        g.add((subject, rdf.type, doap.Project))
    else:
        # Find project URI to use as subject, and optionally the maintainer
        subject = g.value(None, rdf.type, doap.Project)
        ensure(subject is not None, "Unable to find project URI for subject")

    maintainer = g.value(subject, doap.maintainer, None)

    for r, e in entries.items():
        semver = parse_version(e["revision"])
        ver_string = "%03d%03d%03d" % semver

        release = rdflib.BNode("r%s" % ver_string)
        g.add((subject, doap.release, release))
        g.add((release, doap.revision, rdflib.Literal(e["revision"])))

        if "dist" in e:
            g.add((release, doap["file-release"], rdflib.URIRef(e["dist"])))

        utc_date = e["date"].astimezone(datetime.timezone.utc)
        date_str = utc_date.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
        time = rdflib.Literal(date_str, datatype=xsd.dateTime, normalize=False)
        g.add((release, doap.created, time))

        if maintainer is not None:
            g.add((release, dcs.blame, maintainer))

        changeset = rdflib.BNode("c%s" % ver_string)
        g.add((release, dcs.changeset, changeset))
        for index, item in enumerate(e["items"]):
            item_node = rdflib.BNode("i%s%08d" % (ver_string, index))
            g.add((changeset, dcs.item, item_node))
            g.add((item_node, rdfs.label, rdflib.Literal(item)))

    g.serialize(out_file, format="turtle")
Ejemplo n.º 9
0
    def testAll(self):
        for rdf_processor in self._ALL:
            endpoint = mock.Mock(spec=humfrey.sparql.endpoint.Endpoint)
            graph = rdflib.ConjunctiveGraph()
            doc_uri = rdflib.URIRef('http://example.org/doc/Foo')
            subject_uri = rdflib.URIRef('http://example.org/id/Foo')
            subject = resource.Resource(subject_uri, graph, endpoint)

            #import pdb;pdb.set_trace()
            doc_view = views.DocView()
            renderers = Conneg(obj=doc_view).renderers

            request = self.factory.get('')

            doc_view.context = {
                'graph': graph,
                'doc_uri': doc_uri,
                'subject_uri': subject_uri,
                'subject': subject,
                'endpoint': endpoint
            }
            doc_view.context['renderers'] = [
                doc_view.renderer_for_context(request, renderer)
                for renderer in renderers
            ]

            rdf_processor(request=request, context=doc_view.context)

            self.assertFalse(
                endpoint.query.called,
                "The RDF processor should not be touching the endpoint (at the moment)"
            )
            self.check_valid_terms(graph)
            self.assertIsInstance(doc_view.context, (type(None), dict))
Ejemplo n.º 10
0
    def load_sparql(self, sparql_endpoint, verbose=False, hide_base_schemas=True, credentials=None):
        """
        Set up a SPARQLStore backend as a virtual ontospy graph

        Note: we're using a 'SPARQLUpdateStore' backend instead of 'SPARQLStore' cause otherwise authentication fails (https://github.com/RDFLib/rdflib/issues/755)

        """
        try:
            # graph = rdflib.ConjunctiveGraph('SPARQLStore')
            graph = rdflib.ConjunctiveGraph('SPARQLUpdateStore')

            if credentials and type(credentials) == tuple:
                # https://github.com/RDFLib/rdflib/issues/343
                graph.store.setCredentials(credentials[0], credentials[1])
                # graph.store.setHTTPAuth('BASIC') # graph.store.setHTTPAuth('DIGEST')

            graph.open(sparql_endpoint)
            self.rdflib_graph = graph
            self.sparql_endpoint = sparql_endpoint
            self.sources = [sparql_endpoint]
            self.sparqlHelper = SparqlHelper(self.rdflib_graph, self.sparql_endpoint)
            self.namespaces = sorted(self.rdflib_graph.namespaces())
        except:
            printDebug("Error trying to connect to Endpoint.")
            raise
Ejemplo n.º 11
0
    def test_json_to_ttl_match(self):
        json_files = sorted(
            glob(os.path.join(os.path.dirname(__file__), 'json', '*.json')))

        # invalid round trip files
        skip = list(range(352, 380))

        # invalid literal set representation e.g., set((1, True))
        skip_match = [5, 6, 7, 8, 15, 27, 28, 29, 75, 76, 77, 78, 79, 80, 260,
                      261, 262, 263, 264,
                      306, 313, 315, 317, 322, 323, 324, 325, 330, 332, 344,
                      346, 382, 389, 395, 397,
                      ]
        for idx, fname in enumerate(json_files):
            _, ttl_file = os.path.split(fname)
            ttl_file = os.path.join(os.path.dirname(__file__), 'rdf',
                                    ttl_file.replace('json', 'ttl'))
            error_raised = False
            try:
                g = pm.ProvDocument.deserialize(fname)
                if len(g.bundles) == 0:
                    format = 'turtle'
                else:
                    format = 'trig'
                if format == 'trig':
                    ttl_file = ttl_file.replace('ttl', 'trig')

                with open(ttl_file, 'rb') as fp:
                    g_rdf = rl.ConjunctiveGraph().parse(fp, format=format)
                g0_rdf = rl.ConjunctiveGraph().parse(
                    StringIO(g.serialize(format='rdf', rdf_format=format)),
                    format=format)
                if idx not in skip_match:
                    self.assertTrue(find_diff(g_rdf, g0_rdf)[0])
                else:
                    logger.info('Skipping match: %s' % fname)
                if idx in skip:
                    logger.info('Skipping deserialization: %s' % fname)
                    continue
                g1 = pm.ProvDocument.deserialize(
                    content=g.serialize(format='rdf', rdf_format=format),
                    format='rdf', rdf_format=format)
            except Exception as e:
                print(e)
                logger.info(e)
                error_raised = True
            self.assertFalse(error_raised)
Ejemplo n.º 12
0
    def testGetNoReasoning(self):
        graph = rdflib.ConjunctiveGraph(getStore())
        grrm2.EquilibriumStructure.new(graph, "http://example.com/eq1")
        grrm2.TransitionState.new(graph, "http://example.com/ts1")
        grrm2.BarrierlessDissociated.new(graph,
                                         "http://example.com/blessdiss1")
        grrm2.BarrierDissociated.new(graph, "http://example.com/bdiss1")
        grrm2.InterconversionStep.new(graph, "http://example.com/ic1")
        grrm2.Interconversion.new(graph, "http://example.com/iconv1")
        grrm2.RunData.new(graph, "http://example.com/rundata1")
        grrm2.Run.new(graph, "http://example.com/run1")
        grrm2.Molecule.new(graph, "http://example.com/mol1")
        grrm2.RunInput.new(graph, "http://example.com/runin1")
        grrm2.RunOutput.new(graph, "http://example.com/runout1")
        grrm2.InterconversionResult.new(graph, "http://example.com/icres1")

        self.assertEqual(
            grrm2.EquilibriumStructure.get(graph,
                                           "http://example.com/eq1").__class__,
            grrm2.EquilibriumStructure)
        self.assertEqual(
            grrm2.TransitionState.get(graph,
                                      "http://example.com/ts1").__class__,
            grrm2.TransitionState)
        self.assertEqual(
            grrm2.BarrierlessDissociated.get(
                graph, "http://example.com/blessdiss1").__class__,
            grrm2.BarrierlessDissociated)
        self.assertEqual(
            grrm2.BarrierDissociated.get(
                graph, "http://example.com/bdiss1").__class__,
            grrm2.BarrierDissociated)
        self.assertEqual(
            grrm2.InterconversionStep.get(graph,
                                          "http://example.com/ic1").__class__,
            grrm2.InterconversionStep)
        self.assertEqual(
            grrm2.Interconversion.get(graph,
                                      "http://example.com/iconv1").__class__,
            grrm2.Interconversion)
        self.assertEqual(
            grrm2.RunData.get(graph, "http://example.com/rundata1").__class__,
            grrm2.RunData)
        self.assertEqual(
            grrm2.Run.get(graph, "http://example.com/run1").__class__,
            grrm2.Run)
        self.assertEqual(
            grrm2.Molecule.get(graph, "http://example.com/mol1").__class__,
            grrm2.Molecule)
        self.assertEqual(
            grrm2.RunInput.get(graph, "http://example.com/runin1").__class__,
            grrm2.RunInput)
        self.assertEqual(
            grrm2.RunOutput.get(graph, "http://example.com/runout1").__class__,
            grrm2.RunOutput)
        self.assertEqual(
            grrm2.InterconversionResult.get(
                graph, "http://example.com/icres1").__class__,
            grrm2.InterconversionResult)
Ejemplo n.º 13
0
def write_news(name, in_files, out_file):
    import rdflib
    import textwrap
    from time import strftime, strptime

    doap = rdflib.Namespace('http://usefulinc.com/ns/doap#')
    dcs = rdflib.Namespace('http://ontologi.es/doap-changeset#')
    rdfs = rdflib.Namespace('http://www.w3.org/2000/01/rdf-schema#')
    foaf = rdflib.Namespace('http://xmlns.com/foaf/0.1/')
    rdf = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
    m = rdflib.ConjunctiveGraph()

    try:
        for i in in_files:
            m.parse(i, format='n3')
    except:
        Logs.warn('Error parsing data, unable to generate NEWS')
        return

    proj = m.value(None, rdf.type, doap.Project)
    for f in m.triples([proj, rdfs.seeAlso, None]):
        if f[2].endswith('.ttl'):
            m.parse(f[2], format='n3')

    entries = {}
    for r in m.triples([proj, doap.release, None]):
        release = r[2]
        revision = m.value(release, doap.revision, None)
        date = m.value(release, doap.created, None)
        blamee = m.value(release, dcs.blame, None)
        changeset = m.value(release, dcs.changeset, None)

        if revision and date and blamee and changeset:
            entry = '%s (%s) stable;\n' % (name, revision)

            for i in m.triples([changeset, dcs.item, None]):
                entry += '\n  * ' + '\n    '.join(
                    textwrap.wrap(m.value(i[2], rdfs.label, None), width=79))

            entry += '\n\n --'

            blamee_name = m.value(blamee, foaf.name, None)
            blamee_mbox = m.value(blamee, foaf.mbox, None)
            if blamee_name and blamee_mbox:
                entry += ' %s <%s>' % (blamee_name,
                                       blamee_mbox.replace('mailto:', ''))

            entry += '  %s\n\n' % (strftime('%a, %d %b %Y %H:%M:%S +0000',
                                            strptime(date, '%Y-%m-%d')))

            entries[revision] = entry
        else:
            Logs.warn('Ignored incomplete %s release description' % name)

    if len(entries) > 0:
        news = open(out_file, 'w')
        for e in sorted(entries.keys(), reverse=True):
            news.write(entries[e])
        news.close()
Ejemplo n.º 14
0
 def __init__(self, graph=None):
     """
     @brief      Manages an ontology using rdflib
     """
     if graph is not None:
         self._ontology = graph
     else:
         self._ontology = rdflib.ConjunctiveGraph()  # store='Sleepycat' #TODO:
Ejemplo n.º 15
0
def modfy(bundle):
    manifest_path = os.path.join(bundle, 'manifest.ttl')
    resources = os.path.join(os.path.realpath(bundle), 'modgui')
    if not os.path.exists(resources):
        os.mkdir(resources)
    resources = 'file://%s' % resources


    rdfschema = rdflib.Namespace('http://www.w3.org/2000/01/rdf-schema#')
    rdfsyntax = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
    lv2core = rdflib.Namespace('http://lv2plug.in/ns/lv2core#')
    mod = rdflib.Namespace('http://portalmod.com/ns/modgui#')

    manifest = rdflib.ConjunctiveGraph()
    manifest.parse(manifest_path, format='n3')

    for triple in manifest.triples((None, rdfsyntax.type, lv2core.Plugin)):
        url = triple[0]
        for triple in manifest.triples((url, rdfschema.seeAlso, None)):
            ttl_path = triple[2]
            plugin_name = ttl_path.split('/')[-1].replace('.ttl', '')
            plugin = rdflib.ConjunctiveGraph()
            plugin.parse(ttl_path, format='n3')
            try:
                modgui = list(plugin.triples(url, rdfsyntax.type, mod.GUI))[0]
            except:
                pass
            else:
                continue

            for triple in plugin.triples((None, None, None)):
                if triple[0].startswith('http://portalmod.com/ns/extensions/effect#'):
                    plugin.remove(triple)
            gui = rdflib.BNode()
            uri = rdflib.term.URIRef
            resources = 'modgui'
            plugin.add((url, mod.gui, gui))
            plugin.add((gui, mod.resourcesDirectory, uri(resources)))
            plugin.add((gui, mod.iconTemplate, uri('%s/%s.html' % (resources, plugin_name))))
            plugin.add((gui, mod.templateData, uri('%s/%s.json' % (resources, plugin_name))))
            plugin.add((gui, mod.screenshot, uri('%s/%s.png' % (resources, plugin_name))))
            plugin.add((gui, mod.thumbnail, uri('%s/%s-thumb.png' % (resources, plugin_name))))

            plugin.namespace_manager.bind('mod', mod)

            plugin.serialize(ttl_path, format='n3')
Ejemplo n.º 16
0
def query_virtuoso(q):
    endpoint = virtuoso_address
    store = sparqlstore.SPARQLUpdateStore(endpoint)
    gs = rdflib.ConjunctiveGraph(store)
    gs.open((endpoint, endpoint))
    gs1 = gs.get_context(rdflib.URIRef(virtuoso_graph_uri))
    res = gs1.query(q)
    return res
Ejemplo n.º 17
0
 def testParser(self):
     graph = rdflib.ConjunctiveGraph()
     p = parsinghelpers.OutputTSFileParser(
         graph,
         os.path.join(os.path.dirname(__file__), "formaldehyde-newnames",
                      "grrm_HCHO_TS_list.log"))
     self.assertEqual(len(p.moleculeUris()), 9)
     print graph.serialize()
Ejemplo n.º 18
0
 def __init__(self):
     self.relations = []
     self.attributes = []
     self.bnodes = []
     store = TripleIteratorStore(self._handle_attribute_triple,
                                 self._handle_relation_triple,
                                 self._handle_bnode_triple)
     self.graph = rdflib.ConjunctiveGraph(store)
Ejemplo n.º 19
0
def query_from_files(kb, goal, nokbdbg, nolog):
	import pyin
	pyin.nolog = nolog
	pyin.nokbdbg = nokbdbg
#	kb=  open('kb_for_external.nq', 'rb')
#	goal=open('query_for_external.nq', 'rb')

	kb_stream, goal_stream = kb, goal

	implies = rdflib.URIRef("http://www.w3.org/2000/10/swap/log#implies")

	store = OrderedStore()
	kb_graph = rdflib.Graph(store=store, identifier='@default')
	kb_conjunctive = rdflib.ConjunctiveGraph(store=store)
	kb_graph.parse(kb_stream, format='nquads')
	#exit()
	rules = []

	"""
	--------
	kb:
	?x y ?z. 
	query:
	...
	--------
	--------
	?x y ?z. 
	?z w a.
	--------
	
	
	"""

	for s,p,o in kb_graph.triples((None, None, None)):
		_t = Triple(p, [s, o])
		rules.append(Rule(kb_graph, _t, Graph()))
		if p == implies:
			head_triples = kb_conjunctive.triples((None, None, None), o)
			for head_triple in head_triples:
				body = Graph()
				for body_triple in kb_conjunctive.triples((None, None, None), s):
					body.append(Triple((body_triple[1]), [(body_triple[0]), (body_triple[2])]))
				rules.append(Rule(head_triples, Triple((head_triple[1]), [(head_triple[0]), (head_triple[2])]), body))

	goal_rdflib_graph = rdflib.Graph(store=OrderedStore())

	goal_rdflib_graph.parse(goal_stream, format='nquads')

	goal = Graph()
	for s,p,o in goal_rdflib_graph.triples((None, None, None)):
		goal.append(Triple((p), [(s), (o)]))

	for result in query(rules, goal):
		print ()
		o = ' RESULT : '
		for triple in result:
			o += str(triple)
		print (o)
Ejemplo n.º 20
0
def extract_CID_InchiKey(pmids_cids_graph_list, inchikeys_graph_list,
                         path_out):
    # Inti output file
    with open(path_out, "w") as out:
        out_writer = csv.writer(out, delimiter=',')
        m = out_writer.writerow(['CID', 'INCHIKEY'])
    # Init variables
    available_cids = set()
    for pmid_cid_f_input in pmids_cids_graph_list:
        # release memory
        g_pmid_cid = None
        # Import pmid_cid graph
        print("Importing " + pmid_cid_f_input + " ...", end='')
        g_pmid_cid = rdflib.ConjunctiveGraph()
        with gzip.open(pmid_cid_f_input, "rb") as f:
            g_pmid_cid.parse(f, format="turtle")
        # Get all objects
        extracted_objects = [
            uri.toPython().split(
                'http://rdf.ncbi.nlm.nih.gov/pubchem/compound/CID')[1]
            for uri in list(g_pmid_cid.objects())
        ]
        available_cids = available_cids.union(extracted_objects)
        print(" Ok")
    # Then, we browse inchikey files to select CID - inchikey association for which the CID has an associated corpus
    for inchikey_f_input in inchikeys_graph_list:
        g_inchikey = None
        g_inchikey = rdflib.Graph()
        print("treating file " + inchikey_f_input + " ...", end='')
        # Add InchiKeys triples to the graph
        with gzip.open(inchikey_f_input, "rb") as f:
            g_inchikey.parse(f, format="turtle")
        # Get cid - inchikey associations
        cids_inchikeys = list(
            g_inchikey.subject_objects(
                rdflib.URIRef(
                    "http://semanticscience.org/resource/is-attribute-of")))
        inchikeys = [
            cid_inchikey[0].toPython().split(
                "http://rdf.ncbi.nlm.nih.gov/pubchem/inchikey/")[1]
            for cid_inchikey in cids_inchikeys
        ]
        cids = [
            cid_inchikey[1].toPython().split(
                "http://rdf.ncbi.nlm.nih.gov/pubchem/compound/CID")[1]
            for cid_inchikey in cids_inchikeys
        ]
        with open(path_out, "a") as out:
            out_writer = csv.writer(out, delimiter=',')
            for cid_index in range(0, len(cids)):
                if cids[cid_index] in available_cids:
                    m = out_writer.writerow(
                        [cids[cid_index], inchikeys[cid_index]])
        print(" Ok")
    # Release all memory
    g_inchikey = None
    g_pmid_cid = None
    print("End procedure CID - InchiKeys associations !")
Ejemplo n.º 21
0
def _build_cache(source_file, skip_cache=False):
    """Builds the cached data by parsing the RDF taxonomy file or a
    vocabulary file.
    @var source_file: source file of the taxonomy, RDF file
    @keyword skip_cache: boolean, if True, build cache will not be
        saved (pickled) - it is saved as <source_file.db> """

    if rdflib:
        if rdflib.__version__ >= '2.3.2':
            store = rdflib.ConjunctiveGraph()
        else:
            store = rdflib.Graph()
    else:
        store = None


    if skip_cache:
        log.info("You requested not to save the cache to disk.")
    else:
        cache_path = _get_cache_path(source_file)
        cache_dir = os.path.dirname(cache_path)
        # Make sure we have a cache_dir readable and writable.
        try:
            os.makedirs(cache_dir)
        except:
            pass
        if os.access(cache_dir, os.R_OK):
            if not os.access(cache_dir, os.W_OK):
                log.error("Cache directory exists but is not writable. Check your permissions for: %s" % cache_dir)
                raise Exception("Cache directory exists but is not writable. Check your permissions for: %s" % cache_dir)
        else:
            log.error("Cache directory does not exist (and could not be created): %s" % cache_dir)
            raise Exception("Cache directory does not exist (and could not be created): %s" % cache_dir)


    timer_start = time.clock()

    namespace = None
    single_keywords, composite_keywords = {}, {}

    try:
        if not rdflib:
            import rdflib as rdflib_reimport

        log.info("Building RDFLib's conjunctive graph from: %s" % source_file)
        try:
            store.parse(source_file)
        except urllib2.URLError, exc:
            if source_file[0] == '/':
                store.parse("file://" + source_file)
            else:
                store.parse("file:///" + source_file)

    except rdflib_exceptions_Error, e:
        log.error("Serious error reading RDF file")
        log.error(e)
        log.error(traceback.format_exc())
        raise rdflib.exceptions.Error(e)
Ejemplo n.º 22
0
    def run(self, input_file, file_format="trig", was_revision_of=None):
        flask.current_app.managed = True
        if was_revision_of is not None:
            wasRevisionOf = set(
                flask.current_app.db.objects(
                    predicate=np.hasAssertion,
                    subject=rdflib.URIRef(was_revision_of)))
            if len(wasRevisionOf) == 0:
                print("Could not find active nanopublication to revise:",
                      was_revision_of)
                return
            was_revision_of = wasRevisionOf
        g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize(),
                                    store="Sleepycat")
        graph_tempdir = tempfile.mkdtemp()
        g.store.open(graph_tempdir, True)
        # g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize())

        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="Sleepycat")
        nanopub_prepare_graph_tempdir = tempfile.mkdtemp()
        nanopub_prepare_graph.store.open(nanopub_prepare_graph_tempdir, True)
        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:
                    print("Marking as revision of", r)
                    npub.pubinfo.add(
                        (npub.assertion.identifier,
                         flask.current_app.NS.prov.wasRevisionOf, r))
            print('Prepared', npub.identifier)
            nanopubs.append(npub)
        flask.current_app.nanopub_manager.publish(*nanopubs)
        print("Published", npub.identifier)
Ejemplo n.º 23
0
def CREATE_GRAPH():
    """
    Create the global graph.
    """
    global __global_graph__
    try:
        __global_graph__ = rdflib.ConjunctiveGraph()
    except:
        __global_graph__ = rdflib.graph.ConjunctiveGraph()
Ejemplo n.º 24
0
def test_fail_on_non_empty_target(dirs):
    d = Descriptor('test')
    g = rdflib.ConjunctiveGraph()
    bi = Installer(*dirs, graph=g)
    bundles_directory = dirs[1]
    sma = p(bundles_directory, 'test', '1', 'blah')
    makedirs(sma)
    with pytest.raises(TargetIsNotEmpty):
        bi.install(d)
Ejemplo n.º 25
0
 def testRememberNamespace(self):
     g = rdflib.ConjunctiveGraph()
     g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"), ))
     # In 4.2.0 the first serialization would fail to include the
     # prefix for the graph but later serialize() calls would work.
     first_out = g.serialize(format="trig", encoding="latin-1")
     second_out = g.serialize(format="trig", encoding="latin-1")
     self.assertTrue(b"@prefix ns1: <http://example.com/> ." in second_out)
     self.assertTrue(b"@prefix ns1: <http://example.com/> ." in first_out)
Ejemplo n.º 26
0
 def test_load1(self):
     """ Put the appropriate triples in. Try to load them """
     g = R.ConjunctiveGraph()
     self.config['rdf.graph'] = g
     for t in self.trips:
         g.add(t)
     c = self.ctx.Connection(conf=self.config)
     for x in c.load():
         self.assertIsInstance(x, Connection)
Ejemplo n.º 27
0
 def triples(self, sub, pre, obj):
     graph = rdflib.ConjunctiveGraph()
     data = StringInputSource(self.conn.statements_default_graph(
         self.repository,
         'text/plain'
     ))
     graph.parse(data, format="nt")
     t = self.parse_triple(sub, pre, obj)
     return graph.triples(t)
Ejemplo n.º 28
0
    def __init__(self, *args, **kwargs):
        allowed_keys = ['verbose']
        self.__dict__.update((k, False) for k in allowed_keys)
        self.__dict__.update(
            (k, v) for k, v in kwargs.items() if k in allowed_keys)

        self.uri = None
        self.data = None  # the raw data coming back from SciGraph
        self.rdfgraph = rdflib.ConjunctiveGraph()
Ejemplo n.º 29
0
 def __init__(self, subject=None, graph=None, format='n3'):
     if graph:
         self.graph = graph
     else:
         self.graph = rdflib.ConjunctiveGraph()
     self.subject = subject
     self.format = format
     self.parsed_files = set()
     self._data = None
Ejemplo n.º 30
0
def generateroutesdatardf(routes_data):
    routesbaseuri = base_uri + 'route/'
    retRDF = rdflib.ConjunctiveGraph()
    for route in routes_data:
        for p in routes_data[route]:
            pred = rdflib.URIRef(routesbaseuri + p)
            obj = routes_data[route][p]
            retRDF.add((route, pred, rdflib.Literal(obj)))
    return retRDF