sparql-api-codegen


Namesparql-api-codegen JSON
Version 0.0.1 PyPI version JSON
download
home_pageNone
SummaryAutomatically generate python API package from a SPARQL endpoint using its VoID descriptive metadata
upload_time2024-12-18 13:35:14
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2024-present SIB Swiss Institute of Bioinformatics Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords python sparql
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

# ✨ SPARQL API code generator 🐍

</div>

A CLI tool to automatically generate a python package from a SPARQL endpoint VoID description.

It will generate a folder with all requirements for publishing a modern python package containing the classes to automatically work with the data in the endpoint.

Features:

* Each class in the endpoint will be defined as a python class, with fields for each predicates available on a class.
* It will use the classes and predicates labels from their ontology when possible to generate the python classes and their fields
* Type annotations are used for better autocompletion
* Fields of a class are retrieved when the field is called (lazy 🦥)

## 🪄 Usage

> Requirements: Python >=3.9

1. Install the package with `pip` or `pipx`:

   ```sh
   pipx install sparql-api-codegen
   ```

2. Generate the code for a SPARQL endpoint which contains a SPARQL Service Description:

   ```sh
   sparql-api-codegen <sparql-endpoint-url> <folder-for-generated-python-pkg> -i <iri-of-class-to-ignore>
   ```

3. Once the folders have been generated you can get into the folder, check and improve the instructions to run in the `README.md`, improve the metadata in the `pyproject.toml`

Optionally you can ignore some classes. For some endpoints this will be required if the label generated for 2 classes are identical, e.g. for Bgee:

```sh
sparql-api-codegen "https://www.bgee.org/sparql/" "bgee-api" \
	-i http://purl.obolibrary.org/obo/CARO_0000000 \
	-i http://purl.obolibrary.org/obo/SO_0000704 \
	-i http://purl.obolibrary.org/obo/NCIT_C14250
```

Example python API for Bgee:

```python
from bgee_api import AnatomicalEntity, Gene, GeneExpressionExperimentCondition


if __name__ == "__main__":
    all_anats = AnatomicalEntity.get()
    print(len(all_anats), all_anats[0])

    anat = AnatomicalEntity("http://purl.obolibrary.org/obo/AEO_0000013")
    print(anat)
    print(anat.label)
    print(anat.expresses)

    gene= Gene("http://omabrowser.org/ontology/oma#GENE_ENSMUSG00000053483")
    print(gene.label)

    cond = GeneExpressionExperimentCondition("http://bgee.org/#EXPRESSION_CONDITION_101909")
    print(cond.has_a_developmental_stage)
    print(cond.has_anatomical_entity)
```

For UniProt:

```sh
sparql-api-codegen "https://sparql.uniprot.org/sparql/" "uniprot-api" \
	-i http://biohackathon.org/resource/faldo#Region
```

## 🧑‍💻 Development setup

The final section of the README is for if you want to run the package in development, and get involved by making a code contribution.


### 📥️ Clone

Clone the repository:

```bash
git clone https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen
cd sparql-api-codegen
```

### 🐣 Install dependencies

