-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from nextstrain/dedup-ncbi
Dedup ncbi
- Loading branch information
Showing
4 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Deduplicate records by the sample id of the `strain` field. | ||
For example, the following strain names have duplicate sample id 24-005334-001 | ||
- A/chicken/Ohio/24-005334-001/2024 | ||
- A/Chicken/USA/24-005334-001-original/2024 | ||
Only keeps the first record of duplicates and keeps record if the sample id | ||
cannot be parsed from `strain`. | ||
""" | ||
import json | ||
import re | ||
from sys import stderr, stdin, stdout | ||
|
||
|
||
SAMPLE_ID_REGEX = r"^A\/[^\/]*\/[^\/]*\/(\d{2}-\d{6}-\d{3})[^\/]*\/\d{4}$" | ||
STRAIN_FIELD = "strain" | ||
|
||
|
||
if __name__ == "__main__": | ||
seen_sample_ids = set() | ||
|
||
for index, record in enumerate(stdin): | ||
record = json.loads(record).copy() | ||
|
||
strain = record[STRAIN_FIELD] | ||
sample_id_matches = re.match(SAMPLE_ID_REGEX, strain) | ||
if sample_id_matches is not None: | ||
sample_id = sample_id_matches.group(1) | ||
if sample_id in seen_sample_ids: | ||
print( | ||
f"Dropping record {index!r} because it has a duplicate ", | ||
f"sample ID {sample_id!r} in the strain name {strain!r}", | ||
file=stderr | ||
) | ||
continue | ||
|
||
seen_sample_ids.add(sample_id) | ||
|
||
# Not every strain name has a matching sample id | ||
# Just keep records that don't match | ||
json.dump(record, stdout, allow_nan=False, indent=None, separators=',:') | ||
print() |
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
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
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