Skip to main content

pyfastx is a python module for fast random access to sequences from plain and gzipped FASTA file

Project description

Travis CI Appveyor CI Readthedocs Codecov Coveralls PyPI Version Python Version Wheel

a robust python module for fast random access to sequences from plain and gzipped FASTA file

About

The pyfastx is a lightweight Python C extension that enables users to randomly access to sequences from plain and gzipped FASTA files. This module aims to provide simple APIs for users to extract seqeunce from FASTA by identifier and index number. The pyfastx will build indexes stored in a sqlite3 database file for random access to avoid consuming excessive amount of memory. In addition, the pyfastx can parse standard (sequence spread into multiple lines with same length) and nonstandard (lines with different length) FASTA format. This module used kseq.h written by @attractivechaos in klib project to parse plain FASTA file and zran.c written by @pauldmccarthy in project indexed_gzip to index gzipped file for random access.

This project was heavily inspired by @mdshw5’s project pyfaidx and @brentp’s project pyfasta.

Installation

Make sure you have both pip and at least version 3.5 of Python before starting.

You can install pyfastx via the Python Package Index (PyPI)

pip install pyfastx

Update pyfastx module

pip install -U pyfastx

Usage

Read FASTA file

The fastest way to parse flat or gzipped FASTA file without building index.

>>> import pyfastx
>>> for name, seq in pyfastx.Fasta('test/data/test.fa.gz', build_index=False):
>>>     print(name, seq)

Read flat or gzipped FASTA file and build index, support for random access to FASTA.

>>> import pyfastx
>>> fa = pyfastx.Fasta('test/data/test.fa.gz')
>>> fa
<Fasta> test/data/test.fa.gz contains 211 seqs

Get FASTA information

>>> # get sequence counts in FASTA
>>> len(fa)
211

>>> # get total sequence length of FASTA
>>> fa.size
86262

>>> # get GC content of DNA sequence of FASTA
>>> fa.gc_content
43.529014587402344

>>> # get composition of nucleotides in FASTA
>>> fa.composition
{'A': 24534, 'C': 18694, 'G': 18855, 'T': 24179, 'N': 0}

Get sequence from FASTA

>>> # get sequence like a dictionary by identifier
>>> s1 = fa['JZ822577.1']
>>> s1
<Sequence> JZ822577.1 with length of 333

>>> # get sequence like a list by index
>>> s2 = fa[2]
>>> s2
<Sequence> JZ822579.1 with length of 176

>>> # get last sequence
>>> s3 = fa[-1]
>>> s3
<Sequence> JZ840318.1 with length of 134

>>> # check a sequence name weather in FASTA file
>>> 'JZ822577.1' in fa
True

Get sequence information

>>> s = fa[-1]
>>> s
<Sequence> JZ840318.1 with length of 134

>>> # get sequence name
>>> s.name
'JZ840318.1'

>>> # get sequence string
>>> s.seq
'ACTGGAGGTTCTTCTTCCTGTGGAAAGTAACTTGTTTTGCCTTCACCTGCCTGTTCTTCACATCAACCTTGTTCCCACACAAAACAATGGGAATGTTCTCACACACCCTGCAGAGATCACGATGCCATGTTGGT'

>>> # get sequence length
>>> len(s)
134

>>> # get GC content if dna sequence
>>> s.gc_content
46.26865768432617

>>> # get nucleotide composition if dna sequence
>>> s.composition
{'A': 31, 'C': 37, 'G': 25, 'T': 41, 'N': 0}

Sequence slice

Sequence object can be sliced like a python string

>>> # get a sub seq from sequence
>>> ss = seq[10:30]
>>> ss
<Sequence> JZ840318.1 from 11 to 30

>>> ss.name
'JZ840318.1:11-30'

>>> ss.seq
'CTTCTTCCTGTGGAAAGTAA'

>>> ss = s[-10:]
>>> ss
<Sequence> JZ840318.1 from 125 to 134

>>> ss.name
'JZ840318.1:125-134'

>>> ss.seq
'CCATGTTGGT'

Reverse and complement sequence

>>> # get sliced sequence
>>> fa[0][10:20].seq
'GTCAATTTCC'

>>> # get reverse of sliced sequence
>>> fa[0][10:20].reverse
'CCTTTAACTG'

>>> # get complement of sliced sequence
>>> fa[0][10:20].complement
'CAGTTAAAGG'

>>> # get reversed complement sequence, corresponding to sequence in antisense strand
>>> fa[0][10:20].antisense
'GGAAATTGAC'

Get subsequences

Subseuqneces can be retrieved from FASTA file by using a list of [start, end] coordinates

>>> # get subsequence with start and end position
>>> interval = (1, 10)
>>> fa.fetch('JZ822577.1', interval)
'CTCTAGAGAT'

>>> # get subsequences with a list of start and end position
>>> intervals = [(1, 10), (50, 60)]
>>> fa.fetch('JZ822577.1', intervals)
'CTCTAGAGATTTTAGTTTGAC'

>>> # get subsequences with reverse strand
>>> fa.fetch('JZ822577.1', (1, 10), strand='-')
'ATCTCTAGAG'

Get identifiers

Get all identifiers of sequence as a list-like object.

>>> ids = fa.keys()
>>> ids
<Identifier> contains 211 identifiers

>>> # get count of sequence
>>> len(ids)
211

>>> # get identifier by index
>>> ids[0]
'JZ822577.1'

>>> # check identifier where in fasta
>>> 'JZ822577.1' in ids
True

>>> # iter identifiers
>>> for name in ids:
>>>     print(name)

>>> # convert to a list
>>> list(ids)

Testing

The pyfaidx module was used to test pyfastx. To run the tests:

$ python setup.py test

Acknowledgements

kseq.h and zlib was used to parse FASTA format. Sqlite3 was used to store built indexes. pyfastx can randomly access to sequences from gzipped FASTA file mainly attributed to indexed_gzip.

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

pyfastx-0.2.8.tar.gz (35.5 kB view hashes)

Uploaded Source

Built Distributions

pyfastx-0.2.8-cp37-cp37m-win_amd64.whl (501.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pyfastx-0.2.8-cp37-cp37m-win32.whl (514.5 kB view hashes)

Uploaded CPython 3.7m Windows x86

pyfastx-0.2.8-cp37-cp37m-manylinux1_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.7m

pyfastx-0.2.8-cp37-cp37m-manylinux1_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.7m

pyfastx-0.2.8-cp37-cp37m-macosx_10_6_intel.whl (91.9 kB view hashes)

Uploaded CPython 3.7m macOS 10.6+ intel

pyfastx-0.2.8-cp36-cp36m-win_amd64.whl (501.0 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pyfastx-0.2.8-cp36-cp36m-win32.whl (514.6 kB view hashes)

Uploaded CPython 3.6m Windows x86

pyfastx-0.2.8-cp36-cp36m-manylinux1_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.6m

pyfastx-0.2.8-cp36-cp36m-manylinux1_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.6m

pyfastx-0.2.8-cp36-cp36m-macosx_10_6_intel.whl (91.9 kB view hashes)

Uploaded CPython 3.6m macOS 10.6+ intel

pyfastx-0.2.8-cp35-cp35m-win_amd64.whl (501.0 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

pyfastx-0.2.8-cp35-cp35m-win32.whl (514.7 kB view hashes)

Uploaded CPython 3.5m Windows x86

pyfastx-0.2.8-cp35-cp35m-manylinux1_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.5m

pyfastx-0.2.8-cp35-cp35m-manylinux1_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.5m

pyfastx-0.2.8-cp35-cp35m-macosx_10_6_intel.whl (91.9 kB view hashes)

Uploaded CPython 3.5m macOS 10.6+ intel

Supported by

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