Question: Bash loop using Edirect (esearch|efetch)
4
gravatar for fhsantanna
4.4 years ago by
fhsantanna520
Brazil
fhsantanna520 wrote:

I have a text file containing in each line an access number (file 16S_list). I tried to download sequences from genbank using this list through the following script:

#!/bin/bash
while read line; do
esearch -db nucleotide -query $line | efetch -db nucleotide -format fasta 
done < 16S_list

However, it did not worked, just the first sequence is downloaded. After I tried the following script, and this time, it worked:

#!/bin/bash
while read line; do
esearch -db nucleotide -query $line < /dev/null | efetch -db nucleotide -format fasta
done

I did not understand why the first one is not working. Could somebody explain why the first script failed?

while esearch edirect • 3.7k views
ADD COMMENTlink written 4.4 years ago by fhsantanna520

do you have any empty line in 16S_list ? try to quote $line to "$line"

I don't get why the second script would work by reading /dev/null

ADD REPLYlink written 4.4 years ago by Pierre Lindenbaum134k
1

Does the first one work if you set IFS to newline? IFS=$'\n'? Are your lines delimited by \n? I recall having problems with this too. AFAIR the esearch command consumed the entire file (stdin), hence I got output only for the first line. I think I ended up using IFS=$'\n'; for next in $(cat file); do ..; done

ADD REPLYlink modified 4.4 years ago • written 4.4 years ago by 5heikki9.3k
1

It worked. Here is the complete code:

IFS=$'\n'; for next in $(cat 16S_list); do esearch -db nucleotide -query $next | efetch -db nucleotide -format fasta; done

However, I still do not understand why my first script did not worked.

ADD REPLYlink written 4.4 years ago by fhsantanna520
1

The shell splits the file in the cat command whereas in the while loop the esearch command consumes the entire file, i.e. it doesn't care about the linebreaks. It's basically like this

ADD REPLYlink modified 4.4 years ago • written 4.4 years ago by 5heikki9.3k

Great. So my fist code should be altered to the following one:

while read line <&3; do
esearch -db nucleotide -query "$line" | efetch -db nucleotide -format fasta
done 3<16S_list

It worked fine. Thank you very much.

ADD REPLYlink written 4.4 years ago by fhsantanna520

There is no empty line. I quoted, nothing happened. Neither do I...

ADD REPLYlink written 4.4 years ago by fhsantanna520
2

The second script works because the /dev/null redirect prevents esearch from consuming the entire file..

ADD REPLYlink written 4.4 years ago by 5heikki9.3k

Why does esearch consume whole file? Is it a bug? Because when I echo the lines of the list using while loop, everything works fine.

ADD REPLYlink written 4.4 years ago by fhsantanna520

It's not really a bug, but default behavior of the program..

ADD REPLYlink written 4.4 years ago by 5heikki9.3k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2182 users visited in the last hour
_