responses = {}

responses

type(responses)

"""
Explanation: Python dictionaries and graphs
Humberto Ortiz-Zuazaga
I want to show you one way of analyzing sequences that has become increasingly important since next-generation sequencing came to the fore. Many sequence analysis algorithms are based on the notion of graphs. In this context, a graph is a set of verticies (sometimes called nodes) and a set of edges (sometimes called arcs) that connect two nodes.
In order to work with graphs, I will introduce python dictionaries or dict. A data structure native to python that lets us build more complex data structures. Like lists, dictionaries make our life easier.
Dictionaries
We can create an empty dictionarty using curly braces {}
End of explanation
"""


responses["hello"] = "world"

responses

"""
Explanation: A dictionary can store values for a key. In this example, we will store the value "world", at the key "hello".
End of explanation
"""


responses["hola"] = "mundo"

responses

def greet(salutation):
    try:
        print(salutation, responses[salutation])
    except KeyError:
        print("Sorry, don't know how to respond to", salutation)

greet("hello")

greet("hola")

"""
Explanation: One nice property of dicts is that they can store more key/value pairs.
End of explanation
"""


greet("你好")

"""
Explanation: What happens if you ask for an unknown key?
End of explanation
"""


responses["你好"] =  "世界"

greet("你好")

"""
Explanation: We can update the dict, and ask again:
End of explanation
"""


counts = {}

sequence = "acggtattcggt"

for base in sequence:
    if not base in counts:
        counts[base] = 1
    else:
        counts[base] += 1

counts

"""
Explanation: More tricks with dicts
Keys and values don't have to be strings, many other python data types can be used. For example, we can rewrite some of our counting loops to use a dict instead of variables to count nucleotides
End of explanation
"""


k = 3

kmercounts = {}

for i in range(len(sequence) - k + 1):
    kmer = sequence[i:i+k]
    if not kmer in kmercounts:
        kmercounts[kmer] = 1
    else:
        kmercounts[kmer] += 1

kmercounts

"""
Explanation: Since keys can be any string, we can use a similar loop to count triplet (or codon) frequency
End of explanation
"""


from IPython.core.display import SVG, display

display(SVG(filename='fig-graph.svg'))

graph = {}

graph

graph[1] = [2, 3, 4]

graph

graph[2] = [1, 3]

graph[3] = [1, 2]

graph[4] = [1]

graph

"""
Explanation: Using dicts to build graphs
We can use dicts to build data structures to represent graphs. I want to build a graph with 4 nodes, labeled 1, 2, 3, and 4, with edges between 1-2, 1-3, 1-4, and 2-3 as shown in Figure 1. We can use a dict, with the nodes as keys, and the list of nodes connected to the key as the value.
End of explanation
"""


for node in graph:
    print(node, graph[node])

"""
Explanation: Now we can loop over the nodes, and extract the edges.
End of explanation
"""


