123

SEEK ID: https://testing.sysmo-db.org/data_files/2157?version=1

Filename: create_and_link_isa_datafile.ipynb  Download

Format: Plain text document

Size: 17.4 KB

{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "create_and_link_isa_datafile.ipynb",
      "version": "0.3.2",
      "views": {},
      "default_view": {},
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "eHED0l52PClp",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Import the libraries so that they can be used within the notebook\n",
        "\n",
        "* **requests** is used to make HTTP calls\n",
        "* **json** is used to encode and decode strings into JSON\n",
        "* **string** is used to perform text manipulation and checking\n",
        "* **getpass** is used to do non-echoing password input"
      ]
    },
    {
      "metadata": {
        "id": "5EGmpx3Id7py",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "import requests\n",
        "import json\n",
        "import string\n",
        "import getpass"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "u9t7T9pmQJsn",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "The **base_url** holds the URL to the SEEK instance that will be used in the notebook\n",
        "\n",
        "**headers** holds the HTTP headers that will be sent with every HTTP call\n",
        "\n",
        "* **Content-type: application/vnd.api+json** - indicates that any data sent will be in JSON API format\n",
        "* **Accept: application/vnd.api+json** - indicates that the notebook expects any data returned to be in JSON API format\n",
        "* **Accept-Charset: ISO-8859-1** - indicates that the notebook expects any text returned to be in ISO-8859-1 character set"
      ]
    },
    {
      "metadata": {
        "id": "QKepIS_xd7qL",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "base_url = 'https://testing.sysmo-db.org'\n",
        "\n",
        "headers = {\"Content-type\": \"application/vnd.api+json\",\n",
        "           \"Accept\": \"application/vnd.api+json\",\n",
        "           \"Accept-Charset\": \"ISO-8859-1\"}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "W0U3A7RNQJsu",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Create a **requests** HTTP **Session**. A **Session** has re-usable settings such as **headers**\n",
        "\n",
        "The **authorization** is username and password. The user is prompted for this information."
      ]
    },
    {
      "metadata": {
        "id": "UKqc5Sr6d7qS",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "session = requests.Session()\n",
        "session.headers.update(headers)\n",
        "session.auth = (input('Username: '), getpass.getpass('Password: '))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "0xz097gufmP3",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "The **Investigation**, **Study** and **Assay** will be created within **Project** 33"
      ]
    },
    {
      "metadata": {
        "id": "CPcirVGad7qa",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "containing_project_id = 33\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "RO6uFM50fm3A",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Initialize an **investigation** as a hierarchical structure\n",
        "\n",
        "The title of the **investigation** is input by the user\n",
        "\n",
        "The **investigation** is linked to the containing **project**"
      ]
    },
    {
      "metadata": {
        "id": "L57OF0NDfi1X",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "investigation = {}\n",
        "investigation['data'] = {}\n",
        "investigation['data']['type'] = 'investigations'\n",
        "investigation['data']['attributes'] = {}\n",
        "investigation['data']['attributes']['title'] = input('Please enter the name for the investigation: ')\n",
        "investigation['data']['relationships'] = {}\n",
        "investigation['data']['relationships']['projects'] = {}\n",
        "investigation['data']['relationships']['projects']['data'] = [{'id' : containing_project_id, 'type' : 'projects'}]\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "zqhO4RncfnvQ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "**POST** the **investigation** to the SEEK instance\n",
        "\n",
        "Check the status of the response"
      ]
    },
    {
      "metadata": {
        "id": "J5yCrZxWd7qh",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "r = session.post(base_url + '/investigations', json=investigation)\n",
        "r.raise_for_status()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "GkAb7e4afp7Q",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the created **investigation** from JSON into **populated_investigation**"
      ]
    },
    {
      "metadata": {
        "id": "q978_6Xad7qp",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "populated_investigation = r.json()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "jpezMhAsfqY-",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the id and URL to the newly created **investigation**"
      ]
    },
    {
      "metadata": {
        "id": "PLNTdbBud7qw",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "investigation_id = populated_investigation['data']['id']\n",
        "investigation_url = populated_investigation['data']['links']['self']"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "wYgOI2vBg4pR",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Initialize a **study** as a hierarchical structure\n",
        "\n",
        "The title of the **study** is input by the user\n",
        "\n",
        "The **study** is linked to the containing **investigation**"
      ]
    },
    {
      "metadata": {
        "id": "XwZlfPRcd7q3",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "study = {}\n",
        "study['data'] = {}\n",
        "study['data']['type'] = 'studies'\n",
        "study['data']['attributes'] = {}\n",
        "study['data']['attributes']['title'] = input('Please enter the name for the study: ')\n",
        "study['data']['relationships'] = {}\n",
        "study['data']['relationships']['investigation'] = {}\n",
        "study['data']['relationships']['investigation']['data'] = {'id' : investigation_id, 'type' : 'investigations'}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "P9XaltLJfsO4",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "**POST** the **study** to the SEEK instance\n",
        "\n",
        "Check the status of the response"
      ]
    },
    {
      "metadata": {
        "id": "TJmIZVped7rB",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "r = session.post(base_url + '/studies', json=study)\n",
        "r.raise_for_status()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "QQBoe-eYftUM",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the created **study** from JSON into ***populated_study***"
      ]
    },
    {
      "metadata": {
        "id": "AZCIxQ7ld7rJ",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "populated_study = r.json()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "J7mBOjGfftyw",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the id and URL to the newly created **study**"
      ]
    },
    {
      "metadata": {
        "id": "wUjRwfsAd7rT",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "study_id = populated_study['data']['id']\n",
        "study_url = populated_study['data']['links']['self']"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "l1m0WdBwfuYZ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Initialize an **assay** as a hierarchical structure\n",
        "\n",
        "The title of the **assay** is input by the user\n",
        "\n",
        "The **assay** is linked to the containing **study**"
      ]
    },
    {
      "metadata": {
        "id": "nH06WK2Md7ra",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "assay = {}\n",
        "assay['data'] = {}\n",
        "assay['data']['type'] = 'assays'\n",
        "assay['data']['attributes'] = {}\n",
        "assay['data']['attributes']['title'] = input('Please enter the name for the assay: ')\n",
        "assay['data']['attributes']['assay_class'] = {'key' : 'EXP'}\n",
        "assay['data']['attributes']['assay_type'] = {'uri' : 'http://jermontology.org/ontology/JERMOntology#Metabolomics'}\n",
        "assay['data']['attributes']['technology_type'] = {'uri' : 'http://jermontology.org/ontology/JERMOntology#Electrophoresis'}\n",
        "assay['data']['relationships'] = {}\n",
        "assay['data']['relationships']['study'] = {}\n",
        "assay['data']['relationships']['study']['data'] = {'id' : study_id, 'type' : 'studies'}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ULoGuncMfvWA",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "**POST** the **assay** to the SEEK instance\n",
        "\n",
        "Check the status of the response"
      ]
    },
    {
      "metadata": {
        "id": "XErMRQxXd7rh",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "r = session.post(base_url + '/assays', json=assay)\n",
        "r.raise_for_status()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "FHwx_wJYfwMA",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the created **assay** from JSON into ***populated_assay***"
      ]
    },
    {
      "metadata": {
        "id": "90DGQHGod7rr",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "populated_assay = r.json()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "aNisbuWBfwtp",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the id and URL to the newly created **assay**"
      ]
    },
    {
      "metadata": {
        "id": "G_wluR6Vd7ry",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "assay_id = populated_assay['data']['id']\n",
        "assay_url = populated_assay['data']['links']['self']"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "tFUk-pW3tfZ8",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Initialize a **data_file** as a hierarchical structure\n",
        "\n",
        "The title of the **data_file** is input by the user\n",
        "\n",
        "The **data_file** is linked to the containing **project** and to the newly created **assay**"
      ]
    },
    {
      "metadata": {
        "id": "kz0EXrulkMgz",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "data_file = {}\n",
        "data_file['data'] = {}\n",
        "data_file['data']['type'] = 'data_files'\n",
        "data_file['data']['attributes'] = {}\n",
        "data_file['data']['attributes']['title'] = input('Please enter the name for the data_file: ')\n",
        "\n",
        "remote_blob = {'url' : input('Please enter the URL for the remote data: ')}\n",
        "data_file['data']['attributes']['content_blobs'] = [remote_blob]\n",
        "data_file['data']['relationships'] = {}\n",
        "data_file['data']['relationships']['projects'] = {}\n",
        "data_file['data']['relationships']['projects']['data'] = [{'id' : containing_project_id, 'type' : 'projects'}]\n",
        "data_file['data']['relationships']['assays'] = {}\n",
        "data_file['data']['relationships']['assays']['data'] = [{'id' : assay_id, 'type' : 'assays'}]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "8zfedCGqtfaE",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "**POST** the **data_file** to the SEEK instance\n",
        "\n",
        "Check the status of the response"
      ]
    },
    {
      "metadata": {
        "id": "gVJHVELEjl6E",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "r = session.post(base_url + '/data_files', json=data_file)\n",
        "r.raise_for_status()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "96C5Xk9utfaR",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the created **data_file** from JSON into **populated_data_file**"
      ]
    },
    {
      "metadata": {
        "id": "2uFXwFfrjl6M",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "populated_data_file = r.json()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "7K76a1b1tfaZ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Extract the id and URL to the newly created **data_file**"
      ]
    },
    {
      "metadata": {
        "id": "WvypbUN1jl6T",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "data_file_id = populated_data_file['data']['id']\n",
        "data_file_url = populated_data_file['data']['links']['self']"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}
help Creators and Submitter
Creators
Not specified
Submitter
Activity

Views: 1301   Downloads: 174

Created: 7th Jun 2022 at 09:41

Last updated: 27th Nov 2024 at 13:54

help Tags

This item has not yet been tagged.

help Attributions

None

Version History

Version 2 (latest) Created 7th Jun 2022 at 09:52 by Finn Bacall

local file

Version 1 (earliest) Created 7th Jun 2022 at 09:41 by Finn Bacall

No revision comments

Powered by
(v.1.18.0)