API Reference
core
BioCypher core module.
Interfaces with the user and distributes tasks to submodules.
BioCypher
Orchestration of BioCypher operations.
Instantiate this class to interact with BioCypher.
dbms (str): The database management system to use. For supported
systems see SUPPORTED_DBMS.
offline (bool): Whether to run in offline mode. In offline mode
the Knowledge Graph is written to files. In online mode, it
is written to a database or hold in memory.
strict_mode (bool): Whether to run in strict mode. If True, the
translator will raise an error if a node or edge does not
provide source, version, and licence information.
biocypher_config_path (str): Path to the BioCypher config file.
schema_config_path (str): Path to the user schema config
file.
head_ontology (dict): The head ontology defined by URL ('url') and root
node ('root_node').
tail_ontologies (dict): The tail ontologies defined by URL and
join nodes for both head and tail ontology.
output_directory (str): Path to the output directory. If not
provided, the default value 'biocypher-out' will be used.
cache_directory (str): Path to the cache directory.
Source code in biocypher/_core.py
42 43 44 45 46 47 48 49 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 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 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 | |
add(entities)
Add entities to the in-memory database.
Accepts an iterable of tuples (if given, translates to
BioCypherNode or BioCypherEdge objects) or an iterable of
BioCypherNode or BioCypherEdge objects.
entities (iterable): An iterable of entities to add to the database.
Can be 3-tuples (nodes) or 5-tuples (edges); also accepts
4-tuples for edges (deprecated).
None
Source code in biocypher/_core.py
add_edges(edges)
Add new edges to the internal representation.
Initially, receive edges data from adaptor and create internal representation for edges.
edges(iterable): An iterable of edges.
Source code in biocypher/_core.py
add_nodes(nodes)
Add new nodes to the internal representation.
Initially, receive nodes data from adaptor and create internal representation for nodes.
nodes(iterable): An iterable of nodes
Source code in biocypher/_core.py
download(*resources)
Download or load from cache the resources given by the adapter.
resources (iterable): An iterable of resources to download or load
from cache.
None
Source code in biocypher/_core.py
get_kg()
Get the in-memory KG instance.
Depending on the specified dbms this could either be a list of Pandas
dataframes or a NetworkX DiGraph.
Source code in biocypher/_core.py
log_duplicates()
Log duplicate nodes and edges.
Get the set of duplicate nodes and edges encountered and print them to the logger.
Source code in biocypher/_core.py
log_missing_input_labels()
Log missing input labels.
Get the set of input labels encountered without an entry in the
schema_config.yaml and print them to the logger.
Returns
Optional[Dict[str, List[str]]]: A dictionary of Biolink types
encountered without an entry in the `schema_config.yaml` file.
Source code in biocypher/_core.py
merge_edges(edges)
Merge edges into database.
Either takes an iterable of tuples (if given, translates to
BioCypherEdge objects) or an iterable of BioCypherEdge objects.
edges (iterable): An iterable of edges to merge into the database.
bool: True if successful.
Source code in biocypher/_core.py
merge_nodes(nodes)
Merge nodes into database.
Either takes an iterable of tuples (if given, translates to
BioCypherNode objects) or an iterable of BioCypherNode objects.
nodes (iterable): An iterable of nodes to merge into the database.
bool: True if successful.
Source code in biocypher/_core.py
reverse_translate_query(query)
Reverse translate a query from its BioCypher equivalent.
query (str): The BioCypher query to reverse translate.
str: The original query.
Source code in biocypher/_core.py
reverse_translate_term(term)
Reverse translate a term from its BioCypher equivalent.
term (str): The BioCypher term to reverse translate.
str: The original term.
Source code in biocypher/_core.py
show_ontology_structure(**kwargs)
Show the ontology structure using treelib or write to GRAPHML file.
to_disk (str): If specified, the ontology structure will be saved
to disk as a GRAPHML file, to be opened in your favourite
graph visualisation tool.
full (bool): If True, the full ontology structure will be shown,
including all nodes and edges. If False, only the nodes and
edges that are relevant to the extended schema will be shown.
Source code in biocypher/_core.py
summary()
Call convenience and reporting methods.
Shows ontology structure and logs duplicates and missing input types.
to_df()
Create DataFrame using internal representation.
TODO: to_df implies data frame, should be specifically that use case
to_networkx()
translate_query(query)
Translate a query to its BioCypher equivalent.
query (str): The query to translate.
str: The BioCypher equivalent of the query.
Source code in biocypher/_core.py
translate_term(term)
Translate a term to its BioCypher equivalent.
term (str): The term to translate.
str: The BioCypher equivalent of the term.
Source code in biocypher/_core.py
write_edges(edges, batch_size=int(1000000.0))
Write edges to database.
Either takes an iterable of tuples (if given, translates to
BioCypherEdge objects) or an iterable of BioCypherEdge objects.
edges (iterable): An iterable of edges to write to the database.
bool: True if successful.
Source code in biocypher/_core.py
write_import_call()
Write a shell script to import the database.
Shell script is written depending on the chosen DBMS.
Returns
str: path toward the file holding the import call.
Source code in biocypher/_core.py
write_nodes(nodes, batch_size=int(1000000.0), force=False)
Write nodes to database.
Either takes an iterable of tuples (if given, translates to
BioCypherNode objects) or an iterable of BioCypherNode objects.
nodes (iterable): An iterable of nodes to write to the database.
batch_size (int): The batch size to use when writing to disk.
force (bool): Whether to force writing to the output directory even
if the node type is not present in the schema config file.
bool: True if successful.
Source code in biocypher/_core.py
write_schema_info(as_node=False)
Write an extended schema info to file or node.
Creates a YAML file or KG node that extends the schema_config.yaml
with run-time information of the built KG. For instance, include
information on whether something present in the actual knowledge graph,
whether it is a relationship (which is important in the case of
representing relationships as nodes) and the actual sources and
targets of edges. Since this file can be used in place of the original
schema_config.yaml file, it indicates that it is the extended schema
by setting is_schema_info to true.
We start by using the extended_schema dictionary from the ontology
class instance, which contains all expanded entities and relationships.
The information of whether something is a relationship can be gathered
from the deduplicator instance, which keeps track of all entities that
have been seen.
as_node (bool): If True, the schema info is written as a KG node.
If False, the schema info is written to a YAML file.
Source code in biocypher/_core.py
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 776 777 778 779 780 | |
create
BioCypher 'create' module. Handles the creation of BioCypher node and edge dataclasses.
BioCypherEdge
dataclass
Handoff class to represent biomedical relationships in Neo4j.
Has source and target ids, label, property dict; ids and label (in the Neo4j sense of a label, ie, the entity descriptor after the colon, such as ":TARGETS") are non-optional and called source_id, target_id, and relationship_label to avoid confusion with properties called "label", which usually denotes the human-readable form. Relationship labels are written in UPPERCASE and as verbs, as per Neo4j consensus.
Args:
source_id (string): consensus "best" id for biological entity
target_id (string): consensus "best" id for biological entity
relationship_label (string): type of interaction, UPPERCASE
properties (dict): collection of all other properties of the
respective edge
Source code in biocypher/_create.py
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 | |
__post_init__()
Check for reserved keywords.
Source code in biocypher/_create.py
get_dict()
Return dict of ids, label, and properties.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
source_id, target_id and relationship_label as top-level key-value pairs, properties as second-level dict. |
Source code in biocypher/_create.py
get_id()
Returns primary node identifier or None.
Returns:
| Name | Type | Description |
|---|---|---|
str |
Union[str, None]
|
node_id |
get_label()
get_properties()
Returns all other relationship properties apart from primary ids and label as key-value pairs.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
properties |
get_source_id()
Returns primary node identifier of relationship source.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
source_id |
get_target_id()
Returns primary node identifier of relationship target.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
target_id |
BioCypherNode
dataclass
Handoff class to represent biomedical entities as Neo4j nodes.
Has id, label, property dict; id and label (in the Neo4j sense of a label, ie, the entity descriptor after the colon, such as ":Protein") are non-optional and called node_id and node_label to avoid confusion with "label" properties. Node labels are written in PascalCase and as nouns, as per Neo4j consensus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
string
|
consensus "best" id for biological entity |
required |
node_label
|
string
|
primary type of entity, capitalised |
required |
**properties
|
kwargs
|
collection of all other properties to be passed to neo4j for the respective node (dict) |
dict()
|
Todo
- check and correct small inconsistencies such as capitalisation of ID names ("uniprot" vs "UniProt")
- check for correct ID patterns (eg "ENSG" + string of numbers, uniprot length)
- ID conversion using pypath translation facilities for now
Source code in biocypher/_create.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 | |
__post_init__()
Add id field to properties.
Check for reserved keywords.
Replace unwanted characters in properties.
Source code in biocypher/_create.py
get_dict()
Return dict of id, labels, and properties.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
node_id and node_label as top-level key-value pairs, |
dict
|
properties as second-level dict. |
Source code in biocypher/_create.py
get_id()
get_label()
get_preferred_id()
get_properties()
Returns all other node properties apart from primary id and label as key-value pairs.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
properties |
BioCypherRelAsNode
dataclass
Class to represent relationships as nodes (with in- and outgoing edges) as a triplet of a BioCypherNode and two BioCypherEdges. Main usage in type checking (instances where the receiving function needs to check whether it receives a relationship as a single edge or as a triplet).
Args:
node (BioCypherNode): node representing the relationship
source_edge (BioCypherEdge): edge representing the source of the
relationship
target_edge (BioCypherEdge): edge representing the target of the
relationship
Source code in biocypher/_create.py
deduplicate
Deduplicator
Singleton class responsible of deduplicating BioCypher inputs. Maintains sets/dictionaries of node and edge types and their unique identifiers.
Nodes identifiers should be globally unique (represented as a set), while edge identifiers are only unique per edge type (represented as a dict of sets, keyed by edge type).
Stores collection of duplicate node and edge identifiers and types for troubleshooting and to avoid overloading the log.
Source code in biocypher/_deduplicate.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 | |
edge_seen(relationship)
Adds an edge to the instance and checks if it has been seen before.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge
|
BioCypherEdge to be added. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the edge has been seen before, False otherwise. |
Source code in biocypher/_deduplicate.py
get_duplicate_edges()
Function to return a list of duplicate edges.
Returns:
| Name | Type | Description |
|---|---|---|
list |
list of duplicate edges |
Source code in biocypher/_deduplicate.py
get_duplicate_nodes()
Function to return a list of duplicate nodes.
Returns:
| Name | Type | Description |
|---|---|---|
list |
list of duplicate nodes |
Source code in biocypher/_deduplicate.py
node_seen(entity)
Adds a node to the instance and checks if it has been seen before.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
BioCypherNode to be added. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the node has been seen before, False otherwise. |
Source code in biocypher/_deduplicate.py
rel_as_node_seen(rel_as_node)
Adds a rel_as_node to the instance (one entity and two relationships) and checks if it has been seen before. Only the node is relevant for identifying the rel_as_node as a duplicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel_as_node
|
BioCypherRelAsNode
|
BioCypherRelAsNode to be added. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the rel_as_node has been seen before, False otherwise. |
Source code in biocypher/_deduplicate.py
get
BioCypher get module.
Used to download and cache data from external sources.
APIRequest
Bases: Resource
Source code in biocypher/_get.py
__init__(name, url_s, lifetime=0)
Initialize an APIRequest object.
Represents basic information for an API Request.
name(str): The name of the API Request.
url_s(str|list): The URL of the API endpoint.
lifetime(int): The lifetime of the API Request in days. If 0, the
API Request is cached indefinitely.
Source code in biocypher/_get.py
Downloader
Source code in biocypher/_get.py
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 | |
__init__(cache_dir=None)
Initialize the Downloader.
The Downloader is a class that manages resources that can be downloaded and cached locally. It manages the lifetime of downloaded resources by keeping a JSON record of the download date of each resource.
cache_dir (str): The directory where the resources are cached. If
not given, a temporary directory is created.
Source code in biocypher/_get.py
download(*resources)
Download one or multiple resources.
Load from cache if the resource is already downloaded and the cache is not expired.
resources (Resource): The resource(s) to download or load from
cache.
list[str]: The path or paths to the resource(s) that were downloaded
or loaded from cache.
Source code in biocypher/_get.py
get_cached_version(resource)
Get the cached version of a resource.
resource(Resource): The resource to get the cached version of.
list[str]: The paths to the cached resource(s).
Source code in biocypher/_get.py
FileDownload
Bases: Resource
Source code in biocypher/_get.py
__init__(name, url_s, lifetime=0, is_dir=False)
Initialize a FileDownload object.
Represents basic information for a File Download.
name(str): The name of the File Download.
url_s(str|list[str]): The URL(s) of the File Download.
lifetime(int): The lifetime of the File Download in days. If 0, the
File Download is cached indefinitely.
is_dir (bool): Whether the URL points to a directory or not.
Source code in biocypher/_get.py
Resource
Bases: ABC
Source code in biocypher/_get.py
__init__(name, url_s, lifetime=0)
Initialize a Resource.
A Resource is a file, a list of files, an API request, or a list of API requests, any of which can be downloaded from the given URL(s) and cached locally. This class implements checks of the minimum requirements for a resource, to be implemented by a biocypher adapter.
name (str): The name of the resource.
url_s (str | list[str]): The URL or URLs of the resource.
lifetime (int): The lifetime of the resource in days. If 0, the
resource is considered to be permanent.
Source code in biocypher/_get.py
graph
Unified Graph representation for BioCypher.
This module provides a comprehensive Graph class that can represent various graph types including simple graphs, directed graphs, weighted graphs, multigraphs, and hypergraphs. The design focuses on simplicity and extensibility for knowledge representation.
TODO: examine overlap with legacy BioCypher modules, synergise where possible. TODO: evaluate generalised graph class as consensus internal representation as technical intermediate for other output adapters. TODO: validation of new entities against schema. Rollback of inconsistent operations. TODO: retrieval of subgraphs from existing databases.
Edge
dataclass
Represents an edge in the graph.
Source code in biocypher/_graph.py
from_dict(data)
classmethod
Create edge from dictionary representation.
Source code in biocypher/_graph.py
to_dict()
Convert edge to dictionary representation.
EdgeType
Graph
Unified graph representation supporting various graph types.
This class provides a comprehensive graph representation that can handle: - Simple undirected graphs - Directed graphs - Weighted graphs - Multigraphs (multiple edges between same nodes) - Hypergraphs (edges connecting multiple nodes) - Property graphs (nodes and edges with properties)
The design prioritizes simplicity and extensibility for knowledge representation.
Source code in biocypher/_graph.py
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 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
__contains__(node_id)
__init__(name='graph', directed=True)
Initialize a new graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the graph |
'graph'
|
directed
|
bool
|
Whether the graph is directed (default: True) |
True
|
Source code in biocypher/_graph.py
__iter__()
__len__()
__str__()
String representation of the graph.
Source code in biocypher/_graph.py
add_edge(edge_id, edge_type, source, target, properties=None)
Add an edge to the 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
|
dict[str, Any] | None
|
Optional properties dictionary |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if edge was added, False if it already exists |
Source code in biocypher/_graph.py
add_hyperedge(hyperedge_id, hyperedge_type, nodes, properties=None)
Add a hyperedge to the graph.
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
|
dict[str, Any] | None
|
Optional properties dictionary |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if hyperedge was added, False if it already exists |
Source code in biocypher/_graph.py
add_node(node_id, node_type, properties=None)
Add a node to the 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
|
dict[str, Any] | None
|
Optional properties dictionary |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if node was added, False if it already exists |
Source code in biocypher/_graph.py
clear()
Clear all nodes, edges, and hyperedges from the graph.
Source code in biocypher/_graph.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/_graph.py
from_dict(data)
classmethod
Create graph from dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary representation of the graph |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
Graph object |
Source code in biocypher/_graph.py
from_json(json_str)
Load graph data from JSON string into this graph instance.
This method clears the existing graph and loads new data from JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_str
|
str
|
JSON string representation of the graph |
required |
Source code in biocypher/_graph.py
from_json_string(json_str)
classmethod
Create graph from JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_str
|
str
|
JSON string representation of the graph |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
Graph object |
Source code in biocypher/_graph.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/_graph.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/_graph.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/_graph.py
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 |
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/_graph.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/_graph.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_node_ids(node_type=None)
Get all node IDs, optionally filtered by type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
str | None
|
Optional filter by node type |
None
|
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of node IDs |
Source code in biocypher/_graph.py
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/_graph.py
get_statistics()
Get comprehensive graph statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with graph statistics |
Source code in biocypher/_graph.py
get_subgraph(node_ids, include_edges=True)
Extract a subgraph containing specified nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_ids
|
set[str]
|
Set of node IDs to include |
required |
include_edges
|
bool
|
Whether to include edges between included nodes |
True
|
Returns:
| Type | Description |
|---|---|
Graph
|
New Graph object containing the subgraph |
Source code in biocypher/_graph.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 |
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/_graph.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/_graph.py
to_dict()
Convert graph to dictionary representation.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation of the graph |
Source code in biocypher/_graph.py
to_json()
Convert graph to JSON string.
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation of the graph |
HyperEdge
dataclass
Represents a hyperedge connecting multiple nodes.
Source code in biocypher/_graph.py
from_dict(data)
classmethod
Create hyperedge from dictionary representation.
Node
dataclass
Represents a node in the graph.
Source code in biocypher/_graph.py
from_dict(data)
classmethod
logger
Configuration of the module logger.
get_logger(name='biocypher')
Access the module logger, create a new one if does not exist yet.
Method providing central logger instance to main module. Is called
only from main submodule, :mod:biocypher.driver. In child modules,
the standard Python logging facility is called
(using logging.getLogger(__name__)), automatically inheriting
the handlers from the central logger.
The file handler creates a log file named after the current date and time. Levels to output to file and console can be set here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the logger instance. |
'biocypher'
|
Returns:
| Type | Description |
|---|---|
Logger
|
An instance of the Python |
Source code in biocypher/_logger.py
log()
mapping
BioCypher 'mapping' module. Handles the mapping of user-defined schema to the underlying ontology.
OntologyMapping
Class to store the ontology mapping and extensions.
Source code in biocypher/_mapping.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 | |
metadata
Package metadata (version, authors, etc).
get_metadata()
Basic package metadata.
Retrieves package metadata from the current project directory or from the installed package.
Source code in biocypher/_metadata.py
misc
Handy functions for use in various places.
create_tree_visualisation(inheritance_graph)
Create a visualisation of the inheritance tree using treelib.
Source code in biocypher/_misc.py
ensure_iterable(value)
is_nested(lst)
Check if a list is nested.
lst (list): The list to check.
bool: True if the list is nested, False otherwise.
Source code in biocypher/_misc.py
pascalcase_to_sentencecase(s)
Convert PascalCase to sentence case.
s: Input string in PascalCase
string in sentence case form
sentencecase_to_pascalcase(s, sep='\\s')
Convert sentence case to PascalCase.
s: Input string in sentence case
sep: Separator for the words in the input string
string in PascalCase form
Source code in biocypher/_misc.py
sentencecase_to_snakecase(s)
Convert sentence case to snake_case.
s: Input string in sentence case
string in snake_case form
snakecase_to_sentencecase(s)
Convert snake_case to sentence case.
s: Input string in snake_case
string in sentence case form
Source code in biocypher/_misc.py
to_list(value)
to_lower_sentence_case(s)
Convert any string to lower sentence case.
Works with snake_case, PascalCase, and sentence case.
s: Input string
string in lower sentence case form
Source code in biocypher/_misc.py
ontology
BioCypher 'ontology' module to parse and represent ontologies.
Also performs ontology hybridisation and other advanced operations.
Ontology
A class that represents the ontological "backbone" of a KG.
The ontology can be built from a single resource, or hybridised from a combination of resources, with one resource being the "head" ontology, while an arbitrary number of other resources can become "tail" ontologies at arbitrary fusion points inside the "head" ontology.
Source code in biocypher/_ontology.py
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 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 | |
__init__(head_ontology, ontology_mapping=None, tail_ontologies=None)
Initialize the Ontology class.
head_ontology (OntologyAdapter): The head ontology.
tail_ontologies (list): A list of OntologyAdapters that will be
added to the head ontology. Defaults to None.
Source code in biocypher/_ontology.py
get_ancestors(node_label)
Get the ancestors of a node in the ontology.
node_label (str): The label of the node in the ontology.
list: A list of the ancestors of the node.
Source code in biocypher/_ontology.py
get_dict()
Return a dictionary representation of the ontology.
The dictionary is compatible with a BioCypher node for compatibility with the Neo4j driver.
Source code in biocypher/_ontology.py
get_rdf_graph()
Return the merged RDF graph.
Return the merged graph of all loaded ontologies (head and tails).
Source code in biocypher/_ontology.py
show_ontology_structure(to_disk=None, full=False)
Show the ontology structure using treelib or write to GRAPHML file.
to_disk (str): If specified, the ontology structure will be saved
to disk as a GRAPHML file at the location (directory) specified
by the `to_disk` string, to be opened in your favourite graph
visualisation tool.
full (bool): If True, the full ontology structure will be shown,
including all nodes and edges. If False, only the nodes and
edges that are relevant to the extended schema will be shown.
Source code in biocypher/_ontology.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 | |
OntologyAdapter
Class that represents an ontology to be used in the Biocypher framework.
Can read from a variety of formats, including OWL, OBO, and RDF/XML. The ontology is represented by a networkx.DiGraph object; an RDFlib graph is also kept. By default, the DiGraph reverses the label and identifier of the nodes, such that the node name in the graph is the human-readable label. The edges are oriented from child to parent. Labels are formatted in lower sentence case and underscores are replaced by spaces. Identifiers are taken as defined and the prefixes are removed by default.
Source code in biocypher/_ontology.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
__init__(ontology_file, root_label, ontology_file_format=None, head_join_node_label=None, merge_nodes=True, switch_label_and_id=True, remove_prefixes=True)
Initialize the OntologyAdapter class.
ontology_file (str): Path to the ontology file. Can be local or
remote.
root_label (str): The label of the root node in the ontology. In
case of a tail ontology, this is the tail join node.
ontology_file_format (str): The format of the ontology file (e.g. "application/rdf+xml")
If format is not passed, it is determined automatically.
head_join_node_label (str): Optional variable to store the label of the
node in the head ontology that should be used to join to the
root node of the tail ontology. Defaults to None.
merge_nodes (bool): If True, head and tail join nodes will be
merged, using the label of the head join node. If False, the
tail join node will be attached as a child of the head join
node.
switch_label_and_id (bool): If True, the node names in the graph will be
the human-readable labels. If False, the node names will be the
identifiers. Defaults to True.
remove_prefixes (bool): If True, the prefixes of the identifiers will
be removed. Defaults to True.
Source code in biocypher/_ontology.py
get_ancestors(node_label)
get_head_join_node()
get_nx_graph()
get_rdf_graph()
get_root_node()
Get root node in the ontology.
Returns
root_node: If _switch_label_and_id is True, the root node label is
returned, otherwise the root node id is returned.
Source code in biocypher/_ontology.py
has_label(node, g)
Check if the node has a label in the graph.
node (rdflib.URIRef): The node to check
g (rdflib.Graph): The graph to check in
Returns: bool: True if the node has a label, False otherwise
Source code in biocypher/_ontology.py
translate
BioCypher 'translation' module.
Responsible for translating between the raw input data and the BioCypherNode and BioCypherEdge objects.
Translator
Class responsible for exacting the translation process.
Translation is configured in the schema_config.yaml file. Creates a mapping dictionary from that file, and, given nodes and edges, translates them into BioCypherNodes and BioCypherEdges. During this process, can also filter the properties of the entities if the schema_config.yaml file specifies a property whitelist or blacklist.
Provides utility functions for translating between input and output labels and cypher queries.
Source code in biocypher/_translate.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 448 449 450 451 452 453 454 455 456 457 458 | |
__init__(ontology, strict_mode=False)
Initialise the translator.
ontology (Ontology): An Ontology object providing schema and mapping details.
strict_mode:
strict_mode (bool, optional): If True, enforces that every node and edge carries
the required 'source', 'licence', and 'version' properties. Raises ValueError
if these are missing. Defaults to False.
Source code in biocypher/_translate.py
get_missing_biolink_types()
Return a dictionary of non-represented types.
The dictionary contains the type as the key and the number of occurrences as the value.
name_sentence_to_pascal(name)
staticmethod
Convert a name in sentence case to pascal case.
Source code in biocypher/_translate.py
reverse_translate(query)
Reverse translate a cypher query.
Only translates labels as of now.
Source code in biocypher/_translate.py
reverse_translate_term(term)
translate(query)
Translate a cypher query.
Only translates labels as of now.
translate_edges(edge_tuples)
Translate input edge representation.
Translate the edge tuples to a representation that conforms to the schema of the given BioCypher graph. For now requires explicit statement of edge type on pass.
edge_tuples (list of tuples):
collection of tuples representing source and target of
an interaction via their unique ids as well as the type
of interaction in the original database notation, which
is translated to BioCypher notation using the `leaves`.
Can optionally possess its own ID.
Source code in biocypher/_translate.py
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 | |
translate_nodes(node_tuples)
Translate input node representation.
Translate the node tuples to a representation that conforms to the schema of the given BioCypher graph. For now requires explicit statement of node type on pass.
node_tuples (list of tuples): collection of tuples
representing individual nodes by their unique id and a type
that is translated from the original database notation to
the corresponding BioCypher notation.
Source code in biocypher/_translate.py
workflow
Unified BioCypher Workflow API for knowledge graph workflows.
This module provides a streamlined interface for creating and managing knowledge graphs using the unified Graph representation, with optional schema and ontology support. Designed for both agentic and deterministic workflows.
Design Philosophy
This API is designed with the following principles: 1. Agentic-First: Optimized for LLM agent workflows with computable functions 2. Zero Dependencies: Pure Python implementation for maximum compatibility 3. Future-Proof: Native BioCypher objects enable advanced agentic features 4. Migration-Ready: Wrapper methods provide compatibility with existing tools 5. Progressive Validation: Optional validation and deduplication with flexible modes
Validation and Deduplication
Unlike the legacy BioCypher which enforces strict validation and deduplication, this API provides progressive validation with three modes:
- "none" (default): No validation or deduplication - maximum flexibility for agents
- "warn": Log warnings for schema violations and duplicates but continue processing
- "strict": Enforce schema validation and deduplication - fail fast on violations
This approach allows: - Agents to work with maximum flexibility (no validation overhead) - Deterministic workflows to use validation when needed - Gradual migration from legacy BioCypher (start with "none", add validation later)
Future Migration Path
This module represents the future direction of BioCypher's in-memory graph functionality. The plan is to:
- Phase 1 (Current): Keep separate from legacy code, provide compatibility wrappers
- Phase 2: Replace legacy in-memory implementations (PandasKG, NetworkxKG)
- Phase 3: Add advanced agentic features (computable functions, decision logging)
- Phase 4: Integrate with main BioCypher class as unified interface
Agentic Features (Future)
- Computable functions attached to nodes/edges
- Decision logging and reasoning traces
- Counterfactual inference capabilities
- MCP (Model Context Protocol) interface integration
- Local graph computation for agent workflows
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 | |
__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
|
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
|
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
|
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()
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 |
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 |
|---|---|
|
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 |
|---|---|
|
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(name='knowledge_graph', directed=True, schema=None, schema_file=None, head_ontology_url=None, validation_mode='none', deduplication=False)
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 |