download ncbi nucleotide database code example

Example: download ncbi nucleotide database

use LWP::Simple;$query = 'chimpanzee[orgn]+AND+biomol+mrna[prop]';#assemble the esearch URL$base = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/';$url = $base . "esearch.fcgi?db=nucleotide&term=$query&usehistory=y";#post the esearch URL$output = get($url);#parse WebEnv, QueryKey and Count (# records retrieved)$web = $1 if ($output =~ /(\S+)<\/WebEnv>/);$key = $1 if ($output =~ /(\d+)<\/QueryKey>/);$count = $1 if ($output =~ /(\d+)<\/Count>/);#open output file for writingopen(OUT, ">chimp.fna") || die "Can't open file!\n";#retrieve data in batches of 500$retmax = 500;for ($retstart = 0; $retstart < $count; $retstart += $retmax) {        $efetch_url = $base ."efetch.fcgi?db=nucleotide&WebEnv=$web";        $efetch_url .= "&query_key=$key&retstart=$retstart";        $efetch_url .= "&retmax=$retmax&rettype=fasta&retmode=text";        $efetch_out = get($efetch_url);        print OUT "$efetch_out";}close OUT;

Tags:

Misc Example