-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.py
446 lines (423 loc) · 14.9 KB
/
start.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""Stuff to run on app startup."""
import pickle
import numpy as np
import pandas as pd
from os import path
import plotly.figure_factory as ff
import plotly.graph_objs as go
def find_closest(bacteroidetes, firmicutes, samples, n=5):
"""Find the id of the members closest to the input.
Parameters
==========
bacteroidetes : float in [0, 1]
The fraction of bacteroides.
firmicutes : float in [0, 1]
The fraction of firmicutes.
samples : pandas.DataFrame
The sample data frame. Must contain column `Bacteroidetes` and
`Firmicutes` that contain the percentage of those phyla.
Returns
=======
list of str
The id of the n closest individuals.
"""
distance = list()
for index, row in samples.iterrows():
distance.append(
np.sqrt(
(np.square(row["Bacteroidetes"] - bacteroidetes))
+ (np.square(row["Firmicutes"] - firmicutes))
)
)
samples["Distance"] = distance
samples = samples.sort_values(by="Distance")
id = samples.index
sorted_id = id.tolist()
top_5 = sorted_id[0:5]
return top_5
def describe(samples, metadata):
"""Give representative information for set of samples.
Parameters
==========
samples : pandas.Series
The samples to describe.
metadata : pandas.DataFrame
The DataFrame containing additional information for all samples.
Returns
=======
dict
A dictionary containing different characteristics of the samples.
For instance:
- "dogs": How many of the individuals have a dog?
- "ibd": How many of the individuals have IBD?
"""
import pandas as pd
sample_metadata = pd.DataFrame()
metadata.index = metadata["sample_name"]
sample_metadata = metadata.loc[samples.index]
samples_with_dog = sample_metadata[sample_metadata.dog == "true"].shape[0]
samples_with_cat = sample_metadata[sample_metadata.cat == "true"].shape[0]
samples_with_ibd = sample_metadata[
sample_metadata.ibd
== "Diagnosed by a medical professional (doctor, physician assistant)"
].shape[0]
samples_with_diabetes = sample_metadata[
sample_metadata.diabetes
== "Diagnosed by a medical professional (doctor, physician assistant)"
].shape[0]
samples_with_cardio = sample_metadata[
sample_metadata.cardiovascular_disease
== "Diagnosed by a medical professional (doctor, physician assistant)"
].shape[0]
samples_with_cancer = sample_metadata[
sample_metadata.cancer
== "Diagnosed by a medical professional (doctor, physician assistant)"
].shape[0]
samples_female = sample_metadata[sample_metadata.sex == "female"].shape[0]
samples_consume_alcohol = sample_metadata[
sample_metadata.alcohol_consumption == "true"
].shape[0]
samples_with_college = sample_metadata[
sample_metadata.level_of_education == "Bachelor's degree"
].shape[0]
samples_who_smoke = sample_metadata[
(sample_metadata.smoking_frequency == "Rarely (a few times/month)")
& (sample_metadata.smoking_frequency == "Daily")
& (
sample_metadata.smoking_frequency
== "Occasionally (1-2 times/week)"
)
& (sample_metadata.smoking_frequency == "Regularly (3-5 times/week)")
].shape[0]
sample_metadata.fillna(0)
sample_metadata.birth_year = sample_metadata.birth_year.replace(
{"Not applicable": 0}
)
sample_metadata.birth_year = sample_metadata.birth_year.replace(
{"Not provided": 0}
)
samples_older_70 = sample_metadata[
(sample_metadata.birth_year.astype(float) < 1949)
& (sample_metadata.birth_year.astype(float) > 1919)
].shape[0]
sample_metadata.birth_year = sample_metadata.birth_year.replace(
{0: np.NaN}
)
sample_metadata.birth_year = sample_metadata.birth_year.astype(float)
age_average = 2019 - sample_metadata.birth_year.mean()
sample_metadata.bmi = sample_metadata.bmi.replace(
{"Not applicable": np.NaN}
)
sample_metadata.bmi = sample_metadata.bmi.replace({"Not provided": np.NaN})
sample_metadata.bmi = sample_metadata.bmi.replace({0: np.NaN})
sample_metadata.bmi = sample_metadata.bmi.astype(float)
sample_metadata.bmi.loc[(sample_metadata["bmi"] > 40)] = np.NaN
sample_metadata.bmi.loc[(sample_metadata["bmi"] < 13)] = np.NaN
bmi_average = sample_metadata.bmi.mean()
sample_metadata.height_cm = sample_metadata.height_cm.replace(
{"Not applicable": np.NaN}
)
sample_metadata.height_cm = sample_metadata.height_cm.replace(
{"Not provided": np.NaN}
)
sample_metadata.height_cm = sample_metadata.height_cm.replace({0: np.NaN})
sample_metadata.height_cm = sample_metadata.height_cm.astype(float)
sample_metadata.height_cm.loc[
(sample_metadata["height_cm"] > 220)
] = np.NaN
sample_metadata.height_cm.loc[
(sample_metadata["height_cm"] < 130)
] = np.NaN
height_average = sample_metadata.height_cm.mean()
dict = [
samples_with_dog,
samples_with_ibd,
samples_with_diabetes,
samples_with_cancer,
samples_with_college,
samples_older_70,
age_average,
]
return_display = pd.DataFrame(columns=["names", "values", "icon"])
return_display = return_display.append(
{"names": "Dogs", "values": samples_with_dog, "icon": "dog"},
ignore_index=True,
)
return_display = return_display.append(
{"names": "Cats", "values": samples_with_cat, "icon": "cat"},
ignore_index=True,
)
return_display = return_display.append(
{"names": "Cancer", "values": samples_with_cancer, "icon": "ribbon"},
ignore_index=True,
)
return_display = return_display.append(
{
"names": "Diabetes",
"values": samples_with_diabetes,
"icon": "circle",
},
ignore_index=True,
)
return_display = return_display.append(
{"names": "IBD", "values": samples_with_ibd, "icon": "ambulance"},
ignore_index=True,
)
return_display = return_display.append(
{
"names": "College degree",
"values": samples_with_college,
"icon": "graduation-cap",
},
ignore_index=True,
)
return_display = return_display.append(
{"names": "Average age", "values": age_average, "icon": "child"},
ignore_index=True,
)
return_display = return_display.append(
{"names": "Average BMI", "values": bmi_average, "icon": "weight"},
ignore_index=True,
)
return_display = return_display.append(
{
"names": "Average height (cm)",
"values": height_average,
"icon": "ruler-vertical",
},
ignore_index=True,
)
return_display = return_display.append(
{
"names": "Alcohol Consumption",
"values": samples_consume_alcohol,
"icon": "beer",
},
ignore_index=True,
)
return_display = return_display.append(
{
"names": "Cardiovascular disease",
"values": samples_with_cardio,
"icon": "heartbeat",
},
ignore_index=True,
)
return_display = return_display.append(
{"names": "Females", "values": samples_female, "icon": "female"},
ignore_index=True,
)
return_display = return_display.append(
{"names": "Smokers", "values": samples_who_smoke, "icon": "smoking"},
ignore_index=True,
)
return return_display
def firm_plot(samples, firmicutes, healthiest_sample):
"""
Returns a graph of the distribution of the data in a graph
==========
samples : pandas.DataFrame
The sample data frame. Must contain column `Bacteroidetes` and
`Firmicutes` that contain the percentage of those phyla.
Returns
=======
plotly graph
"""
hist_data = [samples["Firmicutes"]]
group_labels = ["Firmicutes"]
firm = ff.create_distplot(hist_data, group_labels, show_hist=False)
firm["layout"].update(title="Firmicutes Sample Distribution ")
firm["layout"].update(
showlegend=False,
annotations=[
dict(
x=firmicutes,
y=0,
xref="x",
yref="y",
text="You are here!",
showarrow=True,
arrowhead=2,
arrowsize=1,
arrowwidth=2,
arrowcolor="#0e0f36",
ax=70,
ay=-30,
bordercolor="#06a300",
borderwidth=2,
borderpad=4,
bgcolor="#69f564",
opacity=0.8,
),
dict(
x=healthiest_sample["Firmicutes"],
y=0,
xref="x",
yref="y",
text="Healthiest sample",
showarrow=True,
arrowhead=2,
arrowsize=1,
arrowwidth=2,
arrowcolor="#0e0f36",
ax=70,
ay=30,
bordercolor="#4c0acf",
borderwidth=2,
borderpad=4,
bgcolor="#b977f2",
opacity=0.8,
),
],
)
return firm
def bact_plot(samples, bacteroidetes, healthiest_sample):
"""
Returns a graph of the distribution of the data in a graph
==========
samples : pandas.DataFrame
The sample data frame. Must contain column `Bacteroidetes` and
`Firmicutes` that contain the percentage of those phyla.
Returns
=======
plotly graph
"""
import plotly.figure_factory as ff
hist_data = [samples["Bacteroidetes"]]
group_labels = ["Bacteroidetes"]
bact = ff.create_distplot(hist_data, group_labels, show_hist=False)
bact["layout"].update(title="Bacteroidetes Sample Distribution ")
bact["layout"].update(
showlegend=False,
annotations=[
dict(
x=bacteroidetes,
y=0,
xref="x",
yref="y",
text="You are here!",
showarrow=True,
arrowhead=2,
arrowsize=1,
arrowwidth=2,
arrowcolor="#0e0f36",
ax=70,
ay=-30,
bordercolor="#06a300",
borderwidth=2,
borderpad=4,
bgcolor="#69f564",
opacity=0.8,
),
dict(
x=healthiest_sample["Bacteroidetes"],
y=0,
xref="x",
yref="y",
text="Healthiest Sample",
showarrow=True,
arrowhead=2,
arrowsize=1,
arrowwidth=2,
arrowcolor="#0e0f36",
ax=70,
ay=30,
bordercolor="#4c0acf",
borderwidth=2,
borderpad=4,
bgcolor="#b977f2",
opacity=0.8,
),
],
)
return bact
def healthiest(samples, metadata):
"""
Return the average firmicutes and bacteroidites levels for the healthiest individuals in the metadata and standard deviation
Parameters
==========
samples : pandas.DataFrame
The sample data frame. Must contain column `Bacteroidetes` and
`Firmicutes` that contain the percentage of those phyla.
metadata : pandas.DataFrame
The DataFrame containing additional information for all samples,
Uses birth_year, alcohol frequency, alzheimer's, bmi, cardiovascular_disease, cancer, depression_bipolar_schizophrenia, diabetes,
ibd, ibs, kidney_disease, liver_disease, lung_disease, mental_illness, skin_condition
Returns
=======
list of two numbers
The bacteroidites and firmicutes ratios for the compiled healthiest individuals
"""
sample_metadata = pd.DataFrame()
metadata.index = metadata["sample_name"]
sample_metadata = metadata.loc[samples.index]
metadata_copy = sample_metadata.copy(deep=True)
metadata_copy = metadata_copy[
metadata_copy.cancer == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.alzheimers == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.cardiovascular_disease == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.diabetes == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.ibd == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.ibs == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.kidney_disease == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.liver_disease == "I do not have this condition"
]
metadata_copy = metadata_copy[
metadata_copy.lung_disease == "I do not have this condition"
]
metadata_copy = metadata_copy[metadata_copy.mental_illness == "false"]
metadata_copy = metadata_copy[
metadata_copy.skin_condition == "I do not have this condition"
]
metadata_copy.bmi = metadata_copy.bmi.replace({"Not applicable": 0})
metadata_copy.bmi = metadata_copy.bmi.replace({"Not provided": 0})
metadata_copy = metadata_copy[metadata_copy.bmi.astype(float) > 18.5]
metadata_copy = metadata_copy[metadata_copy.bmi.astype(float) < 25.0]
metadata_copy.birth_year = metadata_copy.birth_year.replace(
{"Not applicable": 0}
)
metadata_copy.birth_year = metadata_copy.birth_year.replace(
{"Not provided": 0}
)
metadata_copy = metadata_copy[
metadata_copy.birth_year.astype(float) > 1959
]
metadata_copy = metadata_copy[
metadata_copy.birth_year.astype(float) < 1999
]
id_list = metadata_copy["sample_name"].tolist()
healthiest_samples = samples.loc[id_list]
healthiest_sample = healthiest_samples.mean(axis=0)
return healthiest_sample
# Now we want to summarize the data on the phylum level and convert counts
# to percentages. We start by summarizing on the phylum level
phyla = pd.read_csv("data/phyla.csv", index_col=0)
# This is just the metadata
meta = pd.read_csv(
path.join("data", "metadata.tsv.gz"), dtype={"id": str}, sep="\t"
)
# As a last step we will load the PCoA coordinates generated in
# `beta_diversity.py`, select 1000 random individuals and merge the
# coordinates with the phyla abundances
red = pd.read_csv("pcoa.csv", index_col=0)
samples = red.sample(1000)
meta = meta[meta.sample_name.isin(samples.index)]
samples = pd.merge(samples, phyla, left_index=True, right_index=True)
healthiest_sample = healthiest(samples, meta)
firm_plot = firm_plot
bact_plot = bact_plot
# The App will now use the samples DataFrame