Пример #1
0
def test_add_edge_effect_with_weight():
    graph = Graph()
    end = graph.add_node('coffee')
    start = graph.add_node('muffin')
    graph.add_edge(start, end, 44)
    expected = (end, 44)
    actual = graph._adjacency_list[start][0]
    assert actual == expected
Пример #2
0
def test_breadth_first():
    g = Graph()
    v1 = g.add_node('Pandora')
    v2 = g.add_node('Mordor')
    g.add_nondirectional_edge(v1, v2, 110)
    expected = ['Pandora', 'Mordor']
    actual = g.breadth_first(v1)
    assert expected == actual
Пример #3
0
def test_get_nodes():
    graph = Graph()
    graph.add_node('coffee')
    graph.add_node('muffin')
    Vertex('loner')
    expected = 2
    actual = len(graph.get_nodes())
    assert actual == expected
Пример #4
0
def test_get_neighbors():
    graph = Graph()
    end = graph.add_node('coffee')
    start = graph.add_node('muffin')
    graph.add_edge(start, end, 44)
    neighbors = graph.get_neighbors(start)
    assert len(neighbors) == 1
    assert neighbors[0][0].value == 'coffee'
    assert isinstance(neighbors[0][0], Vertex)
    assert neighbors[0][1] == 44
Пример #5
0
def erdos_renyi(n, p):
	g = Graph()

	for i in xrange(n):
		g.add_node(DiscreteVariable([0,1], name=str(i)))

	for i,j in itertools.product(xrange(n), xrange(n)):
		if random.random() < p:
			g.add_edge(g.get_node_by_name(str(i)),g.get_node_by_name(str(j)))
	return g
Пример #6
0
def erdos_renyi(n, p):
    g = Graph()

    for i in xrange(n):
        g.add_node(DiscreteVariable([0, 1], name=str(i)))

    for i, j in itertools.product(xrange(n), xrange(n)):
        if random.random() < p:
            g.add_edge(g.get_node_by_name(str(i)), g.get_node_by_name(str(j)))
    return g
Пример #7
0
def test_add_edge_interloper_start():
    graph = Graph()

    start = Vertex('start')

    end = graph.add_node('end')

    with pytest.raises(KeyError):
        graph.add_edge(start, end)
Пример #8
0
def test_depth_first_3():
    g = Graph()
    v1 = g.add_node('A')
    v2 = g.add_node('B')
    v3 = g.add_node('C')
    v4 = g.add_node('G')
    v5 = g.add_node('D')
    v6 = g.add_node('E')
    v7 = g.add_node('F')
    v8 = g.add_node('H')

    g.add_nondirectional_edge(v1, v2, 110)
    g.add_nondirectional_edge(v1, v5, 43)
    g.add_nondirectional_edge(v2, v3, 50)
    g.add_nondirectional_edge(v2, v5, 12)
    g.add_nondirectional_edge(v3, v4, 75)
    g.add_nondirectional_edge(v5, v6, 21)
    g.add_nondirectional_edge(v5, v8, 29)
    g.add_nondirectional_edge(v5, v7, 23)
    g.add_nondirectional_edge(v7, v8, 14)

    actual = g.depth_first(v5)
    expected = ['D', 'A', 'B', 'C', 'G', 'E', 'H', 'F']
    assert actual == expected
Пример #9
0
def test_get_edges_one_vertex():
    g = Graph()
    v1 = g.add_node('Pandora')
    v2 = g.add_node('Mordor')
    v3 = g.add_node('Monstropolis')
    v4 = g.add_node('Metroville')
    v5 = g.add_node('Naboo')
    v6 = g.add_node('Narnia')

    g.add_nondirectional_edge(v1, v2, 110)
    g.add_nondirectional_edge(v2, v3, 50)
    g.add_nondirectional_edge(v2, v4, 12)
    g.add_nondirectional_edge(v3, v5, 43)
    g.add_nondirectional_edge(v4, v5, 23)
    g.add_nondirectional_edge(v4, v6, 14)
    g.add_nondirectional_edge(v5, v6, 21)

    actual = g.get_edge(['Mordor'])
    expected = (True, '$0')
    assert actual == expected
Пример #10
0
from graphs import Graph

def dfs_visit(graph, source, parent, finished):
    for u in graph.adj_list(source):
        if u not in parent:
            parent[u] = source
            dfs_visit(graph, u, parent, finished)
    finished.append(source)

