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
Introduction
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 is spread into multiple lines with same length) and nonstandard (sequence is spread into one or more 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.
Features
Single file for the Python extension
Lightweight, memory efficient for parsing FASTA file
Fast random access to sequences from gzipped FASTA file
Read sequences from FASTA file line by line
Calculate assembly N50 and L50
Calculate GC content and nucleotides composition
Extract reverse, complement and antisense sequence
Excellent compatibility, support for parsing nonstandard FASTA file
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 longest and shortest sequence
New in pyfastx 0.3.0
>>> # get longest sequence (name, length)
>>> fa.longest
('JZ822609.1', 821)
>>> # get shortest sequence (name, length)
>>> fa.shortest
('JZ822617.1', 118)
Calculate N50 and L50
New in pyfastx 0.3.0
Calculate assembly N50 and L50, return (N50, L50), learn more about N50,L50
>>> # get FASTA N50 and L50
>>> fa.nl(50)
(516, 66)
>>> # get FASTA N90 and L90
>>> fa.nl(90)
(231, 161)
>>> # get FASTA N75 and L75
>>> fa.nl(75)
(365, 117)
Get sequence mean and median length
New in pyfastx 0.3.0
>>> # get sequence average length
>>> fa.mean
408
>>> # get seqeunce median length
>>> fa.median
430
Get sequence counts
New in pyfastx 0.3.0
Get counts of sequences whose length >= specified length
>>> # get counts of sequences with length >= 200 bp
>>> fa.count(200)
173
>>> # get counts of sequences with length >= 500 bp
>>> fa.count(500)
70
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 description
>>> # New in pyfastx 0.3.1
>>> s.description
'R283 cDNA library of flower petals in tree peony by suppression subtractive hybridization Paeonia suffruticosa cDNA, mRNA sequence'
>>> # 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'
Read sequence line by line
New in pyfastx 0.3.0
The sequence object can be iterated line by line as they appear in FASTA file.
>>> for line in fa[0]:
... print(line)
...
CTCTAGAGATTACTTCTTCACATTCCAGATCACTCAGGCTCTTTGTCATTTTAGTTTGACTAGGATATCG
AGTATTCAAGCTCATCGCTTTTGGTAATCTTTGCGGTGCATGCCTTTGCATGCTGTATTGCTGCTTCATC
ATCCCCTTTGACTTGTGTGGCGGTGGCAAGACATCCGAAGAGTTAAGCGATGCTTGTCTAGTCAATTTCC
CCATGTACAGAATCATTGTTGTCAATTGGTTGTTTCCTTGATGGTGAAGGGGCTTCAATACATGAGTTCC
AAACTAACATTTCTTGACTAACACTTGAGGAAGAAGGACAAGGGTCCCCATGT
Search for subsequence
New in pyfastx 0.3.6
Search for subsequence from given sequence and get one-based start position of the first occurrence
>>> # search subsequence in sense strand
>>> fa[0].search('GCTTCAATACA')
262
>>> # check subsequence weather in sequence
>>> 'GCTTCAATACA' in fa[0]
True
>>> # search subsequence in antisense strand
>>> fa[0].search('CCTCAAGT', '-')
301
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.3.6-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5436a086394e5fb66d3895ef41b1437fdc9ca1de9d40bafa139adf5b4bb47bb0 |
|
MD5 | 890d4bda4bbde474fc797f5d7481d1fe |
|
BLAKE2b-256 | 0ee83230cb89022b41ba63d0f91a989dc079a5ab727383ee08fd24adbf16966a |
Hashes for pyfastx-0.3.6-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a82a298985700191a97e24a70d7ca758bd35e43dd1f18f58e0ed96d9df23897 |
|
MD5 | 4e1cd5de46a88909f88531aaa4e1dd93 |
|
BLAKE2b-256 | 31f893c3d3ee97e9810052fa8fbe575883441d53ce53882c1aab2ef6aca81c26 |
Hashes for pyfastx-0.3.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c3eda5f0a83eafa882e1f9be2cc76fcd2209d3ec2a40eb525a3aee6a5aadee5 |
|
MD5 | 92b59fb10607e3affcf7bd09b6057018 |
|
BLAKE2b-256 | c94b56c0e36e3c4c7be3f26e7a9ba5988fcc8f8b50d58c9a115b0790136ed711 |
Hashes for pyfastx-0.3.6-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dc428c4a6599851806fef1d122180eae0d9d70a320a3211ae65b28574f81ef7 |
|
MD5 | 389db6e609b7295697462defc76d3a6d |
|
BLAKE2b-256 | 7b0cf659ab1f65b59c77f84f74dcf136b5f99f14f60f8d66376b6f7a0b25f717 |
Hashes for pyfastx-0.3.6-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efcf3f1ddd4d0cce7fac7dfc28b68293c55254611952a3e2015d11cf721e0d63 |
|
MD5 | eae401d774f556cc611a9298a40d8b19 |
|
BLAKE2b-256 | 97b4386cf1dc5dd619c2a7d72f72d6664386e4aed2a8f700f02ec6875ab90c29 |
Hashes for pyfastx-0.3.6-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b835e6f5ef227877b39c456b8c541931c2e945d80761a4adf76d63eff0ca3042 |
|
MD5 | 39c7608d6ca81110be35e1ab18e19868 |
|
BLAKE2b-256 | 83ca5c91bdec417b4a6d8e47f81efa44d7bca660524eabedc12ecd4db99ecd8f |
Hashes for pyfastx-0.3.6-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e485ee7e7499383a97f7f1849a6c45ae7beb8a60bb949bcf4effb204b3f3b039 |
|
MD5 | e22f032c8a58bf7d22a3e983e9590e21 |
|
BLAKE2b-256 | a7c0990b72679f2b238f7273444953bf0898ed901f496032d62e710a6a51e503 |
Hashes for pyfastx-0.3.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9faa4f29027091d61696d1108b26e741f945f90e38929a9d16572e4ad8d53b59 |
|
MD5 | 7afe07d1849cd1b8ca25f6e74126a02a |
|
BLAKE2b-256 | e8952c8b9669987e3d250b93b9a769f75e14ac3b16590f63122065f47db64216 |
Hashes for pyfastx-0.3.6-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57a14d5a2515ab7c7de1cf378dbdc9f959ca3d62b4a0dc9a5023e5c58344e9ec |
|
MD5 | 51e04d4d9067d59ebf224219db6d7829 |
|
BLAKE2b-256 | 690f883800c4369232f1c61f84d709a044ee07aaf0a8f027a1cbf056a1450625 |
Hashes for pyfastx-0.3.6-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e979463d2c8e88e38103d0e84b47bb3d843866d54722bedb02bc97976768515 |
|
MD5 | 1d55c5c1562512253d19d2dd89e8947c |
|
BLAKE2b-256 | 4893fd8bd19da0dd3becc9f7a52ded2ced01c31fd0336f0ce7f167bc1364d11d |
Hashes for pyfastx-0.3.6-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54d54c0b2e01c87c90b3363094fb9544f0690c421f44e142c654dd68e05e25f6 |
|
MD5 | 0f7dba0b0562d8b2d55cd91e20d918eb |
|
BLAKE2b-256 | 95d16945ac0967d6fc1d05fb05d371592eadca2cb61b365a99c005c68f376898 |
Hashes for pyfastx-0.3.6-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e0ccac98c1b7f4841de59fce40e3917c7ee2746b5ae540a5eed4a60e5e77754 |
|
MD5 | d77b7094423bb5127aaba98c430d2394 |
|
BLAKE2b-256 | c19dd838186bb1673cf2bbd475f0f137bc056ae8aac67ba728210e9869e8c174 |
Hashes for pyfastx-0.3.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32e6cb9ff481dc84215adbe6117e10c8af5340e30a6d7bfa2f4ebfe242894d3f |
|
MD5 | 031565ae173383e43bd8f21ac052957f |
|
BLAKE2b-256 | c3b2821de3cf3cd1c1335ef823eb7f3e13c33639c6b6741a6a84ab33674abf8e |
Hashes for pyfastx-0.3.6-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e58f6e32ba92321cadc354171c10008c82d4167e039a8602d6244a4b851432ab |
|
MD5 | 73fc971cd62cc02e0dddccb23512a4d2 |
|
BLAKE2b-256 | 5de311d8ace83d250024c9848d9ea15c2e04a83c8c5e7333602a3a86cd91dda0 |
Hashes for pyfastx-0.3.6-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d21bbf3fc82f2214c9265ee9724e0ca4b3f4a776b41d94b8480ad6f736724595 |
|
MD5 | 37b506aef945cbd8942f483d00d5913e |
|
BLAKE2b-256 | c084ea4dea72f2c2a4e299af1c17d69b8ec72ee6328379f2267d4ceb6a5f760c |