Last active
September 1, 2023 13:15
-
-
Save SJShaw/14e15ce646968883c38af41a047df9a4 to your computer and use it in GitHub Desktop.
Script to remove specific module(s) results from antiSMASH results JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Removes specific module(s) results from antiSMASH results JSON | |
Modules are expected to be named as per a python `__name__` result, | |
e.g. "antismash.modules.clusterblast" | |
""" | |
from typing import Set | |
import json | |
import sys | |
def remove_module_results(filename: str, targets: Set[str]): | |
""" Removes each target module from the given results JSON""" | |
with open(filename) as handle: | |
data = json.load(handle) | |
removed = set() | |
for record in data["records"]: | |
record_modules = record.get("modules", []) | |
for module in targets: | |
if module in record_modules: | |
removed.add(module) | |
del record_modules[module] | |
new_content = json.dumps(data) | |
with open(filename, "w") as handle: | |
handle.write(new_content) | |
for module in targets.difference(removed): | |
print("no results existed for:", module) | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
print("Usage: %s results_file target [target ...]" % sys.argv[0]) | |
sys.exit(1) | |
remove_module_results(sys.argv[1], set(sys.argv[2:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment