<jupyter_start><jupyter_text>Testing functions in telo_tools for handling individual telomere length data<jupyter_code># enables access to directories/files
import os

# for handling data
import numpy as np
from numpy import array
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

# graphing
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
import seaborn as sns

# for loading telo data column containing individual telomere length values
from ast import literal_eval

# statistics
from scipy import stats

# accessing telo_tools module, which is in the parent directory, for testing
import sys 
sys.path.append('..')
import telo_tools
# fxn for reading files
# test_df['telo data'] = test_df['telo data'].apply(lambda row: np.array(literal_eval(row)))<jupyter_output><empty_output><jupyter_text>Test compiling individual telomere length data and generating data into a dataframe<jupyter_code># compiling test data from excel files into a dict
test_dict = telo_tools.generate_dictionary_for_telomere_length_data('.')
# generating a pandas dataframe from the dict
timepoint_list = ['L-180', 'L-60', 'FD45', 'FD90', 'FD140', 'FD260', 'R+5', 'R+7', 'R+60', 'R+105', 'R+180', 'R+270']

test_df = telo_tools.make_dataframe_from_telomere_data_dict(test_dict, timepoint_list)
test_df.head(4)<jupyter_output><empty_output><jupyter_text>Test counting short/medium/long individual telomeres<jupyter_code># declaring a list of 
timepoint_list = ['L-180', 'L-60', 'FD45', 'FD90', 'FD140', 'FD260', 'R+5', 'R+7', 'R+60', 'R+105', 'R+180', 'R+270']

telo_tools.calculate_apply_teloQuartiles_dataframe(test_df, timepoint_list)<jupyter_output><empty_output><jupyter_text>Test exploding individual telomere length measurements into the dataframe as rows<jupyter_code>test_df2 = telo_tools.explode_individual_telos(test_df)
print(test_df2.shape)
test_df2.head(3)
import importlib
%load_ext autoreload
%autoreload 2
%reload_ext autoreload<jupyter_output><empty_output><jupyter_text>Testing graphing by histogram<jupyter_code>timepoint_list = ['L-270', 'L-180', 'L-60', 'FD45', 'FD90', 'FD140', 'FD260', 'R+5', 'R+7', 'R+60', 'R+105', 'R+180', 'R+270']

telo_tools.histogram_plot_groups(x='individual telos', df=test_df2, 
                      sample_id_col='sample id', groupby='sample id', 
                      num_samps_per_group=3, ordered_timepoint_list=timepoint_list)
all_astros_df = pd.read_csv('../../NASA repo/NASA-astronauts-telomeres-chromosomes/excel data/exploded_telos_astros_df.csv')
all_astros_df.head(4)
all_astros_df['sample id'] = all_astros_df['astro id']
all_astros_df['individual telos'] = all_astros_df['telo data exploded']
import math
def histogram_plot_groups(x=None, df=None, sample_id_col=None, groupby=None, 
                          num_samps_per_group=None, ordered_timepoint_list=None, n_bins=40):
    
    all_samples = list(df[sample_id_col].unique())
    group_df = df.groupby(groupby)
    
    for sample in all_samples:
        timepoint_telo_values_dict = {}
        
        plot_df = group_df.get_group(sample).copy()
        plot_df = order_timepoint_col(plot_df, ordered_timepoint_list)

        sample_unique_timepoints = list(plot_df['timepoint'].unique())
        
        for timepoint in sample_unique_timepoints:
            
            sample_ID_complete = str(list(plot_df[sample_id_col].unique())[0]) + ' ' + str(timepoint)
            timepoint_telo_values_dict[sample_ID_complete] = plot_df[plot_df['timepoint'] == timepoint][x]
            
        if len(timepoint_telo_values_dict.keys()) > 1:
            
            n_rows = len(timepoint_telo_values_dict.keys()) / 2
            n_rows = int(math.ceil(n_rows))
  
            n_bins = n_bins
            fig, axes = plt.subplots(n_rows, 2, sharey=True, sharex=True, constrained_layout=True, figsize = (12, 8))
            sns.set_style(style="darkgrid",rc= {'patch.edgecolor': 'black'})

            for ax, item in zip(axes.flatten(), timepoint_telo_values_dict.items()):

                name, data = item[0], item[1]
                initial_timepoint = timepoint_telo_values_dict[list(timepoint_telo_values_dict.keys())[0]]
                histogram_stylizer_divyBins_byQuartile(fig, ax, n_bins, data, initial_timepoint, f'{name}')
                

def histogram_stylizer_divyBins_byQuartile(fig, ax, n_bins, data, initial_timepoint, name):

    data = data.to_numpy()
    initial_timepoint = initial_timepoint.to_numpy()

    N, bins, patches = ax.hist(data, bins=n_bins, edgecolor='black')

    for a in range(len(patches)):
        if bins[a] <= np.quantile(initial_timepoint, 0.25):
            patches[a].set_facecolor('#fdff38')

        elif np.quantile(initial_timepoint, 0.25) < bins[a] and bins[a] <= np.quantile(initial_timepoint, 0.50):
            patches[a].set_facecolor('#d0fefe')

        elif np.quantile(initial_timepoint, 0.50) < bins[a] and bins[a] <= np.quantile(initial_timepoint, 0.75):
            patches[a].set_facecolor('#d0fefe')

        elif bins[a] > np.quantile(initial_timepoint, 0.75): 
            patches[a].set_facecolor('#ffbacd')
            
    ax.set_title(f"{name}", fontsize=18,)
    ax.tick_params(labelsize=12)
#     ax.xaxis.set_major_locator(plt.MaxNLocator(12))
def order_timepoint_col(df, ordered_timepoint_list):
    df['timepoint'] = df['timepoint'].astype('category')
    df['timepoint'].cat.set_categories(ordered_timepoint_list, inplace=True)
    df = df.sort_values(['sample id', 'timepoint']).reset_index(drop=True).copy()
    
    return df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12116402116402117, 'fraction_numerical': 0.02398589065255732, 'mean_word_length': 4.421606118546845, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '347909', 'license': 'MIT', 'n_tokens_mistral': 2094, 'n_tokens_neox': 1941, 'n_words': 377, 'path': 'examples/manual testing.ipynb', 'repo_name': 'Jared-Luxton/teloFISH_tools', 'size': '5670'}