Skip to main content

PyPI - Version tests workflow OS

fastapy

A lightweight Python package to read and write sequence records in FASTA format.

The design was inspired by the utility of BioPython’s SeqIO, which supports many sequence formats. This repo focuses only on FASTA records. It is faster than BioPython, can handle compressed FASTA files (gzip, bzip2, zip), and has no Python package dependencies.

Requirements

Python >= 3.8

Installation

You can install fastapy from PyPI:

pip install fastapy

or directly from GitHub:

pip install "git+https://github.com/aziele/fastapy.git"

You can also use fastapy without installation since it doesn't have any dependencies. Simply clone or download this repository and you're ready to use it.

git clone https://github.com/aziele/fastapy.git
cd fastapy
python
>>> import fastapy
>>> fastapy.__doc__
'A lightweight Python module to read and write FASTA sequence records'

Quick Start

Typical usage is to read a FASTA file and loop over the sequences record(s).

import fastapy

for record in fastapy.parse('tests/test.fasta'):
    print(record.id, len(record), record.seq[:10], record.desc)

Output:

NP_002433.1  362   METDAPQPGL   RNA-binding protein Musashi homolog 1 [Homo sapiens]
ENO94161.1    79   MKLLISGLGP   RRM domain-containing RNA-binding protein
sequence     292   MKLSKIALMM

Usage

This module contains the Record class representing a FASTA sequence record and the parse() function to read FASTA records from a file.

Record object

Record is an object that contains information on a FASTA sequence record, including id, description, and the sequence itself.

import fastapy

record = fastapy.Record(
    id='NP_950171.2', 
    seq='MEEEAETEEQQRFSYQQRLKAAVHYTVGCLCEEVALDKEMQFSKQTIAAISELTFRQCENFAKDLEMFASICRKRQE',
    desc='APITD1-CORT protein isoform 2 [Homo sapiens]'
)

print(record.id)            # NP_950171.2
print(record.desc)          # APITD1-CORT protein isoform 2 [Homo sapiens]
print(record.seq)           # MEEEAE..
print(record.description)   # >NP_950171.2 G APITD1-CORT protein isoform 2 [Homo sapiens]
print(len(record))          # 77
print('EEEA' in record)     # True

By default, the sequence line is wrapped to 70 characters. You can provide the line length. Use zero (or None) for no wrapping.

print(record)
# >NP_950171.2 APITD1-CORT protein isoform 2 [Homo sapiens]
# MEEEAETEEQQRFSYQQRLKAAVHYTVGCLCEEVALDKEMQFSKQTIAAISELTFRQCENFAKDLEMFAS
# ICRKRQE

print(record.format(wrap=30))
# >NP_950171.2 APITD1-CORT protein isoform 2 [Homo sapiens]
# MEEEAETEEQQRFSYQQRLKAAVHYTVGCL
# CEEVALDKEMQFSKQTIAAISELTFRQCEN
# FAKDLEMFASICRKRQE

print(record.format(wrap=None))
# >NP_950171.2 APITD1-CORT protein isoform 2 [Homo sapiens]
# MEEEAETEEQQRFSYQQRLKAAVHYTVGCLCEEVALDKEMQFSKQTIAAISELTFRQCENFAKDLEMFASICRKRQE

parse

The parse() function is a generator to read FASTA records as Record objects one by one from a file (plain FASTA or compressed using gzip, bzip2, or zip). Because only one record is created at a time, very little memory is required.

import fastapy

for record in fastapy.parse('tests/test.fasta.gz'):
    print(record.id)

For some tasks you may need reusable access to the records. For this purpose, you can use the built-in Python list() function to turn the iterator into a list:

import fastapy

records = list(fastapy.parse('tests/test.fasta.gz'))
print(records[0].id)   # First record
print(records[-1].id)  # Last record

Another common task is to index your records by sequence identifier. Use to_dict() to turn a Record iterator (or list) into a dictionary.

import fastapy

records = fastapy.to_dict(fastapy.parse('tests/test.fasta.gz'))
print(records['NP_002433.1'])   # Use any record id

read

The read() function reads only the first FASTA record from a file. It does not read any subsequent records in the file.

import fastapy

seq_record = fastapy.read('tests/test.fasta')
print(seq_record.id)           # NP_002433.1

Test

You can run tests to ensure that the module works as expected.

pytest

License

GNU General Public License, version 3

Metadata

Release files for fastapy 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapy 1.0.6
File Size Uploaded
fastapy-1.0.6.tar.gz 18.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapy 1.0.6
File Interpreter ABI Platform
fastapy-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 36.5 kB

Release files / fastapy-1.0.6.tar.gz

Download URL fastapy-1.0.6.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
407eeb28e30fdbdfdf74eb64db58bdae469bc98695208bc8579b825e74c7e352
BLAKE2b-256 checksum
How to use checksums
e0b0e3ec6f98e447a557d84d92298b1bc8af99d4c78f64ec822be1cc0bf91b70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release files / fastapy-1.0.6-py3-none-any.whl

Download URL fastapy-1.0.6-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4859769eb690498259bec95dc80018fb7eadfbb5534d83541ba3bf1c2d383e55
BLAKE2b-256 checksum
How to use checksums
d691d8e1a759e743ff5c5970fd41fab47483a4718e6cd29237a942e65e689a03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release history Release notifications | RSS feed

1.1.0

2 release files

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page