-
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.
ingest/joined-ncbi: Dedup by sample id in strain
Resolves <#95> After appending the Andersen lab metadata to the NCBI metadata, dedup the records by sample id within the strain name. Then the sequences are filtered by the final strain names in the metadata TSV.
- Loading branch information
1 parent
e98c98c
commit 58c0508
Showing
2 changed files
with
86 additions
and
35 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