python networkx案例_Python networkx.NetworkXNoCycle方法代码示例

# 需要导入模块: 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值