<jupyter_start><jupyter_text>Data import<jupyter_code>import pandas as pd 
import numpy as np
import plotly.express as px
from sklearn.decomposition import PCA
df = pd.read_csv("../00_data/embryo_transcriptome_germination.csv")
df.shape
df.head(n = 2)<jupyter_output><empty_output><jupyter_text>PCA analysis Transpose to have samples x genesFormat needs to be: _N_ data rows x _M_ features<jupyter_code>X = df.drop(["probe", "gene_model", "annotation"], axis = 1)
X = X.set_index("locus")
X.head(n=2)
t_X = X.transpose()
print(t_X.shape)
print(type(t_X))
t_X.head()<jupyter_output>(18, 22343)
<class 'pandas.core.frame.DataFrame'><jupyter_text>Compute PCABy default, sklearn PCA center the data (mean = 0).If _whiten=True_, it will compute unit component-wise variances.<jupyter_code>n_components = 2 # lower than min(n_samples, n_features)
pca = PCA(n_components=n_components, whiten=True)
X_pca = pca.fit_transform(t_X)
total_var = pca.explained_variance_ratio_.sum() * 100
total_var
#labels = {str(i): f"PC {i+1}" for i in range(n_components)}
#labels['color'] = "Time point (h)"<jupyter_output><empty_output><jupyter_text>Plot PCA with plotly<jupyter_code>sample_labels = df.columns.values.tolist()[4:]
sample_timepoints = [s.split("_")[0] for s in sample_labels]
sample_timepoints_unique_levels = list(set(sample_timepoints))
sample_timepoints[0:5]
df_for_plot.head(5)
df_for_plot = pd.DataFrame(X_pca, columns=["PC1","PC2"])
print(df_for_plot.shape)
df_for_plot["timepoint"] = sample_timepoints
df_for_plot.head(n=5)
custom_axis_labels = {
    str(i): f"PC {i+1} ({var:.1f}%)"
    for i, var in enumerate(pca.explained_variance_ratio_ * 100)
}
custom_axis_labels

fig = px.scatter(
    df_for_plot, x = "PC1", y = "PC2", color = "timepoint", 
    labels = custom_axis_labels, 
    title = "PCA score plot from the embryo transcriptome"
)
fig.update_layout(xaxis_title=custom_axis_labels["0"], yaxis_title=custom_axis_labels["1"])
fig.show()
# save figure
fig.write_image("embryo_transcriptome_pca_score_plot.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13616398243045388, 'fraction_numerical': 0.01708150317227916, 'mean_word_length': 7.2, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '465786', 'license': 'MIT', 'n_tokens_mistral': 826, 'n_tokens_neox': 790, 'n_words': 154, 'path': '04_RNA_clusters/clustering_analysis.ipynb', 'repo_name': 'mgalland/rice-germination', 'size': '2049'}