First consider the naive non-automatic approach to this question.

Lets use Escherichia coli str. K-12 substr. MG1655 as an example it has the taxonomic ID 511145. In the gnome DB of NCBI I can find this entry:

https://www.ncbi.nlm.nih.gov/genome/?term=txid511145[Organism:exp]

The first line says -> Reference genome: Escherichia coli str. K-12 substr. MG1655. Which links to:

https://www.ncbi.nlm.nih.gov/genome/167?genome_assembly_id=161521

Which is what I want. I can no use the RefSeq Accession to get the genome.

Now lets try this automatically:

    esearch -db genome -query "txid511145[orgn]" | esummary

The result is the following:

    <DocumentSummary><Id>167</Id>
    	<Organism_Name>Escherichia coli</Organism_Name>
    	<Organism_Kingdom>Bacteria</Organism_Kingdom>
    	<Organism_Group></Organism_Group>
    	<Organism_Subgroup>Gammaproteobacteria</Organism_Subgroup>
    	<Defline>A well-studied enteric bacterium</Defline>
    	<ProjectID>12319</ProjectID>
    	<Project_Accession>PRJNA12319</Project_Accession>
    	<Status>Complete</Status>
    	<Number_of_Chromosomes>3</Number_of_Chromosomes>
    	<Number_of_Plasmids>10</Number_of_Plasmids>
    	<Number_of_Organelles>0</Number_of_Organelles>
    	<Assembly_Name>ASM2632v1</Assembly_Name>
    	<Assembly_Accession>GCA_000026325.2</Assembly_Accession>
    	<AssemblyID>1181661</AssemblyID>
    	<Create_Date>1998/10/13 00:00</Create_Date>
    	<Options></Options>
    	<Weight>900</Weight>
    	<Chromosome_assemblies>484</Chromosome_assemblies>
    	<Scaffold_assemblies>9772</Scaffold_assemblies>
    	<SRA_genomes>0</SRA_genomes>
    	<TaxId>562</TaxId>
    </DocumentSummary>

The first problem is that this querry brings me to species level (see TaxId 562) and not to the far more specific level of my query (strain).

The specific question in this case would be how can you link the TaxId 511145 to the genome_assembly_id 161521 using an esearch querry. Obvious the connection is there as I can make the link on the website.


    #!/bin/bash
    TAXID="$1"
    if [[ -z "$TAXID" ]]; then
        echo "Usage: retrieveReference.sh NcbiTaxId"
        exit
    else
        if [[ ! -e assembly_summary_genbank.txt ]]; then
            wget ftp://ftp.ncbi.nlm.nih.gov/genomes/genbank/assembly_summary_genbank.txt
        fi
        awk -v TAXID="$TAXID" \
            'BEGIN{FS="\t"}{if($5=="reference genome" && $6==TAXID){print $20}}' \
            assembly_summary_genbank.txt \
    	| awk 'BEGIN{OFS=FS="/"}{print $0,$NF"_genomic.fna.gz"}' \
    	| xargs -n1 wget
    fi