Created
October 18, 2018 00:54
-
-
Save jackkamm/dc46ab7f9f6df8370151459a2d522574 to your computer and use it in GitHub Desktop.
Download Genbank Assembly DB fasta with BioPython
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
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.