Skip to content

Instantly share code, notes, and snippets.

@adyork
Last active October 20, 2022 16:09
Show Gist options
  • Save adyork/a9ce6eba7f70ca525c2981a685cd0ff0 to your computer and use it in GitHub Desktop.
Save adyork/a9ce6eba7f70ca525c2981a685cd0ff0 to your computer and use it in GitHub Desktop.
taxamatch_response_data.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "taxamatch_response_data.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/adyork/a9ce6eba7f70ca525c2981a685cd0ff0/taxamatch_response_data.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ergKkSpqHktq"
},
"source": [
"Match taxonomic names using an api.\n",
"\n",
"Let's look at the type of data we get back."
]
},
{
"cell_type": "code",
"metadata": {
"id": "fwqvYP_WHqzq"
},
"source": [
"# Import packages\n",
"\n",
"import requests\n",
"import pandas as pd"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "F7CQQPQ6On3B"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vKK630JfHzcD"
},
"source": [
"wild_name = \"Gadus morha\"\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "XPn2BdOtKTjb"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "zkJ0HgVYJUcb"
},
"source": [
"# Get match info from World Register of Marine Species\n",
"# Documentation: http://marinespecies.org/rest/\n",
"url = \"http://marinespecies.org/rest/AphiaRecordsByMatchNames?scientificnames[]=Gadus%20morha&marine_only=true\"\n",
"\n",
"response = requests.get(url)\n"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VGcxTGKLLsH6",
"outputId": "edb462a1-13b8-4b9c-f7be-a5b962c60a9c"
},
"source": [
"#ok so did that work? Check the status code.\n",
"response.status_code"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"200"
]
},
"metadata": {},
"execution_count": 88
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TftAlBP7IShM",
"outputId": "8019bd86-8433-47b9-e499-2b14d9814df9"
},
"source": [
"# Check if error has occurred, will return None if no problem occurred\n",
"print(response.raise_for_status())"
],
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"None\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 154
},
"id": "w9pZkFBoLw9g",
"outputId": "b89b4358-a963-4b91-ac87-9cebdf752047"
},
"source": [
"print(type(response.text))\n",
"response.text"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'str'>\n"
]
},
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'[[{\"AphiaID\":126436,\"url\":\"http:\\\\/\\\\/marinespecies.org\\\\/aphia.php?p=taxdetails&id=126436\",\"scientificname\":\"Gadus morhua\",\"authority\":\"Linnaeus, 1758\",\"status\":\"accepted\",\"unacceptreason\":null,\"taxonRankID\":220,\"rank\":\"Species\",\"valid_AphiaID\":126436,\"valid_name\":\"Gadus morhua\",\"valid_authority\":\"Linnaeus, 1758\",\"parentNameUsageID\":125732,\"kingdom\":\"Animalia\",\"phylum\":\"Chordata\",\"class\":\"Actinopteri\",\"order\":\"Gadiformes\",\"family\":\"Gadidae\",\"genus\":\"Gadus\",\"citation\":\"Froese, R. and D. Pauly. Editors. (2021). FishBase. Gadus morhua Linnaeus, 1758. Accessed through: World Register of Marine Species at: http:\\\\/\\\\/marinespecies.org\\\\/aphia.php?p=taxdetails&id=126436 on 2021-10-19\",\"lsid\":\"urn:lsid:marinespecies.org:taxname:126436\",\"isMarine\":1,\"isBrackish\":1,\"isFreshwater\":0,\"isTerrestrial\":0,\"isExtinct\":null,\"match_type\":\"near_1\",\"modified\":\"2008-01-15T17:27:08.177Z\"}]]'"
]
},
"metadata": {},
"execution_count": 89
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ydrZQ9fiMYBz",
"outputId": "59ba3c80-10e9-40e6-bedb-27be310f266e"
},
"source": [
"response.headers"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'date': 'Tue, 19 Oct 2021 11:17:25 GMT', 'server': 'Apache/2.4.51 (Win64)', 'set-cookie': 'PHPSESSID=c72c1c7a817b6b02fcc3a648041736b0; path=/; HttpOnly, vliz_webc=vliz_webc2; path=/', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'cache-control': 'no-store, no-cache, must-revalidate', 'pragma': 'no-cache', 'access-control-allow-origin': '*', 'access-control-allow-headers': 'X-Requested-With, Content-Type, Accept, Origin, Authorization', 'access-control-allow-methods': 'GET, POST, OPTIONS', 'content-length': '876', 'content-type': 'application/json'}"
]
},
"metadata": {},
"execution_count": 90
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ycMJ4vaLMlci",
"outputId": "14a64dbf-cd80-44bd-e839-1cc059fcaecf"
},
"source": [
"j = response.json()\n",
"j"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[[{'AphiaID': 126436,\n",
" 'authority': 'Linnaeus, 1758',\n",
" 'citation': 'Froese, R. and D. Pauly. Editors. (2021). FishBase. Gadus morhua Linnaeus, 1758. Accessed through: World Register of Marine Species at: http://marinespecies.org/aphia.php?p=taxdetails&id=126436 on 2021-10-19',\n",
" 'class': 'Actinopteri',\n",
" 'family': 'Gadidae',\n",
" 'genus': 'Gadus',\n",
" 'isBrackish': 1,\n",
" 'isExtinct': None,\n",
" 'isFreshwater': 0,\n",
" 'isMarine': 1,\n",
" 'isTerrestrial': 0,\n",
" 'kingdom': 'Animalia',\n",
" 'lsid': 'urn:lsid:marinespecies.org:taxname:126436',\n",
" 'match_type': 'near_1',\n",
" 'modified': '2008-01-15T17:27:08.177Z',\n",
" 'order': 'Gadiformes',\n",
" 'parentNameUsageID': 125732,\n",
" 'phylum': 'Chordata',\n",
" 'rank': 'Species',\n",
" 'scientificname': 'Gadus morhua',\n",
" 'status': 'accepted',\n",
" 'taxonRankID': 220,\n",
" 'unacceptreason': None,\n",
" 'url': 'http://marinespecies.org/aphia.php?p=taxdetails&id=126436',\n",
" 'valid_AphiaID': 126436,\n",
" 'valid_authority': 'Linnaeus, 1758',\n",
" 'valid_name': 'Gadus morhua'}]]"
]
},
"metadata": {},
"execution_count": 91
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "g8FAe0WnMpgA",
"outputId": "c2363ea3-50ab-4057-9264-d05fee0af904"
},
"source": [
"print(type(j))\n",
"\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'list'>\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5H9yg3nvN-uq",
"outputId": "5f990435-3d83-4c32-9eb5-659db5b78420"
},
"source": [
"mydata = j[0][0]\n",
"mydata"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'AphiaID': 126436,\n",
" 'authority': 'Linnaeus, 1758',\n",
" 'citation': 'Froese, R. and D. Pauly. Editors. (2021). FishBase. Gadus morhua Linnaeus, 1758. Accessed through: World Register of Marine Species at: http://marinespecies.org/aphia.php?p=taxdetails&id=126436 on 2021-10-19',\n",
" 'class': 'Actinopteri',\n",
" 'family': 'Gadidae',\n",
" 'genus': 'Gadus',\n",
" 'isBrackish': 1,\n",
" 'isExtinct': None,\n",
" 'isFreshwater': 0,\n",
" 'isMarine': 1,\n",
" 'isTerrestrial': 0,\n",
" 'kingdom': 'Animalia',\n",
" 'lsid': 'urn:lsid:marinespecies.org:taxname:126436',\n",
" 'match_type': 'near_1',\n",
" 'modified': '2008-01-15T17:27:08.177Z',\n",
" 'order': 'Gadiformes',\n",
" 'parentNameUsageID': 125732,\n",
" 'phylum': 'Chordata',\n",
" 'rank': 'Species',\n",
" 'scientificname': 'Gadus morhua',\n",
" 'status': 'accepted',\n",
" 'taxonRankID': 220,\n",
" 'unacceptreason': None,\n",
" 'url': 'http://marinespecies.org/aphia.php?p=taxdetails&id=126436',\n",
" 'valid_AphiaID': 126436,\n",
" 'valid_authority': 'Linnaeus, 1758',\n",
" 'valid_name': 'Gadus morhua'}"
]
},
"metadata": {},
"execution_count": 93
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jdradm_fN5bC",
"outputId": "d77a8a33-82c3-4946-e795-9512dfe37ac2"
},
"source": [
"type(mydata)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict"
]
},
"metadata": {},
"execution_count": 94
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "T9A74NFmQ3xC",
"outputId": "3d2843a0-e633-42ec-b9a5-8f67f829c149"
},
"source": [
"# Get match info from GBIF\n",
"# Documentation: https://www.gbif.org/developer/summary\n",
"url = \"https://api.gbif.org/v1/species/match?verbose=true&name=Gadus%20morha\"\n",
"response = requests.get(url)\n",
"print(response.status_code)\n",
"mydict = response.json()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"200\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZhyiCdB1Q41w",
"outputId": "53812a34-5128-4f7c-97e1-654c2609c62a"
},
"source": [
"mydict"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'alternatives': [{'acceptedUsageKey': 5215435,\n",
" 'canonicalName': 'Gadus moro',\n",
" 'class': 'Actinopterygii',\n",
" 'classKey': 204,\n",
" 'confidence': 92,\n",
" 'family': 'Moridae',\n",
" 'familyKey': 3704,\n",
" 'genus': 'Mora',\n",
" 'genusKey': 2417289,\n",
" 'kingdom': 'Animalia',\n",
" 'kingdomKey': 1,\n",
" 'matchType': 'FUZZY',\n",
" 'note': 'Similarity: name=95; authorship=0; classification=-2; rank=5; status=0',\n",
" 'order': 'Gadiformes',\n",
" 'orderKey': 549,\n",
" 'phylum': 'Chordata',\n",
" 'phylumKey': 44,\n",
" 'rank': 'SPECIES',\n",
" 'scientificName': 'Gadus moro Risso, 1810',\n",
" 'species': 'Mora moro',\n",
" 'speciesKey': 5215435,\n",
" 'status': 'SYNONYM',\n",
" 'synonym': True,\n",
" 'usageKey': 2417296},\n",
" {'acceptedUsageKey': 8084280,\n",
" 'canonicalName': 'Gadus morrhua',\n",
" 'class': 'Actinopterygii',\n",
" 'classKey': 204,\n",
" 'confidence': 92,\n",
" 'family': 'Gadidae',\n",
" 'familyKey': 3701,\n",
" 'genus': 'Gadus',\n",
" 'genusKey': 2335052,\n",
" 'kingdom': 'Animalia',\n",
" 'kingdomKey': 1,\n",
" 'matchType': 'FUZZY',\n",
" 'note': 'Similarity: name=95; authorship=0; classification=-2; rank=5; status=0',\n",
" 'order': 'Gadiformes',\n",
" 'orderKey': 549,\n",
" 'phylum': 'Chordata',\n",
" 'phylumKey': 44,\n",
" 'rank': 'SPECIES',\n",
" 'scientificName': 'Gadus morrhua Linnaeus, 1758',\n",
" 'species': 'Gadus morhua',\n",
" 'speciesKey': 8084280,\n",
" 'status': 'SYNONYM',\n",
" 'synonym': True,\n",
" 'usageKey': 2415847}],\n",
" 'canonicalName': 'Gadus morhua',\n",
" 'class': 'Actinopterygii',\n",
" 'classKey': 204,\n",
" 'confidence': 92,\n",
" 'family': 'Gadidae',\n",
" 'familyKey': 3701,\n",
" 'genus': 'Gadus',\n",
" 'genusKey': 2335052,\n",
" 'kingdom': 'Animalia',\n",
" 'kingdomKey': 1,\n",
" 'matchType': 'FUZZY',\n",
" 'note': 'Similarity: name=95; authorship=0; classification=-2; rank=5; status=1; nextMatch=0',\n",
" 'order': 'Gadiformes',\n",
" 'orderKey': 549,\n",
" 'phylum': 'Chordata',\n",
" 'phylumKey': 44,\n",
" 'rank': 'SPECIES',\n",
" 'scientificName': 'Gadus morhua Linnaeus, 1758',\n",
" 'species': 'Gadus morhua',\n",
" 'speciesKey': 8084280,\n",
" 'status': 'ACCEPTED',\n",
" 'synonym': False,\n",
" 'usageKey': 8084280}"
]
},
"metadata": {},
"execution_count": 105
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "S-Aj1lrZR7uB",
"outputId": "46d5be02-10ae-4655-9b3c-d8e9d6f3a0bf"
},
"source": [
"# Get match info from GBIF\n",
"# Documentation: https://www.gbif.org/developer/summary\n",
"url = \"https://api.gbif.org/v1/species/match?verbose=true&name=Gadus%20morha\"\n",
"headers = {'User-Agent': 'XY','Content-type': 'application/json'}\n",
"#data = {'name': 'hello', 'data[]': ['hello', 'world']}\n",
"params = {'verbose': 'FALSE'}\n",
"r = requests.get(url, headers = headers, params=params)\n",
"\n",
"response = requests.get(url)\n",
"print(response.status_code)\n",
"mydict = response.json()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"200\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OXaWfEXDR_mB",
"outputId": "b54f8582-57f0-4065-bde2-60272295879f"
},
"source": [
"response.headers"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Date': 'Tue, 19 Oct 2021 11:12:53 GMT', 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'HEAD, GET, POST, DELETE, PUT, OPTIONS', 'Server': 'Jetty(9.3.z-SNAPSHOT)', 'Cache-Control': 'public, max-age=3601', 'X-Varnish': '738099501 692455716', 'Age': '666', 'Via': '1.1 varnish (Varnish/6.0)', 'Accept-Ranges': 'bytes', 'Content-Length': '1666', 'Connection': 'keep-alive'}"
]
},
"metadata": {},
"execution_count": 102
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "IUDRppz2SUcZ",
"outputId": "40290443-8931-453f-babe-29157f8e0104"
},
"source": [
"mydict"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'alternatives': [{'acceptedUsageKey': 5215435,\n",
" 'canonicalName': 'Gadus moro',\n",
" 'class': 'Actinopterygii',\n",
" 'classKey': 204,\n",
" 'confidence': 92,\n",
" 'family': 'Moridae',\n",
" 'familyKey': 3704,\n",
" 'genus': 'Mora',\n",
" 'genusKey': 2417289,\n",
" 'kingdom': 'Animalia',\n",
" 'kingdomKey': 1,\n",
" 'matchType': 'FUZZY',\n",
" 'note': 'Similarity: name=95; authorship=0; classification=-2; rank=5; status=0',\n",
" 'order': 'Gadiformes',\n",
" 'orderKey': 549,\n",
" 'phylum': 'Chordata',\n",
" 'phylumKey': 44,\n",
" 'rank': 'SPECIES',\n",
" 'scientificName': 'Gadus moro Risso, 1810',\n",
" 'species': 'Mora moro',\n",
" 'speciesKey': 5215435,\n",
" 'status': 'SYNONYM',\n",
" 'synonym': True,\n",
" 'usageKey': 2417296},\n",
" {'acceptedUsageKey': 8084280,\n",
" 'canonicalName': 'Gadus morrhua',\n",
" 'class': 'Actinopterygii',\n",
" 'classKey': 204,\n",
" 'confidence': 92,\n",
" 'family': 'Gadidae',\n",
" 'familyKey': 3701,\n",
" 'genus': 'Gadus',\n",
" 'genusKey': 2335052,\n",
" 'kingdom': 'Animalia',\n",
" 'kingdomKey': 1,\n",
" 'matchType': 'FUZZY',\n",
" 'note': 'Similarity: name=95; authorship=0; classification=-2; rank=5; status=0',\n",
" 'order': 'Gadiformes',\n",
" 'orderKey': 549,\n",
" 'phylum': 'Chordata',\n",
" 'phylumKey': 44,\n",
" 'rank': 'SPECIES',\n",
" 'scientificName': 'Gadus morrhua Linnaeus, 1758',\n",
" 'species': 'Gadus morhua',\n",
" 'speciesKey': 8084280,\n",
" 'status': 'SYNONYM',\n",
" 'synonym': True,\n",
" 'usageKey': 2415847}],\n",
" 'canonicalName': 'Gadus morhua',\n",
" 'class': 'Actinopterygii',\n",
" 'classKey': 204,\n",
" 'confidence': 92,\n",
" 'family': 'Gadidae',\n",
" 'familyKey': 3701,\n",
" 'genus': 'Gadus',\n",
" 'genusKey': 2335052,\n",
" 'kingdom': 'Animalia',\n",
" 'kingdomKey': 1,\n",
" 'matchType': 'FUZZY',\n",
" 'note': 'Similarity: name=95; authorship=0; classification=-2; rank=5; status=1; nextMatch=0',\n",
" 'order': 'Gadiformes',\n",
" 'orderKey': 549,\n",
" 'phylum': 'Chordata',\n",
" 'phylumKey': 44,\n",
" 'rank': 'SPECIES',\n",
" 'scientificName': 'Gadus morhua Linnaeus, 1758',\n",
" 'species': 'Gadus morhua',\n",
" 'speciesKey': 8084280,\n",
" 'status': 'ACCEPTED',\n",
" 'synonym': False,\n",
" 'usageKey': 8084280}"
]
},
"metadata": {},
"execution_count": 101
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "tJ6yLSpEZc0Q"
},
"source": [
"# Global Names Resolver API\n",
"# Documentation:\n",
"# http://gnrd.globalnames.org/apidoc\n",
"# https://apidoc.globalnames.org/gnfinder"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "EpZr0_dpZrf-"
},
"source": [
"## LSID resolver\n",
"## Documentation: http://www.lsid.info/resolver/\n",
"url = \"http://www.lsid.info/resolver/?lsid=urn%3Alsid%3Amarinespecies.org%3Ataxname%3A127160\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "boPL5qU8ajdm"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment