For loop to multiple fasta files using TrimAL
1
0
Entering edit mode
4.3 years ago

Hi, I'm new in bioinformatics and need some help. I have multiple fasta files in the same directory. And I need to trim the sequences inside the files using TrimAL. Since there are 200 files I though it would be easier to do a for loop so I could run all at once. But all examples I found to use for loops did not work. The files are named as EOG091503G5_aligned.fasta, EOG091506R4_aligned.fasta, EOG0915095X_aligned.fasta, EOG0915054S_aligned.fasta, for example.

I have tried something like this and some variants of this script, but nothing worked:

#!/bin/bash

for file in aligned_trim/*_aligned.fasta;
do
    trimal -in $file -out $file_trim.fasta -htmlout $file_trim.html -automated1;
done

I used bash scripts because was the ones that I found people explaining, but if a Python script is the better option for me it's ok too.

I appreciate any help. Thank you in advance.

trimal loop sequences • 2.1k views
ADD COMMENT
0
Entering edit mode

See C: How to assign keys and values in a directory by using python (don't go on the name of the thread, @RamRS's comment is for help with bash loops) for assistance with this. Tip: You need to remove the file extension (.fasta) when capturing the file name in a variable, otherwise you are going to get output filenames that will have .fasta repeated in name.

Alternatively you could do something like

for file in `ls -1 aligned_trim/*_aligned.fasta | sed 's/.fasta//'`

to remove that extension.

ADD REPLY
0
Entering edit mode

I'm going to have a look at it! Thank you very much for the help!!

ADD REPLY
2
Entering edit mode
4.3 years ago

Try:

for file in *_aligned.fasta;
do
    echo trimal -in $file -out ${file%%.fasta}_trim.fasta -htmlout ${file%%.fasta}_trim.html -automated1;
done

output:

trimal -in EOG091503G5_aligned.fasta -out EOG091503G5_aligned_trim.fasta -htmlout EOG091503G5_aligned_trim.html -automated1
 trimal -in EOG0915054S_aligned.fasta -out EOG0915054S_aligned_trim.fasta -htmlout EOG0915054S_aligned_trim.html -automated1
 trimal -in EOG091506R4_aligned.fasta -out EOG091506R4_aligned_trim.fasta -htmlout EOG091506R4_aligned_trim.html -automated1
 trimal -in EOG0915095X_aligned.fasta -out EOG0915095X_aligned_trim.fasta -htmlout EOG0915095X_aligned_trim.html -automated1

Remove echo to run the command

With gnu-parallel:

$ parallel --dry-run trimal -in {} -out {.}_trim.fasta -htmlout {.}_trim.html -automated1 ::: *_aligned.fasta

trimal -in EOG091503G5_aligned.fasta -out EOG091503G5_aligned_trim.fasta -htmlout EOG091503G5_aligned_trim.html -automated1
trimal -in EOG0915054S_aligned.fasta -out EOG0915054S_aligned_trim.fasta -htmlout EOG0915054S_aligned_trim.html -automated1
trimal -in EOG091506R4_aligned.fasta -out EOG091506R4_aligned_trim.fasta -htmlout EOG091506R4_aligned_trim.html -automated1
trimal -in EOG0915095X_aligned.fasta -out EOG0915095X_aligned_trim.fasta -htmlout EOG0915095X_aligned_trim.html -automated1

Remove dry-run to execute the command.

ADD COMMENT
0
Entering edit mode

It worked perfectly, it's going to be very helpful! Thank you so much!!

ADD REPLY

Login before adding your answer.

Traffic: 1477 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6