This is a test version of Biostars. For the public version, visit https://www.biostars.org.
For loop to multiple fasta files using TrimAL

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

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.

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

1 answer

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.

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

Log in to answer this question.