Skip to content

Commit

Permalink
Move region splitting functions to pyfaidx module.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdshw5 committed Oct 28, 2014
1 parent fdc30f4 commit 559a9ca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ A lower-level Faidx class is also available:
Changes
-------

*New in version 0.2.8*:

- Small internal refactoring

*New in version 0.2.7*:

- Faidx and Fasta `strict_bounds` bounds checking logic is more correct
Expand Down
24 changes: 24 additions & 0 deletions pyfaidx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,30 @@ def map_to_function(rname):
return map_to_function


def bed_split(bed_entry):
try:
rname, start, end = bed_entry.rstrip().split()[:3]
except IndexError:
raise BedError('Malformed BED entry! {0}\n'.format(bed_entry.rstrip()))
start, end = (int(start), int(end))
return (rname, start, end)


def ucsc_split(region):
region = region.split()[0]
try:
rname, interval = region.split(':')
except ValueError:
rname = region
interval = None
try:
start, end = interval.split('-')
start, end = (int(start) - 1, int(end))
except (AttributeError, ValueError):
start, end = (None, None)
return (rname, start, end)


if __name__ == "__main__":
import doctest
doctest.testmod()
26 changes: 1 addition & 25 deletions pyfaidx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import argparse
import sys
from pyfaidx import Fasta, wrap_sequence, FetchError, BedError, RegionError
from pyfaidx import Fasta, wrap_sequence, FetchError, BedError, RegionError, ucsc_split, bed_split


def write_sequence(args):
Expand All @@ -41,30 +41,6 @@ def write_sequence(args):
fasta.__exit__()


def bed_split(bed_entry):
try:
rname, start, end = bed_entry.rstrip().split()[:3]
except IndexError:
raise BedError('Malformed BED entry! {0}\n'.format(bed_entry.rstrip()))
start, end = (int(start), int(end))
return (rname, start, end)


def ucsc_split(region):
region = region.split()[0]
try:
rname, interval = region.split(':')
except ValueError:
rname = region
interval = None
try:
start, end = interval.split('-')
start, end = (int(start) - 1, int(end))
except (AttributeError, ValueError):
start, end = (None, None)
return (rname, start, end)


def fetch_sequence(args, fasta, rname, start=None, end=None):
line_len = fasta.faidx.index[rname].lenc
sequence = fasta[rname][start:end]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
setup(
name='pyfaidx',
provides='pyfaidx',
version='0.2.7',
version='0.2.8',
author='Matthew Shirley',
author_email='[email protected]',
url='http://mattshirley.com',
Expand Down

0 comments on commit 559a9ca

Please sign in to comment.