This is a test version of Biostars. For the public version, visit https://www.biostars.org.
sequence splitting

I have a fasta file which contains multiple contigs

>DEHFGCMO_00205
>MDDGIGEH_00111
>FLCICGHF_00226
>FLCICGHF_00253
>DEHFGCMO_01539
>MDDGIGEH_00625

I want to split the contigs based on the first few letters of their names and aggregate them into different fasta files e.g. 1.fasta

>DEHFGCMO_00205 
>DEHFGCMO_01539

2.fasta

>MDDGIGEH_00111 
>MDDGIGEH_00625 

3.fasta

>FLCICGHF_00226 
>FLCICGHF_00253

what should I do? Very grateful for your help.

sequence

Assuming that sequences are single line and sequence names/ids follow similar pattern:

$ awk -F '[>_]' '/^>/ {getline seq;print $0"\n"seq > $2".fa"}' test.fa

1 answer

seqkit split

$ seqkit split --by-id  --id-regexp "^(.+?)_" test.fasta -O result
[INFO] split by ID. idRegexp: ^(.+?)_
[INFO] read sequences ...
[INFO] read 6 sequences
[INFO] write 2 sequences to file: result/test.id_DEHFGCMO.fasta
[INFO] write 2 sequences to file: result/test.id_MDDGIGEH.fasta
[INFO] write 2 sequences to file: result/test.id_FLCICGHF.fasta

Log in to answer this question.