Skip to content

Commit 5e55fc1

Browse files
committed
chore(enumerate.py): limit pathways length to RP2 max depth
1 parent 97a9e96 commit 5e55fc1

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

rp2paths/enumerate.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,41 @@ def check_limits(nb_paths, max_paths, depth, max_depth, logger = logging.getLogg
7979
limit_reached = True
8080
return limit_reached, return_value
8181

82+
def find_max_shortest_path(
83+
start_cmpd,
84+
substrates2reactions,
85+
reaction2products,
86+
logger=logging.getLogger(__name__),
87+
):
88+
"""
89+
Find the maximum from amongst all the shortest path lengths from
90+
start_cmpd to each compound.
91+
"""
92+
shortest_lengths = {}
93+
sub = start_cmpd
94+
shortest_lengths[sub] = 0
95+
for iter in range(1000): # arbitrary large number to avoid infinite loops
96+
if sub not in substrates2reactions:
97+
continue
98+
for rxn in substrates2reactions[sub]:
99+
products = reaction2products[rxn]
100+
for product in products:
101+
current_length = shortest_lengths.get(product, float('inf'))
102+
new_length = shortest_lengths.get(sub, 0) + 1
103+
if new_length < current_length:
104+
shortest_lengths[product] = new_length
105+
# Update sub to the next compound to explore
106+
next_subs = [p for rxn in substrates2reactions.get(sub, []) for p in reaction2products[rxn]]
107+
if not next_subs:
108+
break
109+
sub = next_subs[0] # just pick one to continue
110+
111+
# Finally extract the maximum shortest path lengths
112+
max_shortest_length = max(shortest_lengths.values(), default=0)
113+
logger.debug(f"Maximum shortest path length from {start_cmpd} is {max_shortest_length}")
114+
115+
return max_shortest_length
116+
82117
def enumerate_longest_paths(
83118
start_cmpd,
84119
substrates2reactions,
@@ -94,6 +129,17 @@ def enumerate_longest_paths(
94129
"""
95130
all_paths = []
96131

132+
# Limit pathways length to max. depth of RetroPath2.0 or less
133+
depth_rp2 = find_max_shortest_path(
134+
start_cmpd,
135+
substrates2reactions,
136+
reaction2products,
137+
logger=logger
138+
)
139+
if max_depth == 0 or max_depth > depth_rp2:
140+
max_depth = depth_rp2
141+
logger.info(f"Setting maximum depth to {max_depth} based on RetroPath2.0 analysis.")
142+
97143
def dfs(current_cmpd, current_path, depth, consumed_cmpds):
98144
"""
99145
Depth-first search to enumerate all longest paths from the current compound.

0 commit comments

Comments
 (0)