Python Forum
Please suggest python code to format DNA sequence FASTA file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please suggest python code to format DNA sequence FASTA file
#1
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) 
Reply
#2
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
Reply
#3
Thanks @Larz, your code is for splicing the given sequence, I would appreciate if you could suggest code for formatting as above.
Reply
#4
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.
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 696 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Problem with importing Python file in Visual Studio Code DXav 7 5,070 Jun-15-2022, 12:54 PM
Last Post: snippsat
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,395 Feb-13-2022, 07:13 PM
Last Post: menator01
  Reshape txt file into particular format using python shantanu97 0 1,423 Dec-10-2021, 11:44 AM
Last Post: shantanu97
  How can we transcode encoding file uml url format Anldra12 9 3,377 Jul-25-2021, 09:30 AM
Last Post: Anldra12
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,820 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  How to design a save file format? philipbergwerf 5 4,126 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  Request for Python code - Features extraction from JSON file (cuckoo) thinker 1 2,187 Apr-07-2021, 04:40 PM
Last Post: Larz60+
  Python - Import file sequence into Media Pool jensenni 1 2,129 Feb-02-2021, 05:11 PM
Last Post: buran
  Skeleton file export error Python Code pepapoha 4 3,484 Nov-17-2020, 02:06 AM
Last Post: pepapoha

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020