def createLocationObservation(iesGraph,mmsi,timestamp,lat,lon,obs=None): print(mmsi,timestamp) #add the location transponder - We don't know this is necessarily a vessel. All we know is that we have a LocationTransponder. lt = createLocationTransponder(iesGraph=iesGraph,mmsi=mmsi) #Now create the observation event lo = instantiate(iesGraph=iesGraph,_class=locationObservation) #If track emulation is not required, obs will be None. If it's not None, make the LocationObservation (lo) part of the overall track observation if obs: addToGraph(iesGraph=iesGraph,subject=lo,predicate=ipao,obj=obs) #...and the ParticularPeriod in which the observation occurred putInPeriod(iesGraph=iesGraph,item=lo,iso8601TimeString=timestamp) #And involve the transponder in that location observation ltPart = instantiate(iesGraph=iesGraph,_class=observedTarget) addToGraph(iesGraph=iesGraph,subject=ltPart,predicate=ipo,obj=lt) #participation of the transponder addToGraph(iesGraph=iesGraph,subject=ltPart,predicate=ipi,obj=lo) #participation in the LocationObservation #Now the observed location, a geopoint with a lat and long - using a geohash to give each point a unique uri gp = URIRef(dataUri+"geohash_"+Geohash.encode(lat,lon)) instantiate(iesGraph=iesGraph,_class=geoPoint,instance=gp) #Add the lat and long values as identifiers of the geopoint...firstly creating repeatable URIs for them so they don't overwrite latObj = URIRef(gp.toPython()+"_lat") lonObj = URIRef(gp.toPython()+"_lon") instantiate(iesGraph=iesGraph, _class=latitude,instance=latObj) instantiate(iesGraph=iesGraph, _class=longitude,instance=lonObj) addToGraph(iesGraph=iesGraph,subject=gp,predicate=iib,obj=latObj) addToGraph(iesGraph=iesGraph,subject=gp,predicate=iib,obj=lonObj) #Add the representation values to the lat and lon objects addToGraph(iesGraph=iesGraph,subject=latObj,predicate=rv,obj=Literal(lat, datatype=XSD.string)) addToGraph(iesGraph=iesGraph,subject=lonObj,predicate=rv,obj=Literal(lon, datatype=XSD.string)) #Now the participation of the GeoPoint in the Observation gpPart = instantiate(iesGraph=iesGraph,_class=observedLocation) addToGraph(iesGraph=iesGraph,subject=gpPart,predicate=ipo,obj=gp) #participation of the GeoPoint addToGraph(iesGraph=iesGraph,subject=gpPart,predicate=ipi,obj=lo) #participation in the LocationObservation
def frequency_from_rdf(term): if isinstance(term, str): try: term = URIRef(uris.validate(term)) except uris.ValidationError: pass if isinstance(term, Literal): if term.toPython() in UPDATE_FREQUENCIES: return term.toPython() if isinstance(term, RdfResource): term = term.identifier if isinstance(term, URIRef): if EUFREQ in term: return EU_RDF_REQUENCIES.get(term) _, _, freq = namespace_manager.compute_qname(term) return freq
def duration(value, df): event_uris = df['uri'] unit = 'hour' datetime_now = datetime.utcnow().replace(tzinfo=tz.tzutc()).astimezone( tz.tzlocal()) datum_uri = URIRef('{}{}'.format( ns, md5('{}{}'.format(datetime_now, 'datum').encode()).hexdigest())) arithmetic_mean_calculation_uri = URIRef('{}{}'.format( ns, md5('{}{}'.format( datetime_now, 'arithmetic_mean_calculation').encode()).hexdigest())) dataset_uri = URIRef('{}{}'.format( ns, md5('{}{}'.format(datetime_now, 'dataset').encode()).hexdigest())) g.add((obo['atmospheric aerosol formation event'], RDFS.label, Literal('atmospheric aerosol formation event'))) g.add((obo['scalar measurement datum'], RDFS.label, Literal('scalar measurement datum'))) g.add((obo['has measurement unit label'], RDFS.label, Literal('has measurement unit label'))) g.add((obo['has measurement value'], RDFS.label, Literal('has measurement value'))) g.add((obo['time unit'], RDFS.label, Literal('time unit'))) g.add((obo['hour'], RDFS.label, Literal('hour'))) g.add((obo['average value'], RDFS.label, Literal('average value'))) g.add((obo['is_specified_output_of'], RDFS.label, Literal('is_specified_output_of'))) g.add((obo['has_specified_output'], RDFS.label, Literal('has_specified_output'))) g.add((obo['arithmetic mean calculation'], RDFS.label, Literal('arithmetic mean calculation'))) g.add((obo['has_specified_input'], RDFS.label, Literal('has_specified_input'))) g.add((obo['data set'], RDFS.label, Literal('data set'))) g.add((obo['has part'], RDFS.label, Literal('has part'))) g.add((obo['data item'], RDFS.label, Literal('data item'))) g.add((obo['hour'], RDF.type, obo['time unit'])) g.add((arithmetic_mean_calculation_uri, RDF.type, obo['arithmetic mean calculation'])) g.add((dataset_uri, RDF.type, obo['data set'])) g.add((datum_uri, RDF.type, obo['scalar measurement datum'])) g.add((datum_uri, RDF.type, obo['average value'])) g.add((datum_uri, RDF.type, orkg['ResearchResult'])) g.add((datum_uri, obo['is about'], obo['atmospheric aerosol formation event'])) g.add((datum_uri, obo['has measurement value'], Literal(value, datatype=XSD.decimal))) g.add((datum_uri, obo['has measurement unit label'], obo['hour'])) g.add((datum_uri, obo['is_specified_output_of'], arithmetic_mean_calculation_uri)) g.add((arithmetic_mean_calculation_uri, obo['has_specified_output'], datum_uri)) g.add((arithmetic_mean_calculation_uri, obo['has_specified_input'], dataset_uri)) for event_uri in event_uris: g.add((URIRef(event_uri), RDF.type, obo['data item'])) g.add((dataset_uri, obo['has part'], URIRef(event_uri))) g.add((dataset_uri, RDF.type, prov['Entity'])) g.add((datum_uri, RDF.type, prov['Entity'])) g.add((arithmetic_mean_calculation_uri, RDF.type, prov['Activity'])) g.add((datum_uri, prov['wasDerivedFrom'], dataset_uri)) g.add((datum_uri, prov['wasGeneratedBy'], arithmetic_mean_calculation_uri)) g.add((arithmetic_mean_calculation_uri, prov['used'], dataset_uri)) g.add((arithmetic_mean_calculation_uri, prov['startedAtTime'], Literal(datetime_now.isoformat(), datatype=XSD.dateTime))) g.add((arithmetic_mean_calculation_uri, prov['endedAtTime'], Literal(datetime_now.isoformat(), datatype=XSD.dateTime))) return datum_uri.toPython()
def createLocationObservation(iesGraph, ping, obs=None, transponder=None, measures=None): mmsi = str(ping[0]) timestamp = str(ping[1]) lat = float(ping[2]) lon = float(ping[3]) print(lat, lon) if transponder == None: transponder = ies.createLocationTransponder(iesGraph=iesGraph, mmsi=mmsi) lo = ies.instantiate(iesGraph=iesGraph, _class=ies.locationObservation) #If track emulation is not required, obs will be None. If it's not None, make the LocationObservation (lo) part of the overall track observation if obs: ies.addToGraph(iesGraph=iesGraph, subject=lo, predicate=ies.ipao, obj=obs) #...and the ParticularPeriod in which the observation occurred ies.putInPeriod(iesGraph=iesGraph, item=lo, timeString=timestamp) #And involve the transponder in that location observation ltPart = ies.instantiate(iesGraph=iesGraph, _class=ies.observedTarget) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.ipo, obj=transponder) #participation of the transponder ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.ipi, obj=lo) #participation in the LocationObservation #Now the observed location, a geopoint with a lat and long - using a geohash to give each point a unique uri gp = URIRef(ies.dataUri + "latlong" + str(lat) + "," + str(lon)) ies.instantiate(iesGraph=iesGraph, _class=ies.geoPoint, instance=gp) #Add the lat and long values as identifiers of the geopoint...firstly creating repeatable URIs for them so they don't overwrite latObj = URIRef(gp.toPython() + "_lat") lonObj = URIRef(gp.toPython() + "_lon") ies.instantiate(iesGraph=iesGraph, _class=ies.latitude, instance=latObj) ies.instantiate(iesGraph=iesGraph, _class=ies.longitude, instance=lonObj) ies.addToGraph(iesGraph=iesGraph, subject=gp, predicate=ies.iib, obj=latObj) ies.addToGraph(iesGraph=iesGraph, subject=gp, predicate=ies.iib, obj=lonObj) #Add the representation values to the lat and lon objects ies.addToGraph(iesGraph=iesGraph, subject=latObj, predicate=ies.rv, obj=Literal(lat, datatype=XSD.string)) ies.addToGraph(iesGraph=iesGraph, subject=lonObj, predicate=ies.rv, obj=Literal(lon, datatype=XSD.string)) #Now the participation of the GeoPoint in the Observation gpPart = ies.instantiate(iesGraph=iesGraph, _class=ies.observedLocation) ies.addToGraph(iesGraph=iesGraph, subject=gpPart, predicate=ies.ipo, obj=gp) #participation of the GeoPoint ies.addToGraph(iesGraph=iesGraph, subject=gpPart, predicate=ies.ipi, obj=lo) #participation in the LocationObservation #This code fires if the measure classes etc. have been provided - it takes the speed and course over ground and processes that. if measures: sogVal = float(ping[4]) cogVal = float(ping[5]) sog = ies.addMeasure(iesGraph=iesGraph, measureClass=measures["sogClass"], value=sogVal, uom=measures["knots"]) cog = ies.addMeasure(iesGraph=iesGraph, measureClass=measures["cogClass"], value=cogVal, uom=measures["degTN"]) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.och, obj=sog) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.och, obj=cog)
def would_you_like_to_know_more_question_mark(): # resolving differences between classes more_ids = set(( 'http://uri.neuinfo.org/nif/nifstd/readable/ChEBIid', 'http://uri.neuinfo.org/nif/nifstd/readable/GOid', 'http://uri.neuinfo.org/nif/nifstd/readable/MeshUid', 'http://uri.neuinfo.org/nif/nifstd/readable/PMID', 'http://uri.neuinfo.org/nif/nifstd/readable/UmlsCui', 'http://uri.neuinfo.org/nif/nifstd/readable/bamsID', 'http://uri.neuinfo.org/nif/nifstd/readable/bonfireID', 'http://uri.neuinfo.org/nif/nifstd/readable/cell_ontology_ID', 'http://uri.neuinfo.org/nif/nifstd/readable/definingCitationID', 'http://uri.neuinfo.org/nif/nifstd/readable/definingCitationURI', 'http://uri.neuinfo.org/nif/nifstd/readable/emapMouseStageDataID', 'http://uri.neuinfo.org/nif/nifstd/readable/emapMouseStageDiagramID', 'http://uri.neuinfo.org/nif/nifstd/readable/externalSourceId', 'http://uri.neuinfo.org/nif/nifstd/readable/externalSourceURI', 'http://uri.neuinfo.org/nif/nifstd/readable/gbifID', 'http://uri.neuinfo.org/nif/nifstd/readable/gbifTaxonKeyID', 'http://uri.neuinfo.org/nif/nifstd/readable/gene_Ontology_ID', #'http://uri.neuinfo.org/nif/nifstd/readable/hasExternalSource', 'http://uri.neuinfo.org/nif/nifstd/readable/hasGenbankAccessionNumber', 'http://uri.neuinfo.org/nif/nifstd/readable/imsrStandardStrainName', 'http://uri.neuinfo.org/nif/nifstd/readable/isReplacedByClass', 'http://uri.neuinfo.org/nif/nifstd/readable/jaxMiceID', 'http://uri.neuinfo.org/nif/nifstd/readable/ncbiTaxID', 'http://uri.neuinfo.org/nif/nifstd/readable/neuronamesID', 'http://uri.neuinfo.org/nif/nifstd/readable/nifID', 'http://uri.neuinfo.org/nif/nifstd/readable/sao_ID', 'http://uri.neuinfo.org/nif/nifstd/readable/umls_ID', 'http://www.geneontology.org/formats/oboInOwl#id', )) outside = [] eee = {} resolver_not_ilx_only_but_not_in_scigraph = set() # resources.ttl _res = Graph().parse((gitf / 'NIF-Ontology/ttl/resources.ttl').as_posix(), format='turtle') reslookup = {uri:[l] for uri, l in _res.subject_objects(rdfs.label)} for uri in chain(h_uris, resolver_not_ilx_only): if 'uri.neuinfo.org' in uri: try: meta = sgg.getNode(uri.toPython())['nodes'][0]['meta'] asdf = {hng.qname(k):v for k, v in meta.items() if k in more_ids} except TypeError: resolver_not_ilx_only_but_not_in_scigraph.add(uri) # resources.ttl ;) if uri in reslookup: # no differentia asdf = False else: asdf = False print('WTF', uri) if asdf: #print(uri, asdf) eee[uri] = asdf for l in asdf.values(): for e in l: outside.append(e) outside_dupes = [v for v, c in Counter(outside).most_common() if c > 1] eee_dupes = {k:v for k, v in eee.items() if anyMembers(outside_dupes, *(e for l in v.values() for e in l))} #for uri, meta in sorted(eee_dupes.items(), key=lambda a:sorted(a[1].values())): #print(uri.toPython(), sorted((e.replace('PMID: ', 'PMID:'), k) for k, l in meta.items() for e in l)) # attempt to deal with label mappings iexisting = defaultdict(set) iiexisting = {} for i, existing in zip(datal('ilx'), datal('iri')): #if 'uri.neuinfo.org' in existing: if 'interlex.org' not in existing and 'neurolex.org' not in existing: iexisting[i].add(URIRef(existing)) iiexisting[URIRef(existing)] = i iexisting = {**iexisting} _ilabs = {k:l for k, l in zip(datal('ilx'), datal('label'))} def inner(iri): resp = sgv.findById(iri) if resp is not None: l = resp['labels'] else: l = [] #_ilabs[iiexisting[iri]] + '** already in ilx **'] #print('trouble?', iri) # ilx only return iri, l #labs = {k:v[0] if v else '<--NO-LABEL-->' for k, v in Async()(deferred(inner)(id_) for id_ in chain(h_uris, (e for s in iexisting.values() for e in s)))} labs = {k:v[0] if v else '<--NO-LABEL-->' for k, v in Async()(deferred(inner)(id_) for id_ in h_uris)} ilabs = {k:l.lower() for k, l in zip(datal('ilx'), datal('label'))} iilabs = {v:k for k, v in ilabs.items()} assert len(ilabs) == len(iilabs) missing_map = {k:iilabs[v.lower()] for k, v in labs.items() if v and v.lower() in iilabs} # XXX this is not valid missing_existing = {i:[m, *iexisting[i]] for m, i in missing_map.items() if i in iexisting} missing_equivs = {next(iter(iexisting[i])):i for m, i in missing_map.items() if i in iexisting} eid = NIFRID.externalSourceId.toPython() ded = owl.deprecated.toPython() # SP: -> swissprot vs uniprot mmr = [] proto_mmr_1_to_1 = {} arrr = defaultdict(set) uniprot_iuphar = set() for uri, ilx_frag in {**missing_equivs, **missing_map}.items(): uri = URIRef(uri) try: meta = sgg.getNode(uri.toPython())['nodes'][0]['meta'] except TypeError: # just ignore these, they are ilx only :/ meta = {} if eid in meta: src = meta[eid][0] if src.startswith('SP:'): src = tc.yellow(src.replace('SP:', 'http://www.uniprot.org/uniprot/')) #elif src.startswith('IUPHAR:'): #pass #else: #src = 'TODO' elif ded in meta and meta[ded]: src = tc.red('ded ') else: src = 'TODO' val = labs[uri] if uri in labs else _ilabs[ilx_frag] + ' **' if uri in eee: differentia = str(eee[uri]) for v in eee[uri].values(): for e in v: arrr[e].add(uri) if 'SP:' in e or 'IUPHAR:' in e: uniprot_iuphar.add(uri) else: differentia = '' if uri in _ilx and uri in all_uris: ruri = SGG[hng.qname(uri)] ruri = tc.blue(f'{ruri:<60}') else: ruri = uri ruri = f'{ruri:<60}' v = ' '.join((f'{val:<60}', src, ruri, ilxb[ilx_frag], differentia)) mmr.append(v) proto_mmr_1_to_1[uri] = v src = None arrr = {**arrr} arrr_not_1_to_1 = {k:v for k, v in arrr.items() if len(v) > 1} #arrr_n11_uris = set((u.toPython() for v in arrr_not_1_to_1.values() for u in v)) arrr_n11_uris = set.union(*arrr_not_1_to_1.values()) mmr_1_to_1 = {k:v for k, v in proto_mmr_1_to_1.items() if k not in arrr_n11_uris} no_uniprot = {k:v for k, v in proto_mmr_1_to_1.items() if k not in uniprot_iuphar} arrr_n11_text = '\n'.join(f'{k:<15} {sorted(_.toPython() for _ in v)}' for k, v in arrr_not_1_to_1.items()) mmr.sort() mmr_text = '\n'.join(mmr) mmr_1_to_1_text = '\n'.join(sorted(mmr_1_to_1.values())) no_uniprot_text = '\n'.join(sorted(no_uniprot.values()))