forked from christacaggiano/celfie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
197 lines (93 loc) · 4.14 KB
/
demo.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# coding: utf-8
# # imports
# In[24]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import numpy as np
from sklearn.preprocessing import normalize
import scipy.cluster.hierarchy as shc
import numpy.ma as ma
np.seterr(divide='ignore', invalid='ignore')
# In[25]:
# import R to be used in python
# sorry R people that may be reading this code
import rpy2
get_ipython().run_line_magic('load_ext', 'rpy2.ipython')
# In[26]:
# set white seaborn plotting style
sns.set(context='talk', style='white', rc={'figure.facecolor':'white'}, font_scale=1)
sns.set_style('ticks')
# # read in data
# In[27]:
# tissue proportion estimates generated by CelFiE
tissue_proportions = pd.read_csv("cfDNA/ONT_playing/clean_dorado/bed/celfie_output/1_tissue_proportions.txt", delimiter="\t")
# In[28]:
# rename column for nice plotting
tissue_proportions.rename(columns={"Unnamed: 0": "samples"}, inplace=True)
# In[29]:
# change the orientation of the data so that it is easy to plot with seaborn
tissue_proportions = tissue_proportions.melt("samples", var_name="tissue", value_name="estimate")
# In[31]:
plt.hist(tissue_proportions, )
# In[32]:
# mean values of placenta
placenta_estimates = tissue_proportions[tissue_proportions["tissue"] == "placenta"]
placenta_estimates.groupby("status")["estimate"].mean()
# # calculate p-value in R
# In[33]:
# save it to be read by R; can also just do this stand alone in R
tissue_proportions.to_csv("celfie_demo/sample_output/tissue_proportions.csv", index=False)
# In[34]:
get_ipython().run_cell_magic('R', '', '# R code to calculate the p-value between the estimates of preg/not preg women\n\ndf = read.csv("celfie_demo/sample_output/tissue_proportions.csv") # re-read in the dataframe for this R code \ndf$status <- as.character(df$status)\ndf$status[df$status == "not pregnant"] <- 1\ndf$status[df$status == "pregnant"] <- 0\ndf$status <- as.numeric(df$status)\n\nplacenta = subset(df, df$tissue=="placenta")\n\nsummary(glm(status ~ estimate, data = placenta))\n')
# # methylation proportions
# ## reference methylation
# In[35]:
reference = pd.read_csv("celfie_demo/sample_data.txt", delimiter="\t").iloc[:, 33:] # read in reference only
# In[36]:
reference_meth = reference.iloc[:, 3::2].values # methylation values
reference_depth = reference.iloc[:, 4::2].values # depth values
# In[37]:
reference_prop = reference_meth/reference_depth # convert to proportion
# ## estimated methylation by CelFiE
# In[38]:
estimated = pd.read_csv("celfie_demo/sample_output/1_methylation_proportions.txt", delimiter="\t")
# In[39]:
estimated_prop = estimated.iloc[:, 1:].values
# In[40]:
# calculate the correlation between the reference data and that estimated by CelFiE
correlation = []
for i in range(reference_prop.shape[1]):
corr = ma.corrcoef(ma.masked_invalid(reference_prop[:, i]),
ma.masked_invalid(estimated_prop[:, i]))[0, 1] # mask Nans
correlation.append(corr)
# # plot correlation
# In[41]:
sns.barplot(x=list(estimated)[1:-1], y=correlation, color="#61c2d3")
plt.xticks(rotation=90)
plt.xlabel("tissue")
plt.ylabel("correlation")
plt.show()
# # hierarchical clustering
# In[42]:
# append the unknown to the reference data so that they can be clustered together
reference_unknown = np.append(reference_prop, estimated_prop[:, -1].reshape(-1, 1), axis=1)
# In[43]:
# drop any missing values
reference_unknown = pd.DataFrame(reference_unknown)
reference_unknown = reference_unknown.dropna()
# In[44]:
reference_unknown_scaled = normalize(reference_unknown) # normalize data
reference_unknown_scaled = pd.DataFrame(reference_unknown_scaled, columns=list(estimated)[1:]) # make dataframe again
# In[45]:
sns.set_context("poster")
plt.figure(figsize=(10, 7))
plt.title("Dendrograms")
shc.set_link_color_palette(['black', 'black'])
dend = shc.dendrogram(shc.linkage(reference_unknown_scaled.T, method='ward'), orientation='left',
labels=reference_unknown_scaled.columns, leaf_font_size = 20)
plt.title("ALS unknown")
plt.show()
# In[ ]: