Skip to main content

RSBio-Seq

Cargo tests Downloads PyPI - Version Upload to PyPI License: Apache 2.0 OR GPL v3

██████  ███████ ██████  ██  ██████        ███████ ███████  ██████  
██   ██ ██      ██   ██ ██ ██    ██       ██      ██      ██    ██ 
██████  ███████ ██████  ██ ██    ██ █████ ███████ █████   ██    ██ 
██   ██      ██ ██   ██ ██ ██    ██            ██ ██      ██ ▄▄ ██ 
██   ██ ███████ ██████  ██  ██████        ███████ ███████  ██████  
                                                              ▀▀   

RSBio-Seq reads and writes FASTA and FASTQ files in plain text (fasta, fa, fna, fastq, fq) and compressed formats (.gz). Indexed reading and writing are supported for both FASTA and FASTQ, including BGZF-compressed files.

Installation

Use the following command to install from PyPI.

pip install rsbio-seq

2. Build and install from source

To build from source, make sure you have the following programs installed.

To build and install the development version of the wheel.

maturin develop # this installs the development version in the env
maturin develop --release # this installs a release version in the env

To build a release mode wheel for installation, use this command.

maturin build --release

You will find the whl file inside the target/wheels directory. Your whl file will have a name depicting your python environment and CPU architecture. The built wheel can be installed using this command.

