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 Heng Li 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.get_seq('JZ822577.1', interval)
'CTCTAGAGAT'
>>> # get subsequences with a list of start and end position
>>> intervals = [(1, 10), (50, 60)]
>>> fa.get_seq('JZ822577.1', intervals)
'CTCTAGAGATTTTAGTTTGAC'
>>> # get subsequences with reverse strand
>>> fa.get_seq('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.4-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 099a0709decfeeb4dddfd271282af7b0a88a0ee1b5e156340c87ce36797bb85f |
|
MD5 | 4badb8cf8ff5d396a7abe3398057f5e5 |
|
BLAKE2b-256 | 670b0d920b2584dcc8e492bb4e63cc16c4133c72ba3783c8170c187456bf88c9 |
Hashes for pyfastx-0.2.4-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85c18afa826127ff60dabe3b7d0bba7a5d73c838ec79a53efb218eeafe1ad04e |
|
MD5 | f37c0a2169305d741b99279066077cbd |
|
BLAKE2b-256 | 09505da46e1916ed70bbf21aaf78ae8d6939bc191cd52d88010afe90d8dee9c4 |
Hashes for pyfastx-0.2.4-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ac9b0765cb8c2c58511853c414ad6e9315982abc02cf44bc3f4e75d1c411f27 |
|
MD5 | 270db09560415f2c5b0cfe29e82fa9da |
|
BLAKE2b-256 | 2ba586aa23d2ae4547fd3499b88e4cfc400708648bc91989528bd80fb3e5c5ac |
Hashes for pyfastx-0.2.4-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef50d91de3758e6a1746445fb68363cc92ebf0da016541904278d9f53961f734 |
|
MD5 | 51e1af794388306cb5c197e6860eec8c |
|
BLAKE2b-256 | b196fccd9bd639c15fd6d7db4dd1e8f50bee922bda604b82dd0ae914b75f8ad6 |
Hashes for pyfastx-0.2.4-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 265e636e93eb5dcacf0b5664a774d98017fb3211a3fbc33b7c646103a50c4330 |
|
MD5 | c91fdd68b1dad57ad8ad3894e2124684 |
|
BLAKE2b-256 | 0d8314b1653e97864832a50ffb4e3d9cd7f5f1a0bca5f4361172f43afe8dab94 |
Hashes for pyfastx-0.2.4-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f6277175a3b38cb455c14358b1bdcb997c8cdc3511f5b7980b541c3179bfe31 |
|
MD5 | ad128f1f34ae2bacf55bae36bb87803b |
|
BLAKE2b-256 | 1c092ff1512f54d1b8af9f74af17d14a2fdfdba4e49858f046b1d1040900dc5f |
Hashes for pyfastx-0.2.4-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79e6f234f3012a31a2f252256003f97759236094c7f584407419c4cf0d279757 |
|
MD5 | 9e708ed51b332ef9493a8d422b5b31c9 |
|
BLAKE2b-256 | c36d2dcd34009a2cc33ca1f62d2a871f57611aacf0d1e5e264f647cdc15bc01a |
Hashes for pyfastx-0.2.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc0566c370a1dbb971ac38a7f79c91ae0ccc6444b001f7b3161ed3e70296aa38 |
|
MD5 | 28aa206a67b2b1aaa89c99a659c89c91 |
|
BLAKE2b-256 | b033d21bd1e112930ce0a6d76684c56c5af2343210990b1962bea67006136c09 |
Hashes for pyfastx-0.2.4-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f62ea39e517d7ade2a66e2e6b89ecd09910726f70c068f04cfd2c775cf82db3 |
|
MD5 | 4a6369809e8690174b4a77f24702a712 |
|
BLAKE2b-256 | f45c6cc7acc3b85e2fa691ad57d908a6f63d6543211518ee26fe71b2e0031fe9 |
Hashes for pyfastx-0.2.4-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 930f2fa203226f2cf94e0e00b49087b8751c955b47bfbb15164fce4bbf3d6432 |
|
MD5 | 25cbf2fb7edb0fcbd43eee5a1fde62a2 |
|
BLAKE2b-256 | 72c5f56baff02c31dbecbc6a3e5230abe7bcda95fbfc67c59b03e2a465bc0d63 |
Hashes for pyfastx-0.2.4-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c98b2da7c063f95b50f1cadc3246c904f6bda87fa72dd57f66c183c03797e899 |
|
MD5 | 8d7d5334d16eb0dfc8e1b214ddb64b51 |
|
BLAKE2b-256 | 11ae1e08f1bcd24ef327903d58a9ef726ceee37b2af7c01b9531fe88673e1538 |
Hashes for pyfastx-0.2.4-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29edc5233285b9d9d6b1792ee5d3eb41e873a3ee1d8ebb7df3d2cdeec3310c5c |
|
MD5 | c544e69a3ebc2af4c656dabb27e39598 |
|
BLAKE2b-256 | f35ef1844f13fc3fcda774273124ae83c1ddbc3580b93c32805a7acd0243d01a |
Hashes for pyfastx-0.2.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 505be2ca75a311fbb0550a3e3b0e89eaabdcd10b665b59389f1737fd07921b3b |
|
MD5 | f9455d756b145526989f936e6e569bc2 |
|
BLAKE2b-256 | f106e799ab1911a6e66a438f7c6a09f8913dfdae868a5924fcd1ad9155ed029f |
Hashes for pyfastx-0.2.4-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 257054f039dc74c46319c794c4b78447614a29bbf0d867aab663e1def899fe3c |
|
MD5 | a2d2b9818aa494fa9a8af8c00f338ba1 |
|
BLAKE2b-256 | af77d1f57d56647507681680f6809ddab0296c0397e0ec874e80e5572ceb157b |
Hashes for pyfastx-0.2.4-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62e5b3b90721a5d844d5ce6a24def7da19d8002725fea3db2d958541fad280b9 |
|
MD5 | f0dd29c918bbba69ddd4f65338209cbc |
|
BLAKE2b-256 | fa011b27193a128248e31ff79d443d488ffdfe26fb27ed36c82a6b4934fafdc1 |