Infrastructure Module¶
Infrastructure and utility classes.
alienbio.infra
¶
Infrastructure: entity base classes, I/O, serialization.
Entity
¶
Base class for all biology objects.
Entities have a three-part structure (like a function call): - head: the entity type name (e.g., "Chemistry", "Molecule") - args: ordered children (contained entities) - attributes: keyword arguments (semantic content)
Entities form a tree structure with bidirectional links: - _parent: link to containing entity - _children: dict of child entities by local name - _top: either a Dat (for root entities) or the root Entity (for non-roots)
The _top field enables O(1) access to both root() and dat(). Names are derived by walking up the parent chain until a DAT anchor is found, then building the qualified path.
Source code in src/alienbio/infra/entity.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
local_name
property
¶
Name within parent's children dict.
parent
property
¶
Link to containing entity.
children
property
¶
Child entities by local name (read-only view).
head
property
¶
The entity's head (type name).
This is the registered name used in serialization.
full_name
property
¶
Full path from DAT anchor (e.g., 'runs/exp1.cytoplasm.glucose').
Walks up the parent chain until a DAT anchor is found, then builds the path from there.
__init_subclass__(head=None, **kwargs)
¶
Auto-register subclasses in the head registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head
|
Optional[str]
|
Optional head name for serialization. If not provided, uses the class name. |
None
|
Example
class Molecule(Entity): # registers as "Molecule" pass
class Molecule(Entity, head="Mol"): # registers as "Mol" pass
Source code in src/alienbio/infra/entity.py
__init__(name, *, parent=None, dat=None, description='')
¶
Initialize an entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Local name within parent's children dict |
required |
parent
|
Optional[Entity]
|
Link to containing entity (optional if dat provided) |
None
|
dat
|
Optional[Dat]
|
DAT anchor to filesystem (optional if parent provided) |
None
|
description
|
str
|
Human-readable description |
''
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither parent nor dat is provided |
ValueError
|
If name contains spaces |
Source code in src/alienbio/infra/entity.py
hydrate(data, *, dat=None, parent=None, local_name=None)
classmethod
¶
Create an entity instance from a dict.
This is the standard way to convert YAML/JSON data to typed objects. Subclasses should override to handle their specific fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dict containing entity data |
required |
dat
|
Optional[Dat]
|
DAT anchor (if this is a root entity) |
None
|
parent
|
Optional[Entity]
|
Parent entity (if this is a child) |
None
|
local_name
|
Optional[str]
|
Override the local name (defaults to data.get("name")) |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
New instance of the entity class |
Example
mol = MoleculeImpl.hydrate({"name": "A", "bdepth": 0}) chem = ChemistryImpl.hydrate({"molecules": {...}, "reactions": {...}})
Source code in src/alienbio/infra/entity.py
attributes()
¶
Semantic content of this entity (override in subclasses).
Returns a dict of the entity's keyword arguments - its semantic content excluding head and children (args).
Subclasses should override this to include their specific fields.
Source code in src/alienbio/infra/entity.py
dat()
¶
Get the DAT anchor for this entity's tree.
O(1) operation using the _top field.
Source code in src/alienbio/infra/entity.py
root()
¶
Get the root entity (the ancestor with the DAT anchor).
O(1) operation using the _top field.
Source code in src/alienbio/infra/entity.py
set_parent(parent)
¶
Set the parent entity.
Handles registration/deregistration in parent's children dict. Updates _top for this entity and all descendants.
If parent is None, reparents to orphan root (entities are never invalid).
Source code in src/alienbio/infra/entity.py
detach()
¶
Detach this entity from its parent.
The entity is reparented to the orphan root and remains fully valid. It can be re-attached later using set_parent().
Prints as ORPHAN:name after detaching.
Source code in src/alienbio/infra/entity.py
to_dict(recursive=False, _root=None)
¶
Convert entity to dictionary representation for serialization.
The dict has three parts (like a function call): - head: the entity type name - args: children (contained entities) - only if present and recursive - **attributes: semantic content (name, description, subclass fields)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recursive
|
bool
|
If True, include children recursively |
False
|
_root
|
Optional[Entity]
|
Internal - the root entity we're serializing from (to detect children with different roots that need absolute refs) |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with entity fields suitable for YAML/JSON serialization. |
Source code in src/alienbio/infra/entity.py
to_str(depth=-1)
¶
String representation of entity tree.
Returns a function-call style representation showing the entity and optionally its children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
int
|
How deep to recurse into children. -1 = unlimited, 0 = just this entity, 1 = include immediate children, etc. |
-1
|
Returns:
| Type | Description |
|---|---|
str
|
String like "World(Cytoplasm(Glucose, ATP), Nucleus)" |
Example
entity.to_str() # full tree entity.to_str(0) # just "World" entity.to_str(1) # "World(Cytoplasm, Nucleus)"
Source code in src/alienbio/infra/entity.py
ancestors()
¶
descendants()
¶
save()
¶
Save this entity tree to disk.
Must be called on the root entity (the one with the DAT anchor). Serializes the entire entity tree to entities.yaml in the DAT folder.
Raises:
| Type | Description |
|---|---|
ValueError
|
If not called on a root entity |
ValueError
|
If called on orphan root (orphans cannot be saved) |
Source code in src/alienbio/infra/entity.py
__repr__()
¶
Full reconstructible representation.
Source code in src/alienbio/infra/entity.py
__str__()
¶
Short display form using PREFIX:path if IO available.
Falls back to full_name if no IO or prefix matches.
Source code in src/alienbio/infra/entity.py
IO
¶
Entity I/O: naming, formatting, lookup, and persistence.
IO handles all external representation concerns for entities: - Prefix bindings: Maps short prefixes (R:, W:) to Entity or path string - Formatting: Converts entities to PREFIX:path strings - Lookup: Converts PREFIX:path strings back to entities - Persistence: Load/save entities via DAT
The 'D:' prefix is always bound to the data root as an escape hatch.
Note: For data path, use Dat.manager.sync_folder (single source of truth).
Source code in src/alienbio/infra/io.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
orphan_root
property
¶
Lazy-initialized root entity for orphaned entities.
Detached entities are re-parented here instead of becoming invalid. The ORPHAN: prefix is automatically bound to this root.
prefixes
property
¶
Current prefix bindings (read-only copy).
__init__()
¶
Initialize IO.
Source code in src/alienbio/infra/io.py
bind_prefix(prefix, target)
¶
Bind a prefix to an entity or path string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Short prefix string (e.g., "R", "W", "M") |
required |
target
|
Entity | str
|
Entity to bind, or path string to DAT location |
required |
Example
io.bind_prefix("W", world_entity) # bind to Entity io.bind_prefix("R", "runs/experiment1") # bind to path
Source code in src/alienbio/infra/io.py
unbind_prefix(prefix)
¶
Remove a prefix binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Prefix to unbind |
required |
Returns:
| Type | Description |
|---|---|
Optional[Entity | str]
|
The previously bound target, or None if not bound |
Source code in src/alienbio/infra/io.py
resolve_prefix(prefix)
¶
Get the entity bound to a prefix.
If prefix is bound to a path string, loads/creates an Entity for it. The special prefix 'D' always resolves to the data root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Prefix to resolve |
required |
Returns:
| Type | Description |
|---|---|
Entity
|
The entity bound to this prefix |
Raises:
| Type | Description |
|---|---|
KeyError
|
If prefix is not bound |
Source code in src/alienbio/infra/io.py
ref(entity, prefer_short=True, absolute=False)
¶
Get reference string for entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
Entity
|
Entity to get reference for |
required |
prefer_short
|
bool
|
If True, uses shortest matching prefix (ignored if absolute) |
True
|
absolute
|
bool
|
If True, returns absolute format |
False
|
Returns:
| Type | Description |
|---|---|
str
|
String in PREFIX:path format (e.g., "W:cytoplasm.glucose") |
str
|
or absolute format (e.g., "") |
Example
io.ref(glucose) # -> "W:cytoplasm.glucose" io.ref(glucose, absolute=True) # -> ""
Source code in src/alienbio/infra/io.py
lookup(string)
¶
Look up entity by reference string.
Supports two formats: - PREFIX:path (e.g., "W:cytoplasm.glucose") - prefix-relative - (e.g., "") - absolute
For absolute format, loads the DAT if not already loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
Reference string in either format |
required |
Returns:
| Type | Description |
|---|---|
Entity
|
The entity at the specified path |
Raises:
| Type | Description |
|---|---|
ValueError
|
If string format is invalid |
KeyError
|
If prefix is not bound or path not found |
Example
io.lookup("W:cytoplasm.glucose") # prefix-relative io.lookup("") # absolute
Source code in src/alienbio/infra/io.py
resolve_refs(obj)
¶
Recursively replace
Walks a data structure (dict, list, or scalar) and replaces any
strings matching the
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Data structure to process (dict, list, or scalar) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
New structure with entity references resolved |
Example
data = yaml.safe_load(file)
data = io.resolve_refs(data) #
Source code in src/alienbio/infra/io.py
insert_refs(obj)
¶
Recursively replace Entity objects with
Walks a data structure (dict, list, or scalar) and replaces any
Entity objects with their
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Data structure to process (dict, list, or scalar) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
New structure with entities replaced by reference strings |
Example
output = io.insert_refs(data) # Entity →
Source code in src/alienbio/infra/io.py
load(path)
¶
Load a Dat from data path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path relative to data root, or absolute path |
required |
Returns:
| Type | Description |
|---|---|
Dat
|
The loaded Dat |
save(obj, path)
¶
Save object as Dat to data path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to save. If dict, uses as spec. Otherwise wraps in {"value": obj}. |
required |
path
|
str | Path
|
Path relative to Dat.manager.sync_folder |
required |
Returns:
| Type | Description |
|---|---|
Dat
|
The created Dat |