DNA-LLM/viral_complexity
0
1import pandas as pd2import numpy as np3from datasets import load_dataset4import matplotlib.pyplot as plt5from scipy.interpolate import interp1d6from shiny import render7from shiny.express import input, output, ui8# from utils import (9# generate_2d_sequence,10# plot_seq_full_label11# )12import os13import matplotlib as mpl14import seaborn as sns15mpl.rcParams.update(mpl.rcParamsDefault)16 17ds = load_dataset('Hack90/virus_tiny', keep_in_memory = True, cache_dir = None)18df_virus = pd.DataFrame(ds['train'])19 20def shannon_entropy(seq):21 seq=re.sub("[^ATCG]","",seq)22 seq = seq.replace('A', 'T')23 seq = seq.replace('G', 'C')24 p = seq.count('T') / len(seq)25 e = 8.69 - 8.3126 c_h = ((-p * math.log(p)) - (1-p)* math.log(1-p)) * math.log((1-p)/p)27 c_h = c_h /e28 seq=seq.replace('T', '5 ')29 seq=seq.replace('C', '4 ')30 seq = np.array(seq.split()).astype(int)31 shann = -sum((p*math.log(p), ((1-p)*math.log(1-p))))32 shann = shann/233 return c_h , shann34 35 36ui.page_opts(fillable=True)37 38with ui.navset_card_tab(id="tab"):39 with ui.nav_panel("Species View"):40 ui.panel_title("What is the distribution of complexity across viral species?")41 with ui.card():42 ui.input_slider("sample", "samples", 0, len(df_virus), 40)43 44 def plot_loss_rates(df,samples):45 complexity = []46 for k in range(len(df.iloc[:samples])):47 complexity.append(shannon_entropy(df['sequence'].iloc[k]))48 49 df_nana = pd.DataFrame(complexity)50 df_nana['x'] = df_nana[1] * 251 df_nana['y'] = df_nana[0]52 53 54 # fig, ax = plt.subplots()55 56 57 # Create a figure and axis58 fig, ax = plt.subplots()59 60 # Create the scatter plot61 scatter = ax.scatter(df_nana['x'], df_nana['y'], s=0.5)62 63 # Add a colorbar64 cbar = fig.colorbar(scatter, ax=ax)65 cbar.set_label('Label')66 67 # Set labels and title68 # ax.set_xlabel('X')69 # ax.set_ylabel('Y')70 # ax.set_title(f"Loss ra")71 # ax.set_xlabel("Training steps")72 # ax.set_ylabel("Loss rate")73 return fig74 75 @render.plot()76 def plot_context_size_scaling():77 fig = plot_loss_rates(df_virus,input.sample() )78 if fig:79 return fig80 # with ui.nav_panel("Histone Modification"):81 # ui.panel_title("Is there a pattern to histone modification?")82 # with ui.layout_columns():83 # with ui.card():84 # ui.input_slider("sample_histone", "sample", 0, df_histone_len, 40)85 86 87 # def plot_histone(df,sample):88 # y_values = generate_2d_sequence(df['seq'].iloc[sample])[0]89 # x_values = generate_2d_sequence(df['seq'].iloc[sample])[1]90 91 # integers = str((np.argwhere(df['labels'][sample] == np.amax(df['labels'][sample]))).flatten().tolist())92 # # Create a DataFrame with the x values, y values, and integers93 # data = {'x': x_values, 'y': y_values, 'color': integers}94 95 # fig, ax = plt.subplots()96 97 # sns.scatterplot(x='x', y='y', hue='color', data=data, palette='viridis', ax=ax)98 # ax.legend()99 # # ax.set_title(f"Loss ra")100 # # ax.set_xlabel("Training steps")101 # # ax.set_ylabel("Loss rate")102 # return fig 103 # @render.plot()104 # def plot_histones_two():105 # fig = plot_histone(df_histone,input.sample_histone() )106 # if fig:107 # return fig108 # with ui.nav_panel("Enhancer Annontations"):109 # ui.panel_title("Is there a pattern to enhancer annotations?")110 # with ui.layout_columns():111 # with ui.card():112 # ui.input_slider("sample_enhancer", "sample", 0, df_enhancer_annotation_len, 40)113 # @render.plot()114 # def plot_enhancer():115 # fig = plot_loss_rates(df_enhancer_annotation,input.sample_enhancer() , True)116 # if fig:117 # return fig 118 119 