pyfastx is a python module for fast random access to sequences from plain and gzipped FASTA file
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pyfastx-0.2.11-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d729ddc8a29e5a705f515a8d82acf1fa1b567357b5602e0d12f2eee521776073 |
|
MD5 | 18866f60d0afa3ea2fd863b01a5a0297 |
|
BLAKE2b-256 | 44bc33679fb90de90818acc2c62ec154a1b692c324ebbce2443dc2c5cf237fec |
Hashes for pyfastx-0.2.11-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02c5697daa54851d2748f185b21ee2aa2b0e6d89de0cf42eec464c4a88c1b510 |
|
MD5 | 85e971f468a846672d116cd077101fe8 |
|
BLAKE2b-256 | 734334e0bcc055fdbe3b89a4990bf22c7782224f890e9b9d917ac3528e0a6ec4 |
Hashes for pyfastx-0.2.11-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6748c04c1117be334b24d265acf53f6442d1446e0e38fca0a81eed7e0bd67e52 |
|
MD5 | 51f64ffaa557ea277f380730aa1b8ca7 |
|
BLAKE2b-256 | 546ff4ce9f478963d1a390b6541948e11efa7ade8249a7b06425d8c0773d3eaa |
Hashes for pyfastx-0.2.11-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36595974955d94962b6556af94ab207982720e7d16bd5f764ab4d538bc653b74 |
|
MD5 | 7d185b5003cb4823c6931aac1c18c896 |
|
BLAKE2b-256 | 62c7951a749dd8766309759fc207c5498a5c234cb94c981e38508a3a0aaeb362 |
Hashes for pyfastx-0.2.11-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca93bda5d08c057f750a627a0a867df1750c1dd22d050059f06b4b7351ab1ea7 |
|
MD5 | 572e7fae142390100224ecd74d645e83 |
|
BLAKE2b-256 | 112d2458dac4774063c139ca4c10c21b5e88ad526baca083149bdc8fe01c3d03 |
Hashes for pyfastx-0.2.11-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b2c1e4a87772841fef6e8046801e7f86e58b2e85a64cecb272aa44d5e38dc8a |
|
MD5 | 1d4353bf9cd18dadb371640119d465ee |
|
BLAKE2b-256 | 7ea978a6485ea5c93592e0a1b6d714f30ee1b295676c4b849e1f182343ff1752 |
Hashes for pyfastx-0.2.11-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbec774f2b416b3fc32ecb293c0786172fd290c702a7083a8b8469fb7112747c |
|
MD5 | 5d1b5260921f5ccb1c34626d51b92e00 |
|
BLAKE2b-256 | db5ad7f60791abe4c23532178bd2c4562c01308ed1a36107343f9ce294556700 |
Hashes for pyfastx-0.2.11-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f21c72e834b43aaa0ed95252add82cb9353ffb7d19ae6370777eb38def3eca9 |
|
MD5 | 574573ea45e390bf3548874a7070f7dc |
|
BLAKE2b-256 | be50aef5ab372edbef6e425e33a06e85832db42a8ca8dac460e152fed47eda37 |
Hashes for pyfastx-0.2.11-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 183e29382cada0fdf81d0ed73a5c6b3a0e9e5c251c58d03842f315e08af7f57b |
|
MD5 | 0d0cbe4074becca03bdcbc02b4050196 |
|
BLAKE2b-256 | 3266955462ef7d29074569798a47ed1f24920002cb68d033679475b1d3b987bc |
Hashes for pyfastx-0.2.11-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da4c8a809d86c869637625798c10a71e1d5db68e764f5a54b96f68e174c6c84e |
|
MD5 | 5b9dc3f76132d1af4f9c61c2a6aa6cce |
|
BLAKE2b-256 | bce927d975677d8add9791dfba256ee7473b4957ea4197e828c1bd74b3bc5809 |
Hashes for pyfastx-0.2.11-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de094943dc101e69011031ad969439ceed84d57361cd5f6b79a4c806a3e328b9 |
|
MD5 | 1f4216116a63f4b80efb1a6fbffbbc6e |
|
BLAKE2b-256 | 16b656949d298e3a67d6ac97a4b6a2824597da174e5065bb314c6d15ad002bcd |
Hashes for pyfastx-0.2.11-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd98cb6f9ca31a504aca0950e72ae430876d9c002117364b06d33292efe241fd |
|
MD5 | 6d038516796c7f8494343b0ce6ac0550 |
|
BLAKE2b-256 | f0949f0ce4678a2fac4751e9f8841d788c7d5183a003d55a4eb5c9c1bef62c4a |
Hashes for pyfastx-0.2.11-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 800aed0d0435b8b2a422afd045be606da5ec3df5a115854ae3add507e0539508 |
|
MD5 | 038737ecefdd2ac179464c9a4072d0e5 |
|
BLAKE2b-256 | 520d9c1e78777a0cab69a2e7e2c17542a03e0c35c408158d9034f09e1d4f31e0 |
Hashes for pyfastx-0.2.11-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cc31736ebf291b8fac6db0f7afa14b5322e31477938ddebe71093953c9b57c5 |
|
MD5 | 10cdaa568a1d98397188d8d184f0b0f6 |
|
BLAKE2b-256 | e6fa8c92edb44e2c2a53f4ee60b343f6f2952c907a3d7c9f0ef81722c7d6891f |
Hashes for pyfastx-0.2.11-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aa9f755e6a6b7c2611eff36fa76266a5543fb266b389e0ad5fe969f36c91b98 |
|
MD5 | d855588c15b2f15a2fc50dc7388867de |
|
BLAKE2b-256 | 7a5d71d0630343f6aba9fd5e2a1fbd44fb681845d1897ad4bd18a64780eeeb32 |
Hashes for pyfastx-0.2.11-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d2e727ca6942d7c4e24f3a5b0765b4fd005b8ab26259b1ae1fc965db7ec0c22 |
|
MD5 | acbb0f0f49dccdd751633ec2f540c5ff |
|
BLAKE2b-256 | ddced70273a00f51ef27036b567ab0799616b1b5803835f6b3b09c392a8e89e9 |
Hashes for pyfastx-0.2.11-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71635ca610ddd07e179bc5e3f7e6bba5b63b6094d58d9eeb2f5cd53aca7a60e9 |
|
MD5 | b36731e0ab868c9713a680bba6307b28 |
|
BLAKE2b-256 | 24d27be7804b9384e91683dcda09414211994c57bbf0ffb76d84724017de0a6d |
Hashes for pyfastx-0.2.11-cp34-cp34m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3f27e791ae450d0100833a9b47f347b3e16c76521e0497d014b2e9a1e465f75 |
|
MD5 | 2f6000dbde2bc2ade84fe6c480f1300b |
|
BLAKE2b-256 | 7f717644d10016f1ab20cb2ebb64774dab56f894a90b094e26019e5c3c7db394 |