forked from Fudenberg-Research-Group/qbio577_fall2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hwutils.py
108 lines (94 loc) · 3.67 KB
/
hwutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# useful libraries to import
import pandas as pd
import numpy as np
import sklearn.decomposition
import matplotlib.pyplot as plt
import seaborn as sns
import colorcet as cc
import math
def plot_pca( pca ,
bigwig_metadata=None,
metadata_label_column=None,
alpha=0.5,
lw=0,
figsize=(8,8)):
"""
Skeleton for plotting PCA and annotating the plot.
Can be modified/extended to answer various questions.
"""
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"
if metadata_label_column is not None:
if bigwig_metadata is None:
raise ValueError("must provide metadata table to label by a metadata column")
labels = [bigwig_metadata.query(
"`File accession`==@ file_accession ").loc[:,metadata_label_column].values[0]
for file_accession in pca.feature_names_in_]
le = sklearn.preprocessing.LabelEncoder()
le.fit(labels)
labels_t = le.transform(labels)
#print(np.unique(labels))
else:
labels_t = None
fig, ax = plt.subplots(figsize=figsize)
if labels_t is not None:
number_labels = np.unique(labels).size
else:
number_labels = 1
palette = sns.color_palette(cc.glasbey, n_colors=number_labels)
sns.scatterplot(pca.components_[0],
pca.components_[1],
hue = labels_t,
alpha=alpha,
lw=lw,
palette=palette)
ax.xaxis.set_tick_params(labelsize=10)
if metadata_label_column is not None:
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, np.unique(labels), bbox_to_anchor=(1,1))
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1), ncol=math.ceil(number_labels/12))
#kwargs={'fontsize':'4'}
#if labels is not None:
# for i in range(0, len(pca.components_[0])):
# ax.text(pca.components_[0][i], pca.components_[1][i], f'{labels[i]}', **kwargs)
def plot_pca_celltype( pca,
bigwig_metadata=None,
metadata_label_column=None,
alpha=0.5,
lw=0,
figsize=(8,8)):
"""
Skeleton for plotting PCA and annotating the plot.
Can be modified/extended to answer various questions.
"""
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"
if metadata_label_column is not None:
if bigwig_metadata is None:
raise ValueError("must provide metadata table to label by a metadata column")
labels = [bigwig_metadata.query(
"`File accession`==@ file_accession ").loc[:,metadata_label_column].values[0]
for file_accession in pca.index]
le = sklearn.preprocessing.LabelEncoder()
le.fit(labels)
labels_t = le.transform(labels)
#print(np.unique(labels))
else:
labels_t = None
fig, ax = plt.subplots(figsize=figsize)
if labels_t is not None:
number_labels = np.unique(labels).size
else:
number_labels = 1
palette = sns.color_palette(cc.glasbey, n_colors=number_labels)
sns.scatterplot(pca[0],
pca[1],
hue = labels_t,
alpha=alpha,
lw=lw,
palette=palette)
ax.xaxis.set_tick_params(labelsize=10)
if metadata_label_column is not None:
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, np.unique(labels), bbox_to_anchor=(1,1))
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1), ncol=math.ceil(number_labels/12))