Package: kgx
Knowledge Graph ExchangeKGX (Knowledge Graph Exchange) is a Python library and set of command line utilities for exchanging
Knowledge Graphs (KGs) that conform to or are aligned to theBiolink Model.The core datamodel is aProperty Graph(PG), represented
internally in Python using anetworkx MultiDiGraph model.KGX allows conversion to and from:RDF serializations (read/write) and SPARQL endpoints (read)Neo4j endpoints (read) or Neo4j dumps (write)CSV/TSV and JSON (seeassociated data formatsandexample script to load CSV/TSV to Neo4j)Reasoner Standard API formatOBOGraph JSON formatKGX will also provide validation, to ensure the KGs are conformant to the Biolink Model: making sure nodes are
categorized using Biolink classes, edges are labeled using valid Biolink relationship types, and valid properties are used.Internal representation is a property graph, specifically a networkx MultiDiGraph.The structure of this graph is expected to conform to the Biolink Model standard, as specified in theKGX format specification.In addition to the main code-base, KGX also provides a series ofcommand line operations.Example usageValidate:poetryrunkgxvalidate-itsvtests/resources/merge/test2_nodes.tsvtests/resources/merge/test2_edges.tsvMerge:poetryrunkgxmerge—merge-configtests/resources/test-merge.yamlGraph Summary:poetryrunkgxgraph-summary-itests/resources/graph_nodes.tsv-osummary.txtTransform:poetryrunkgxtransform—transform-configtests/resources/test-transform-tsv-rdf.yamlError Detection and ReportingNon-redundant JSON-formatted structured error logging is now provided in KGX Transformer, Validator, GraphSummary and MetaKnowledgeGraph operations.  See the various unit tests for the general design pattern (using the Validator as an example here):fromkgx.validatorimportValidatorfromkgx.transformerimportTransformerValidator.set_biolink_model("2.11.0")# Validator assumes the currently set Biolink Releasevalidator=Validator()transformer=Transformer(stream=True)transformer.transform(input_args={"filename":["graph_nodes.tsv","graph_edges.tsv",],"format":"tsv",},output_args={"format":"null"},inspector=validator,)# Both the Validator and the Transformer can independently capture errors# The Validator, from the overall semantics of the graph...# Here, we just report severe Errors from the Validator (no Warnings)validator.write_report(open("validation_errors.json","w"),"Error")# The Transformer, from the syntax of the input files...# Here, we catch *all* Errors and Warnings (by not providing a filter)transformer.write_report(open("input_errors.json","w"))The JSON error outputs will look something like this:{"ERROR":{"MISSING_EDGE_PROPERTY":{"Required edge property 'id' is missing":["A:123->X:1","B:456->Y:2"],"Required edge property 'object' is missing":["A:123->X:1"],"Required edge property 'predicate' is missing":["A:123->X:1"],"Required edge property 'subject' is missing":["A:123->X:1","B:456->Y:2"]}},"WARNING":{"DUPLICATE_NODE":{"Node 'id' duplicated in input data":["MONDO:0010011","REACT:R-HSA-5635838"]}}}This system reduces the significant redundancies of earlier line-oriented KGX  logging text output files, in that graph entities with the same class of error are simply aggregated in lists of names/identifiers at the leaf level of the JSON structure.The top level JSON tags originate from theMessageLevelclass and the second level tags from theErrorTypeclass in theerror_detectionmodule, while the third level messages are hard coded aslog_errormethod messages in the code.It is likely that additional error conditions within KGX can be efficiently captured and reported in the future using this general framework.InstallationInstalling from PyPIKGX is available on PyPI and can be installed usingpipas follows,pipinstallkgxTo install a particular version of KGX, be sure to specify the version number,pipinstallkgx==0.5.0Installing from GitHubClone the GitHub repository and then install,gitclonehttps://github.com/biolink/kgxcdkgx
poetryinstallSetting up a testing environment for Neo4jThis release of KGX supports graph source and sink transactions with the 4.3 release of Neo4j.KGX has a suite of tests that rely on Docker containers to run Neo4j specific tests.To set up the required containers, first installDockeron your local machine.Once Docker is up and running, run the following commands:dockerrun-d--rm--namekgx-neo4j-integration-test-p7474:7474-p7687:7687--envNEO4J_AUTH=neo4j/testneo4j:4.3dockerrun-d--rm--namekgx-neo4j-unit-test-p8484:7474-p8888:7687--envNEO4J_AUTH=neo4j/testneo4j:4.3Note:Setting up the Neo4j container is optional. If there is no container set up
then the tests that rely on them are skipped.