CMD + K

fastaparser

Community

A Python FASTA file Parser and Writer

Installation

To install this package, run one of the following:

Conda
$conda install kronopt::fastaparser

Usage Tracking

1.1.1
1.0
2 / 8 versions selected
Downloads (Last 6 months): 0

Description

A Python FASTA file Parser and Writer

The FASTA file format is a standard text-based format for representing nucleotide and aminoacid sequences (usual file extensions include: .fasta, .fna, .ffn, .faa and .frn). FastaParser is able to parse such files and extract the biological sequences within into Python objects. It can also handle and manipulate such sequences as well as write sequences to new or existing FASTA files.

Read FASTA files

Generate python objects from FASTA files:

>>> import fastaparser
>>> with open("fasta_file.fasta") as fasta_file:
        parser = fastaparser.Reader(fasta_file)
        for seq in parser:
            # seq is a FastaSequence object
            print('ID:', seq.id)
            print('Description:', seq.description)
            print('Sequence:', seq.sequence_as_string())

output:

ID: sp|P04439|HLAA_HUMAN
Description: HLA class I histocompatibility antigen, A alpha chain OS=Homo sapi...
Sequence: MAVMAPRTLLLLLSGALALTQTWAGSHSMRYFFTSVSRPGRGEPRFIAVGYVDDTQFVRFDSDAASQRM...
ID: sp|P15822|ZEP1_HUMAN
Description: Zinc finger protein 40 OS=Homo sapiens OX=9606 GN=HIVEP1 PE=1 SV=3...
Sequence: MPRTKQIHPRNLRDKIEEAQKELNGAEVSKKEILQAGVKGTSESLKGVKRKKIVAENHLKKIPKSPLRN...

or just parse FASTA headers and sequences, which is much faster but less feature rich:

>>> import fastaparser
>>> with open("fasta_file.fasta") as fasta_file:
        parser = fastaparser.Reader(fasta_file, parse_method='quick')
        for seq in parser:
            # seq is a namedtuple('Fasta', ['header', 'sequence'])
            print('Header:', seq.header)
            print('Sequence:', seq.sequence)

output

Header: >sp|P04439|HLAA_HUMAN HLA class I histocompatibility antigen, A alpha c...
Sequence: MAVMAPRTLLLLLSGALALTQTWAGSHSMRYFFTSVSRPGRGEPRFIAVGYVDDTQFVRFDSDAASQRM...
Header: >sp|P15822|ZEP1_HUMAN Zinc finger protein 40 OS=Homo sapiens OX=9606 GN...
Sequence: MPRTKQIHPRNLRDKIEEAQKELNGAEVSKKEILQAGVKGTSESLKGVKRKKIVAENHLKKIPKSPLRN...

Write FASTA files

Create FASTA files from FastaSequence objects:

>>> import fastaparser
>>> with open("fasta_file.fasta", 'w') as fasta_file:
        writer = fastaparser.Writer(fasta_file)
        fasta_sequence = fastaparser.FastaSequence(
            sequence='ACTGCTGCTAGCTAGC',
            id='id123',
            description='test sequence')
        writer.writefasta(fasta_sequence)

or single header and sequence strings:

>>> import fastaparser
>>> with open("fasta_file.fasta", 'w') as fasta_file:
        writer = fastaparser.Writer(fasta_file)
        writer.writefasta(('id123 test sequence', 'ACTGCTGCTAGCTAGC'))

About

Summary

A Python FASTA file Parser and Writer

Last Updated

Jan 28, 2020 at 22:36

License

GNU General Public v3 (GPLv3)

Supported Platforms

win-64
noarch