Answers.Body: <p>I recommend to use <strong>Biopython</strong>:</p>
  <pre class="lang-py prettyprint-override"><code>from Bio import Entrez
  
  Entrez.email = &quot;your@somemail.com&quot;
  Entrez.api_key = &quot;YOUR_API_KEY&quot;
  
  search_result = Entrez.read(Entrez.esearch(db=&quot;nucleotide&quot;, term=&quot;ITS1[Gene Name]&quot;))
  with Entrez.efetch(
      db=&quot;nucleotide&quot;, id=search_result[&quot;IdList&quot;], rettype=&quot;fasta&quot;, retmode=&quot;text&quot;
  ) as handle, open(filepath, &quot;w&quot;) as file:
      file.write(handle.read())
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-08T16:26:53.653Z
Answers.LastActivityDate: 2022-06-08T16:26:53.653Z
Answers.UserDisplayName: Vovin
Answers.Body: <p>For batch operations, it would be better to acquire a <a href="https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/" rel="nofollow noreferrer">NCBI API key</a> and combine <a href="https://www.gnu.org/software/parallel/" rel="nofollow noreferrer">GNU parallel</a> and <a href="https://www.ncbi.nlm.nih.gov/books/NBK179288/" rel="nofollow noreferrer">Entrez Direct</a> .
  If you already have the accession numbers of ITS1 and ITS2 genes, put the accession numbers in a text file like &quot;accession-number.txt&quot;. Otherwise search the ITS genes you want to download and then get the accession number:</p>
  <pre><code>esearch -db nuccore -query 'you query term[Gene Name]' |
    efetch -format acc &gt;accession-number.txt
  </code></pre>
  <p>To download all the sequences in fasta format.:</p>
  <pre><code>parallel -a accession-number.txt -j8 'efetch -db nuccore -id {} -format fasta &gt;{}.fa'
  </code></pre>
  <p>Here <code>-j8</code> is used to control the number of jobs run in parallel, it should be less than the limit of 10.</p>
  <p>To get related taxonomy (for eg, taxonomy rank, scientific name, taxonomy id, genus and species) of each sequence:</p>
  <pre><code>body='BEGIN { OFS = &quot;\t&quot; } { print &quot;{}&quot;, <span class="math-container">$0}'
  paralle -a accession-number.txt -j8 "elink -db nuccore -id {} -target taxonomy -name nuccore_taxonomy | 
    esummary |
    xtract -pattern DocumentSummary -element Rank ScientificName TaxId Genus Species |
    awk '$</span>body'&quot;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T08:21:41.670Z
Answers.Text: Thank you! I thought, that they do not allow to make more than 10 requests per second for an API key regardless the fact it is parallel or serial. Which is real restrictions?
Answers.UserDisplayName: Vovin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-16T02:11:13.160Z
Answers.Text: I think the real restriction is 10 requests per second. You can try `-j` with a number larger than 10 to see if there are any errors.
Answers.UserDisplayName: Forrest Vigor
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-17T04:15:57.397Z
Answers.Text: To get taxonomy info without awk: ` paralle -a accession-number.txt -j8 "elink -db nuccore -id {} -target taxonomy -name nuccore_taxonomy |
    esummary |
    xtract -pattern DocumentSummary -pfx '{}\t' -element Rank ScientificName TaxId Genus Species"`
Answers.UserDisplayName: Forrest Vigor
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T01:42:23.110Z
Answers.LastActivityDate: 2022-06-15T01:42:23.110Z
Answers.UserDisplayName: Forrest Vigor
Body: <p>I would like to download all the ITS1 and ITS2 genes from NCBI in a fasta file. And, I'd like to download even the related taxonomy of each sequence.</p>
  <p>Thanks,</p>
  <p>Marco</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-06-08T17:52:35.107Z
Comments.Text: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Comments.UserDisplayName: Community
Tags: <phylogenetics><taxonomy>
Title: How can I download from NCBI all the ITS genes and the related taxonomy?
ThreadText: # How can I download from NCBI all the ITS genes and the related taxonomy?
  
  (Asked by: username_0 on 2022-06-08)
  
  I would like to download all the ITS1 and ITS2 genes from NCBI in a fasta file. And, I'd like to download even the related taxonomy of each sequence.
  
  Thanks,
  
  username_0
  
  **Tags:** phylogenetics, taxonomy
  
  > **Comments:**
  > **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2022-06-08. Score: 4
  
  I recommend to use **Biopython**:
  
  ```py
  from Bio import Entrez
  
  Entrez.email = "your@somemail.com"
  Entrez.api_key = "YOUR_API_KEY"
  
  search_result = Entrez.read(Entrez.esearch(db="nucleotide", term="ITS1[Gene Name]"))
  with Entrez.efetch(
      db="nucleotide", id=search_result["IdList"], rettype="fasta", retmode="text"
  ) as handle, open(filepath, "w") as file:
      file.write(handle.read())
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2022-06-15. Score: 3
  
  For batch operations, it would be better to acquire a [NCBI API key](https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/) and combine [GNU parallel](https://www.gnu.org/software/parallel/) and [Entrez Direct](https://www.ncbi.nlm.nih.gov/books/NBK179288/) .
  If you already have the accession numbers of ITS1 and ITS2 genes, put the accession numbers in a text file like "accession-number.txt". Otherwise search the ITS genes you want to download and then get the accession number:
  
  ```
  esearch -db nuccore -query 'you query term[Gene Name]' |
    efetch -format acc >accession-number.txt
  
  ```
  
  To download all the sequences in fasta format.:
  
  ```
  parallel -a accession-number.txt -j8 'efetch -db nuccore -id {} -format fasta >{}.fa'
  
  ```
  
  Here `-j8` is used to control the number of jobs run in parallel, it should be less than the limit of 10.
  
  To get related taxonomy (for eg, taxonomy rank, scientific name, taxonomy id, genus and species) of each sequence:
  
  ```
  body='BEGIN { OFS = "\t" } { print "{}", $0}'
  paralle -a accession-number.txt -j8 "elink -db nuccore -id {} -target taxonomy -name nuccore_taxonomy | 
    esummary |
    xtract -pattern DocumentSummary -element Rank ScientificName TaxId Genus Species |
    awk '$body'"
  
  ```
  
  > **Comments:**
  > **username_2**: Thank you! I thought, that they do not allow to make more than 10 requests per second for an API key regardless the fact it is parallel or serial. Which is real restrictions?  
  > **username_3**: I think the real restriction is 10 requests per second. You can try `-j` with a number larger than 10 to see if there are any errors.  
  > **username_3**: To get taxonomy info without awk: ` paralle -a accession-number.txt -j8 "elink -db nuccore -id {} -target taxonomy -name nuccore_taxonomy |   esummary |   xtract -pattern DocumentSummary -pfx '{}\t' -element Rank ScientificName TaxId Genus Species"`  
  
  ---
  
  