Skip to content

Instantly share code, notes, and snippets.

@jackkamm
Created October 18, 2018 00:54
Show Gist options
  • Save jackkamm/dc46ab7f9f6df8370151459a2d522574 to your computer and use it in GitHub Desktop.
Save jackkamm/dc46ab7f9f6df8370151459a2d522574 to your computer and use it in GitHub Desktop.
Download Genbank Assembly DB fasta with BioPython
import os
import urllib
import xml.etree.ElementTree as ET
from Bio import Entrez
Entrez.email = "jack.kamm@czbiohub.org"
uid = ET.parse(Entrez.esearch(db="assembly", term="GCA_000150765.1")).getroot().find("IdList")[0].text
ftp_path = ET.parse(Entrez.esummary(db="assembly", id=uid)).getroot().find("DocumentSummarySet").find("DocumentSummary").find("FtpPath_GenBank").text
basename = os.path.basename(ftp_path)
fasta_path = os.path.join(ftp_path, basename + "_genomic.fna.gz")
urllib.request.urlretrieve(fasta_path, os.path.basename(fasta_path))
@jackkamm
Copy link
Author

While NCBI entrez tools can fetch fasta files from some db, unfortunately this doesn't work for the assembly db. Instead, we must use the Entrez tools to fetch the ftp URL, and then download with some other tool. Here, I use BioPython as a wrapper around Entrez to extract this ftp path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment