# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import NetworkXNoCycle [as 别名]
def construct_graph_from_nucleotides(
nucleotides: List[Nucleotide],
incoming_nucleotides: Optional[List[Nucleotide]] = None
) -> nx.DiGraph:
"""
Construct graph from nucleotides
Parameters
----------
nucleotides
nucleotides
incoming_nucleotides
incoming nucleotides, which will be added to nucleotides to construct
the graph, but their connections to their inbound nodes will be
ignored
Returns
-------
graph
graph with nucleotides as vertices
Raises
------
ValueError
if graph contains loops
"""
graph = nx.DiGraph()
incoming_nucleotides = incoming_nucleotides or []
all_nucleotides = nucleotides + incoming_nucleotides
all_nucleotides_dict = {each_nucleotide.name: each_nucleotide
for each_nucleotide in all_nucleotides}
for each_nucleotide in nucleotides:
inbound_nodes = each_nucleotide.inbound_nodes
if not inbound_nodes:
graph.add_node(each_nucleotide)
nucleotide_name = each_nucleotide.name
for each_in_node_name in inbound_nodes:
u_node = _get_incoming_node(all_nucleotides_dict, each_in_node_name)
v_node = all_nucleotides_dict[nucleotide_name]
graph.add_edge(u_node, v_node)
try:
cycles = nx.find_cycle(graph)
raise ValueError("Cycles in the DNA helix graph were found! "
"({})".format(cycles))
except nx.NetworkXNoCycle:
pass
return graph