BioCypherWorkflow
Unified BioCypher interface for knowledge graph workflows.
This class provides a clean, simple API for creating and managing knowledge graphs with optional schema and ontology support. Designed for both agentic and deterministic workflows.
Source code in biocypher/_workflow.py
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 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 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | |
__contains__(node_id)
__init__(name='workflow_graph', directed=True, schema=None, schema_file=None, head_ontology_url=None, validation_mode='none', deduplication=False)
Initialize the workflow with a unified graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the knowledge graph |
'workflow_graph'
|
directed
|
bool
|
Whether the graph is directed (default: True) |
True
|
schema
|
dict[str, Any] | None
|
Dictionary defining the knowledge graph schema |
None
|
schema_file
|
str | None
|
Path to YAML schema file |
None
|
head_ontology_url
|
str | None
|
URL to ontology file (defaults to Biolink model) |
None
|
validation_mode
|
str
|
Validation level ("none", "warn", "strict") |
'none'
|
deduplication
|
bool
|
Whether to enable deduplication (default: False) |
False
|
Source code in biocypher/_workflow.py
__len__()
__str__()
String representation of the workflow.
Source code in biocypher/_workflow.py
add_edge(edge_id, edge_type, source, target, **properties)
Add an edge to the knowledge graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
str
|
Unique identifier for the edge |
required |
edge_type
|
str
|
Type/category of the edge |
required |
source
|
str
|
Source node ID |
required |
target
|
str
|
Target node ID |
required |
**properties
|
Any
|
Edge properties as keyword arguments |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if edge was added, False if it already exists |
Example
workflow.add_edge("interaction_1", "interaction", "protein_1", "protein_2", confidence=0.8, method="yeast_two_hybrid")
Source code in biocypher/_workflow.py
add_hyperedge(hyperedge_id, hyperedge_type, nodes, **properties)
Add a hyperedge connecting multiple nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_id
|
str
|
Unique identifier for the hyperedge |
required |
hyperedge_type
|
str
|
Type/category of the hyperedge |
required |
nodes
|
set[str]
|
Set of node IDs to connect |
required |
**properties
|
Any
|
Hyperedge properties as keyword arguments |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if hyperedge was added, False if it already exists |
Example
workflow.add_hyperedge("complex_1", "protein_complex", {"protein_1", "protein_2", "protein_3"}, name="transcription_factor_complex")
Source code in biocypher/_workflow.py
add_node(node_id, node_type, **properties)
Add a node to the knowledge graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Unique identifier for the node |
required |
node_type
|
str
|
Type/category of the node |
required |
**properties
|
Any
|
Node properties as keyword arguments |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if node was added, False if it already exists |
Example
workflow.add_node("protein_1", "protein", name="TP53", function="tumor_suppressor")
Source code in biocypher/_workflow.py
clear()
Clear all nodes and edges from the graph.
copy()
Create a copy of the workflow and its graph.
Returns:
| Type | Description |
|---|---|
BioCypherWorkflow
|
New BioCypherWorkflow instance |
Source code in biocypher/_workflow.py
export_schema(filepath)
Export the current schema to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to save the schema file |
required |
Source code in biocypher/_workflow.py
find_connected_components(node_id, max_depth=2)
Find connected components around a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Starting node ID |
required |
max_depth
|
int
|
Maximum depth to explore |
2
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with nodes and edges in the component |
Source code in biocypher/_workflow.py
find_paths(source, target, max_length=3)
Find all paths between two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Source node ID |
required |
target
|
str
|
Target node ID |
required |
max_length
|
int
|
Maximum path length |
3
|
Returns:
| Type | Description |
|---|---|
list[list[Edge]]
|
List of paths, each path is a list of Edge objects |
Source code in biocypher/_workflow.py
from_json(json_data)
Import knowledge graph from JSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_data
|
str
|
JSON string containing graph data |
required |
Source code in biocypher/_workflow.py
get_connected_edges(node_id, direction='both')
Get edges connected to a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Node identifier |
required |
direction
|
str
|
"in", "out", or "both" |
'both'
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
List of connected Edge objects |
Source code in biocypher/_workflow.py
get_edge(edge_id)
Get an edge by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
str
|
Edge identifier |
required |
Returns:
| Type | Description |
|---|---|
Edge | None
|
Edge object or None if not found |
get_edges(edge_type=None)
Get all edges, optionally filtered by type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_type
|
str | None
|
Optional filter by edge type |
None
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
List of Edge objects |
Source code in biocypher/_workflow.py
get_edges_between(source, target, edge_type=None)
Get edges between two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Source node ID |
required |
target
|
str
|
Target node ID |
required |
edge_type
|
str | None
|
Optional filter by edge type |
None
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
List of Edge objects |
Source code in biocypher/_workflow.py
get_graph()
get_hyperedge(hyperedge_id)
Get a hyperedge by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_id
|
str
|
Hyperedge identifier |
required |
Returns:
| Type | Description |
|---|---|
HyperEdge | None
|
HyperEdge object or None if not found |
Source code in biocypher/_workflow.py
get_hyperedges(hyperedge_type=None)
Get all hyperedges, optionally filtered by type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_type
|
str | None
|
Optional filter by hyperedge type |
None
|
Returns:
| Type | Description |
|---|---|
list[HyperEdge]
|
List of HyperEdge objects |
Source code in biocypher/_workflow.py
get_neighbors(node_id, direction='both')
Get neighboring nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Node identifier |
required |
direction
|
str
|
"in", "out", or "both" |
'both'
|
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of neighboring node IDs |
Source code in biocypher/_workflow.py
get_node(node_id)
Get a node by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Node identifier |
required |
Returns:
| Type | Description |
|---|---|
Node | None
|
Node object or None if not found |
get_nodes(node_type=None)
Get all nodes, optionally filtered by type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
str | None
|
Optional filter by node type |
None
|
Returns:
| Type | Description |
|---|---|
list[Node]
|
List of Node objects |
Source code in biocypher/_workflow.py
get_schema()
Get the current schema configuration.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Dictionary representing the schema or None if no schema |
get_statistics()
Get comprehensive graph statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with graph statistics |
get_summary()
Get a human-readable summary of the graph.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with graph summary |
Source code in biocypher/_workflow.py
has_edge(edge_id)
Check if an edge exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
str
|
Edge identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if edge exists |
has_hyperedge(hyperedge_id)
Check if a hyperedge exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_id
|
str
|
Hyperedge identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if hyperedge exists |
has_node(node_id)
Check if a node exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Node identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if node exists |
load(filepath)
Load the graph from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to load the graph from |
required |
Source code in biocypher/_workflow.py
query_edges(edge_type=None)
Query edges in the knowledge graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_type
|
str | None
|
Optional filter by edge type |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of edge dictionaries |
Source code in biocypher/_workflow.py
query_hyperedges(hyperedge_type=None)
Query hyperedges in the knowledge graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_type
|
str | None
|
Optional filter by hyperedge type |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of hyperedge dictionaries |
Source code in biocypher/_workflow.py
query_nodes(node_type=None)
Query nodes in the knowledge graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
str | None
|
Optional filter by node type |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of node dictionaries |
Source code in biocypher/_workflow.py
remove_edge(edge_id)
Remove an edge from the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
str
|
Edge identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if edge was removed, False if not found |
Source code in biocypher/_workflow.py
remove_node(node_id)
Remove a node and all its connected edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
Node identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if node was removed, False if not found |
Source code in biocypher/_workflow.py
save(filepath)
Save the graph to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to save the graph |
required |
to_json()
Export the knowledge graph to JSON format.
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation of the graph |
to_networkx()
Convert to NetworkX graph for compatibility with existing tools.
Returns:
| Type | Description |
|---|---|
Any
|
networkx.DiGraph: NetworkX representation of the graph |
Note
This method provides compatibility with existing NetworkX-based tools while maintaining the native BioCypher object structure. Future versions may use this as the primary backend.
Source code in biocypher/_workflow.py
to_pandas()
Convert to Pandas DataFrames for compatibility with existing tools.
Returns:
| Type | Description |
|---|---|
Any
|
dict[str, pd.DataFrame]: Dictionary of DataFrames, one per node/edge type |
Note
This method provides compatibility with existing Pandas-based tools while maintaining the native BioCypher object structure. Future versions may use this as the primary backend.
Source code in biocypher/_workflow.py
validate_against_schema(node_type, properties)
Validate node properties against schema (if available).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
str
|
Type of node to validate |
required |
properties
|
dict[str, Any]
|
Properties to validate |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if valid, False otherwise |
Source code in biocypher/_workflow.py
create_workflow
Create a new knowledge graph workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the knowledge graph |
'knowledge_graph'
|
directed
|
bool
|
Whether the graph is directed |
True
|
schema
|
dict[str, Any] | None
|
Dictionary defining the knowledge graph schema |
None
|
schema_file
|
str | None
|
Path to YAML schema file |
None
|
head_ontology_url
|
str | None
|
URL to ontology file |
None
|
validation_mode
|
str
|
Validation level ("none", "warn", "strict") |
'none'
|
deduplication
|
bool
|
Whether to enable deduplication |
False
|
Returns:
| Type | Description |
|---|---|
BioCypherWorkflow
|
BioCypherWorkflow instance |