pip install target/wheels/*.whl

Usage

Once installed you can import the library and use as follows.

Reading

from rsbio_seq import SeqReader, Sequence, ascii_to_phred

# each seq entry is of type Sequence
seq: Sequence

for seq in SeqReader("path/to/seq.fasta.gz"):
    print(seq.id)
    print(seq.seq)
    # for fastq quality line
    print(seq.qual) # prints IIII
    print(ascii_to_phred(seq.qual)) # prints [40, 40, 40, 40]
    # optional description attribute
    print(seq.desc)

Reading from FASTA or FASTQ (fai/fai+gzi) indexes

The indexed reader retrieves FASTA and FASTQ records by ID from plain text or BGZF-compressed files. Both formats require a .fai index; FASTQ indexes also contain quality offsets.

from rsbio_seq import SeqReaderIndexed, Sequence

seqs = SeqReaderIndexed(
    "path/to/seq.fa",
    "path/to/seq.fa.fai"
)
seq: Sequence = seqs["Record_1"]
print(seq.id)
print(seq.seq)
print(seq.desc)

"Record_2" in seqs # returns a boolean

For FASTQ, use the same API with a .fq or .fastq file and its index. The returned record includes its quality string.

seqs = SeqReaderIndexed("path/to/seq.fq", "path/to/seq.fq.fai")
seq = seqs["Read_1"]
print(seq.seq)
print(seq.qual)

BGZF-compressed FASTA and FASTQ files also require a .gzi index. Indexed access requires BGZF compression rather than ordinary gzip.

seqs = SeqReaderIndexed(
    "path/to/seq.fa.gz",
    "path/to/seq.fa.gz.fai",
    "path/to/seq.fa.gz.gzi",
)
seq: Sequence = seqs["Record_1"]
print(seq.id)
print(seq.seq)
print(seq.desc)

"Record_2" in seqs # returns a boolean

# BGZF-compressed FASTQ
reads = SeqReaderIndexed(
    "path/to/seq.fq.gz",
    "path/to/seq.fq.gz.fai",
    "path/to/seq.fq.gz.gzi",
)
read = reads["Read_1"]
print(read.seq)
print(read.qual)

Using an invalid key will result in KeyError.

Writing

from rsbio_seq import SeqWriter, Sequence, phred_to_ascii

# writing fasta
seq = Sequence("id", "desc", "ACGT") # id, description, sequence
writer = SeqWriter("out.fasta")
writer.write(seq)
writer.close()

# writing fastq
seq = Sequence("id", "desc", "ACGT", "IIII") # id, description, sequence, quality
writer = SeqWriter("out.fastq")
writer.write(seq)
writer.close()

# writing gzipped
seq = Sequence("id", "desc", "ACGT", "IIII") # id, description, sequence, quality
writer = SeqWriter("out.fq.gz")
writer.write(seq)
writer.close()

# writing gzipped with phred score translation
qual = phred_to_ascii([40, 40, 40, 40])
seq = Sequence("id", "desc", "ACGT", qual) # id, description, sequence, quality
writer = SeqWriter("out.fq.gz")
writer.write(seq)
writer.close()

Note: close() is only required if you want to read the file again in the same function/code scope. Closing opened files is a good practice either way.

We provide two utility functions for your convenience.

  • phred_to_ascii - convert phred scores list of numbers to a string
  • ascii_to_phred - convert the quality string to a list of numbers

RSBio-Seq reads and write quality string in ascii format only. Please use these helper functions to translate if you intend to read them.

Writing FASTA or FASTQ with indexes (fai/fai+gzi)

Set index=True to write a FASTA or FASTQ file with a .fai index. A .gz suffix selects BGZF compression and also creates a .gzi index. Index paths are inferred from the output filename. Call close() to finish writing the file and indexes before reading them.

# Plain text
seq = Sequence("id", "desc", "ACGT") # id, description, sequence
writer = SeqWriter("out.fasta", index=True)
writer.write(seq)
writer.close()

# Compressed
seq = Sequence("id", "desc", "ACGT") # id, description, sequence
writer = SeqWriter("out.fa.gz", index=True)
writer.write(seq)
writer.close()

FASTQ indexes include offsets for both sequences and quality strings.

from rsbio_seq import SeqWriter, SeqReaderIndexed, Sequence

seq = Sequence("Read_1", "desc", "ACGT", "IIII")

# Creates out.fq and out.fq.fai
writer = SeqWriter("out.fq", index=True)
writer.write(seq)
writer.close()

# Creates out.fq.gz, out.fq.gz.fai, and out.fq.gz.gzi
writer = SeqWriter("out.fq.gz", index=True)
writer.write(seq)
writer.close()

reads = SeqReaderIndexed("out.fq.gz", "out.fq.gz.fai", "out.fq.gz.gzi")
read = reads["Read_1"]
print(read.seq)   # ACGT
print(read.qual)  # IIII

For wrapped output, pass a line width of at least 10 to writer.write(seq, wrap=60). FASTQ sequences and quality strings are wrapped at the same width.

Authors

Support and contributions

Please get in touch via author websites or GitHub issues. Thanks!

License

RSBio-Seq is dual licensed under the Apache License, Version 2.0 or the GNU General Public License, Version 3 only, at your option.

SPDX license expression: Apache-2.0 OR GPL-3.0-only.

Metadata

Release files for rsbio-seq 1.0.0

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

Built distributions (wheels)

Table of built distributions (wheels) for rsbio-seq 1.0.0
File
rsbio_seq-1.0.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
rsbio_seq-1.0.0-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details
rsbio_seq-1.0.0-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details
rsbio_seq-1.0.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
rsbio_seq-1.0.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
rsbio_seq-1.0.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
rsbio_seq-1.0.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 2.9 MB

Release files / rsbio_seq-1.0.0-cp39-abi3-win_amd64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-win_amd64.whl
Size 264.8 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
ba3ce0316baac006b4e6e6b5ce70886226f60825e9ac2f437a1747d5152fc934
BLAKE2b-256 checksum
How to use checksums
404f659a0f709c71d420b96b169d3f38d1b700e0df73a4cb195be0eb715b6651
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rsbio_seq-1.0.0-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-musllinux_1_2_x86_64.whl
Size 593.6 kB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
41b1859e3edc87f13dba3b2498d991f8fbfb25c50b4cad88523a49536bba941c
BLAKE2b-256 checksum
How to use checksums
675d674fabd0791a115a03cb601f0fd52078089ca1c585a55ef6e34e1a1ddf92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rsbio_seq-1.0.0-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-musllinux_1_2_aarch64.whl
Size 558.4 kB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
ab18a5f30525e00a4afccfc4d14818433691b680922224fff10d089a06284478
BLAKE2b-256 checksum
How to use checksums
a9bb0f93b956cf616d72d9bc3badec00390cd3f750fff071fc5223491edc688c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rsbio_seq-1.0.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 382.4 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
a216cc3216c2da9d81bd87e5c3b0f59c355ee06618789f4699a9e3ef63556d77
BLAKE2b-256 checksum
How to use checksums
04717487faf7cfe63957c59fc6670a4c4add5e4fb614e66cc3300537e07731ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rsbio_seq-1.0.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 380.3 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
9130260028f225a4bc3ce062dc1f1980c331d05a1aeecec92bf734465fb3b1de
BLAKE2b-256 checksum
How to use checksums
358f85eeb0a62432740f429c14f593085fd242311b1b99e30148e189f6aab0d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rsbio_seq-1.0.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-macosx_11_0_arm64.whl
Size 356.5 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e44dc67e37fec9b456ee0cfaeb85d3f1fd2fe0bf0e93460e56adea1b3162072a
BLAKE2b-256 checksum
How to use checksums
3040c5b770f1f25f10960b8cd9faa46b58d8d3e205e4cb7a941574040d5cfc2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rsbio_seq-1.0.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL rsbio_seq-1.0.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 366.0 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
dfa36bf84c76f67364bf2f5e4d700b28c859f09be2e5e2a0421b567e5987c16a
BLAKE2b-256 checksum
How to use checksums
06aec3669df99e5ae307b14769b8f4a56b22491035cbf7bf51e12856b12c4e0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.0.0 This release

7 release files

0.1.5

7 release files

0.1.4

7 release files

0.1.3

7 release files

0.1.2

7 release files

0.1.1

7 release files

0.1.0

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