refscan is a command-line tool people can use to scan the NMDC MongoDB database
for referential integrity violations.
%% This is the source code of a Mermaid diagram, which GitHub will render as a diagram.
%% Note: PyPI does not render Mermaid diagrams, and instead displays their source code.
%% Reference: https://github.com/pypi/warehouse/issues/13083
graph LR
schema[LinkML<br>schema]
database[(MongoDB<br>database)]
script[["refscan"]]
violations["List of<br>violations"]
references["List of<br>references"]:::dashed_border
schema --> script
database --> script
script -.-> references
script --> violations
classDef dashed_border stroke-dasharray: 5 5
In addition to using refscan to scan the NMDC MongoDB database for referential integrity violations,
people can use refscan to generate graphs (diagrams) depicting which collections' documents (or which classes'
instances) can contain references to which other collections' documents (or classes' instances) while still being
schema compliant.
Here is a summary of how each of refscan's main functions works under the hood.
refscan does this in two stages:
- It uses the LinkML schema to determine where references can exist in a MongoDB database that conforms to the schema.
Example: The schema might say that, if a document in the
biosample_setcollection has a field namedassociated_studies, that field must contain a list ofids of documents in thestudy_setcollection. - It scans the MongoDB database to check the integrity of all the references that do exist.
Example: For each document in the
biosample_setcollection that has a field namedassociated_studies, for each value in that field, confirm there is a document having thatidin thestudy_setcollection.
refscan does this in three stages:
- It uses the LinkML schema to determine where references can exist in a MongoDB database that conforms to the schema.
- It formats that list of references into a data structure compatible with
Cytoscape.js. - It outputs an HTML document that uses
Cytoscape.jsto visualize that data structure as a graph.
refscan was designed under the assumption that every document in every collection described by the schema has
a field named type, whose value is the class_uri of the schema class the document represents an instance
of. refscan uses that class_uri value (in that type field) to determine the name of that schema class,
whose definition refscan then uses to determine which fields of that document can contain references.
Assuming you have pipx installed, you can install the tool by running the following command:
pipx install refscan
pipxis a tool people can use to download and install Python scripts that are hosted on PyPI. You can installpipxby running$ python -m pip install pipx.
Once installed, you can display the tool's --help snippet by running:
refscan --helpAt the time of this writing, the tool's --help snippet is:
Usage: refscan [OPTIONS] COMMAND [ARGS]...
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --help Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโ Commands โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ version Show version number and exit. โ
โ scan Scan the NMDC MongoDB database for referential integrity violations. โ
โ graph Generate an interactive graph of the references described by a schema. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏEach command has its own --help snippet.
At the time of this writing, the --help snippet for the scan command is:
Usage: refscan scan [OPTIONS]
Scan the NMDC MongoDB database for referential integrity violations.
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ * --schema FILE Filesystem path at which the YAML file โ
โ representing the schema is located. โ
โ [default: None] โ
โ [required] โ
โ --database-name TEXT Name of the database. โ
โ [default: nmdc] โ
โ --mongo-uri TEXT Connection string for accessing the โ
โ MongoDB server. If you have Docker โ
โ installed, you can spin up a temporary โ
โ MongoDB server at the default URI by โ
โ running: $ docker run --rm --detach -p โ
โ 27017:27017 mongo โ
โ [env var: MONGO_URI] โ
โ [default: mongodb://localhost:27017] โ
โ --verbose Show verbose output. โ
โ --skip-source-collection,--skip TEXT Name of collection you do not want to โ
โ search for referring documents. Option โ
โ can be used multiple times. โ
โ [default: None] โ
โ --reference-report FILE Filesystem path at which you want the โ
โ program to generate its reference โ
โ report. โ
โ [default: references.tsv] โ
โ --violation-report FILE Filesystem path at which you want the โ
โ program to generate its violation โ
โ report. โ
โ [default: violations.tsv] โ
โ --no-scan Generate a reference report, but do โ
โ not scan the database for violations. โ
โ --locate-misplaced-documents For each referenced document not found โ
โ in any of the collections the schema โ
โ allows, also search for it in all โ
โ other collections. โ
โ --help Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏAs documented in the --help snippet above, you can provide the MongoDB connection string to the tool via either
(a) the --mongo-uri option; or (b) an environment variable named MONGO_URI. The latter can come in handy
when the MongoDB connection string contains information you don't want to appear in your shell history,
such as a password.
Here's how you could create that environment variable:
export MONGO_URI='mongodb://username:password@localhost:27017'As documented in the --help snippet above, you can provide the path to a YAML-formatted LinkML schema file to the tool
via the --schema option.
Show/hide tips for getting a schema file
If you have curl installed, you can download a YAML file from GitHub by running the following command (after replacing
the {...} placeholders and customizing the path):
# Download the raw content of https://github.com/{user_or_org}/{repo}/blob/{branch}/path/to/schema.yaml
curl -o schema.yaml https://raw.githubusercontent.com/{user_or_org}/{repo}/{branch}/path/to/schema.yamlFor example:
# Download the raw content of https://github.com/microbiomedata/nmdc-schema/blob/main/nmdc_schema/nmdc_materialized_patterns.yaml
curl -o schema.yaml https://raw.githubusercontent.com/microbiomedata/nmdc-schema/main/nmdc_schema/nmdc_materialized_patterns.yaml
# Download the raw content of https://github.com/microbiomedata/nmdc-schema/blob/v11.2.1/nmdc_schema/nmdc_materialized_patterns.yaml
curl -o schema.yaml https://raw.githubusercontent.com/microbiomedata/nmdc-schema/v11.2.1/nmdc_schema/nmdc_materialized_patterns.yamlWhile refscan is running, it will display console output indicating what it's currently doing.
Once the scan is complete, the reference report (TSV file) and violation report (TSV file) will be available in the current directory (or in custom directories, if any were specified via CLI options).
At the time of this writing, the --help snippet for the graph command is:
Usage: refscan graph [OPTIONS]
Generate an interactive graph of the references described by a schema.
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ * --schema FILE Filesystem path at which the YAML file โ
โ representing the schema is located. โ
โ [default: None] โ
โ [required] โ
โ --graph FILE Filesystem path at which you want refscan to โ
โ generate the graph. โ
โ [default: graph.html] โ
โ --subject [collection|class] Whether you want each node of the graph to โ
โ represent a collection or a class. โ
โ [default: collection] โ
โ --verbose Show verbose output. โ
โ --help Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏYou can update the tool to the latest version available on PyPI by running:
pipx upgrade refscanYou can uninstall the tool from your computer by running:
pipx uninstall refscanYou can also run refscan via a container image hosted by the GitHub Container Registry.
docker run --rm -it refscan --helpNote: When running
refscanvia a container image, you can reference your host machine via the special hostname, "host.docker.internal".In other words,
$ docker run refscan --mongo-uri mongodb://host.docker.internal:27017does the same thing as$ refscan --mongo-uri mongodb://localhost:27017, except the first command runsrefscanwithin a container while the second one runs it directly on your host machine.
We use uv to both (a) manage dependencies and (b) build distributable packages that can be published to PyPI.
pyproject.toml: Configuration file foruvand other toolsuv.lock: List of dependencies, both direct and indirect/transitive
Note: We initialized this repository using Poetry. We switched from Poetry to
uvat around commit#1449ceca.
git clone https://github.com/microbiomedata/refscan.git
cd refscanYou can set up a Python virtual environment by issuing the following command from the root directory of the repository:
uv syncThat command will:
- Create a Python virtual environment at
.venv(if one doesn't already exist there) - Install all dependencies described in
uv.lockinto that Python virtual environment - Uninstall all dependencies not described in
uv.lockfrom that Python virtual environment
Now that you have set up a Python virtual environment, you can activate it by issuing the following command:
source .venv/bin/activateNote: Once you're ready to deactivate the Python virtual environment, you can do so by running
$ deactivate.
Edit the tool's source code and documentation however you want.
While editing the tool's source code, you can run the tool as you normally would in order to test things out.
uv run refscan --helpWe use mypy as the static type checker for refscan.
You can perform static type checking by running the following command from the root directory of the repository:
uv run mypyWe use pytest as the testing framework for refscan.
Tests are defined in the tests directory.
You can run the tests by running the following command from the root directory of the repository:
uv run pytestWe use ruff as the code formatter for refscan.
We mostly use it with its default rules. All of the ways we deviate from those are listed
in the [tool.ruff] section of pyproject.toml.
You can check the code's compliance with the "formatter rules" by running this command from the root directory of the repository:
uv run ruff format --checkThat will output a list of files that don't comply. To see the violations, themselves, you can run:
uv run ruff format --diffYou can format the code by omitting the --check and --diff flags:
uv run ruff formatWe also use ruff as the code linter for refscan.
We use it with its default rules, plus some additional ones,
all of which are listed in the [tool.ruff.lint] section of pyproject.toml.
You can check the code's compliance with the "linter rules" by running this command from the root directory of the repository:
uv run ruff checkWhenever someone publishes a GitHub Release in this repository, a GitHub Actions workflow will automatically build a package and publish it to PyPI. That package will have a version identifier that matches the name of the Git tag associated with the Release.
In case you want to test the build process locally, you can do so by running:
uv buildThat will create both a source distribution file (whose name ends with
.tar.gz) and a wheel file (whose name ends with.whl) in thedistdirectory.
