Python access to the DINA API
The features, tests, and installations were tested with Python 3.12 and pip 24.0 and are recommended for users to install. To do this, run the following commands:
sudo apt install python3
sudo apt install python3-pip- Currently supported schemas:
- Collecting Event
- Storage Unit Usage
- Person
- Material Sample
- Metadata
- Storage Unit
- Form Template
- Split Configuration
- Metagenomics Batch
- Metagenomics Batch Item
- Molecular Analysis Result
- Molecular Analysis Run
- Molecular Analysis Run Item
- Project
- Currently supported APIs:
- Agent API
- Person
- Collection API
- CollectingEvent
- StorageUnit
- StorageUnitUsage
- FormTemplate
- SplitConfiguration
- MaterialSample
- Organism
- Project
- Object Store API
- Any Object Store API endpoint using ObjectStoreApi's CRUD methods
- SeqDB API
- PCR Batch
- PCR Batch Item
- SEQ Reaction
- Metagenomics Batch
- Metagenomics Batch Item
- Molecular Analysis Result
- Molecular Analysis Run
- Molecular Analysis Run Item
- Export API
- Any Export API endpoint using DinaExportApi's CRUD methods
- Agent API
upload_file:
- metavar="<file_path> : (str) = Path to the file to be uploaded.",
- help="Upload a file to Object Store. Argument: file_path"
upload_dir:
- metavar="<dir_path> : (str) = Path to the directory to be uploaded.",
- help="Upload all files in a directory to Object Store",
verbose:
- help="Verbosity of logs. Primarily for debugging.",
- action="store_true",
create_metadatas:
- metavar="<dir_path> : (str) = Path to the directory to be uploaded.",
- help="Upload all files in a directory to Object Store and create metadatas according to constants defined in ./dina-api-config.yml",
create_form_template:
- metavar="<file_path> : (str) = Path to the file to be parsed and created.",
- help="Create a form template according to specs defined in a yaml file such as ./form-template-sample.yml",
create_split_configuration:
- metavar="<file_path> : (str) = Path to the file to be parsed and created.",
- help="Create a split configuration according to specs defined in a yaml file such as ./split-configuration-sample.yml",
- APIs (Unit Test with Magic Mock):
- Collection API
- Collecting Event
- Managed Attributes
- Material Sample
- Organism
- Object Store API
- Metadata
- Object Export
- Agent API
- Person
- Export API
- Data Export
- Collection API
- Schemas:
- Collecting Event
- Form Template
- Managed Attribute
- Material Sample
- Metadata
- Person
- Split Configuration
- Storage Unit Usage
- Metagenomics Batch
- Metagenomics Batch Item
- Molecular Analysis Result
- Molecular Analysis Run
- Molecular Analysis Run Item
- Project
Before installing, it's recommended to use a virtual environment in Python but not required. Create a virtual environment for the dependencies required.
NOTE: Change the Python version in the following command to match your current version. It is recommended for the user to use Python version 3.12 or higher.
sudo apt install python3.12-venv
python3 -m venv env
source env/bin/activateFrom inside of the dina-py root folder, install the library and all required dependencies:
pip install -e .
pip install -r requirements.txtThe username and password are set via environment variables. This can be done via the command line:
set keycloak_username=username
set keycloak_password=passwordOr programmatically using:
os.environ["keycloak_username"] = "username"
os.environ["keycloak_password"] = "password"- Make a copy of keycloak-config-sample.yml and rename to keycloak-config.yml in the root of dina-py directory, open keycloak-config.yml using Notepad
- In keycloak-config.yml, change url, keycloak_username, keycloak_password as needed
- Make a copy of dina-api-config-sample.yml and rename to dina-api-config.yml in the root of dina-py directory, open dina-api-config.yml using Notepad
Sample usage of DINA_API_CLIENT:
(.venv) C:\Users\<your_account>\dina-py> python .\dinapy\client\dina_api_client.py -upload_dir '<object_upload_dir>'- Or run the following command to upload a specific file:
(.venv) C:\Users\<your_account>\dina-py> python .\dinapy\client\dina_api_client.py -upload_file '<object_upload_dir>/<file_name>'In code:
def test_create_delete_entity(self):
schema = MaterialSampleSchema()
material_sample_api = MaterialSampleAPI()
try:
material_sample_attributes = MaterialSampleAttributesDTOBuilder().group("aafc").materialSampleName("test").materialSampleType("WHOLE_ORGANISM")\
.build()
material_sample = MaterialSampleDTOBuilder().attributes(material_sample_attributes).build()
serialized_material_sample = schema.dump(material_sample)
pp = pprint.PrettyPrinter(indent=0)
pp.pprint(serialized_material_sample)
response = material_sample_api.create_entity(serialized_material_sample)
id = response.json()['data']['id']
if response.status_code == 201:
response = material_sample_api.remove_entity(id)
self.assertEqual(response.status_code,204)
except ValidationError as e:
self.fail(f"Validation failed with error: {e.messages}")