-
Notifications
You must be signed in to change notification settings - Fork 3k
Add fasta support #7851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
georgia-hf
wants to merge
8
commits into
huggingface:main
Choose a base branch
from
georgia-hf:add-fasta-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add fasta support #7851
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a4c1f18
adding fasta
georgiachanning 204b892
adding fasta
georgiachanning 1a697ae
DOC: remove mode parameter in docstring of pdf and video feature (#7848)
CloseChoice 4fb0866
release: 4.4.1 (#7849)
lhoestq ff5db68
dev version (#7850)
lhoestq 02bc224
adding docs
georgiachanning 014059c
Merge branch 'huggingface:main' into add-fasta-support
georgia-hf ccdd83a
adding fastq support
georgiachanning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,137 @@ | ||
| import itertools | ||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Dict, Iterable, Optional | ||
| from typing import List as ListT | ||
|
|
||
| import pyarrow as pa | ||
| from Bio import SeqIO | ||
|
|
||
| import datasets | ||
| from datasets.features import Value | ||
| from datasets.table import table_cast | ||
| from datasets.utils.file_utils import xopen | ||
|
||
|
|
||
|
|
||
| logger = datasets.utils.logging.get_logger(__name__) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from Bio import SeqIO | ||
|
|
||
| # Common FASTA extensions; .gz will be handled by dl_manager.extract_on_the_fly | ||
| EXTENSIONS = [".fa", ".fasta", ".fna", ".ffn", ".faa", ".frn", ".fa.gz", ".fasta.gz"] | ||
|
|
||
|
|
||
| @dataclass | ||
| class FASTAConfig(datasets.BuilderConfig): | ||
| """BuilderConfig for FASTA.""" | ||
|
|
||
| batch_size: Optional[int] = None | ||
| columns: Optional[ListT[str]] = None # subset of ["id", "description", "sequence"] | ||
| features: Optional[datasets.Features] = None | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
|
|
||
| class FASTA(datasets.ArrowBasedBuilder): | ||
| """ArrowBasedBuilder that converts FASTA files to Arrow tables.""" | ||
|
|
||
| BUILDER_CONFIG_CLASS = FASTAConfig | ||
|
|
||
| def _info(self): | ||
| if ( | ||
| self.config.columns is not None | ||
| and self.config.features is not None | ||
| and set(self.config.columns) != set(self.config.features) | ||
| ): | ||
| raise ValueError( | ||
| "The columns and features argument must contain the same columns, but got " | ||
| f"{self.config.columns} and {self.config.features}", | ||
| ) | ||
| # Default features if not provided | ||
| if self.config.features is None: | ||
| self.config.features = datasets.Features( | ||
| {"id": Value("string"), "description": Value("string"), "sequence": Value("string")} | ||
| ) | ||
| return datasets.DatasetInfo(features=self.config.features) | ||
|
|
||
| def _split_generators(self, dl_manager): | ||
| if not self.config.data_files: | ||
| raise ValueError(f"At least one data file must be specified, but got data_files={self.config.data_files}") | ||
| dl_manager.download_config.extract_on_the_fly = True | ||
| data_files = dl_manager.download_and_extract(self.config.data_files) | ||
|
|
||
| splits = [] | ||
| for split_name, files in data_files.items(): | ||
| if isinstance(files, str): | ||
| files = [files] | ||
| # Expand dirs/globs into concrete file iterables | ||
| files = [dl_manager.iter_files(file) for file in files] | ||
|
|
||
| # Optionally narrow features to requested columns | ||
| if self.config.columns is not None and set(self.config.columns) != set(self.info.features): | ||
| self.info.features = datasets.Features( | ||
| {col: feat for col, feat in self.info.features.items() if col in self.config.columns} | ||
| ) | ||
|
|
||
| splits.append(datasets.SplitGenerator(name=split_name, gen_kwargs={"files": files})) | ||
|
|
||
| return splits | ||
|
|
||
| def _cast_table(self, pa_table: pa.Table) -> pa.Table: | ||
| if self.info.features is not None: | ||
| pa_table = table_cast(pa_table, self.info.features.arrow_schema) | ||
| return pa_table | ||
|
|
||
| def _generate_tables(self, files): | ||
| # files is an iterable of iterables (one per user provided path) | ||
| effective_cols = list(self.info.features.keys()) | ||
| batch_size_cfg = self.config.batch_size or self._writer_batch_size or 10_000 | ||
|
|
||
| for file_idx, file in enumerate(itertools.chain.from_iterable(files)): | ||
| # Stream-parse and yield Arrow tables in batches | ||
| try: | ||
| batch = {col: [] for col in effective_cols} | ||
| row_count = 0 | ||
| for rec in _iter_fasta_records(file): | ||
| row = { | ||
| "id": rec["id"], | ||
| "description": rec["description"], | ||
| "sequence": rec["sequence"], | ||
| } | ||
| for col in effective_cols: | ||
| batch[col].append(row[col]) | ||
| row_count += 1 | ||
|
|
||
| if row_count % batch_size_cfg == 0: | ||
| pa_table = pa.Table.from_pydict(batch) | ||
| yield f"{file_idx}_{row_count - batch_size_cfg}", self._cast_table(pa_table) | ||
| batch = {col: [] for col in effective_cols} | ||
|
|
||
| # Flush tail | ||
| if batch and any(len(v) for v in batch.values()): | ||
| start = row_count - len(next(iter(batch.values()))) if row_count else 0 | ||
| pa_table = pa.Table.from_pydict(batch) | ||
| yield f"{file_idx}_{start}", self._cast_table(pa_table) | ||
|
|
||
| except ValueError as e: | ||
| logger.error(f"Failed to read file '{file}' with error {type(e)}: {e}") | ||
| raise | ||
|
|
||
|
|
||
| # ┌─────────────┐ | ||
| # │ FASTA I/O │ | ||
| # └─────────────┘ | ||
|
|
||
|
|
||
| def _iter_fasta_records(path: str) -> Iterable[Dict[str, str]]: | ||
| """ | ||
| Streaming FASTA parser that yields dicts with keys: id, description, sequence. | ||
| - Supports regular files and fsspec paths (including gzip://) | ||
| - Uses xopen to handle compressed files and streaming paths | ||
| """ | ||
| # Use xopen to handle fsspec paths (e.g., gzip://file::path.gz) and regular paths | ||
| # Open in text mode for BioPython's SeqIO.parse | ||
| with xopen(path, "r", encoding="utf-8") as f: | ||
| for r in SeqIO.parse(f, "fasta"): | ||
| yield {"id": r.id, "description": r.description, "sequence": str(r.seq)} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also add the upper versions of the extensions here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, but why do none of the other extensions have uppers?