<jupyter_start><jupyter_text>Differential Gene Expression<jupyter_code>%pwd
%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from matplotlib.patches import Patch
import pandas as pd
import numpy as np
import seaborn as sns
from scipy import stats
from imblearn.over_sampling import SMOTE
import csv

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
matplotlib.rcParams['font.sans-serif'] = "Helvetica"
matplotlib.rcParams['font.family'] = "sans-serif"

#Random Number
r = 42<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>#Loading in data
df = pd.read_csv('Output/expression_matrix.txt', index_col=0).T
dfanno = pd.read_csv('Output/metadata.txt', index_col=0, header=None, squeeze = True)

#Load Genes
genes = pd.read_csv('Output/100_ranked_genes.txt', squeeze=True, header=0)
genes
#Choose Genes
df = df[genes]
df = df.astype(float)
df
#Oversampling
X, y = SMOTE(random_state=r, n_jobs=-1).fit_resample(df, dfanno)
def fixedWidthClusterMap(df, cmap='RdBu_r', s=30, top=0.05, side=20, num=20, bar=0, up=0, t=0.02, z_score=True):
    # Calulate the figure size
    dpi = plt.rcParams['figure.dpi']
    mw = plt.rcParams['figure.subplot.right']-plt.rcParams['figure.subplot.left']
    mh = plt.rcParams['figure.subplot.top']-plt.rcParams['figure.subplot.bottom']
    Ny,Nx = df.shape
    w = (Nx*s/dpi)/0.8/mw
    h = (Ny*s/dpi)/0.8/mh
    axWidth = (Nx*s)/(w*dpi)
    axHeight = (Ny*s)/(h*dpi)
    
    # do the actual plot
    if z_score:
        grid = sns.clustermap(df, figsize=(w, h), metric="euclidean", center=0, cmap=cmap, cbar_kws={"orientation": "horizontal"}, z_score=0)
    else:
        grid = sns.clustermap(df, figsize=(w, h), metric="euclidean", center=0, cmap=cmap, cbar_kws={"orientation": "horizontal"})


    # resize dendrograms to match
    row = grid.ax_row_dendrogram.get_position()
    grid.ax_row_dendrogram.set_position([row.x0-row.width*(side-1), row.y0, 
                                         row.width*side, axHeight])
    col = grid.ax_col_dendrogram.get_position()
    grid.ax_col_dendrogram.set_position([col.x0, col.y0+col.height*0.01-col.height*up,
                                         axWidth, col.height*top])

    # resize heatmap
    hm = grid.ax_heatmap.get_position()
    grid.ax_heatmap.set_position([hm.x0, hm.y0, 
                                  axWidth, axHeight])

    cbar = grid.cax.get_position()
    grid.cax.set_position([cbar.x0, cbar.y0+col.height*top-col.height*bar-col.height*up, .8, t/(num/20)])

    return grid
#Heatmap Gene Expression
def geheatmapclust(X, c, title='', top=0.05, side=20, num=20, s=30, bar=0, up=0, t=0.02):
    #Gene Expression
    ge = pd.DataFrame()
    c = c[c!="NA"]
    for i in c.unique():
        ge[i] = X.T[c[c==i].index].mean(axis=1)

    #Sort
    ge['Standard Deviation'] = ge.std(axis=1) #Standard Deviation
    ge = ge.sort_values(by=['Standard Deviation'], ascending=False)
    ge = ge.drop(['Standard Deviation'], axis=1) 
    
    #Heatmap
    if num=='all':
        cm = fixedWidthClusterMap(ge, top=top, side=side, num=ge.shape[1], bar=bar, s=s, up=up, t=t)
    else:
        cm = fixedWidthClusterMap(ge[:num], top=top, side=side, num=num, bar=bar, s=s, up=up, t=t)
geheatmapclust(X, y, s=30, num='all', t=0.001, bar=0.15)
ge = pd.DataFrame()
c = y[y!="NA"]
for i in c.unique():
    ge[i] = X.T[c[c==i].index].mean(axis=1)

#Sort
ge['Standard Deviation'] = ge.std(axis=1) #Standard Deviation
ge = ge.sort_values(by=['Standard Deviation'], ascending=False)
ge = ge.drop(['Standard Deviation'], axis=1) 
ge
table = pd.DataFrame(stats.zscore(ge.T).T, columns=ge.columns, index=ge.index)
table = table[['AD', 'LMCI', 'EMCI', 'CN']]
table.index = table.index.str.replace(' / ','_')
table
fixedWidthClusterMap(table, z_score=False, t=0.003, bar=0.15)
plt.savefig('Graphs/Clustering/ge.pdf', format='pdf', transparent=True)
table.to_csv('Output/differential_gene_expression.txt', quoting=csv.QUOTE_MINIMAL)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1621160409556314, 'fraction_numerical': 0.020965382740126767, 'mean_word_length': 4.103233830845771, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '183104', 'license': 'MIT', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1579, 'n_words': 293, 'path': 'Code/Differential Gene Expression.ipynb', 'repo_name': 'andrewni4313/FUNCTIONAL-GENETIC-BIOMARKERS-OF-ALZHEIMER-S-DISEASE-AND-GENE-EXPRESSION-FROM-PERIPHERAL-BLOOD', 'size': '4102'}