0

For context: I am running Prokka (as a novice bioinformatician with no Prokka and little Bash experience) and I have multiple FASTA files.

I have been told I need to produce a Bash loop that will allow me to input the FASTA files 1 by 1.

For example: my directory with all the FASTA (.fna) files is called StaphFNA, it has 1000 FASTA files in there. They are all named with ID numbers - but they do not increment as normal (so they aren't numbered 1, 2, 3 etc) there are random jumps e.g., one first be 34872 and the next one is 8933.

I need the first FASTA file within StaphFNA to be inputted, processed in Prokka, then outputted to a file called 'StaphProkka', then the 2nd FASTA file to go through the same process - being outputted into 'StaphProkka' again. On and on until all 1000 are complete.

As you can imagine, its a bit laborious having to do this 1000 times, so I wish to make a loop, but I am new to bash and I am struggling.

My Prokka argument (which I believe to be correct):

prokka /Users/me/Documents/StaphFNA --outdir StaphProkka --prefix staph_aur --kingdom bacteria --locusting staphylococcus

My For loop (which I know is wrong):

i = 0
for i in StaphFNA/.fna*; do
    prokka /Users/me/Documents/StaphFNA/$i --outdir StaphProkka --prefix staph_aur --kingdom bacteria --locusting staphylococcus
i = i + 1
done
3
  • Do you need to specify an output file name or does prokka do it for you? If, for example, you run prokka foo.fna --outdir StaphProkka, does that create a file called StaphProkka/foo.prokka or similar?
    – terdon
    Oct 27, 2022 at 13:02
  • Remove i = 0 and remove i = i + 1. Also if you want to test first, write echo in front of prokka. It will show all commands that will be run when you do it without echo
    – Garo
    Oct 27, 2022 at 13:05
  • This question is perfectly on topic and welcome here, but you might also be interested in our sister site: Bioinformatics.
    – terdon
    Oct 27, 2022 at 14:02

1 Answer 1

1

If you don't need to specify an output file and prokka produces differently named files based on its input, all you need is:

for file in /Users/me/Documents/StaphFNA/*.fna; do
    prokka "$file" \ 
        --outdir StaphProkka \
        --prefix staph_aur \
        --kingdom bacteria \
        --locusting staphylococcus 
done

The for variable in dir/* pattern will iterate over every file or directory in dir/ and save its pathname as $variable, so you don't need a counter or anything else. If you did need to use a counter, for example to deal with file1.fa, file2.fa etc, and didn't want to also deal with otherfile.fa, you would so something like this (note the lack of spaces around the =, that's iportant):

for ((i=0; i<=10; i++)); do
    someCommand "file${i}.fa"
done

Or, if you need to explicitly increment the counter (which is rare), you could do:

i=0
while [ "$i" -le 10 ]; do
    echo someCommand "file${i}.fa"
    (( i++ ))
done
6
  • Should be [ "$i" -le 10 ]...i=$(( i + 1 )) (sh) or (( i <= 10 )) ... (( i++ )) (ksh/zsh/bash) Oct 27, 2022 at 14:25
  • @StéphaneChazelas you mean the quotes? OK, but since we are controlling the value here, it didn't seem too important. As for the rest, I used (( i++ )) yes, since this is tagged bash. Am I missing something else?
    – terdon
    Oct 27, 2022 at 14:37
  • See Security implications of forgetting to quote a variable in bash/POSIX shells (especially the part after exasperating :-) You're controlling the value, but there's still no reason to invoke split+glob, and you don't know what the value of $IFS will be in all contexts wherever that code will be used. Oct 27, 2022 at 15:30
  • Ha! Fair enough, @StéphaneChazelas, I certainly don't want to exasperate you :) And you'll notice I have quoted it already after your last comment. I was just wondering if this isn't one of the few, 100% safe cases given that I include a i=0 just there, so it seems to me that there is no way the lack of quotes could possibly cause an issue. Am I wrong?
    – terdon
    Oct 27, 2022 at 17:23
  • You've not read the part after exasperating, have you? Oct 27, 2022 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .