# AUTOGENERATED FILE! PLEASE DON'T EDIT
"""All tools related to GenBank file format. Expected to use behind the
"gb" module name, like this::
from k1lib.imports import *
cat("abc.gb") | gb.feats()
"""
from k1lib import cli
__all__ = ["feats", "origin"]
[docs]class feats(cli.BaseCli):
"""Fetches features, each on a separate stream"""
[docs] def __ror__(self, it):
it = it | cli.grep("FEATURES", 0, 1e9).till("ORIGIN") | cli.rows()[1:-1]
cache = []
for line in it:
if line[4:9] != " ": # new section detected
if len(cache) > 0: yield iter(cache)
cache = []
cache.append(line)
if len(cache) > 0: yield iter(cache)
[docs] @staticmethod
def filt(*terms:str) -> cli.BaseCli:
"""Filters for specific terms in all the features texts. If there
are multiple terms, then filters for first term, then second, then third,
so the term's order might matter to you"""
if len(terms) == 0: return cli.identity()
if len(terms) > 1: return cli.deref() | cli.init.serial(*(feats.filt(term) for term in terms))
return cli.toList().all() | cli.filt(lambda F: F | cli.grep(terms[0]) | cli.shape(0) > 0)
[docs] @staticmethod
def tag(tag:str) -> cli.BaseCli:
"""Gets a single tag out. Applies this on a single feature only"""
class _tag(cli.BaseCli):
def __ror__(self, it):
lines = it | cli.grep(f"/{tag}").till(f"/(?!{tag})") | cli.deref()
# check if on same line
if len(lines) > 1 and lines[-1].lstrip().startswith("/"): lines.pop()
return (lines | cli.op().split(f"/{tag}=\"").all() | cli.joinStreams() | ~cli.head(1)\
| cli.op().strip().all() | cli.join("")).rstrip("\"")
return _tag()
[docs]class origin(cli.BaseCli):
"""Return the origin section of the genbank file"""
[docs] def __ror__(self, it):
return it | cli.grep("ORIGIN", 0, 1e9) | ~cli.head(1) | cli.op().strip().all()\
| cli.op().split(" ").all() | cli.cut()[1:] | cli.join("").all()\
| cli.remove("/") | cli.join("")