Install [Hatch](https://hatch.pypa.io), a modern build system, as well as project and virtual env management tool recommended by the Python Packaging Authority. This will automatically handle virtual environments and make sure all dependencies are installed when you run a script in the project:

```bash
pipx install hatch
```

Or you could install in your favorite virtual env:

```bash
pip install -e ".[test]"
```

### 🛠️ Develop

Test with the Bgee endpoint:

```bash
hatch run sparql-api-codegen "https://www.bgee.org/sparql/" "bgee-api" \
    -i http://purl.obolibrary.org/obo/CARO_0000000 \
    -i http://purl.obolibrary.org/obo/SO_0000704 \
    -i http://purl.obolibrary.org/obo/NCIT_C14250
```

### ☑️ Run tests

Make sure the existing tests still work by running the test suite and linting checks. Note that any pull requests to the fairworkflows repository on github will automatically trigger running of the test suite;

```bash
hatch run test
```

To display all logs when debugging:

```bash
hatch run test -s
```

### ♻️ Reset the environment

In case you are facing issues with dependencies not updating properly you can easily reset the virtual environment with:

```bash
hatch env prune
```

Manually trigger installing the dependencies in a local virtual environment:

```bash
hatch -v env create
```

### 🏷️ New release process

The deployment of new releases is done automatically by a GitHub Action workflow when a new release is created on GitHub. To release a new version:

1. Make sure the `PYPI_TOKEN` secret has been defined in the GitHub repository (in Settings > Secrets > Actions). You can get an API token from PyPI at [pypi.org/manage/account](https://pypi.org/manage/account).
2. Increment the `version` number in the `pyproject.toml` file in the root folder of the repository.

    ```bash
    hatch version fix
    ```

3. Create a new release on GitHub, which will automatically trigger the publish workflow, and publish the new release to PyPI.

You can also build and publish from your computer:

```bash
hatch build
hatch publish
```

### TODO

- Bulk load with preloaded fields

  ```python
  all_anats_preloaded: list[AnatomicalEntity] = bulk_load(AnatomicalEntity, ["label", "expresses"])
  # Or
  all_anats_preloaded: list[AnatomicalEntity] = AnatomicalEntity.get(["label", "expresses"])
  ```

  > Allow also to pass a list of IRI (optional, if not we get all?)

- Returns pandas matrix with filters?

  ```python
  pandas_matrix = BiologicalEntity.get_matrix(
      filter_has_a_developmental_stage="http://some_dev_stage",
      filter_has_anatomical_entity="some anatomical entity",
  )
  ```

  > Also enable to filter on labels instead of IRI?

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "sparql-api-codegen",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Vincent Emonet <vincent.emonet@sib.swiss>",
    "keywords": "Python, SPARQL",
    "author": null,
    "author_email": "Vincent Emonet <vincent.emonet@sib.swiss>",
    "download_url": "https://files.pythonhosted.org/packages/29/97/13fc8ede010dbf0cae020c1bf791036c5eb3912572e519c5f2ed1a7e61b4/sparql_api_codegen-0.0.1.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n\n# \u2728 SPARQL API code generator \ud83d\udc0d\n\n</div>\n\nA CLI tool to automatically generate a python package from a SPARQL endpoint VoID description.\n\nIt will generate a folder with all requirements for publishing a modern python package containing the classes to automatically work with the data in the endpoint.\n\nFeatures:\n\n* Each class in the endpoint will be defined as a python class, with fields for each predicates available on a class.\n* It will use the classes and predicates labels from their ontology when possible to generate the python classes and their fields\n* Type annotations are used for better autocompletion\n* Fields of a class are retrieved when the field is called (lazy \ud83e\udda5)\n\n## \ud83e\ude84 Usage\n\n> Requirements: Python >=3.9\n\n1. Install the package with `pip` or `pipx`:\n\n   ```sh\n   pipx install sparql-api-codegen\n   ```\n\n2. Generate the code for a SPARQL endpoint which contains a SPARQL Service Description:\n\n   ```sh\n   sparql-api-codegen <sparql-endpoint-url> <folder-for-generated-python-pkg> -i <iri-of-class-to-ignore>\n   ```\n\n3. Once the folders have been generated you can get into the folder, check and improve the instructions to run in the `README.md`, improve the metadata in the `pyproject.toml`\n\nOptionally you can ignore some classes. For some endpoints this will be required if the label generated for 2 classes are identical, e.g. for Bgee:\n\n```sh\nsparql-api-codegen \"https://www.bgee.org/sparql/\" \"bgee-api\" \\\n\t-i http://purl.obolibrary.org/obo/CARO_0000000 \\\n\t-i http://purl.obolibrary.org/obo/SO_0000704 \\\n\t-i http://purl.obolibrary.org/obo/NCIT_C14250\n```\n\nExample python API for Bgee:\n\n```python\nfrom bgee_api import AnatomicalEntity, Gene, GeneExpressionExperimentCondition\n\n\nif __name__ == \"__main__\":\n    all_anats = AnatomicalEntity.get()\n    print(len(all_anats), all_anats[0])\n\n    anat = AnatomicalEntity(\"http://purl.obolibrary.org/obo/AEO_0000013\")\n    print(anat)\n    print(anat.label)\n    print(anat.expresses)\n\n    gene= Gene(\"http://omabrowser.org/ontology/oma#GENE_ENSMUSG00000053483\")\n    print(gene.label)\n\n    cond = GeneExpressionExperimentCondition(\"http://bgee.org/#EXPRESSION_CONDITION_101909\")\n    print(cond.has_a_developmental_stage)\n    print(cond.has_anatomical_entity)\n```\n\nFor UniProt:\n\n```sh\nsparql-api-codegen \"https://sparql.uniprot.org/sparql/\" \"uniprot-api\" \\\n\t-i http://biohackathon.org/resource/faldo#Region\n```\n\n## \ud83e\uddd1\u200d\ud83d\udcbb Development setup\n\nThe final section of the README is for if you want to run the package in development, and get involved by making a code contribution.\n\n\n### \ud83d\udce5\ufe0f Clone\n\nClone the repository:\n\n```bash\ngit clone https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen\ncd sparql-api-codegen\n```\n\n### \ud83d\udc23 Install dependencies\n\nInstall [Hatch](https://hatch.pypa.io), a modern build system, as well as project and virtual env management tool recommended by the Python Packaging Authority. This will automatically handle virtual environments and make sure all dependencies are installed when you run a script in the project:\n\n```bash\npipx install hatch\n```\n\nOr you could install in your favorite virtual env:\n\n```bash\npip install -e \".[test]\"\n```\n\n### \ud83d\udee0\ufe0f Develop\n\nTest with the Bgee endpoint:\n\n```bash\nhatch run sparql-api-codegen \"https://www.bgee.org/sparql/\" \"bgee-api\" \\\n    -i http://purl.obolibrary.org/obo/CARO_0000000 \\\n    -i http://purl.obolibrary.org/obo/SO_0000704 \\\n    -i http://purl.obolibrary.org/obo/NCIT_C14250\n```\n\n### \u2611\ufe0f Run tests\n\nMake sure the existing tests still work by running the test suite and linting checks. Note that any pull requests to the fairworkflows repository on github will automatically trigger running of the test suite;\n\n```bash\nhatch run test\n```\n\nTo display all logs when debugging:\n\n```bash\nhatch run test -s\n```\n\n### \u267b\ufe0f Reset the environment\n\nIn case you are facing issues with dependencies not updating properly you can easily reset the virtual environment with:\n\n```bash\nhatch env prune\n```\n\nManually trigger installing the dependencies in a local virtual environment:\n\n```bash\nhatch -v env create\n```\n\n### \ud83c\udff7\ufe0f New release process\n\nThe deployment of new releases is done automatically by a GitHub Action workflow when a new release is created on GitHub. To release a new version:\n\n1. Make sure the `PYPI_TOKEN` secret has been defined in the GitHub repository (in Settings > Secrets > Actions). You can get an API token from PyPI at [pypi.org/manage/account](https://pypi.org/manage/account).\n2. Increment the `version` number in the `pyproject.toml` file in the root folder of the repository.\n\n    ```bash\n    hatch version fix\n    ```\n\n3. Create a new release on GitHub, which will automatically trigger the publish workflow, and publish the new release to PyPI.\n\nYou can also build and publish from your computer:\n\n```bash\nhatch build\nhatch publish\n```\n\n### TODO\n\n- Bulk load with preloaded fields\n\n  ```python\n  all_anats_preloaded: list[AnatomicalEntity] = bulk_load(AnatomicalEntity, [\"label\", \"expresses\"])\n  # Or\n  all_anats_preloaded: list[AnatomicalEntity] = AnatomicalEntity.get([\"label\", \"expresses\"])\n  ```\n\n  > Allow also to pass a list of IRI (optional, if not we get all?)\n\n- Returns pandas matrix with filters?\n\n  ```python\n  pandas_matrix = BiologicalEntity.get_matrix(\n      filter_has_a_developmental_stage=\"http://some_dev_stage\",\n      filter_has_anatomical_entity=\"some anatomical entity\",\n  )\n  ```\n\n  > Also enable to filter on labels instead of IRI?\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2024-present SIB Swiss Institute of Bioinformatics\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "Automatically generate python API package from a SPARQL endpoint using its VoID descriptive metadata",
    "version": "0.0.1",
    "project_urls": {
        "Documentation": "https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen",
        "History": "https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen/releases",
        "Homepage": "https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen",
        "Source": "https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen",
        "Tracker": "https://github.com/TRIPLE-CHIST-ERA/sparql-api-codegen/issues"
    },
    "split_keywords": [
        "python",
        " sparql"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c977c30bcd0e7cd1aa790a47df87a5e0b11afb94a88dfcc52f56146ca648ed63",
                "md5": "291a62433f6ec5926b8c58c4c190c151",
                "sha256": "2ca87b7901728dac7aa6e7c22b9c3273edfe74583942b4416620efe2e36ce7fc"
            },
            "downloads": -1,
            "filename": "sparql_api_codegen-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "291a62433f6ec5926b8c58c4c190c151",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 12927,
            "upload_time": "2024-12-18T13:35:15",
            "upload_time_iso_8601": "2024-12-18T13:35:15.777220Z",
            "url": "https://files.pythonhosted.org/packages/c9/77/c30bcd0e7cd1aa790a47df87a5e0b11afb94a88dfcc52f56146ca648ed63/sparql_api_codegen-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "299713fc8ede010dbf0cae020c1bf791036c5eb3912572e519c5f2ed1a7e61b4",
                "md5": "bf040e24549fc4c792cf4e6725668f4e",
                "sha256": "d31e941d15ed67aa0fad023ad57e5675396ad5d51021c7773ba88e8e92dfdd7f"
            },
            "downloads": -1,
            "filename": "sparql_api_codegen-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "bf040e24549fc4c792cf4e6725668f4e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 11337,
            "upload_time": "2024-12-18T13:35:14",
            "upload_time_iso_8601": "2024-12-18T13:35:14.364384Z",
            "url": "https://files.pythonhosted.org/packages/29/97/13fc8ede010dbf0cae020c1bf791036c5eb3912572e519c5f2ed1a7e61b4/sparql_api_codegen-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-18 13:35:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TRIPLE-CHIST-ERA",
    "github_project": "sparql-api-codegen",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sparql-api-codegen"
}
        
Elapsed time: 0.45755s