def dfs(graph):
    parent = {}
    finished = []
    vertices = graph.vertices()
    for v in vertices:
        if v not in parent:
            parent[v] = None
            dfs_visit(graph, v, parent, finished)
    finished = list(reversed(finished))
    return parent, finished
    
if __name__ == '__main__':
    g = Graph()
    for key in [1, 2, 3, 4]:
        g.add_node(key)
    g.add_edge(1, 2)
    g.add_edge(2, 3)
    g.add_edge(3, 4)
    g.add_edge(4, 1)
    print(dfs(g))
Пример #11
0
    queue.append(start)
    
    # do BFS
    while(len(queue) > 0):
        next_node = queue.pop(0)
        
        for neighbor in graph.get_edges(next_node):
            if neighbor not in visited:
                queue.append(neighbor)
        
        visited.append(next_node)
        
    # print results
    return {"reachable_nodes": visited}
    
# test
if __name__ == "__main__":
    my_graph = Graph()
    
    node1 = Node(10, 1)
    node2 = Node(13, 2)
    node3 = Node(20, 3)
    
    my_graph.add_node(node1)
    my_graph.add_edge((node2, node3))
    my_graph.add_edge((node3, node1))
    
    my_graph.print_nodes()
    my_graph.print_edges()
    
    print breadth_first_search(my_graph, node2)
Пример #12
0
def test_add_edge_works():
    graph = Graph()
    start = graph.add_node('start')
    end = graph.add_node('end')
    graph.add_edge(start, end)
Пример #13
0
def test_add_node():
    graph = Graph()
    actual = graph.add_node('testing').value
    expected = 'testing'
    assert actual == expected
Пример #14
0
def test_size():
    graph = Graph()
    graph.add_node('testing')
    expected = 1
    actual = graph.size()
    assert actual == expected
