Python Program to read .fasta file to .txt file
0
1
Entering edit mode
5.3 years ago

My file is dna.fasta

>dna1
ATATTCGGATTCGAGCTTAGGT

I want to convert it into dna.txt?

Python • 6.1k views
ADD COMMENT
3
Entering edit mode

There's no reason to do this...file extensions are arbitrary, and fasta files are already text files.

ADD REPLY
0
Entering edit mode

for example there is file dna.fasta with some sequence i want to convert this file in dna.txt format

ADD REPLY
1
Entering edit mode

To add to Joe's explanation: files with .fasta extension are usually of the type ASCII text, which means plain text. They have the .fasta extension only for convenience, to signify that sequences in it have a header and a string of letters. You can give those files literally any extension (and many are used, such as .fa, .faa, .fas, .fna) without changing anything about the file contents. All of them should work just like a file with .txt extension.

ADD REPLY
0
Entering edit mode

i want to count NT after it converts in .txt

ADD REPLY
1
Entering edit mode

Please stop changing the question. Either ensure you ask the right question to start with, or open a new question, because now you’re confusing the thread for other people in future.

ADD REPLY
0
Entering edit mode

To make it simple, please show an example of how dna.txt is supposed to look like. Then we can provide adequate solutions.

Show the data! Words are meaningless until you finally show how this txt file is suppoed to look like.

ADD REPLY
0
Entering edit mode

.txt isnt a 'format' in the way you're thinking of.

.fasta is already .txt, you just rename it and change the extension if you desperately need it to be .txt.

ADD REPLY
0
Entering edit mode

To make it simple, please show an example of how dna.txt is supposed to look like. Then we can provide adequate solutions.

ADD REPLY
3
Entering edit mode

Python:

fh = open('dna.fasta')
s = fh.read()
fh.close()
oh = open('dna.txt', 'w')
oh.write(s)
oh.close()
ADD REPLY
2
Entering edit mode

oh is open handle or OH :)

ADD REPLY
0
Entering edit mode

dna.fastq has format as

dna00022 ATATTCGGATTCGAGCTTAGGT

and

dna.txt has format as

ATATTCGGATTCGAGCTTAGGT

along with it should count the number of nucleotides in dna.txt

by using python program in idle script

ADD REPLY
0
Entering edit mode

Do you just want to remove the header lines (i.e. the > sequence_name lines that appear before each sequence)?

ADD REPLY
0
Entering edit mode

You didn’t ask for a script which counts nucleotide length.

You need to fix your question, ideally with some example input and output.

ADD REPLY
2
Entering edit mode

a.zielezinski's post inspired me to come up with really complicated ways to do this task.

Here is one:

python -c "print('>dna1\nATATTCGGATTCGAGCTTAGGT\n')" > dna.txt

If you really want to complicate the living daylights out of this, here is another solution:

(lambda _, __, ___, ____, _____, ______, _______, ________:
    getattr(
        __import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
        ().__class__.__eq__.__class__.__name__[:__] +
        ().__iter__().__class__.__name__[_____:________]
    )(
        _, (lambda _, __, ___: _(_, __, ___))(
            lambda _, __, ___:
                chr(___ % __) + _(_, __, ___ // __) if ___ else
                (lambda: _).func_code.co_lnotab,
            _ << ________,

            (((((_____ << __) + _) << ____) - _____) << ((_______ << _____)
             - _____)) - (((((_ << ____) - _) << ___) - _) << ((((___ << __)
             + _) << ____) + ___)) - (((___ << _______) - (_ << ____)
             + _) << ((___ << ______) + _______)) - (((_ << ________)
             - _____) << ((___ << ______) - (_ << _))) + (((_____ << ______)
             + _____) << ((((___ << __) - _) << ____) + (_ << __)))
             + (((((_ << ____) + _) << ____) + _) << ((((___ << __)
             - _) << ____) - (___ << _))) - (((___ << ______)
             - _______) << ((_____ << _____))) + (((_ << ________)
             + _____) << ((((_____ << __) - _) << ___) - (_ << _)))
             + (((_______ << __) + _) << (((((_ << ___) + _)) << ____)
             - (_ << _))) + (((((___ << __) + _) << ____)
             + _____) << ((_ << _______) + (_ << _))) + (((((___ << __)
             - _) << ____) - _______) << ((((_ << ____) - _) << ___)
             - _)) - (((_ << _______) - ___) << ((_______ << ____)
             - _)) - (((_______ << ____) + _) << ((((___ << __)
             + _) << ___) - _)) - (((_______ << ____)
             + _) << ((___ << _____) - _)) - (((((_ << ____) - _) << ___)
             + _) << ((((___ << __) - _) << ___) - _)) - (((((___ << __)
             - _) << ___) - _) << ((_____ << ____) - _)) - (((((___ << __)
             - _) << ___) - _) << (((((_ << ___) + _)) << ___) - _))
             - (((_ << _______) - ___) << ((_ << ______) - _))
             - (((((___ << __) - _) << ___) - _) << ((_______ << ___)
             - _)) - (((_ << ______) - _) << ((___ << ____)))
             + (((_____ << ___) + _) << ((_____ << ___) - (_ << _)))
             - (((_______ << __) + _) << ((_ << _____) - _))
             - (((((_ << ____) - _) << __) + _) << ((___ << ___) - _))
             - ((((((_ << ___) + _)) << __) - _) << ((_ << ____) - _))
             - (_______ << ((_____ << _))) + (_ << ______) - (_ << _)
        )
    )
)(
    *(lambda _, __, ___: _(_, __, ___))(
        (lambda _, __, ___:
            [__(___[(lambda: _).func_code.co_nlocals])] +
            _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []
        ),
        lambda _: _.func_code.co_argcount,
        (
            lambda _: _,
            lambda _, __: _,
            lambda _, __, ___: _,
            lambda _, __, ___, ____: _,
            lambda _, __, ___, ____, _____: _,
            lambda _, __, ___, ____, _____, ______: _,
            lambda _, __, ___, ____, _____, ______, _______: _,
            lambda _, __, ___, ____, _____, ______, _______, ________: _
        )
    )
)

Save this code as task.py and type:

python task.py > dna.txt

This will work only for python 2.7. I will post python 3.x code if there is interest.

ADD REPLY
2
Entering edit mode

Still laughing. This one has made my day. "I will post python 3.x code if there is interest."

ADD REPLY
0
Entering edit mode

I am not laughing sir this is really complecated.

ADD REPLY
1
Entering edit mode

You can edit files in fasta in whatever text editor, i think it does not have sense, but you can change the extension in the prompt shell by by typing: mv file.fasta file.txt

ADD REPLY
0
Entering edit mode

tikshyadav19, please say what you are ultimately trying to do. Are you trying to use a program that requires FASTA data in a different format?

ADD REPLY

Login before adding your answer.

Traffic: 1563 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