Skip to content

Commit

Permalink
Add extract() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolan Woods committed Mar 6, 2021
1 parent a2e3f8a commit 6d5acb7
Show file tree
Hide file tree
Showing 4 changed files with 15,912 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ directly rather than trying to copy/paste the data.

`split()`_ and `let()`_ functions are available in addition to the JMESPath standard functions

`extract(Seq, SeqFeature)` is also made available to allow access to the `SeqFeature.extract()`_ function within the query

Examples:
Append a new record::

Expand Down Expand Up @@ -98,4 +100,5 @@ See CONTRIBUTING.rst_ for information on contributing to this repo.
.. _constructor parameters: https://biopython.org/DIST/docs/api/Bio.SeqRecord.SeqRecord-class.html#__init__
.. _JMESPath playground: https://glenveegee.github.io/jmespath-edit/
.. _split(): https://github.com/jmespath/jmespath.py/issues/159
.. _let(): https://github.com/jmespath/jmespath.site/pull/6
.. _let(): https://github.com/jmespath/jmespath.site/pull/6
.. _SeqFeature.extract(): https://biopython.org/docs/latest/api/Bio.SeqFeature.html#Bio.SeqFeature.SeqFeature.extract
9 changes: 9 additions & 0 deletions biopython_convert/JMESPathGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import itertools
import types

from Bio.Seq import Seq
from Bio.SeqFeature import SeqFeature

from collections import deque

# Register generator type in jmespath
Expand All @@ -14,6 +17,8 @@
# Register biopython types in jmespath
jmespath.functions.TYPES_MAP['Seq'] = 'string'
jmespath.functions.REVERSE_TYPES_MAP['string'] += ('Seq',)
jmespath.functions.REVERSE_TYPES_MAP['Seq'] = ('Seq',)
jmespath.functions.REVERSE_TYPES_MAP['SeqFeature'] = ('SeqFeature',)
jmespath.functions.TYPES_MAP['ExactPosition'] = 'number'
jmespath.functions.REVERSE_TYPES_MAP['number'] += ('ExactPosition',)

Expand Down Expand Up @@ -68,6 +73,10 @@ def _func_let(self, lexical_scope, expref, **kwargs):
def _func_split(self, on, val):
return val.split(on)

@jmespath.functions.signature({'types': ['Seq']}, {'types': ['SeqFeature']})
def _func_extract(self, seq, feature):
return feature.extract(seq)


class _Expression(jmespath.visitor._Expression):
def __init__(self, expression, interpreter, context):
Expand Down
Loading

0 comments on commit 6d5acb7

Please sign in to comment.