In [2]:
import random
Given the list of proteins (proteins), below, create a random graph. Each str in the list proteins refers to a single protein. There are 8 proteins in the list, and therefore $8^2$ possible interactions among them (proteins can interact with themselves).
Each time you evaluate a pair of proteins, call the interaction() function (defined below) to see whether there is an interaction between them. If so, print the names of the interacting proteins. Otherwise, move on.
Hint: you can print several variables on one line using commas. E.g.
print prot1, '----', prot2
In [6]:
proteins = ['Grf', 'Odi', 'Jon', 'Ner', 'Poo', 'Bin', 'Her', 'Liz']
def interaction(criterion=0.25):
"""
Decide whether or not an interaction occurs.
Parameters
----------
criterion : float
Probability of interaction, 0.0 < p < 1.0. (default: 0.25)
Returns
-------
bool
"""
return random.random() < criterion
If you havent already, turn your code from E.1 into a function that accepts a list of proteins, and a probability of interaction. When you call the interaction() function, you should pass that probability along as a parameter.
Hint: the =0.25 in the means that the default value is 0.25. In other words, if you call interaction() without any parameters, criterion will be set to 0.25. If you do pass a parameter, e.g. interaction(0.1), then criterion will be 0.1.
In the exercises above, you used the function interaction() to decide whether or not two proteins interacted with each other. That function returned True around 25% of the time. In E.1.2 you allowed the probability of interaction to be modified.
Now, modify your function from E.1.2 to accept a custom decision function (interaction, above) as a parameter! Then write your own version of interaction that only draws an edge if two proteins share at least one character in their names. Hint: you might want to convert names to lowercase before comparing them.
A network or graph is a collection of nodes (points, vertices) and edges (relations, links). You'll sometimes see people write things like this:
$ G = (V, E) $
which is just a silly way of saying what I just explained. $V$ are the nodes, $E$ are the edges.
A node can be anything. In a social network, nodes represent people. In a computer network, nodes might be various kinds of computers. In a gene regulatory network, nodes represent genes or gene-products.
Edges represent relations between nodes. Depending on the research context, those relations could be just about anything. They could represent the fact that two people know each other, that two machines are connected by cables, or that one gene turns another gene off. The Big Thing That Matters here is that each edge has two nodes. In a directed graph, where relations flow in one direction (e.g. gene A downregulates gene B), we call these two nodes the source and the target. Undirected graphs are graphs in which where relations are symmetric (e.g. two people know each other).
It is typical to assign attributes to edges, especially "weights". This might represent, for example, how many times one person sends another person an email in a week.
Knowing all of that, and recalling notebook 2. Objects and types, design three classes that represent a directed graph: you should write a Node class, an Edge class, and a Graph class. Think about what attributes they should have (e.g. Edges should have weights).
In [ ]: