1

I have a script I wish to run through a directory of files:

awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' < TNGA_C_0hr_Joined_Asz1.fastqsanger | awk 'NR%2==0' | sed 's/TGAGATGTGA/*/p'  | cut -d '*' -f 2 -s | sort -k 1.180,1.194 -u | cat -n | sed 's/^/>/' | tr "[\t]" "\n" > TNGA_C_0hr_Asz1_Collapsed.fasta

I have tested this script, and it works fine for what I need it to do.

What I want to do is create a for loop that will apply it to all the files in a directory. I've tried the below, and it did not work. Variations of it have also been tried.

for filename in /Test/*.fastqsanger; do
    awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' < filename.fastqsanger | awk 'NR%2==0' | sed 's/TGAGATGTGA/*/p'  | cut -d '*' -f 2 -s | sort -k 1.180,1.194 -u | cat -n | sed 's/^/>/' | tr "[\t]" "\n" > filename.fasta
done

I'm fairly new to programming so any help is appreciated.

  • 1
    Shouldn't your script be using $filename instead of filename.fastqsanger? By the way, when you get an error message, you should give the exact message to make the error more obvious. – Julie Pelletier Aug 24 '16 at 3:43
  • Thanks Julie, that hint was all I needed to crack it. Here's the modified script:for filename in .fastqsanger; do awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' < $filename | awk 'NR%2==0' | sed 's/TGAGATGTGA/*/p' | cut -d '' -f 2 -s | sort -k 1.180,1.194 -u | cat -n | sed 's/^/>/' | tr "[\t]" "\n" > $filename.fasta; done – Luke Bridgman Aug 24 '16 at 3:51
  • The output at the end would be TNGA_C_0hr_Joined_Asz1.fastqsanger.fasta. Is that what you want? – SACHIN GARG Aug 24 '16 at 4:08
1

Your script is not using the loop's filename variable. filename.fastqsanger does not refer to a variable and implies that the full filename is not already present although it is.

Here is the corrected version:

for filename in /Test/*.fastqsanger; do
    awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' < $filename | awk 'NR%2==0' | sed 's/TGAGATGTGA/*/p'  | cut -d '*' -f 2 -s | sort -k 1.180,1.194 -u | cat -n | sed 's/^/>/' | tr "[\t]" "\n" > $filename.fasta
done
-1

You are not using the filename variable in the line below:

for filename in /Test/*.fastqsanger; do
    awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' < filename.fastqsanger | awk 'NR%2==0' | sed 's/TGAGATGTGA/*/p'  | cut -d '*' -f 2 -s | sort -k 1.180,1.194 -u | cat -n | sed 's/^/>/' | tr "[\t]" "\n" > filename.fasta
done

Try:

for filename in /Test/*.fastqsanger
do
    fname=`basename $filename .fastqsanger`
    awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' < $filename | awk 'NR%2==0' | sed 's/TGAGATGTGA/*/p'  | cut -d '*' -f 2 -s | sort -k 1.180,1.194 -u | cat -n | sed 's/^/>/' | tr "[\t]" "\n" > $fname.fasta
done

This saves the basename of the file in $fname so that:

/Test/abcd.fastqsanger becomes /Test/abcd.fasta instead of /Test/abcd.fastqsanger.fasta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.