<jupyter_start><jupyter_text>Query mRNA data<jupyter_code>mRNA_q = "SELECT ParticipantBarcode, original_gene_symbol, normalized_count FROM `tcga_201607_beta.mRNA_KIRP`"
mRNA_df = pd.read_gbq(mRNA_q, 'p1rcc-aqua', dialect='standard')
mRNA_df = mRNA_df.dropna().drop_duplicates(subset=['ParticipantBarcode', 'original_gene_symbol'], keep=False)
mRNA_data = mRNA_df.pivot('ParticipantBarcode', 'original_gene_symbol', 'normalized_count')
mRNA_data = mRNA_data.reset_index()
mRNA_data.head()<jupyter_output><empty_output><jupyter_text>Query Clinical Data<jupyter_code>clin_data_q = "SELECT * FROM `tcga_201607_beta.clin_data_KIRP`"
clin_data_KIRP = pd.read_gbq(clin_data_q, 'p1rcc-aqua', dialect='standard')
# # for now, use csv
# clin_data = pd.read_csv("data1.csv")
# clin_data_KIRP = clin_data[clin_data['Study'] == "KIRP"]<jupyter_output><empty_output><jupyter_text>Combine mRNA and Clinical Data<jupyter_code>df_all = mRNA_data.merge(clin_data_KIRP, left_on="ParticipantBarcode", right_on="ParticipantBarcode", how="left")
df_all.head()<jupyter_output><empty_output><jupyter_text>Functions to Plot<jupyter_code>gene_name = 'MET'
color_by = "age_at_initial_pathologic_diagnosis"
import matplotlib.colors as mcolors
import matplotlib.cm as cm

plt.figure(figsize=(20,10))
ax = sns.stripplot(x='ParticipantBarcode', y=gene_name, hue=color_by, data=df_all, palette='ocean', jitter=True, edgecolor='none', alpha=.60)
ax.legend_.remove()
ax.set(xticklabels=[])
sns.despine()
df_all.describe()

# Drawing the side color bar
normalize = mcolors.Normalize(vmin=df_all[color_by].min(), vmax=df_all[color_by].max())
colormap = cm.viridis

for n in df_all[color_by]:
    plt.plot(color=colormap(normalize(n)))

scalarmappaple = cm.ScalarMappable(norm=normalize, cmap=colormap)
scalarmappaple.set_array(df_all[color_by])
plt.colorbar(scalarmappaple)<jupyter_output><empty_output><jupyter_text>Barplot of gene expression across patients  Insert gene name and variable to color by below<jupyter_code>gene_name = "MET"
# color_by = "age_at_initial_pathologic_diagnosis"
color_by = "tumor_type"<jupyter_output><empty_output><jupyter_text><jupyter_code>if df_all[color_by].dtype == 'O':
    plot_1gene_cat_color()
else:
    plot_1gene_cont_color()
df_all[gene_name].describe()<jupyter_output><empty_output><jupyter_text><jupyter_code># # query database
# query = 'SELECT * FROM tcga_201607_beta.mRNA_KIRP WHERE HGNC_gene_symbol = "' + gene_name + '"'
# df = gbq.read_gbq(query, project_id='p1rcc-aqua')
# # plot
# plt.figure(figsize=(20,10))
# ax = plt.subplot() # Defines ax variable by creating an empty plot
# for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
#              ax.get_xticklabels() + ax.get_yticklabels()):
#     item.set_fontsize(25)

# plt.bar(df['ParticipantBarcode'],df['normalized_count'])
# plt.xticks([])
# plt.xlabel("Patients")
# plt.ylabel("Normalized Count")
# ax.set_title(gene_name + " gene expression", fontsize=30)
# plt.show()<jupyter_output><empty_output><jupyter_text>Scatter plot to compare gene expressions for 2 genes  Insert gene names below<jupyter_code>gene_name1 = "NFE2L2"
gene_name2 = "MET"
from scipy import stats
stats.spearmanr(df_all[gene_name1], df_all[gene_name2])
plt.figure(figsize=(10,10))
ax = plt.subplot() # Defines ax variable by creating an empty plot
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(25)
plt.scatter(all_data[gene_name1],all_data[gene_name2])
plt.xlabel("Normalized Count for " + gene_name1)
plt.ylabel("Normalized Count for " + gene_name2)
ax.set_title(gene_name1 + " and " + gene_name2 + " gene expression", fontsize=30)
plt.show()<jupyter_output><empty_output><jupyter_text><jupyter_code># query1 = 'SELECT * FROM tcga_201607_beta.mRNA_KIRP WHERE HGNC_gene_symbol = "' + gene_name1 + '"'
# df1 = gbq.read_gbq(query1, project_id='p1rcc-aqua')
# query2 = 'SELECT * FROM tcga_201607_beta.mRNA_KIRP WHERE HGNC_gene_symbol = "' + gene_name2 + '"'
# df2 = gbq.read_gbq(query2, project_id='p1rcc-aqua')
# df = df1.merge(df2, left_on="ParticipantBarcode", right_on="ParticipantBarcode", how="left")
# plt.figure(figsize=(10,10))
# ax = plt.subplot() # Defines ax variable by creating an empty plot
# for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
#              ax.get_xticklabels() + ax.get_yticklabels()):
#     item.set_fontsize(25)
# plt.scatter(df['normalized_count_x'],df['normalized_count_y'])
# plt.xlabel("Normalized Count for " + gene_name1)
# plt.ylabel("Normalized Count for " + gene_name2)
# ax.set_title(gene_name1 + " and " + gene_name2 + " gene expression", fontsize=30)
# plt.show()<jupyter_output><empty_output><jupyter_text>Scatter plot to compare gene expressions for 2 genes colored by another variable from clinical data table<jupyter_code>plt.figure(figsize=(10,10))
ax = plt.subplot() # Defines ax variable by creating an empty plot
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(25)
plt.scatter(all_data[gene_name1],all_data[gene_name2])
plt.xlabel("Normalized Count for " + gene_name1)
plt.ylabel("Normalized Count for " + gene_name2)
ax.set_title(gene_name1 + " and " + gene_name2 + " gene expression", fontsize=30)
plt.show()
import pandas as pd
clin_data = pd.read_csv("data1.csv")
clin_data = pd.read_csv("data1.csv")
clin_data_KIRP = clin_data[clin_data['Study'] == "KIRP"]
clin_data_KIRP.head()
df_all = df.merge(clin_data_KIRP, left_on="ParticipantBarcode", right_on="ParticipantBarcode", how="left")
df_all.head()
plt.figure(figsize=(15,15))
ax = plt.subplot() # Defines ax variable by creating an empty plot
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(25)
plt.scatter(df_all['normalized_count_x'],df_all['normalized_count_y'], c=df_all['gender'], alpha=0.2)
plt.xlabel("Normalized Count for " + gene_name1)
plt.ylabel("Normalized Count for " + gene_name2)
cbar=plt.colorbar()
cbar.set_label("Gender")
ax.set_title(gene_name1 + " and " + gene_name2 + " gene expression colored by gender", fontsize=30)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1415261044176707, 'fraction_numerical': 0.020240963855421686, 'mean_word_length': 6.941326530612245, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '255565', 'license': 'MIT', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2402, 'n_words': 396, 'path': 'numfah/plots_gene_expression.ipynb', 'repo_name': 'SVAI/p2rcc', 'size': '6225'}