Skip to content

Instantly share code, notes, and snippets.

@wasade
Created April 9, 2014 15:24
Show Gist options
  • Select an option

  • Save wasade/10282769 to your computer and use it in GitHub Desktop.

Select an option

Save wasade/10282769 to your computer and use it in GitHub Desktop.
Cythoning of parse fasta
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"cimport cython\n",
"from cpython cimport bool\n",
"from string import strip\n",
"\n",
"cdef bool is_fasta_label(str x):\n",
" \"\"\"Checks if x looks like a FASTA label line.\"\"\"\n",
" return x.startswith('>')\n",
"\n",
"cdef bool is_blank_or_comment(str x):\n",
" \"\"\"Checks if x is blank or a FASTA comment line.\"\"\"\n",
" return (not x) or x.startswith('#') or x.isspace()\n",
"\n",
"\n",
"cdef bool is_blank(str x):\n",
" \"\"\"Checks if x is blank.\"\"\"\n",
" return (not x) or x.isspace()\n",
"\n",
"cdef bool is_empty(str line):\n",
" \"\"\"Returns True empty lines and lines consisting only of whitespace.\"\"\"\n",
" return (not line) or line.isspace()\n",
"\n",
"def FastaFinder():\n",
" \"\"\"Returns function that returns successive labeled records from file.\n",
" \"\"\"\n",
" def parser(lines):\n",
" curr = []\n",
" for l in lines:\n",
" \n",
" line = strip(l)\n",
" \n",
" if is_blank_or_comment(line):\n",
" continue\n",
" # if we find the label, return the previous record\n",
" if is_fasta_label(line):\n",
" if curr:\n",
" yield curr\n",
" curr = []\n",
" curr.append(line)\n",
" # don't forget to return the last record in the file\n",
" if curr:\n",
" yield curr\n",
" return parser\n",
"\n",
"def parse_fasta(object infile, \n",
" str label_characters='>'):\n",
" finder = FastaFinder()\n",
" for rec in finder(infile):\n",
" # first line must be a label line\n",
" if not rec[0][0] in label_characters: \n",
" raise ValueError(\"Found Fasta record without label line: %s\" %\n",
" rec)\n",
" # record must have at least one sequence\n",
" if len(rec) < 2:\n",
" raise ValueError(\"Found label line without sequences: %s\" %\n",
" rec)\n",
" \n",
" label = rec[0][1:].strip()\n",
" seq = ''.join(rec[1:])\n",
" \n",
" yield label, seq"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = '/Users/mcdonadt/not_backed_up/gg_13_5.fasta'\n",
"from gzip import open as gzopen\n",
"def unroll_parser(f, parser):\n",
" for rec in parser(f):\n",
" pass\n",
"%time unroll_parser(open(f), parse_fasta)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"CPU times: user 4.56 s, sys: 472 ms, total: 5.03 s\n",
"Wall time: 5.03 s\n"
]
}
],
"prompt_number": 42
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from skbio.parse.sequences import parse_fasta as skbio_parse_fasta\n",
"%time unroll_parser(open(f), skbio_parse_fasta)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"CPU times: user 8.19 s, sys: 502 ms, total: 8.7 s\n",
"Wall time: 8.7 s\n"
]
}
],
"prompt_number": 43
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment