-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTutorial3.py
More file actions
232 lines (149 loc) · 5.22 KB
/
Tutorial3.py
File metadata and controls
232 lines (149 loc) · 5.22 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
#!/usr/bin/env python
# coding: utf-8
# # Functions in python
#
# Functions help to organize a script into modular chunks
#
# Functions are especially useful for repetitive, complex tasks
#
#
# ## Basic syntax
#
# def function_name(parameters):
# statement(s)
# #commonly print or return something
#
#
# What is the difference between the print and return commands in functions?
#
def absolute_value(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
print(num)
else:
print(-num)
absolute_value(2)
absolute_value(-4)
test=absolute_value(2)
type(test)
def absolute_value(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
return num
else:
return -num
absolute_value(2)
absolute_value(-4)
test=absolute_value(2)
type(test)
# Function to count the number of bases in a string of nucleotides
def base_count(dna, base):
i = 0
for c in dna:
if c == base:
i += 1
return i
result=base_count("AATTAGCCTTA", "A")
print(result)
#More sophisticated printing?
print('%s appears %d times in %s' % (base, result, dna)) #Why does this break? Due to variable scoping.
# Function to count the number of bases in a string of nucleotides
def base_count(dna, base):
i = 0
for c in dna:
if c == base:
i += 1
result=i
#More sophisticated printing
print('%s appears %d times in %s' % (base, result, dna))
print('{} appears {} times in {}'.format(base, result, dna))
print(f'{base} appears {result} times in {dna}') #using fstrings
base_count("AATTAGCCTTA", "A")
# Function to count the number of times each unique base appears in a string of nucleotides
def base_count_v2(dna):
all_freq = {}
for i in dna:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print(all_freq)
base_count_v2("AATTAGCCTTA")
# ## How to test if my function is working as intended?
#
# Positive controls, defined input should give expected output (e.g. assert in python)
# Printing variables and messages throughout function body can help in debugging
#
#Model random mutations in a string of nucleotides
import random
random.seed(a=1000)
def mutate(dna):
dna_list = list(dna)
mutation_site = random.randint(0, len(dna_list) - 1)
dna_list[mutation_site] = random.choice(list("ATCG"))
return "".join(dna_list)
dna = "ACGGAGATTTCGGTATGCAT"
print("Starting DNA:", dna)
print(base_count_v2(dna))
nmutations = 10000
for i in range(nmutations):
dna = mutate(dna)
print("DNA after %d mutations:" % nmutations, dna)
print(base_count_v2(dna))
# # List and dictionary comprehensions
#
# Compact way of creating lists and dictionaries
##making a list by typing everything out is impractical
my_list=[1,2,3,4,5,6,6,8,9,10,11,12,13,14,15]
#a list comprehension is more compact
my_list2=[i for i in range(1,16,1)]
print(my_list)
print(my_list2)
#Alternating even and odd labels
my_list3 = ["Even" if i%2==0 else "Odd" for i in range(10)]
print(my_list3)
##Dictionary comprehensions
#Minimal syntax for dictionary comprehensions
#dictionary = {key: value for vars in iterable}
#Recall Dictionaries are data types in Python which allows us to store data in key/value pairs.
my_dict = {"Adenine":'A', "Thymine":'T', "Guanine":'G', "Cytosine":'C', "Uracil":"U"}
print(my_dict)
#Dictionary comprehensions allow us to generate new dictionaries in a compact manner
my_dict2 = {k: ('purine' if v in ("A","G") else 'pyrimidine') for k,v in my_dict.items()}
print(my_dict2)
#An example of generating a dictionary matching each letter in the English alphabet to a number
import string
numbers=[i for i in range(1,27)]
#Python's zip() function creates an iterator that will aggregate elements from two or more iterables.
object= zip(numbers, string.ascii_uppercase)
my_dict3 = {k:v for k,v in object}
print(my_dict3)
# ## Exercises
#
#
# Ex 1)
# Use list comprehension to obtain a list of the squares of the first 10 positive integers.
#
# Ex 2)
# 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]
#
#
# Ex 3)
# 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.
#
#
#