Paul
Paul

Reputation: 1117

Looping over two sets of files

I have a set of files with the following extension

  A.fa
  A_cod.fa
  B.fa
  B_cod.fa
  C.fa
  C_cod.fa
  D.fa
  D_cod.fa

For each file I had to do it as

 prank -convert -d=A.fa -dna=A_cod.fa -o=A.alignment -keep

I wanted to loop over the set of files and do the above instead of each file and tried:

  for f in *.fa and for f1 in *_cod.fa; do prank -convert -d=$f -dna=$f1 -o=$f.alignment -keep; done ;

But this does not work. So for each file A -d should read in .fa file and -dna should read in the corresponding _cod.fa file and simialrly for B and so on.

Upvotes: 2

Views: 67

Answers (2)

Thor
Thor

Reputation: 47099

Assuming your files are alphabetically ordered and are not named with white-space characters, you can use columns and a while loop like this:

ls | columns -c2 | while read cod_fa fa; do 
    prank -convert -d=$fa -dna=$cod_fa -o=A.alignment -keep
done

You better test the loop with an echo in-front of prank first.

Upvotes: 1

codeforester
codeforester

Reputation: 42999

Since you have a _cod.fa file for each .fa file, we can do this with a single Bash loop:

#!/bin/bash
for f in *.fa; do
  [[ "${f/*_/}" = "cod.fa" ]] && continue # skip *_cod.fa files
  cod_file="${f//.fa}_cod.fa"
  [[ ! -f "$cod_file" ]] && continue      # _cod.fa doesn't exist
  prank -convert -d="$f" -dna="$cod_file" -o="$f.alignment" -keep
done

Upvotes: 2

Related Questions