text: user15596: 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,
  Marco
  
  user15583: For batch operations, it would be better to acquire a NCBI API key and combine GNU parallel and Entrez Direct .
  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'"
  
  
  user15161: I recommend to use Biopython:
  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())
  
  
original_text: 15596: <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>
   15583: <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>
   15161: <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>
  