Biostar Beta. Not for public use.
Bash loop using Edirect (esearch|efetch)
3
Entering edit mode
12 months ago
fhsantanna • 440
Brazil

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?

ADD COMMENTlink
0
Entering edit mode

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
1
Entering edit mode

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
0
Entering edit mode

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
1
Entering edit mode

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
0
Entering edit mode

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
0
Entering edit mode

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

ADD REPLYlink
2
Entering edit mode

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

ADD REPLYlink
0
Entering edit mode

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
0
Entering edit mode

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

ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.1