Skip to main content

A very simple fasta file parser.

Project description

CI PyPI Python

FastaFrames

Convert between UniProt FASTA files and pandas DataFrames.

Installation

pip install fastaframes

Quick Start

Read a FASTA file into a DataFrame

from fastaframes import to_df

df = to_df("proteins.fasta")
print(df.head())

Write a DataFrame back to FASTA

from fastaframes import to_fasta

to_fasta(df, output_file="output.fasta")

Work with individual entries

from fastaframes import fasta_to_entries, entries_to_fasta

for entry in fasta_to_entries("proteins.fasta"):
    print(entry.unique_identifier, entry.protein_name)

# Filter and write back
entries = [e for e in fasta_to_entries("proteins.fasta") if e.organism_name == "Homo sapiens"]
entries_to_fasta(entries, output_file="human_only.fasta")

Multiple input formats

from io import StringIO
from fastaframes import to_df

# From a file path
df = to_df("proteins.fasta")

# From a string
df = to_df(">sp|P12345|EXAMPLE_HUMAN Example protein OS=Homo sapiens OX=9606\nMSEQUENCE\n")

# From a file object
with open("proteins.fasta") as f:
    df = to_df(f)

# From a StringIO
df = to_df(StringIO(">sp|P12345|EXAMPLE_HUMAN\nMSEQUENCE\n"))

Skip malformed entries

from fastaframes import to_df

df = to_df("messy_data.fasta", skip_error=True)

Wrap sequence lines

from fastaframes import to_fasta

# Wrap sequences at 60 characters (default is a single line per record)
to_fasta(df, output_file="wrapped.fasta", max_sequence_length=60)

Preserve non-standard header fields

Any KEY=value field beyond the standard PN/OS/OX/GN/PE/SV is kept in FastaEntry.additional_fields and re-emitted on serialization.

Error handling

All package errors derive from FastaFramesError. Malformed FASTA raises FastaFormatError (also a ValueError); unsupported inputs raise InvalidInputError (also a TypeError).

from fastaframes import to_df, FastaFormatError

try:
    df = to_df("example.fasta")
except FastaFormatError as err:
    print(err.header, err.reason)

Logging

FastaFrames logs under the fastaframes logger and stays silent until you configure logging:

import logging
logging.getLogger("fastaframes").setLevel(logging.DEBUG)

DataFrame Columns

Given this FASTA entry:

>sp|A0A087X1C5|CP2D7_HUMAN Putative cytochrome P450 2D7 OS=Homo sapiens OX=9606 GN=CYP2D7 PE=5 SV=1
MGLEALVPLAMIVAIFLLLVDLMHRHQRWAARYPPGPLPLPGLGNLLHVDFQNTPYCFDQ

to_df produces:

db unique_identifier entry_name protein_name organism_name organism_identifier gene_name protein_existence sequence_version protein_sequence
sp A0A087X1C5 CP2D7_HUMAN Putative cytochrome P450 2D7 Homo sapiens 9606 CYP2D7 5 1 MGLEALVPLAMIVAIFLLLVDLMHRHQRWAARYPPGPLPLPGLGNLLHVDFQNTPYCFDQ

Column descriptions (following the UniProt FASTA header format):

Column Description
db Database source: sp (Swiss-Prot) or tr (TrEMBL)
unique_identifier Primary UniProtKB accession number
entry_name UniProtKB entry name
protein_name Recommended protein name (RecName or first SubName)
organism_name Scientific name of the source organism
organism_identifier NCBI taxonomy identifier
gene_name First gene name (if available)
protein_existence Numerical evidence code for protein existence
sequence_version Sequence version number
protein_sequence Amino acid sequence

Development

pip install -e ".[dev]"

Common commands via just:

just check      # Run all checks (lint, typecheck, test)
just lint       # Lint with ruff
just fmt        # Format with ruff
just typecheck  # Type check with ty
just test       # Run tests
just test -v    # Run tests verbosely

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastaframes-1.4.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastaframes-1.4.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file fastaframes-1.4.0.tar.gz.

File metadata

  • Download URL: fastaframes-1.4.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for fastaframes-1.4.0.tar.gz
Algorithm Hash digest
SHA256 13233d21daec4c94e0d842c76879c6b28fd13cb99fda95e715b3c9bcadef1c1f
MD5 62d7f6329cb7a719aa0d271c52eb4342
BLAKE2b-256 d325b5b489fc72f7123d8a224459ec78a713c3477f4f6e195bbe085908a9e777

See more details on using hashes here.

File details

Details for the file fastaframes-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: fastaframes-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for fastaframes-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42b6c699d6353adabc0e4ae85d80a33b68855f61a59533deaa9f551e5829efe9
MD5 fa7956cb21ab7d150e17769bad315ab7
BLAKE2b-256 180ea309183b0197ebd4be0d501f1ca3ce9727a04a2f58b354877b8948e11583

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page