Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
add option for 1-based coords when using Fasta.sequence refs #1
Browse files Browse the repository at this point in the history
  • Loading branch information
James Casbon committed May 31, 2011
1 parent 7ab5abe commit 131db65
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.rst
Expand Up @@ -42,14 +42,18 @@ Slicing
>>> f['chr1'][::3]
'AGTCAGTCAGTCAGTCAGTCAGTCAGT'

# can query by a 'feature' dictionary
# can query by a 'feature' dictionary (note this is one based coordinates)
>>> f.sequence({'chr': 'chr1', 'start': 2, 'stop': 9})
'CTGACTGA'

# same as:
>>> f['chr1'][1:9]
'CTGACTGA'

# use python, zero based coords
>>> f.sequence({'chr': 'chr1', 'start': 2, 'stop': 9}, one_based=False)
'TGACTGA'

# with reverse complement (automatic for - strand)
>>> f.sequence({'chr': 'chr1', 'start': 2, 'stop': 9, 'strand': '-'})
'TCAGTCAG'
Expand Down
7 changes: 5 additions & 2 deletions pyfasta/fasta.py
Expand Up @@ -114,7 +114,7 @@ def __getitem__(self, i):
return self.chr[i]

def sequence(self, f, asstring=True, auto_rc=True
, exon_keys=None):
, exon_keys=None, one_based=True):
"""
take a feature and use the start/stop or exon_keys to return
the sequence from the assocatied fasta file:
Expand All @@ -123,6 +123,8 @@ def sequence(self, f, asstring=True, auto_rc=True
: if false, return as a numpy array
auto_rc : if True and the strand of the feature == -1, return
the reverse complement of the sequence
one_based: if true, query is using 1 based closed intervals, if false
semi-open zero based intervals
>>> from pyfasta import Fasta
>>> f = Fasta('tests/data/three_chrs.fasta')
Expand Down Expand Up @@ -183,7 +185,8 @@ def sequence(self, f, asstring=True, auto_rc=True
sequence = self._seq_from_keys(f, fasta, exon_keys)

if sequence is None:
sequence = fasta[(f['start'] - 1): f['stop']]
start = (f['start'] - 1) if one_based else f['start']
sequence = fasta[start: f['stop']]

if auto_rc and f.get('strand') in (-1, '-1', '-'):
sequence = complement(sequence)[::-1]
Expand Down

0 comments on commit 131db65

Please sign in to comment.