-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblemSet2_sol.py
More file actions
407 lines (339 loc) · 12.4 KB
/
ProblemSet2_sol.py
File metadata and controls
407 lines (339 loc) · 12.4 KB
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
# %% [md]
# # Problem Set 2
#
# Questions are based on tutorials 2 and 3.
#
# ## Set up
# %%
import pandas as pd
import numpy as np
import scipy
from numpy import random
species = ['E.coli', 'C.acnes', 'K.pneumoniae', 'H.sapiens']
n = 100
a = np.random.uniform(0,1,n) * 0.2
b = a * 3 + np.random.uniform(0,1,n) * 0.5
c = np.random.uniform(0,1,n) * 0.2
d = c * 0.5 + np.random.uniform(0,1,n) * 0.1
absolute_abundance = np.array([a,b,c,d])
relative_abundance = absolute_abundance / absolute_abundance.sum(axis=0)
# %% [markdown]
# # Futher reading
# 1. [List comprehension](https://www.pythonforbeginners.com/basics/list-comprehensions-in-python#:~:text=List%20comprehensions%20provide%20a%20concise,be%20anything%2C%20meaning%20you%20can)
# 1. [Breaking a loop](https://www.digitalocean.com/community/tutorials/how-to-use-break-continu# %% [md]
# # Problem Set 2
#
# Questions are based on tutorials 2 and 3.
#
# ## Set up
# %%
import pandas as pd
import numpy as np
import scipy
from numpy import random
species = ['E.coli', 'C.acnes', 'K.pneumoniae', 'H.sapiens']
n = 100
a = np.random.uniform(0,1,n) * 0.2
b = a * 3 + np.random.uniform(0,1,n) * 0.5
c = np.random.uniform(0,1,n) * 0.2
d = c * 0.5 + np.random.uniform(0,1,n) * 0.1
absolute_abundance = np.array([a,b,c,d])
relative_abundance = absolute_abundance / absolute_abundance.sum(axis=0)
# %% [markdown]
# # Futher reading
# 1. [List comprehension](https://www.pythonforbeginners.com/basics/list-comprehensions-in-python#:~:text=List%20comprehensions%20provide%20a%20concise,be%20anything%2C%20meaning%20you%20can)
# 1. [Breaking a loop](https://www.digitalocean.com/community/tutorials/how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3)
# 1. Check out this [guide](https://www.geeksforgeeks.org/check-multiple-conditions-in-if-statement-python/) to compare multiple conditions.
# 1. [Alpha diversity](https://en.wikipedia.org/wiki/Diversity_index)
# 1. Read this page on how to loop through columns instead of rows of a 2D numpy array
# %% [markdown]
# # Questions
# %%
# 1. Loop through samples to print the relative abundance of K.pneumoniae
relative_abundance.T # Tranposes the data, i.e. flip rows to columns
for sample in relative_abundance.T:
print(sample[2])
# %%
# 1. If sample 1's *E.coli's* relative abundance is greater than 0.5, print('E.coli's rel abd is greater than 0.5')
for sample in relative_abundance.T:
if sample[0] > 0.5:
print("E.coli's rel abd is greater than 0.5")
# Try greater than 0.2
for sample_index,sample in enumerate(relative_abundance.T):
if sample[0] > 0.2:
print("Sample {}'s E.coli's rel abd is {:.3f}, which is greater than 0.2".format(sample_index, sample[0]))
# %%
# 1. If sample 2's *E.coli's* relative abundance is lower than 0.5 AND *H.sapiens'* rel abd is lower than 0.3, print('Both conditions are satisfied')
for sample in relative_abundance.T:
if sample[0] < 0.5 and sample[3] < 0.3:
print('Both conditions are satisfied')
# %%
# 1. Find all samples where *H.sapiens* relative abundance is greater than 0.2.
sample_list = []
for sample in relative_abundance.T:
if sample[3] > 0.2:
sample_list.append(sample)
sample_list = np.array(sample_list)
# %%
# 1. Find the ratio of *E.coli* / *C.acnes* for the first 10 samples.
ratio = []
for sample in relative_abundance.T[0:10]:
ratio.append(sample[0]/sample[1])
# %%
# 1. Print rel abd of *C.acnes* starting from sample 0 until *C.acnes* > 0.3
# Method 1
count = 0
while relative_abundance.T[count,1] <= 0.3:
print(relative_abundance[count, 1])
count += 1
# Method 2
for sample in relative_abundance.T:
if sample[1] > 0.3:
break
print(sample[1])
# %%
# 1. Find geometric mean of all the samples
def geometric_mean(input):
cumulative_product = 1
for i in input:
assert i > 0
cumulative_product = i*cumulative_product
gm = cumulative_product**(1/len(input))
return gm
gmean_list = []
for sample in relative_abundance.T:
gmean_list.append(geometric_mean(sample))
gmean_list
# %%
# 1. Remove *H.sapiens* and rebalance the rest of the abundances so that they sum to 1
# Method 1
new_absolute_abundance = relative_abundance[1:]
new_relative_abundance = new_absolute_abundance / new_absolute_abundance.sum(axis=0)
# Method 2
new_relative_abundance = []
for sample in relative_abundance.T:
new_sample = sample[1:]
new_sample = new_sample/new_sample.sum()
new_relative_abundance.append(new_sample)
new_relative_abundance = np.array(new_relative_abundance)
# %%
# 1. Find the alpha diversity of each sample using the Shannon index
def shannon(input):
sum_plogp = 0
for p in input:
assert p > 0
sum_plogp += -p*np.log(p)
return np.array(sum_plogp)
shannon_list = []
for sample in relative_abundance.T:
shannon_list.append(shannon(sample))
np.array(shannon_list)
# %%
# 1. Find the alpha diversity of each sample using the Simpson index
def simpson(input):
sum_pp = 0
for p in input:
assert p > 0
sum_pp += p*p
return np.array(sum_pp)
simpson_list = []
for sample in relative_abundance.T:
simpson_list.append(simpson(sample))
np.array(simpson_list)
# %%
# 1. Use list comprehension to obtain a list of the squares of the first 10 positive integers.
list_first_10_int = [i**1 for i in range(1,11)]
# %%
# 1. Write a Python function that that prints out the first n rows of Pascal's triangle.
# Expected output for 6 rows:
# ```
# [1]
# [1, 1]
# [1, 2, 1]
# [1, 3, 3, 1]
# [1, 4, 6, 4, 1]
# [1, 5, 10, 10, 5, 1]
# ```
def pascal(n):
pascal_row = [1]
for i in range(n):
print(pascal_row)
old_pascal_row = list(pascal_row)
pascal_row = np.array([0] + old_pascal_row) + np.array(old_pascal_row + [0])
pascal(6)
# %%
# 1. Given a nucleotide sequence:
# ATGCAAGCGGATATGCATGGAAAACTTCACGCTGCCTTAGAAGATGGTTTCTTCCTCTTTTTTTGAACAGCAACAACAACCTAACATTTATTATGACACAACCACCGATCAAGAAGAC
#
# Write a function count_substring(dna, substring), to count how many times a certain substring appears in the string of nucleotides.
# For example, the function returns 6 when called with the substring 'CAA'and the nucleotide string above.
dna = 'ATGCAAGCGGATATGCATGGAAAACTTCACGCTGCCTTAGAAGATGGTTTCTTCCTCTTTTTTTGAACAGCAACAACAACCTAACATTTATTATGACACAACCACCGATCAAGAAGAC'
substring = 'CAA'
def count_substring(dna, substring):
count = 0
for i in range(len(dna)-len(substring)):
if dna[i:i+len(substring)] == substring:
count += 1
return count
count_substring(dna, substring)
# %%
# 1. Write a Python function that returns a list of boolean where the Shannon index > Simpson index in the relative abundance data
#
# Expected output format:
# ```
# [True, False, True, True, True, False...]
# ```
def check_shannon_simpson(relative_abundance):
boolean_list = []
for sample in relative_abundance.T:
boolean_list.append(shannon(sample) > simpson(sample))
print('{:.3f}, {:.3f}'.format(shannon(sample), simpson(sample)))
return boolean_list
check_shannon_simpson(relative_abundance)
e-and-pass-statements-when-working-with-loops-in-python-3)
# 1. Check out this [guide](https://www.geeksforgeeks.org/check-multiple-conditions-in-if-statement-python/) to compare multiple conditions.
# 1. [Alpha diversity](https://en.wikipedia.org/wiki/Diversity_index)
# 1. Read this page on how to loop through columns instead of rows of a 2D numpy array
# %% [markdown]
# # Questions
# %%
# 1. Loop through samples to print the relative abundance of K.pneumoniae
for sample in relative_abundance.T:
print(sample[2])
# %%
# 1. If sample 1's *E.coli's* relative abundance is greater than 0.5, print('E.coli's rel abd is greater than 0.5')
for sample in relative_abundance.T:
if sample[0] > 0.5:
print("E.coli's rel abd is greater than 0.5")
# Try greater than 0.2
for sample_index,sample in enumerate(relative_abundance.T):
if sample[0] > 0.2:
print("Sample {}'s E.coli's rel abd is {:.3f}, which is greater than 0.2".format(sample_index, sample[0]))
# %%
# 1. If sample 2's *E.coli's* relative abundance is lower than 0.5 AND *H.sapiens'* rel abd is lower than 0.3, print('Both conditions are satisfied')
for sample in relative_abundance.T:
if sample[0] < 0.5 and sample[3] < 0.3:
print('Both conditions are satisfied')
# %%
# 1. Find all samples where *H.sapiens* relative abundance is greater than 0.2.
sample_list = []
for sample in relative_abundance.T:
if sample[3] > 0.2:
sample_list.append(sample)
sample_list = np.array(sample_list)
# %%
# 1. Find the ratio of *E.coli* / *C.acnes* for the first 10 samples.
ratio = []
for sample in relative_abundance.T[0:10]:
ratio.append(sample[0]/sample[1])
# %%
# 1. Print rel abd of *C.acnes* starting from sample 0 until *C.acnes* > 0.3
# Method 1
count = 0
while relative_abundance.T[count,1] <= 0.3:
print(relative_abundance[count, 1])
count += 1
# Method 2
for sample in relative_abundance.T:
if sample[1] > 0.3:
break
print(sample[1])
# %%
# 1. Find geometric mean of all the samples
def geometric_mean(input):
cumulative_product = 1
for i in input:
assert i > 0
cumulative_product = i*cumulative_product
gm = cumulative_product**(1/len(input))
return gm
gmean_list = []
for sample in relative_abundance.T:
gmean_list.append(geometric_mean(sample))
gmean_list
# %%
# 1. Remove *H.sapiens* and rebalance the rest of the abundances so that they sum to 1
# Method 1
new_absolute_abundance = relative_abundance[1:]
new_relative_abundance = new_absolute_abundance / new_absolute_abundance.sum(axis=0)
# Method 2
new_relative_abundance = []
for sample in relative_abundance.T:
new_sample = sample[1:]
new_sample = new_sample/new_sample.sum()
new_relative_abundance.append(new_sample)
new_relative_abundance = np.array(new_relative_abundance)
# %%
# 1. Find the alpha diversity of each sample using the Shannon index
def shannon(input):
sum_plogp = 0
for p in input:
assert p > 0
sum_plogp += -p*np.log(p)
return np.array(sum_plogp)
shannon_list = []
for sample in relative_abundance.T:
shannon_list.append(shannon(sample))
np.array(shannon_list)
# %%
# 1. Find the alpha diversity of each sample using the Simpson index
def simpson(input):
sum_pp = 0
for p in input:
assert p > 0
sum_pp += p*p
return np.array(sum_pp)
simpson_list = []
for sample in relative_abundance.T:
simpson_list.append(simpson(sample))
np.array(simpson_list)
# %%
# 1. Use list comprehension to obtain a list of the squares of the first 10 positive integers.
list_first_10_int = [i**1 for i in range(1,11)]
# %%
# 1. Write a Python function that that prints out the first n rows of Pascal's triangle.
# Expected output for 6 rows:
# ```
# [1]
# [1, 1]
# [1, 2, 1]
# [1, 3, 3, 1]
# [1, 4, 6, 4, 1]
# [1, 5, 10, 10, 5, 1]
# ```
def pascal(n):
pascal_row = [1]
for i in range(n):
print(pascal_row)
old_pascal_row = list(pascal_row)
pascal_row = np.array([0] + old_pascal_row) + np.array(old_pascal_row + [0])
pascal(6)
# %%
# 1. Given a nucleotide sequence:
# ATGCAAGCGGATATGCATGGAAAACTTCACGCTGCCTTAGAAGATGGTTTCTTCCTCTTTTTTTGAACAGCAACAACAACCTAACATTTATTATGACACAACCACCGATCAAGAAGAC
#
# Write a function count_substring(dna, substring), to count how many times a certain substring appears in the string of nucleotides.
# For example, the function returns 6 when called with the substring 'CAA'and the nucleotide string above.
dna = 'ATGCAAGCGGATATGCATGGAAAACTTCACGCTGCCTTAGAAGATGGTTTCTTCCTCTTTTTTTGAACAGCAACAACAACCTAACATTTATTATGACACAACCACCGATCAAGAAGAC'
substring = 'CAA'
def count_substring(dna, substring):
count = 0
for i in range(len(dna)-len(substring)):
if dna[i:i+len(substring)] == substring:
count += 1
return count
count_substring(dna, substring)
# %%
# 1. Write a Python function that returns a list of boolean where the Shannon index > Simpson index in the relative abundance data
#
# Expected output format:
# ```
# [True, False, True, True, True, False...]
# ```
def check_shannon_simpson(relative_abundance):
boolean_list = []
for sample in relative_abundance.T:
boolean_list.append(shannon(sample) > simpson(sample))
print('{:.3f}, {:.3f}'.format(shannon(sample), simpson(sample)))
return boolean_list
check_shannon_simpson(relative_abundance)