Created
January 7, 2022 19:31
-
-
Save even4void/eaf85cec81c52b64db69f91586fa73d8 to your computer and use it in GitHub Desktop.
Fast Fasta TUI viewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import statistics | |
import sys | |
from Bio import SeqIO | |
term_width = os.get_terminal_size().columns | |
term_height = os.get_terminal_size().lines | |
id_width = 15 | |
default_color = "\033[0m" | |
bold_color = "\033[1m" | |
underline_color = "\033[4m" | |
orange_color = "\033[33m" | |
sep = " ... " | |
def gc(x): | |
"""Compute GC content.""" | |
val = sum([x.seq.count(k) for k in ["g", "c"]]) / len(x.seq) * 100 | |
return val | |
def process(file): | |
records = list(SeqIO.parse(file, "fasta")) | |
nr = len(records) | |
avg_len = round(statistics.mean([len(r.seq) for r in records]), 1) | |
avg_gc = round(statistics.mean([gc(r) for r in records]), 1) | |
trunc = False | |
if nr > term_height - 2: | |
records = ( | |
records[: int((term_height - 1) / 2)] | |
+ records[-(int((term_height - 1) / 2)) :] | |
) | |
trunc = True | |
nr_trunc = len(records) | |
opt = "(" + str(nr_trunc) + " displayed) " if trunc else "" | |
header = ( | |
underline_color | |
+ str(nr) | |
+ " seq. " | |
+ opt | |
+ "avg length: " | |
+ str(avg_len) | |
+ " GC: " | |
+ str(avg_gc) | |
+ "%" | |
+ default_color | |
) | |
print(header) | |
for r in records: | |
if len(str(r.id)) < id_width: | |
id = str(r.id).ljust(id_width) | |
else: | |
id = str(r.id)[: id_width - 1].ljust(id_width) | |
seq = str(r.seq).lower() | |
seq_width = int((term_width - id_width - len(sep)) / 2) | |
lineout = ( | |
bold_color + id + default_color + seq[:seq_width] + sep + seq[-seq_width:] | |
) | |
print(lineout) | |
if __name__ == "__main__": | |
process(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Homemade previewer for Fasta files (require
Biopython
). There are better alternatives.Preview:
(Bold syntax and underline rendered in terminal only.)