<jupyter_start><jupyter_text>Data types and data structures - continued BIOINF 575 - Fall 2020 Typing is dynamic but strict in python (objects have type).The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.Numeric Types — int (subtype: bool), float, complex.Sequence Types — string, list, tuple, range.There are two major categories: mutable and immutable types.Immutable variables cannot be changed, while Mutable variables can be changed.Integer, Floats, Strings, Tuples, Boolean are immutable.List are mutable types.https://docs.python.org/3/library/stdtypes.html Check/display the type of a variable with the function type(). No automatic coercion of  types, explicit conversion onlyException: print() - converts to strings where possible.________ List/check the functions and attributes available for a data type or a variable dir(\)Functions preceeded by \_\_ (e.g. \_\_lt\_\_ - less than) are special internal functions used by python functions and operators.    Example:```pythondir(str)```    ________________      Sequence types: String, List, and Tuple - are iterable ________________ StringSequence of characters - immutable https://www.geeksforgeeks.org/python-strings/An Introduction to Programming for Bioscientists: A Python-Based Primer   https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1004867 Concatenation (+ operator)<jupyter_code>"Complex " + 'text'<jupyter_output><empty_output><jupyter_text>Repetition (* operator)<jupyter_code>"hi"*3<jupyter_output><empty_output><jupyter_text>Simple, double or triple quotes:<jupyter_code>'''
I am testing the 
triple quotes - a comment
'''
# this is a COMMENT
test_message =<jupyter_output><empty_output><jupyter_text>Common Sequence operationsOperation Description:x in sTrue if an item of s is equal to x, else Falsex not in sFalse if an item of s is equal to x, else Trues + tthe concatenation of s and ts * n or n * sequivalent to adding s to itself n timess[i]ith item of s, origin 0s[i:j]slice of s from i to js[i:j:k]slice of s from i to j with step klen(s)length of smin(s)smallest item of smax(s)largest item of ss.index(x[, i[, j]])index of the first occurrence of x in s (at or after index i and before index j)s.count(x)total number of occurrences of x in s<jupyter_code>#in

#subsetting - start (from 0 included): end (not included): step

"rep"*4
#min max ord chr - characters and strings (text) is compared using the alphabetical order

#len

#index 

#count<jupyter_output><empty_output><jupyter_text>Subsetting s[i:j:k]If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0.The slice of s from i to j is defined as the sequence of items with index k such that i If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.The slice of s from i to j with step k is defined as the sequence of items with index x = i + n\*k such that 0 In other words, the indices are i, i+k, i+2\*k, i+3\*k and so on, stopping when j is reached (but never including j). When k is positive, i and j are reduced to len(s) if they are greater. When k is negative, i and j are reduced to len(s) - 1 if they are greater. If i or j are omitted or None, they become “end” values (which end depends on the sign of k). Note, k cannot be zero. If k is None, it is treated like 1.https://docs.python.org/3/library/stdtypes.html<jupyter_code>#more subsetting<jupyter_output><empty_output><jupyter_text>Exercise Create the sequence "echo echo echo " by using the operator *. Find the position of the second e in "sequence of nucleotides".Retrieve the word "the" from "immunotherapy" using subsetting. _______________ Range - an immutable sequence of numbers It is commonly used for looping a specific number of times.  It is not generated until needed - lazy loading - generator type range(stop)range(start, stop[, step])<jupyter_code>dir(range)
list(range(10))
range(10)
list(range(5, 31, 5))<jupyter_output><empty_output><jupyter_text>__________ List - a collection of elements, allows duplicates, is orderred, and is mutable (changeable) A list may be constructed in several ways:Using a pair of square brackets to denote the empty list: []Using square brackets, separating items with commas: [a], [a, b, c]Using a list comprehension: [x for x in iterable]Using the type constructor: list() or list(iterable)<jupyter_code>dir(list)
patient_BMIs = [25, 18, 30, 16, 22, 32, 28]
patient_BMIs<jupyter_output><empty_output><jupyter_text>Size/length of a list - builtin len function Save the list in a different variable<jupyter_code>patient_BMIs_followup<jupyter_output><empty_output><jupyter_text>Retrieve elements from a list subsetting/slicing - [:] Negative indexing - retrieving elements from the end of the list [:] Change a value in a list patient_BMIs_followup and patient_BMIs are references to the same objectWhen the referred object changes the values display for both referring variables change. To make a copy of a list: copy_list = initial_list[:] Similarly we can do copy_list = list(initial_list)<jupyter_code>patient_BMIs_followup 

# Change a value


patient_BMIs<jupyter_output><empty_output><jupyter_text>To copy a list through subsetting works ... until it doesn't.When the list contains sublists.The copy module has a deepcopy funtion.<jupyter_code>#Add elements to a list - append function


#Order the elements of a list - sort function

# We can add different types of elements to a list - append funtion

# We can include the elements of another list into our list - extend function 


"""insert an element at a certain position
insert(position, element)"""


#Change list elements through subsetting and assignment - [:] = 


#Check if an element is in our list - the in operator

#Retrieve the position of an element in a list - index function


#Count the occurences of an element in a list - count function<jupyter_output><empty_output><jupyter_text>Remove list elements remove() removes the first matching value, not a specific index.<jupyter_code><jupyter_output><empty_output><jupyter_text>del command removes the item at a specific index.<jupyter_code><jupyter_output><empty_output><jupyter_text>pop() removes the item at a specific index and returns it.<jupyter_code>patient_BMIs<jupyter_output><empty_output><jupyter_text>clear() removes all the elements of a list Exercise Create a list of sublists of 4 organisms, 4 genes and 4 values (10,20,30,10)Check if human is in the organism list.Find the index of value 10 in the values list.Count how many times 10 occurs in gene. String join() - makes a string outof the elements of a list using a given string as a separator/delimiter<jupyter_code>",".join(["Make","a","sentence."])<jupyter_output><empty_output><jupyter_text>String split() - always creates a list - splits a string by a given separator/delimiter<jupyter_code>"Get the, words".split(",")<jupyter_output><empty_output><jupyter_text>____________ Tuple - a collection of elements, allows duplicates, is orderred, and is unchangeableFaster than lists. Tuples may be constructed in a number of ways:Using a pair of parentheses to denote the empty tuple: ()Using a trailing comma for a singleton tuple: a, or (a,)Separating items with commas: a, b, c or (a, b, c)Using the tuple() built-in: tuple() or tuple(iterable)<jupyter_code>dir(tuple)
(2,)
gene = ("Gene1", "This is an important gene", 10, 200.5)
gene[0]
gene[-1]
# Unpacking a tuple assign each element of a tuple to a variable.

gene_symbol, gene_description, exon_number, expression = gene

print(gene_symbol)
print(gene_description)
print(exon_number)
print(expression)
len(gene)<jupyter_output><empty_output><jupyter_text>Tuple specifics, easy switch of values.<jupyter_code>a, b = 3, 4
print(a)
print(b)
a, b = b, a
print(a)
print(b)<jupyter_output><empty_output><jupyter_text>Exercise Check if value 10 is in gene.Find the index of value 10 in gene.Count how many times 10 occurs in gene. Concatenating immutable sequences always results in a new objectThis means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below:* if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete* if concatenating tuple objects, extend a list insteadSome sequence types (such as range) only support item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition.The index() funtion raises ValueError when x is not found in shttps://docs.python.org/3/library/stdtypes.html __________________ Dictionaries - a collection of key:value pairs, is unordered, has no duplicates, is changeable, and is indexed. The mapping type Dictionaries can be created by placing a comma-separated list of key: value pairs within braces, or by the dict constructor dict(**kwarg)dict(mapping, **kwarg)dict(iterable, **kwarg)Dictionaries are unorderred<jupyter_code>dir(dict)
d1 = {"Id1": "Description of Gene1", "Id2": "Description of Gene2"}
d1
d2 = {"Id2": "Description of Gene2", "Id1": "Description of Gene1"}
d2
d1 == d2
# Check if key in dictionary
"Id1" in d1
'''
Length of dictionary
'''
print(len(d1))<jupyter_output><empty_output><jupyter_text>Retrieving a dictionary value.<jupyter_code>d1["Id1"]
d1.get("Id1")<jupyter_output><empty_output><jupyter_text>Add element<jupyter_code>d1["Id4"] = "Description of Gene3"
d1["Id0"] = "Description of Gene4"
# update if key exists, add a new element if it does not
d1.update({"Id5":"description"})
d1
d1.update({"Id5":"Description of Gene5"})
d1.values()
d1.keys()
d1.items()<jupyter_output><empty_output><jupyter_text>Removing elements is done using: del(), pop(), popitem() or clear().It is done using the key of an element.popitem() remove the last element.<jupyter_code>d1.popitem()<jupyter_output><empty_output><jupyter_text>Exercise Remove the element with Id2 from the dictionary _______________ Sets - a collection of elements, is unordered, has no duplicates, can be changeable or unchangeable. A set object is an unordered collection of distinct objects.A set is mutable, unless it is a frozenset.To create a set use braces to separate set elements or the set([iterable]) constructor.Elements cannot be changed/updated, but can be added and removed.The update() function add multiple elements.<jupyter_code>dir(set)
model_organisms = {"human", "mouse", "rat", "fruit fly", "worm", "E coli"}
model_organisms
"human" in model_organisms
model_organisms.add("yeast")
model_organisms
model_organisms.update(["zebrafish","frog"])
model_organisms
# raises exception if element does not exist

model_organisms.remove("rat")
model_organisms
# does not raise exception if element does not exist

model_organisms.discard("rat")
model_organisms
model_organisms.discard("frog")
model_organisms
set1 = {"human", "mouse", "rat", "fruit fly", "worm", "E coli"}
set2 = {"rat, zebrafish", "frog", "yeast", "worm"}
set3 = set1.union(set2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0883448642895157, 'fraction_numerical': 0.011797055171190349, 'mean_word_length': 5.5628637951105935, 'pattern_counts': {'":': 6, '<': 89, '<?xml version=': 0, '>': 89, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '114931', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3281, 'n_words': 1416, 'path': 'class_notebooks/datatypes_cont_clean.ipynb', 'repo_name': 'mitreac/b57520', 'size': '11274'}