Python Forum

Full Version: Please suggest python code to format DNA sequence FASTA file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am newbie in python programming, kindly help as I am having multi-DNA sequence file in FASTA format (test.fasta) as:

>AB1|sp.1 CTCTTCGTTCCATCTCATGCGGTTCTTTCTTCACGAGTATTTGAATTCGAACCGTCTTGTATCTAAAAGAACTCTA
>AB2|sp.1 CTCTTCGTTCCATTTATTACGGTTCTTTCTTCACGAGTATTTGAATTCGAACCGTCTTATTACTACAAAGAACTCTA
>AB3|sp.2 TTACTAAACATTATCCAAGATTGTTATTGTTTCTATATAATTCTCATGTATATGAATATGAATCCATCCTCTTTTTTCT
>AB4|sp.2 TTACTAAAAGTAATCCAAGATTGTTATTGTTTCTATATATTCGAACGTATATGAATATGAATCCATCCTCTTTTTTCT

and want to format it to text (with tab delimitation) as:
Sequence Class
CTCTTCGTTCCATCTCATGCGGTTCTTTCTTCACGAGTATTTGAATTCGAACCGTCTTGTATCTAAAAGAACTCTA AB1|sp.1
CTCTTCGTTCCATTTATTACGGTTCTTTCTTCACGAGTATTTGAATTCGAACCGTCTTATTACTACAAAGAACTCTA AB2|sp.1
TTACTAAACATTATCCAAGATTGTTATTGTTTCTATATAATTCTCATGTATATGAATATGAATCCATCCTCTTTTTTCT AB3|sp.2
TTACTAAAAGTAATCCAAGATTGTTATTGTTTCTATATATTCGAACGTATATGAATATGAATCCATCCTCTTTTTTCT AB4|sp.2

Kindly provide python code (3.x) for formatting as above.

I tried importing sequence file, it displayed correctly but was not able to write further command ... Kindly help

from Bio import SeqIO
for seq_record in SeqIO.parse("test.fasta", "fasta"):
    print(seq_record.seq)
    print(seq_record.id) 
Don't know if this is of any use for you or not, but wrote it some time ago: https://python-forum.io/Thread-Extractin...5#pid69005
Thanks @Larz, your code is for splicing the given sequence, I would appreciate if you could suggest code for formatting as above.
if your data file is as sample (your first post) and verbatim, let me know that, I can work from it.
If you are using actual fasta file, I need to know as it contains header information which has a totally different file format.
(Oct-21-2019, 06:21 AM)Larz60+ Wrote: [ -> ]if your data file is as sample (your first post) and verbatim, let me know that, I can work from it.
If you are using actual fasta file, I need to know as it contains header information which has a totally different file format.

@Larz60+ Sorry for the delayed reply, hereby attaching the FASTA format file FASTA file