forked from Fudenberg-Research-Group/qbio577_fall2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hwutils.py
40 lines (33 loc) · 1.14 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
# useful libraries to import
import pandas as pd
import numpy as np
import sklearn.decomposition
import matplotlib.pyplot as plt
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.
"""
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 = le.transform(labels)
else:
labels = None
plt.figure(figsize=figsize)
plt.scatter(pca.components_[0],
pca.components_[1],
c = labels,
alpha=alpha,
lw=lw
)