Giter Site home page Giter Site logo

kronopt / fastaparser Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 1.0 229 KB

A Python FASTA file Parser and Writer.

Home Page: https://fastaparser.readthedocs.io/en/latest/

License: GNU General Public License v3.0

Python 98.61% Makefile 1.39%
python python-3 fasta parser fasta-parser fasta-reader fasta-writer

fastaparser's Introduction

FastaParser

python versions build status code style: black coverage docs status license

pypi pypi downloads

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.

Installation

With pip:

$ pip install fastaparser

Usage

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())
            print()

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)
            print()

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'))

Documentation

Documentation for FastaParser is available here: https://fastaparser.readthedocs.io/en/latest

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.