# DTP Explain: `dtp_mondo_relationships`

## 1. Data source and pipeline role

- `data_source.name`: `mondo_relationships`
- `source_system`: `MONDO`
- source URL in seed: `not_applicable`
- format in seed: empty / not applicable

Pipeline role:
- relationship-only DTP
- consumes staging generated by `dtp_mondo`
- inserts MONDO-derived links into `EntityRelationship`

Operational dependency:
- run this only after `dtp_mondo` transform/load and after target master domains are present in DB (Diseases, Genes, Chemicals, etc.).

## 2. Extract

Behavior:
- no extraction
- returns informational success message

Reason:
- source relationship data is produced by `dtp_mondo`

## 3. Transform

Behavior:
- no transform
- returns informational success message

Reason:
- relationship staging is produced by `dtp_mondo` transform

## 4. Load

Input file:
- `<processed_dir>/<source_system>/mondo/relationship_data.parquet`
- note: reads from parent source folder `mondo`

Main goal:
- resolve source/target entities and relationship type IDs
- insert rows into `EntityRelationship`

### 4.1 Preload maps

Group map:
- loads all `EntityGroup`
- maps `EntityGroup.name.lower() -> id`

Relationship type map:
- loads all `EntityRelationshipType`
- maps `EntityRelationshipType.code.lower() -> id`

### 4.2 Insert flow

Before insertion:
- deletes all existing rows for current data source:
  - `EntityRelationship.data_source_id = self.data_source.id`

Per row:
1. read `term1_code`, `term2_code`, `relation_type`, `term1_group`, `term2_group`
2. map groups + relation type to IDs
3. resolve source entity by exact alias match + active entity + source group
4. resolve target entity by exact alias match + active entity + target group
5. insert `EntityRelationship` with:
   - `entity_1_id`, `entity_2_id`
   - `entity_1_group_id`, `entity_2_group_id`
   - `relationship_type_id`
   - `data_source_id`, `etl_package_id`

Commit:
- commits at end of batch

## 5. Rejections and diagnostics

Rows not loaded:
- missing mapped group ID
- missing mapped relationship type ID
- source/target entity not found by alias and group
- unexpected row-level exceptions

Diagnostic output:
- `<processed_dir>/<source_system>/mondo/relationship_data_not_loaded.csv`

Operational behavior:
- recreates entity index group and switches DB to read mode at end

## 6. Dependency checklist (recommended)

Before running `mondo_relationships`, ensure:
1. `dtp_mondo` completed and generated `relationship_data.parquet`
2. target domains referenced by MONDO are loaded (at least Diseases, Genes, Chemicals)
3. relationship types present in `EntityRelationshipType` for expected codes

Important note:
- MONDO transform can emit `is_a`, `Disease_has_disruption`, `located_in`, `in_taxon`, and fallback raw predicates.
- If a code is absent in `EntityRelationshipType`, row is skipped and exported to `relationship_data_not_loaded.csv`.
