Standalone comprehensive genome metadata standardization and sequence download toolkit.
Project description
FetchM2
FetchM2 is a comprehensive standalone command-line toolkit for bacterial genome metadata analysis, metadata standardization, audit reporting, and optional genome sequence download.
FetchM2 is designed as the updated successor to the original FetchM standalone tool. It keeps the same practical command-line workflow, but adds many more standardized metadata fields, richer filtering, packaged curation rules, audit outputs, and reproducible test data.
Recommended one-command workflow:
fetchm2 run --input ncbi_dataset.tsv --outdir results --download
Key Features
- Standalone command-line tool installable with
pipor a conda environment. - Reads NCBI Genome Datasets TSV/CSV exports.
- Optionally fetches linked BioSample metadata from NCBI with retry, cache, and fallback lookup support.
- Supports offline analysis when metadata columns are already present.
- Applies packaged deterministic standardization rules.
- Writes clean CSV and TSV metadata outputs.
- Generates metadata analysis tables and figures automatically.
- Produces audit summaries and review queues.
- Downloads genome FASTA files from NCBI.
- Supports flexible sequence-download filtering by standardized metadata.
- Includes
test.tsv, matching the public FetchM-style test dataset layout. - Includes
examples/offline_metadata.tsvfor fast local smoke testing.
Installation
Option 1: pip
python -m venv fetchm2-env
source fetchm2-env/bin/activate
pip install fetchm2==0.1.4
Verify:
fetchm2 --version
Option 2: conda / mamba environment
Clone the repository and create the environment:
git clone https://github.com/Tasnimul-Arabi-Anik/FetchM2.git
cd FetchM2
mamba env create -f environment.yml
conda activate fetchm2
If you use conda instead of mamba:
conda env create -f environment.yml
conda activate fetchm2
The conda environment includes taxonkit, which can improve host lineage enrichment for less common TaxIDs. FetchM2 still works without taxonkit; common host lineages are bundled.
Option 3: developer install
git clone https://github.com/Tasnimul-Arabi-Anik/FetchM2.git
cd FetchM2
python -m pip install -e ".[dev]"
pytest
Quick Start
Run the bundled standalone smoke test:
fetchm2 metadata --input examples/offline_metadata.tsv --outdir demo_out --offline
Run the FetchM-style test dataset:
fetchm2 metadata --input test.tsv --outdir test_out --offline
test.tsv contains assembly-level NCBI dataset columns and BioSample accessions. In offline mode, FetchM2 analyzes assembly statistics and any metadata already present in the table. To populate host, source, sample, environment, and geography from NCBI BioSample records, run without --offline.
Run metadata retrieval with BioSample enrichment:
fetchm2 metadata --input test.tsv --outdir test_out_live --workers 3 --sleep 0.4
Use an NCBI API key for larger jobs:
export NCBI_API_KEY=YOUR_NCBI_API_KEY
export NCBI_EMAIL=you@example.com
fetchm2 metadata --input ncbi_dataset.tsv --outdir results --workers 6 --sleep 0.15
Run metadata standardization and sequence download in one command:
fetchm2 run --input ncbi_dataset.tsv --outdir results --download
Typical Species/Genus Workflow
- Download an NCBI Genome Datasets TSV or CSV for your target species or genus.
- Run FetchM2:
fetchm2 run --input ncbi_dataset.tsv --outdir results --download
- Review the main outputs:
results/metadata_output/fetchm2_clean.csvresults/metadata_analysis/metadata_analysis_report.mdresults/audit/standardization_audit.mdresults/audit/production_readiness_gate.mdresults/sequence/
For large NCBI retrieval jobs without an API key, use a conservative request delay:
fetchm2 run --input ncbi_dataset.tsv --outdir results --download --workers 3 --sleep 0.4
Metadata Retrieval Workflow
FetchM2 can work in two modes.
Offline mode:
- Uses metadata columns already present in the input table.
- Applies standardization rules.
- Generates audit and metadata analysis outputs.
- Does not contact NCBI.
Live BioSample mode:
- Reads BioSample accessions from NCBI dataset exports.
- Retrieves BioSample records through NCBI E-utilities.
- Uses direct BioSample XML first, then an
esummaryfallback when the direct record lacks usable attributes. - Tracks raw BioSample attribute names and matched standardized attribute names.
- Uses a local SQLite cache so repeated runs do not refetch the same BioSample records.
- Uses request throttling, retry, and backoff behavior for temporary NCBI rate-limit or server errors.
Important output columns from retrieval include:
BioSampleBioSample Taxonomy NameMetadata Fetch StatusMetadata Fetch ReasonMetadata Fetch ErrorMetadata Raw Attribute NamesMetadata Matched Attribute Names
FetchM2 currently recognizes common BioSample attribute aliases for host, source, sample type, isolation site, collection date, geography, environmental medium/broad/local scale, host disease, and host health state.
Main Commands
fetchm2 metadata --help
fetchm2 run --help
fetchm2 seq --help
fetchm2 audit --help
fetchm2 validate --help
fetchm2 analyze --help
fetchm2 metadata
Reads an NCBI dataset TSV/CSV, optionally fetches BioSample metadata, standardizes fields, and writes clean outputs.
Example:
fetchm2 metadata \
--input ncbi_dataset.tsv \
--outdir results \
--ani OK \
--checkm 95 \
--workers 6
Common options:
--input: NCBI dataset TSV/CSV.--outdir: output directory.--ani: filter by ANI Check status, for exampleOK.--checkm: minimum CheckM completeness.--api-key: NCBI API key; can also useNCBI_API_KEY.--email: NCBI email; can also useNCBI_EMAIL.--workers: BioSample fetch worker count.--sleep: shared request delay between NCBI calls. Use a slower value such as0.4to0.5for unauthenticated larger jobs.--offline: skip NCBI fetching and standardize existing columns only.--no-analysis: skip automaticmetadata_analysis/table and figure generation.
fetchm2 run
Runs metadata analysis and, if requested, sequence download.
fetchm2 run \
--input ncbi_dataset.tsv \
--outdir results \
--ani OK \
--checkm 95 \
--download \
--country Bangladesh \
--host "Homo sapiens" \
--year-from 2018 \
--year-to 2024
fetchm2 seq
Downloads genome FASTA files using the standardized clean metadata table.
fetchm2 seq \
--input results/metadata_output/fetchm2_clean.csv \
--outdir results/sequence \
--host "Homo sapiens" \
--country Bangladesh \
--year-from 2018 \
--year-to 2024
Check expected sequences without downloading:
fetchm2 seq \
--input results/metadata_output/fetchm2_clean.csv \
--outdir results/sequence \
--country Bangladesh \
--check-only
fetchm2 audit
Audits an existing standardized output:
fetchm2 audit \
--input results/metadata_output/fetchm2_clean.csv \
--outdir results/audit_rerun
fetchm2 validate
Runs the same production-readiness checks as audit, but names the workflow explicitly for CLI validation:
fetchm2 validate \
--input results/metadata_output/fetchm2_clean.csv \
--outdir results/validation
fetchm2 analyze
Generates metadata analysis outputs from any existing clean metadata CSV.
fetchm2 analyze \
--input results/metadata_output/fetchm2_clean.csv \
--outdir results/metadata_analysis_rerun \
--top-n 30
Metadata Outputs
FetchM2 writes:
metadata_output/fetchm2_clean.csvmetadata_output/fetchm2_clean.tsvmetadata_output/fetchm2_report.mdaudit/standardization_summary.csvaudit/top_host_review_needed.csvaudit/standardization_audit.mdmetadata_analysis/metadata_analysis_report.mdmetadata_analysis/tables/field_coverage_summary.csvmetadata_analysis/tables/top_values_by_field.csvmetadata_analysis/tables/numeric_summary.csvmetadata_analysis/figures/*.png
Typical output structure:
results/
├── metadata_output/
│ ├── fetchm2_clean.csv
│ ├── fetchm2_clean.tsv
│ └── fetchm2_report.md
├── metadata_analysis/
│ ├── metadata_analysis_report.md
│ ├── tables/
│ └── figures/
├── audit/
│ ├── standardization_summary.csv
│ ├── standardization_audit.md
│ ├── production_readiness_gate.md
│ ├── production_readiness_gate.json
│ ├── top_host_review_needed.csv
│ ├── non_country_values_in_country.csv
│ ├── country_continent_mismatch.csv
│ ├── country_subcontinent_mismatch.csv
│ ├── invalid_collection_years.csv
│ ├── invalid_host_like_sample_type.csv
│ ├── source_like_mapped_hosts.csv
│ ├── source_like_unmapped_hosts_for_review.csv
│ ├── broad_vocabulary_leakage.csv
│ ├── sequence_readiness.csv
│ └── rule_count_summary.csv
└── sequence/
├── *.fna
├── failed_accessions.txt
├── sequence_download_summary.csv
└── fetchm2_sequence_cache.sqlite3
Standardized Metadata Fields
FetchM2 keeps the original input columns and adds standardized fields.
Host Standardization
Original FetchM had host-oriented metadata summaries. FetchM2 expands this into detailed host standardization:
Host_OriginalHost_CleanedHost_SDHost_TaxIDHost_RankHost_SuperkingdomHost_PhylumHost_ClassHost_OrderHost_FamilyHost_GenusHost_SpeciesHost_Common_NameHost_Context_SDHost_Match_MethodHost_ConfidenceHost_Review_Status
Examples:
human,human blood,Homosapinesvariants can map toHomo sapiens, TaxID9606.cattle fecescan map toBos taurus, TaxID9913, while also preserving feces/stool as sample metadata.bacteria culture,DH5a, lab strain terms, missing values, and source/material terms are blocked from becoming host values.
Source, Sample, and Environment
FetchM2 standardizes source/sample/environment fields into:
Sample_Type_SDSample_Type_SD_BroadIsolation_Source_SDIsolation_Source_SD_BroadIsolation_Site_SDEnvironment_Medium_SDEnvironment_Medium_SD_BroadEnvironment_Broad_Scale_SDEnvironment_Local_Scale_SD
Examples:
blood->Sample_Type_SD=bloodurine->Sample_Type_SD=urinefeces,faeces,stool->Sample_Type_SD=feces/stoolsoil->Environment_Medium_SD=soilsediment->Environment_Medium_SD=sedimentwastewater,sewage->Environment_Medium_SD=wastewater/sewagehospital surface-> healthcare/source contextrectal swab-> sample type plus anatomical site when available
Geography and Date
FetchM2 standardizes:
CountryContinentSubcontinentCollection_Year
It also blocks common false positives such as:
Hospitalas countryground turkeyas TurkeyGuinea pigas GuineaNorway ratas NorwayAspergillus nigeras Niger
Disease and Health State
FetchM2 includes:
Host_Disease_SDHost_Health_State_SD
These are conservative deterministic fields. Disease words are not treated as sample material unless an actual specimen is present.
Sequence Download Features
FetchM2 downloads genome FASTA files from the NCBI genomes FTP structure using Assembly Accession and Assembly Name.
Filtering options:
--host--host-rank--country--continent--subcontinent--sample-type--isolation-source--environment-medium--year-from--year-to--max-genomes
Download control:
--download-workers--retries--retry-delay--keep-gz--check-only
Outputs:
- genome FASTA files
failed_accessions.txtsequence_download_summary.csvfetchm2_sequence_cache.sqlite3
Test Dataset
FetchM2 includes:
test.tsv: FetchM-style NCBI dataset example copied from the public FetchM test dataset.examples/test_ncbi_dataset.tsv: same dataset stored under examples.examples/offline_metadata.tsv: small annotated metadata table for fast offline testing.
Run:
fetchm2 metadata --input test.tsv --outdir test_run --offline
fetchm2 audit --input test_run/metadata_output/fetchm2_clean.csv --outdir test_run/audit_check
For BioSample metadata retrieval:
fetchm2 metadata --input test.tsv --outdir test_run_live --workers 3 --sleep 0.34
Rule Files Packaged With FetchM2
FetchM2 ships deterministic rules in src/fetchm2/data/:
host_synonyms.csvhost_negative_rules.csvcontrolled_categories.csvapproved_broad_categories.csvgeography_reviewed_rules.csvcollection_date_reviewed_rules.csvcountry_mapping.json
These rules let the standalone tool produce richer standardized fields without needing a web database.
API Keys
For NCBI, use environment variables:
export NCBI_API_KEY=YOUR_NCBI_API_KEY
export NCBI_EMAIL=you@example.com
Do not put API keys in scripts, notebooks, README files, Git commits, or issue reports.
Validation
Run local validation:
pytest
python -m build
python -m twine check dist/*
python -m pip install dist/fetchm2-*.whl
fetchm2 metadata --input examples/offline_metadata.tsv --outdir smoke_out --offline
fetchm2 validate --input smoke_out/metadata_output/fetchm2_clean.csv --outdir smoke_out/validation
fetchm2 seq --input smoke_out/metadata_output/fetchm2_clean.csv --outdir smoke_seq --country Bangladesh --check-only
The validation report is in:
docs/VALIDATION_REPORT.md
More analysis details:
docs/METADATA_ANALYSIS.md
docs/STANDARDIZATION.md
docs/SEQUENCE_DOWNLOAD.md
docs/RELEASE_CHECKLIST.md
License
MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fetchm2-0.1.4.tar.gz.
File metadata
- Download URL: fetchm2-0.1.4.tar.gz
- Upload date:
- Size: 396.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
428f2c7c969c081d3b9c2e25d5b1bfd364cdad3bb32faee6ac5f86ae94170980
|
|
| MD5 |
3b21f70c2e54298b49b8a6823018b37d
|
|
| BLAKE2b-256 |
17859c5f9873af720b3070274b537d832c30e1a4c992331abeb11f9ecb977776
|
File details
Details for the file fetchm2-0.1.4-py3-none-any.whl.
File metadata
- Download URL: fetchm2-0.1.4-py3-none-any.whl
- Upload date:
- Size: 374.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72e40f3cf5355f44c3d6562e93743f84a61b9d03f7602e8b19b44d71c96776bd
|
|
| MD5 |
bcf1dd0132f2f1892b767b36e260fdfc
|
|
| BLAKE2b-256 |
1ddc9ae8f82ceb57b9038d0a25f8463c7243f8e304fa8e14d11a1ffc371c08bc
|