Skip to main content

Backend of OpenCloning, a web application to generate molecular cloning strategies in json format, and share them with others.

Project description

Python tests codecov

OpenCloning Backend API

This API is part of a bigger application, before going further, please go to the main project readme, where you can find an introduction.

This python API is built with FastAPI and is for in silico cloning.

Summary

Read main project readme first.

This API provides a series of entry points. The API documentation can be accessed here. You can use the documentation page to try some request directly on the browser. Otherwise, the API is open for you to make requests from a python script or command line at: https://api.opencloning.org/.

Scripting with pydna

You can write python scripts to automate cloning using the python library pydna, which is now integrated with the OpenCloning data model. See the documentation for how to get started.

Migrating between model versions and fixing model bugs

  • The data model changes, so the json files you created may not be compatible with the newest version of the library, which uses the latest data mode. You can easily fix this using python -m opencloning_linkml.migrations.migrate file.json see full documentation.
  • Before version 0.3, there was a bug for assembly fields that included locations spanning the origin. See the details and how to fix it in the documentation of this file.

Getting started

If you want to quickly set up a local instance of the frontend and backend of the application, check getting started in 5 minutes in the main repository.

Running locally

You can install this as a python package:

# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
source .venv/bin/activate
# Install the package from pypi
pip install opencloning
# Run the API (uvicorn should be installed in the virtual environment)
uvicorn opencloning.main:app

Running locally if you want to contribute

For the management of the dependencies poetry is used, if you don't have it, visit https://python-poetry.org/.

In the project directory:

# This should install the dependencies and create a virtual environment
poetry install

# Install the pre-commit hooks
pre-commit install

# Activate the virtual environment (used to be `poetry shell`)
poetry env activate

The virtual environment is installed in the project folder. This is convenient if you are using an IDE for development. For settings of vscode see the folder .vscode.

Now you should be able to run the api by running:

# The --reload argument will reload the API if you make changes to the code
uvicorn opencloning.main:app --reload --reload-exclude='.venv'

Then you should be able to open the API docs at http://127.0.0.1:8000/docs to know that your API is working.

Running locally with docker :whale:

If you want to serve the full site (backend and frontend) with docker, check getting started in 5 minutes in the main repository.

If you want to serve only the backend from a docker container, an image is available at manulera/opencloningbackend. The image is built from the Dockerfile in the root of this repository and exposes the port 3000. To run it:

docker build -t manulera/opencloningbackend .
docker run -d --name backendcontainer -p 8000:8000 manulera/opencloningbackend

If you don't want to download the repository and build the image, you can fetch the latest image from dockerhub.

docker pull manulera/opencloningbackend
docker run -d --name backendcontainer -p 8000:8000 manulera/opencloningbackend

The api will be running at http://localhost:8000, so you should be able to access the docs at http://localhost:8000/docs.

Connecting to the frontend

If you want to receive requests from the frontend, or from another web application you may have to include the url of the frontend application in the CORS exceptions. By default, if you run the dev server with uvicorn opencloning.main:app --reload --reload-exclude='.venv', the backend will accept requests coming from http://localhost:3000, which is the default address of the frontend dev server (ran with yarn start).

If you want to change the allowed origins, you can do so via env variables (comma-separated). e.g.:

ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001 uvicorn opencloning.main:app --reload --reload-exclude='.venv'

Similarly, the frontend should be configured to send requests to the backend address, see here.

Serving the frontend from the backend

You may prefer to handle everything from a single server. You can do so by:

  • Build the frontend with yarn build.
  • Copy the folder build from the frontend to the root directory of the backend, and rename it to frontend.
  • Set the environment variable SERVE_FRONTEND=1 when running the backend. By default this will remove all allowed origins, but you can still set them with ALLOWED_ORIGINS.
  • Set the value of backendUrl in frontend/config.js to /.
  • Now, when you go to the root of the backend (e.g. http://localhost:8000), you should receive the frontend instead of the greeting page of the API.

You can see how this is done in this docker image and docker-compose file.

Contributing :hammer_and_wrench:

Check contribution guidelines in the main repository for general guidelines.

For more specific tasks:

Running the tests locally

pytest -v -ks

Notes

Ping a particular library version from github:

poetry add git+https://github.com/pydna-group/pydna#4fd760d075f77cceeb27969e017e04b42f6d0aa3

When installing the last version, sometimes poetry may not be able to access the latest version and complain with Could not find a matching version, in that case you can do:

poetry cache clear pypi --all

Generating API stubs

For the frontend, it may be useful to produce stubs (I use them for writing the tests). See how this is implemented by looking at the RecordStubRoute class in api_config_utils.py. To run the dev server and record stubs:

RECORD_STUBS=1 uvicorn opencloning.main:app --reload --reload-exclude='.venv'

This will record the stubs (requests and responses) in the stubs folder.

Catalogs

Catalogs are used to map ids to urls for several plasmid collections. They are stored in the src/opencloning/catalogs folder.

To update the catalogs, run the following command:

poetry run python scripts/update_catalogs.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opencloning-1.0.1.tar.gz (130.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opencloning-1.0.1-py3-none-any.whl (144.1 kB view details)

Uploaded Python 3

File details

Details for the file opencloning-1.0.1.tar.gz.

File metadata

  • Download URL: opencloning-1.0.1.tar.gz
  • Upload date:
  • Size: 130.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for opencloning-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f6b904ef3ebecfa2cc88905bab79d435ef0687a475fc30aa4f46f5c6610528dc
MD5 4a84064926e38d3245157aeb73839ebe
BLAKE2b-256 09bf044643af0198fff220336e206887d5178607d915c9cb02bbfcd852c09cde

See more details on using hashes here.

Provenance

The following attestation bundles were made for opencloning-1.0.1.tar.gz:

Publisher: post_release.yml on manulera/OpenCloning_backend

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file opencloning-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: opencloning-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 144.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for opencloning-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ed0e89d1c2c0a7c102145b5fe504cc5332cd3aa3283fad0e19b2afaf7853d73
MD5 f25057e3e37dd19b9a94fdb33150f5b1
BLAKE2b-256 a7e60e2f482a01ac1d571a814fa82b31f62f7e7b32599a5e99228e7d34c5a9cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for opencloning-1.0.1-py3-none-any.whl:

Publisher: post_release.yml on manulera/OpenCloning_backend

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page