diff options
Diffstat (limited to 'gn3/api')
-rw-r--r-- | gn3/api/metadata.py | 434 |
1 files changed, 0 insertions, 434 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 79c0417..dfedd27 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -1033,437 +1033,3 @@ CONSTRUCT { }) except (RemoteDisconnected, URLError): return jsonify({}) - - -@metadata.route("/genotypes/<name>", methods=["GET"]) -def genotypes(name): - """Fetch a genotype's metadata given it's name""" - try: - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?genotype ?predicate ?object . - ?species rdfs:label ?speciesName . -} WHERE { - ?genotype rdf:type gnc:Genotype ; - rdfs:label "$name" ; - ?predicate ?object . - OPTIONAL { - ?species ^xkos:classifiedUnder ?genotype ; - rdfs:label ?speciesName . - } -} -""").substitute(prefix=RDF_PREFIXES, name=name)) - results = json.loads(sparql.queryAndConvert().serialize(format="json-ld")) - if not results: - return jsonify({}) - frame = { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "gnt": "http://genenetwork.org/term/", - "xkos": "http://rdf-vocabulary.ddialliance.org/xkos#", - "gnc": "http://genenetwork.org/category/", - "xsd": "http://www.w3.org/2001/XMLSchema#", - "name": "rdfs:label", - "chr": "gnt:chr", - "mb": "gnt:mb", - "mbMm8": "gnt:mbMm8", - "mb2016": "gnt:mb2016", - "sequence": "gnt:hasSequence", - "source": "gnt:hasSource", - "species": "xkos:classifiedUnder", - "alternateSource": "gnt:hasAltSourceName", - "comments": "rdfs:comments", - "chrNum": { - "@id": "gnt:chrNum", - "@type": "xsd:int", - } - }, - "type": "gnc:Genotype", - } - return jsonld.compact(jsonld.frame(results, frame), frame) - except (RemoteDisconnected, URLError): - return jsonify({}) - - -@metadata.route("/genewikis/gn/<symbol>", methods=["GET"]) -def get_gn_genewiki_entries(symbol): - """Fetch the GN and NCBI GeneRIF entries""" - try: - args = request.args - page = args.get("page", 0) - page_size = args.get("per-page", 10) - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?symbol ex:entries [ - rdfs:comment ?comment ; - ex:species ?species_ ; - dct:created ?createTime ; - dct:references ?pmids ; - dct:creator ?creator ; - gnt:belongsToCategory ?categories ; - ] . - ?symbol rdf:type gnc:GNWikiEntry ; - ex:totalCount ?totalCount ; - ex:currentPage $offset . -} WHERE { -{ - SELECT ?symbol ?comment (GROUP_CONCAT(DISTINCT ?speciesName; SEPARATOR='; ') AS ?species_) - ?createTime ?creator - (GROUP_CONCAT(DISTINCT ?pubmed; SEPARATOR='; ') AS ?pmids) - (GROUP_CONCAT(DISTINCT ?category; SEPARATOR='; ') AS ?categories) - WHERE { - ?symbol rdfs:label ?label ; - rdfs:comment _:entry . - ?label bif:contains "'$symbol'" . - _:entry rdf:type gnc:GNWikiEntry ; - rdfs:comment ?comment . - OPTIONAL { - ?species ^xkos:classifiedUnder _:entry ; - ^skos:member gnc:Species ; - skos:prefLabel ?speciesName . - } . - OPTIONAL { _:entry dct:created ?createTime . } . - OPTIONAL { _:entry dct:references ?pubmed . } . - OPTIONAL { - ?investigator foaf:name ?creator ; - ^dct:creator _:entry . - } . - OPTIONAL { _:entry gnt:belongsToCategory ?category . } . - } GROUP BY ?comment ?symbol ?createTime ?creator ORDER BY ?createTime LIMIT $limit OFFSET $offset -} - -{ - SELECT (COUNT(DISTINCT ?comment)/$limit+1 AS ?totalCount) WHERE { - ?symbol rdfs:comment _:entry ; - rdfs:label ?label . - _:entry rdfs:comment ?comment ; - rdf:type gnc:GNWikiEntry . - ?label bif:contains "'$symbol'" . - } -} -} -""").substitute(prefix=RDF_PREFIXES, symbol=symbol, - limit=page_size, offset=page)) - results = sparql.queryAndConvert() - results = json.loads(results.serialize(format="json-ld")) - context = { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "ex": "http://example.org/stuff/1.0/", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "gnt": "http://genenetwork.org/term/", - "gnc": "http://genenetwork.org/category/", - "dct": "http://purl.org/dc/terms/", - "xsd": "http://www.w3.org/2001/XMLSchema#", - "entries": "ex:entries", - "comment": "rdfs:comment", - "species": "ex:species", - "category": 'gnt:belongsToCategory', - "author": "dct:creator", - "pubmed": "dct:references", - "currentPage": "ex:currentPage", - "pages": "ex:totalCount", - "created": { - "@id": "dct:created", - "@type": "xsd:datetime" - }, - }, - "type": "gnc:GNWikiEntry" - } - return jsonld.compact( - jsonld.frame(results, context), - context) - except (RemoteDisconnected, URLError): - return jsonify({}) - -@metadata.route("/genewikis/ncbi/<symbol>", methods=["GET"]) -def get_ncbi_genewiki_entries(symbol): - """Fetch the NCBI GeneRIF entries""" - try: - args = request.args - page = args.get("page", 0) - page_size = args.get("per-page", 10) - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?symbol ex:entries [ - rdfs:comment ?comment ; - gnt:hasGeneId ?geneId ; - ex:species ?species_ ; - dct:created ?createTime ; - dct:references ?pmids ; - dct:creator ?creator ; - ] . - ?symbol rdf:type gnc:GNWikiEntry ; - ex:totalCount ?totalCount ; - ex:currentPage $offset . -} WHERE { -{ - SELECT ?symbol ?comment ?geneId (GROUP_CONCAT(DISTINCT ?speciesName; SEPARATOR='; ') AS ?species_) - ?createTime ?creator - (GROUP_CONCAT(DISTINCT ?pubmed; SEPARATOR='; ') AS ?pmids) - WHERE { - ?symbol rdfs:label ?label ; - rdfs:comment _:entry . - ?label bif:contains "'$symbol'" . - _:entry rdf:type gnc:NCBIWikiEntry ; - rdfs:comment ?comment . - OPTIONAL { - ?species ^xkos:classifiedUnder _:entry ; - ^skos:member gnc:Species ; - skos:prefLabel ?speciesName . - } . - OPTIONAL { _:entry gnt:hasGeneId ?geneId . } . - OPTIONAL { _:entry dct:created ?createTime . } . - OPTIONAL { _:entry dct:references ?pubmed . } . - OPTIONAL { - ?investigator foaf:name ?creator ; - ^dct:creator _:entry . - } . - } GROUP BY ?comment ?symbol ?createTime ?creator ?geneId ORDER BY ?createTime LIMIT $limit OFFSET $offset -} - -{ - SELECT (COUNT(DISTINCT ?comment)/$limit+1 AS ?totalCount) WHERE { - ?symbol rdfs:comment _:entry ; - rdfs:label ?label . - _:entry rdfs:comment ?comment ; - rdf:type gnc:NCBIWikiEntry . - ?label bif:contains "'$symbol'" . - } -} -} -""").substitute(prefix=RDF_PREFIXES, symbol=symbol, - limit=page_size, offset=page)) - results = sparql.queryAndConvert() - results = json.loads(results.serialize(format="json-ld")) - context = { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "ex": "http://example.org/stuff/1.0/", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "gnt": "http://genenetwork.org/term/", - "gnc": "http://genenetwork.org/category/", - "dct": "http://purl.org/dc/terms/", - "xsd": "http://www.w3.org/2001/XMLSchema#", - "entries": "ex:entries", - "comment": "rdfs:comment", - "category": 'gnt:belongsToCategory', - "author": "dct:creator", - "species": "ex:species", - "geneId": "gnt:hasGeneId", - "pubmed": "dct:references", - "currentPage": "ex:currentPage", - "pages": "ex:totalCount", - "created": { - "@id": "dct:created", - "@type": "xsd:datetime" - }, - }, - "type": "gnc:GNWikiEntry" - } - return jsonld.compact( - jsonld.frame(results, context), - context) - except (RemoteDisconnected, URLError): - return jsonify({}) - - -@metadata.route("/species", methods=["GET"]) -def list_species(): - """List all species""" - try: - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?species ?predicate ?object . -} WHERE { - ?species ^skos:member gnc:Species ; - ?predicate ?object . - VALUES ?predicate { - rdfs:label skos:prefLabel - skos:altLabel gnt:shortName - gnt:family skos:notation - } - -} -""").substitute(prefix=RDF_PREFIXES)) - results = sparql.queryAndConvert() - results = json.loads( - results.serialize(format="json-ld") - ) - return jsonld.compact(results, { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "skos": "http://www.w3.org/2004/02/skos/core#", - "gnt": "http://genenetwork.org/term/", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "name": "rdfs:label", - "family": "gnt:family", - "shortName": "gnt:shortName", - "alternateName": "skos:altLabel", - "taxonomicId": "skos:notation", - "fullName": "skos:prefLabel", - }, - }) - except (RemoteDisconnected, URLError): - return jsonify({}) - -@metadata.route("/species/<name>", methods=["GET"]) -def fetch_species(name): - """Fetch a Single species information""" - try: - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?species ?predicate ?object . -} WHERE { - ?species ^skos:member gnc:Species ; - gnt:shortName "$name" ; - ?predicate ?object . - VALUES ?predicate { - rdfs:label skos:prefLabel - skos:altLabel gnt:shortName - gnt:family skos:notation - } - -} -""").substitute(prefix=RDF_PREFIXES, name=name)) - results = sparql.queryAndConvert() - results = json.loads( - results.serialize(format="json-ld") - ) - return jsonld.compact(results, { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "skos": "http://www.w3.org/2004/02/skos/core#", - "gnt": "http://genenetwork.org/term/", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "name": "rdfs:label", - "family": "gnt:family", - "shortName": "gnt:shortName", - "alternateName": "skos:altLabel", - "taxonomicId": "skos:notation", - "fullName": "skos:prefLabel", - }, - }) - except (RemoteDisconnected, URLError): - return jsonify({}) - - -@metadata.route("/groups", methods=["GET"]) -def groups(): - """Fetch the list of groups""" - try: - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?group ?predicate ?object . -} WHERE { - ?group ^skos:member gnc:Set ; - ?predicate ?object . - VALUES ?predicate { - rdfs:label skos:prefLabel - gnt:geneticType gnt:mappingMethod - gnt:code gnt:family - } - -} -""").substitute(prefix=RDF_PREFIXES)) - results = sparql.queryAndConvert() - results = json.loads( - results.serialize(format="json-ld") - ) - return jsonld.compact(results, { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "skos": "http://www.w3.org/2004/02/skos/core#", - "gnt": "http://genenetwork.org/term/", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "name": "rdfs:label", - "family": "gnt:family", - "shortName": "gnt:shortName", - "code": "gnt:code", - "mappingMethod": "gnt:mappingMethod", - "geneticType": "gnt:geneticType", - "fullName": "skos:prefLabel", - }, - }) - except (RemoteDisconnected, URLError): - return jsonify({}) - - -@metadata.route("/groups/<name>", methods=["GET"]) -def fetch_group_by_species(name): - """Fetch the list of groups""" - try: - sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) - sparql.setQuery(Template(""" -$prefix - -CONSTRUCT { - ?group ?predicate ?object . -} WHERE { - ?species gnt:shortName "$name" ; - ^skos:member gnc:Species . - ?group ^skos:member gnc:Set ; - xkos:generalizes ?species ; - ?predicate ?object . - VALUES ?predicate { - rdfs:label skos:prefLabel - gnt:geneticType gnt:mappingMethod - gnt:code gnt:family - } - -} -""").substitute(prefix=RDF_PREFIXES, name=name)) - results = sparql.queryAndConvert() - results = json.loads( - results.serialize(format="json-ld") - ) - return jsonld.compact(results, { - "@context": { - "data": "@graph", - "type": "@type", - "id": "@id", - "skos": "http://www.w3.org/2004/02/skos/core#", - "gnt": "http://genenetwork.org/term/", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", - "name": "rdfs:label", - "family": "gnt:family", - "shortName": "gnt:shortName", - "code": "gnt:code", - "mappingMethod": "gnt:mappingMethod", - "geneticType": "gnt:geneticType", - "fullName": "skos:prefLabel", - }, - }) - except (RemoteDisconnected, URLError): - return jsonify({}) |