Download mid/pipeline/2_p90_split.py from HuggingFaceBio/FineDNA: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/datasets/HuggingFaceBio/FineDNA/resolve/main/mid/pipeline/2_p90_split.py
- Command line
-
hf download hf://datasets/HuggingFaceBio/FineDNA/mid/pipeline/2_p90_split.py
-
curl -L -o 2_p90_split.py https://huggingface.co/datasets/HuggingFaceBio/FineDNA/resolve/main/mid/pipeline/2_p90_split.py
2.66 kB
| """Step 2: split the regex-filtered documents by how long their longest DNA sequence is. | |
| Every DNA "word" in a document is found with tag_dna_sequences: a whitespace-delimited token of at least 6 letters that | |
| is pure A/C/G/T, or IUPAC codes (12+ letters, at most 20% ambiguous), with surrounding punctuation and 5'/3' markers | |
| stripped. A document whose longest DNA word is at least 39 bp, the 90th percentile of that length across the regex | |
| data, goes to p90; everything else goes to rest. Short DNA words are mostly primers and motifs; p90 documents carry | |
| real sequences. | |
| python 2_p90_split.py --input ./dna_regex/fineweb_edu --p90 ./p90/fineweb_edu --rest ./rest/fineweb_edu | |
| Input and output are jsonl.gz files with a "text" field, as written by step 1. One output file per input file. | |
| """ | |
| import argparse | |
| import glob | |
| import gzip | |
| import json | |
| import os | |
| from tag_dna_sequences import tag_dna_sequences | |
| P90_THRESHOLD = 39 # bp: the 90th percentile of the longest DNA word per document | |
| MIN_LEN = 6 # bp: shortest token counted as DNA | |
| def longest_dna(text: str) -> int: | |
| _, _, seqs = tag_dna_sequences(text, min_len=MIN_LEN) | |
| return max((len(s) for s in seqs), default=0) | |
| def main(): | |
| ap = argparse.ArgumentParser(description="Split DNA-bearing documents into p90 (longest DNA >= 39 bp) and rest.") | |
| ap.add_argument("--input", required=True, help="folder of jsonl.gz files from step 1") | |
| ap.add_argument("--p90", required=True, help="output folder for p90 documents") | |
| ap.add_argument("--rest", required=True, help="output folder for the rest") | |
| ap.add_argument("--threshold", type=int, default=P90_THRESHOLD) | |
| a = ap.parse_args() | |
| os.makedirs(a.p90, exist_ok=True) | |
| os.makedirs(a.rest, exist_ok=True) | |
| n_p90 = n_rest = 0 | |
| for path in sorted(glob.glob(os.path.join(a.input, "**", "*.jsonl.gz"), recursive=True)): | |
| name = os.path.basename(path) | |
| with gzip.open(path, "rt", encoding="utf-8") as fin, \ | |
| gzip.open(os.path.join(a.p90, name), "wt", encoding="utf-8") as fp90, \ | |
| gzip.open(os.path.join(a.rest, name), "wt", encoding="utf-8") as frest: | |
| for line in fin: | |
| if not line.strip(): | |
| continue | |
| text = json.loads(line).get("text") or "" | |
| if longest_dna(text) >= a.threshold: | |
| fp90.write(line if line.endswith("\n") else line + "\n"); n_p90 += 1 | |
| else: | |
| frest.write(line if line.endswith("\n") else line + "\n"); n_rest += 1 | |
| print(f"p90: {n_p90:,} documents rest: {n_rest:,} documents") | |
| if __name__ == "__main__": | |
| main() | |