This is a FastAPI project which uses strawberry to query an elasticsearch database.
This API is live at - http://annoq.org/api-v2/
- /graphql - Graphql endpoint of the API where all the queries can be made through strawberry graphiQL.
- /annotations - Returns a json with the annotation tree which has field names for strawberry queries.
- /download/{folder}/{name} - Downloads a text file using the download path returned in the download graphql query.
Before you begin, make sure you have the following installed:
- Python: Install Python 3.9 or later. You can download it from python.org.
- Docker Desktop: Install Docker Desktop. You can download it from docker-desktop.
- Clone this repository.
git clone https://github.com/USCbiostats/annoq-api-v2.git
cd annoq-api-v2
- Create a python virtual environment and activate it.
python3 -m venv venv
source venv/bin/activate
- Install the dependencies
pip install -r requirements.txt
- Make sure that the Docker Desktop is running. Build the Docker image and start the container.
docker-compose up --build
- Once the image and containers are made, the containers can be started from Docker Desktop or using the following command
docker-compose up
The fastAPI application would be running on http://0.0.0.0:8000 and the elasticsearch instance would be on http://0.0.0.0:9200
Follow the https://github.com/USCbiostats/annoq-database repository and use the sample_data folder to setup the sample data for elasticsearch. If necessary, modify file .env to reflect URL of database.
Annoq has 500+ attributes, so the strawberry type for it had to be generated dynamically as it would not make sense to manually write 500 fields. This class has to be executed whenever where are any changes in the schema:
First a json schema was generated which takes the mapping for the elasticsearch database and creates a schema for a pydantic Baseclass. After this scripts/class_generators/class_schema.json was generated. The python file of the pydantic Baseclass - models/Snps.py is generated using datamodel-codegen.
All of this can be done using the bash script and running the following command -
scripts/class_generators/generate_model.sh
Make sure that the above scripts has permissions, if not run
chmod +x scripts/class_generators/generate_model.sh
Change the cron_job.sh file change downloads in line 1 to the absolute path of the download folder in this repo after cloning and then run the following command which clears the download folder once a week at midnight.
chmod +x cron_job.sh
./cron_job.sh
python -m src.main
To run the tests on the code use the following command
python -m pytest test