Collection_Data_Types.rst 8.38 KB
 Bertrand NÉRON committed Jul 14, 2014 1 2 3 4 ``````.. sectnum:: :start: 4 `````` Bertrand NÉRON committed Jul 12, 2014 5 6 7 8 9 ``````.. _Collection_Data_types: ********************* Collection Data Types ********************* `````` Bertrand NÉRON committed Jul 14, 2014 10 11 12 13 `````` Exercices ========= `````` Bertrand NÉRON committed Jul 17, 2014 14 15 16 ``````Exercice -------- `````` Bertrand NÉRON committed Jul 18, 2014 17 18 ``````| Draw the representation in memory of the following expressions. | what is the data type of each object? `````` Bertrand NÉRON committed Jul 17, 2014 19 20 21 22 23 24 25 26 `````` :: x = [1, 2, 3, 4] y = x[1] y = 3.14 x[1] = 'foo' `````` Bertrand NÉRON committed Jul 18, 2014 27 28 29 30 31 ``````.. figure:: _static/figs/list_1.png :width: 400px :alt: set :figclass: align-center `````` Bertrand NÉRON committed Jul 17, 2014 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 `````` Exercice -------- wihout using python shell, what is the results of the following statements: .. note:: sum is a function which return the sum of each elements of a list. :: x = [1, 2, 3, 4] x[3] = -4 # what is the value of x now ? y = sum(x)/len(x) #what is the value of y ? why ? y = 0 because sum(x) is an integer, len(x) is also an integer so in python2.x the result is an integer, all the digits after the periods are discarded. In python3 we will obtain the expected result (see :ref:``) Exercice -------- How to compute safely the average of a list? :: float(sum(l))/float(len(l) exercise -------- `````` Bertrand NÉRON committed Jul 18, 2014 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 ``````Draw the representation in memory of the following expressions. :: x = [1, ['a','b','c'], 3, 4] y = x[1] y[2] = 'z' # what is the value of x ? .. figure:: _static/figs/list_2-1.png :width: 400px :alt: set :figclass: align-center .. container:: clearer .. image :: _static/figs/spacer.png When we execute *y = x[1]*, we create ``y`` wich reference the list ``['a', 'b', 'c']``. This list has 2 references on it: ``y`` and ``x[1]`` . .. figure:: _static/figs/list_2-2.png :width: 400px :alt: set :figclass: align-center .. container:: clearer .. image :: _static/figs/spacer.png This object is a list so it is a mutable object. So we can access **and** modify it by the two ways ``y`` or ``x[1]`` :: x = [1, ['a','b','z'], 3, 4] exercise -------- `````` Bertrand NÉRON committed Jul 17, 2014 105 106 107 108 109 110 111 112 113 ``````generate a list containing all codons. :: codons = [] for a in 'acgt': for b in 'acgt': for c in 'acgt': codon = a + b + c codons.append(codon) `````` Bertrand NÉRON committed Jul 14, 2014 114 115 116 117 118 119 120 121 122 123 124 125 126 127 ``````exercice -------- From a list return a new list without any duplicate, regardless of the order of items. For example: :: >>> l = [5,2,3,2,2,3,5,1] >>> uniqify(l) >>> [1,2,3,5] #is one of the solutions solution :: >>> list(set(l)) `````` Bertrand NÉRON committed Jul 17, 2014 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 `````` exercice -------- let the following enzymes collection: :: import collections RestrictEnzyme = collections.namedtuple("RestrictEnzyme", "name comment sequence cut end") ecor1 = RestrictEnzyme("EcoRI", "Ecoli restriction enzime I", "gaattc", 1, "sticky") ecor5 = RestrictEnzyme("EcoRV", "Ecoli restriction enzime V", "gatatc", 3, "blunt") bamh1 = RestrictEnzyme("BamHI", "type II restriction endonuclease from Bacillus amyloliquefaciens ", "ggatcc", 1, "sticky") hind3 = RestrictEnzyme("HindIII", "type II site-specific nuclease from Haemophilus influenzae", "aagctt", 1 , "sticky") taq1 = RestrictEnzyme("TaqI", "Thermus aquaticus", "tcga", 1 , "sticky") not1 = RestrictEnzyme("NotI", "Nocardia otitidis", "gcggccgc", 2 , "sticky") sau3a1 = RestrictEnzyme("Sau3aI", "Staphylococcus aureus", "gatc", 0 , "sticky") hae3 = RestrictEnzyme("HaeIII", "Haemophilus aegyptius", "ggcc", 2 , "blunt") sma1 = RestrictEnzyme("SmaI", "Serratia marcescens", "cccggg", 3 , "blunt") and the 2 dna fragments: :: dna_1 = """tcgcgcaacgtcgcctacatctcaagattcagcgccgagatccccgggggttgagcgatccccgtcagttggcgtgaattcag cagcagcgcaccccgggcgtagaattccagttgcagataatagctgatttagttaacttggatcacagaagcttccaga ccaccgtatggatcccaacgcactgttacggatccaattcgtacgtttggggtgatttgattcccgctgcctgccagg""" dna_2 = """gagcatgagcggaattctgcatagcgcaagaatgcggccgcttagagcgatgctgccctaaactctatgcagcgggcgtgagg attcagtggcttcagaattcctcccgggagaagctgaatagtgaaacgattgaggtgttgtggtgaaccgagtaag agcagcttaaatcggagagaattccatttactggccagggtaagagttttggtaaatatatagtgatatctggcttg""" | which enzymes cut the dna_1 ? | the dna_2 ? | the dna_1 but not the dna_2? :: dna_1 = dna_1.replace('\n', '') dans_2 = dna_2.replace('\n', '') enzymes = [ecor1, ecor5, bamh1, hind3, taq1, not1, sau3a1, hae3, sma1] digest_1 = [] for enz in enzymes: pos = dna_1.find(enz.sequence) if pos != -1: digest_1.append(enz) with this first algorithm we find if an enzyme cut the dna but we cannot find all cuts in the dna for an enzyme. If we find a cutting site, we must search again starting at the first nucleotid after the begining of the match until the end of the the dna, for this we use the start parameter of the find function, and so on. As we don't know how many loop we need to scan the dna until the end we use a ``while`` loop testing for the presence of a cutting site.:: digest_1 = [] for enz in enzymes: pos = dna_1.find(enz.sequence) while pos != -1: digest_1.append(enz) pos = dna_1.find(enz.sequence, pos + 1) digest_2 = [] for enz in enzymes: pos = dna_2.find(enz.sequence) while pos != -1: digest_2.append(enz) pos = dna_2.find(enz.sequence, pos + 1) cut_dna_1 = set(digest_1) cut_dna_2 = set(digest_2) cut_dna_1_not_dna_2 = cut_dna_1 - cut_dna_2 If we want also the position, for instance to compute the fragments of dna. :: digest_1 = [] for enz in enzymes: pos = dna_1.find(enz.sequence) while pos != -1: digest_1.append((enz, pos)) pos = dna_1.find(enz.sequence, pos + 1) from operator import itemgetter digest_1.sort(key=itemgetter(1)) [(e.name, d) for e, d in digest_1] digest_2 = [] for enz in enzymes: pos = dna_2.find(enz.sequence) while pos != -1: digest_2.append((enz, pos)) pos = dna_2.find(enz.sequence, pos + 1) cut_dna_1 = set([e.name for e in digest_1]) cut_dna_2 = set([e.name for e in digest_2]) cut_dna_1_not_dna_2 = cut_dna_1 - cut_dna_2 `````` Bertrand NÉRON committed Jul 14, 2014 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 `````` exercice -------- From a list return a new list without any duplicate, but keeping the order of items. For example: :: >>> l = [5,2,3,2,2,3,5,1] >>> uniqify_with_order(l) >>> [5,2,3,1] solution :: >>> uniq = [] >>> for item in l: >>> if item not in uniq: >>> uniq.append(item) solution :: >>> uniq_items = set() >>> l_uniq = [x for x in l if x not in uniq_items and not uniq_items.add(x)] exercice -------- list and count occurences of every 3mers in the following sequence :: s = """gtcagaccttcctcctcagaagctcacagaaaaacacgctttctgaaagattccacactcaatgccaaaatataccacag gaaaattttgcaaggctcacggatttccagtgcaccactggctaaccaagtaggagcacctcttctactgccatgaaagg aaaccttcaaaccctaccactgagccattaactaccatcctgtttaagatctgaaaaacatgaagactgtattgctcctg atttgtcttctaggatctgctttcaccactccaaccgatccattgaactaccaatttggggcccatggacagaaaactgc agagaagcataaatatactcattctgaaatgccagaggaagagaacacagggtttgtaaacaaaggtgatgtgctgtctg gccacaggaccataaaagcagaggtaccggtactggatacacagaaggatgagccctgggcttccagaagacaaggacaa ggtgatggtgagcatcaaacaaaaaacagcctgaggagcattaacttccttactctgcacagtaatccagggttggcttc tgataaccaggaaagcaactctggcagcagcagggaacagcacagctctgagcaccaccagcccaggaggcacaggaaac acggcaacatggctggccagtgggctctgagaggagaaagtccagtggatgctcttggtctggttcgtgagcgcaacaca""" and finally print the results one 3mer and it's occurence per line. write first the pseudocode, then implement it. bonus: print the kmer by incresing occurences. solution :: s = s.replace('\n', '') kmers = {} for i in range(len(s) - 3): kmer = s[i:i+3] kmers[kmer] = kmers.get(kmer, 0) + 1 for kmer, occurence in kmers.items(): print kmer, " = ", occurence solution bonus :: list_of_kmers = kmers.items() from operator import itemgetter list_of_kmers.sort(key=itemgetter(1)) for kmer, occurence in list_of_kmers: print kmer, " = ", occurence solution bonus :: list_of_kmers = kmers.items() list_of_kmers.sort(key = lambda kmer: kmer[1]) for kmer, occurence in list_of_kmers: print kmer, " = ", occurence exercice -------- given the following dict : :: d = {1 : 'a', 2 : 'b', 3 : 'c' , 4 : 'd'} We want obtain a new dict with the keys and the values inverted so we will obtain: :: inverted_d {'a': 1, 'c': 3, 'b': 2, 'd': 4} solution :: inverted_d = {} for key in d.keys(): inverted_d[d[key]] = key solution :: inverted_d = {v : k for k, v in d.items()}``````