Пример #15
0
def get_graph(my_orfs):
	G = Graph(directed=True)
	pgap = my_orfs.pstop

	for orf in my_orfs.iter_orfs():
		if(orf.frame > 0):
			source = Node('CDS', 'start', orf.frame, orf.start)
			target = Node('CDS', 'stop', orf.frame, orf.stop)
		else:
			source = Node('CDS', 'stop', orf.frame, orf.stop)
			target = Node('CDS', 'start', orf.frame, orf.start)
		G.add_edge(Edge(source, target, orf.weight))
	#-------------------------------Check for long noncoding regions that would break the path---------#
	bases = [None] * my_orfs.contig_length
	for orfs in my_orfs.iter_in():
		for orf in orfs:
			mi = min(orf.start, orf.stop)
			ma = max(orf.start, orf.stop)
			for n in range(mi, min(ma, my_orfs.contig_length-1)):
				try:
					bases[n] = n
				except:
					sys.stderr.write("error in breaking region"+str(n))
			break
	last = 0
	for base in bases:
		if(base):
			if(base-last > 300):
				for right_node in G.iternodes():
					for left_node in G.iternodes():
						l = left_node.position
						r = right_node.position
						if(last+1 >= l > last-300 and base-1 <= r < base+300):
							if(left_node.frame*right_node.frame > 0):
								if(left_node.type == 'stop' and right_node.type =='start' and left_node.frame > 0):
									score = score_gap(r-l-3, 'same', pgap)
									G.add_edge(Edge(left_node, right_node, score))	
								elif(left_node.type == 'start' and right_node.type =='stop' and left_node.frame < 0):
									score = score_gap(r-l-3, 'same', pgap)
									G.add_edge(Edge(left_node, right_node, score ))	
							else:
								if(left_node.type == 'stop' and right_node.type =='stop' and left_node.frame > 0):
									score = score_gap(r-l-3, 'diff', pgap)
									G.add_edge(Edge(left_node, right_node, score ))	
								elif(left_node.type == 'start' and right_node.type =='start' and left_node.frame < 0):
									score = score_gap(r-l-3, 'diff',pgap)
									G.add_edge(Edge(left_node, right_node, score ))	
			last = base
	#-------------------------------Add in tRNA data---------------------------------------------------#
	
	add_trnas(my_orfs, G)
	
	#-------------------------------Connect the open reading frames to each other----------------------#
	for right_node in G.iternodes():
		r = right_node.position
		if(right_node.gene == 'CDS'):
			r_other = my_orfs.other_end[r]
		else:
			r_other = my_orfs.other_end['t'+str(r)]
		for left_node in G.iternodes():
			l = left_node.position
			if(left_node.gene == 'CDS'):
				l_other = my_orfs.other_end[l]
			else:
				l_other = my_orfs.other_end['t'+str(l)]
			if(0 < r-l < 300):
				if(l in my_orfs and my_orfs.other_end[l] in my_orfs[l]):
					o1 = my_orfs.get_orf(my_orfs.other_end[l], l).pstop
				elif(l in my_orfs):
					o1 = my_orfs.get_orf(l, my_orfs.other_end[l]).pstop
				else:
					o1 = pgap
				if(r in my_orfs and my_orfs.other_end[r] in my_orfs[r]):
					o2 = my_orfs.get_orf(my_orfs.other_end[r], r).pstop
				elif(r in my_orfs):
					o2 = my_orfs.get_orf(r, my_orfs.other_end[r]).pstop
				else:
					o2 = pgap
				pstop = ave([o1, o2])

				#trna
				if(left_node.gene == 'tRNA' or right_node.gene == 'tRNA'):
					if(left_node.frame*right_node.frame > 0 and left_node.type != right_node.type):
						if(left_node.frame > 0 and left_node.type == 'stop') or (left_node.frame < 0 and left_node.type == 'start'):
							if not G.has_edge(Edge(left_node, right_node, 1)):
								score = score_gap(r-l-3, 'same', pgap)
								G.add_edge(Edge(left_node, right_node, score ))	

					elif(left_node.frame*right_node.frame < 0 and left_node.type == right_node.type):
						if(left_node.frame > 0 and left_node.type == 'stop') or (left_node.frame < 0 and left_node.type == 'start'):
							if not G.has_edge(Edge(left_node, right_node, 1)):
								score = score_gap(r-l-3, 'same', pgap)
								G.add_edge(Edge(left_node, right_node, score ))	
				# same directions
				elif(left_node.frame*right_node.frame > 0):
					if(left_node.type == 'stop' and right_node.type =='start'):
						if(left_node.frame > 0):
							score = score_gap(r-l-3, 'same', pgap)
							G.add_edge(Edge(left_node, right_node, score ))	
						else:
							if(left_node.frame != right_node.frame):
								if(r < l_other and r_other < l):
									score = score_overlap(r-l+3, 'same', pstop)
									G.add_edge(Edge(right_node, left_node, score ))	
					if(left_node.type == 'start' and right_node.type =='stop'):
						if(left_node.frame > 0):
							if(left_node.frame != right_node.frame):
								if(r < l_other and r_other < l):
									score = score_overlap(r-l+3, 'same', pstop)
									G.add_edge(Edge(right_node, left_node, score ))	
						else:
							score = score_gap(r-l-3, 'same', pgap)
							G.add_edge(Edge(left_node, right_node, score ))	
				# different directions
				else:
					if(left_node.type == 'stop' and right_node.type =='stop'):
						if(right_node.frame > 0):
							if(r_other+3 < l and r < l_other):
								score = score_overlap(r-l+3, 'diff', pstop)
								G.add_edge(Edge(right_node, left_node, score ))	
						else:
							score = score_gap(r-l-3, 'diff', pgap)
							G.add_edge(Edge(left_node, right_node, score ))	
					if(left_node.type == 'start' and right_node.type =='start'):
						if(right_node.frame > 0 and r-l > 2):
							score = score_gap(r-l-3, 'diff', pgap)
							G.add_edge(Edge(left_node, right_node, score ))	
						elif(right_node.frame < 0):
							#print(r_other, l, r, l_other)
							if(r_other < l and r < l_other):
								score = score_overlap(r-l+3, 'diff', pstop)
								G.add_edge(Edge(right_node, left_node, score ))	
	#-------------------------------Connect open reading frames at both ends to a start and stop-------#
	source = Node('source', 'source', 0, 0)
	target = Node('target', 'target', 0, my_orfs.contig_length+1)
	G.add_node(source)
	G.add_node(target)
	for node in G.iternodes():
		if(node.position <= 2000):
			if( (node.type == 'start' and node.frame > 0) or (node.type =='stop' and node.frame < 0) ):
				score = score_gap(node.position, 'same', pgap)
				G.add_edge(Edge(source, node, score))
		if(my_orfs.contig_length - node.position <= 2000):
			if( (node.type == 'start' and node.frame < 0) or (node.type =='stop' and node.frame > 0) ):
				score = score_gap(my_orfs.contig_length-node.position, 'same', pgap)
				G.add_edge(Edge(node, target, score))

	return G