Created
September 19, 2023 21:03
-
-
Save apcamargo/3a139411e8637307973b76cce972861a to your computer and use it in GitHub Desktop.
Retrieve NCBI assembly accessions from GenBank accessions using E-utilities
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
import subprocess | |
def get_assembly_accession(genbank_accession): | |
p1 = subprocess.Popen( | |
["elink", "-db", "nuccore", "-target", "assembly", "-id", genbank_accession], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
p2 = subprocess.Popen( | |
["efetch", "-format", "docsum"], | |
stdin=p1.stdout, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
p3 = subprocess.Popen( | |
["xtract", "-pattern", "DocumentSummary", "-element", "AssemblyAccession"], | |
stdin=p2.stdout, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
text=True, | |
) | |
stdout, stderr = p3.communicate() | |
if p3.returncode: | |
raise RuntimeError(stderr) | |
return stdout.strip("\n") | |
get_assembly_accession("CP080583.1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment