Created
March 22, 2025 13:13
-
-
Save harijay/e08ed2a901e8181a50a7bb93923c9d11 to your computer and use it in GitHub Desktop.
This file contains 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
import random | |
import pandas | |
import string | |
import great_tables | |
from great_tables import GT,loc,style | |
aa_list = ["A","C","D","E","F","G","H","I","K","L","M","N","P","R","S","T","V","W","Y"] | |
def create_random_aa_seq(size=200): | |
aa_seq = [] | |
for i in range(size): | |
aa_seq.append(random.choice(aa_list)) | |
return "".join(aa_seq) | |
def mutate_sequence_randomly(input_seq, num_pos = 10): | |
positions_to_mutate = list(range(len(input_seq))) | |
output_seq = list(input_seq) | |
for i in range(num_pos): | |
pos = random.choice(positions_to_mutate) | |
output_seq[pos] = random.choice(aa_list) | |
return "".join(output_seq) | |
def create_dummy_pat_ids(): | |
random_choice = range(1,9,1) | |
random_num = [] | |
for i in range(8): | |
random_num.append(str(random.choice(random_choice))) | |
suffix = "".join(random_num) | |
return f"US{suffix}" | |
def write_alignment_string_css_ver2(seq_top, seq_bot): | |
ref_id_top = create_mock_id() | |
ref_id_bot = create_mock_id() | |
outstring = f""" | |
<div style="font-family: 'Courier New', monospace; white-space: pre-wrap; word-break: break-all; width:100%"> | |
{ref_id_top}: {seq_top} | |
{' '*len(ref_id_top)}: {''.join(['|' if a == b else ' ' for a, b in zip(seq_top, seq_bot)]).lower()} | |
{ref_id_bot}: {seq_bot} | |
</div> | |
""" | |
return outstring | |
def create_mock_id(max_len = 10): | |
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=max_len)) | |
def create_dummy_dataframe_css_ver2(num_elements=100): | |
pat_id = [] | |
seq_top = create_random_aa_seq(150) | |
seq_bot = [] | |
ali = [] | |
for i in range(num_elements): | |
pat_id.append(create_dummy_pat_ids()) | |
seq_bottom_new = mutate_sequence_randomly(seq_top) | |
seq_bot.append(seq_bottom_new) | |
ali.append(write_alignment_string_css_ver2(seq_top, seq_bottom_new)) | |
data = {"pat_id": pat_id, "seq_bot": seq_bot, "Alignment": ali} | |
return pandas.DataFrame(data) | |
df_css_ver2 = create_dummy_dataframe_css_ver2() | |
gt_css_ver2 = ( | |
GT(df_css_ver2) | |
.tab_style( | |
style=style.text( # Correctly calling the style function | |
whitespace="pre-wrap", | |
font="Roboto", | |
), | |
locations=loc.body(columns="Alignment"), | |
) | |
.tab_style( | |
style=style.css( | |
"table-layout: fixed; width: 100%;" | |
), | |
locations=loc.body(), | |
) | |
.tab_style( | |
style=style.css( | |
"min-width: 2000px;" | |
), | |
locations=loc.body(columns="Alignment") | |
) | |
) | |
gt_css_ver2.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment