# 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.bioinfo.cli import *
cat("abc.gb") | gb.feats()
"""
import k1lib.bioinfo.cli as _cli
from k1lib.bioinfo.cli.init import BaseCli as _BaseCli
from typing import List as _List
import time as _time
from collections import deque as _deque
__all__ = ["feats", "origin"]
[docs]class feats(_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) -> _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.init.serial(*(feats.filt(term) for term in terms))
return ((_cli.grep(*terms) | _cli.shape(0)) & _cli.identity()).all()\
| ~_cli.isValue(0, 0) | _cli.cut(1)
[docs] @staticmethod
def tag(tag:str) -> _BaseCli:
"""Gets a single tag out. Applies this on a single feature only"""
class _tag(_BaseCli):
def __ror__(self, it):
lines = it | _cli.grep(f"/{tag}").till("/") | _cli.dereference()
# check if on same line
if len(lines) > 1 and lines[-1].lstrip().startswith("/"): lines.pop()
return (lines | _cli.split(f"/{tag}=\"") | ~_cli.head(1)\
| _cli.strip() | _cli.to1Str("") | _cli.item()).rstrip("\"")
return _tag()
[docs]class origin(_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.strip()\
| _cli.table(" ") | _cli.cut()[1:] | _cli.stitch("")\
| _cli.remove("/") | _cli.to1Str("") | _cli.item()