def determineAtomGraphPruning(g, ag, sroot, target, prunedGraphSize): for i in range(1, len(ag.atomTypes[target]) + 1): g.addEdge("%s-%s" % (target, i), target, 1.0) g.addEdge(target, "%s-%s" % (target, i), 10000) (sd, sp) = dijkstra(g, sroot) (td, tp) = dijkstra(g, target) for i in range(1, len(ag.atomTypes[target]) + 1): g.removeEdge("%s-%s" % (target, i), target) g.removeEdge(target, "%s-%s" % (target, i)) revDists = {} removals = set() for u in g.V: removals.add(u) if u in sd and u in td: d = sd[u] + td[u] if d in revDists: revDists[d].append(u) else: revDists[d] = [u] rdists = revDists.keys() rdists.sort() count = 0 ind = 0 inclusions = set() cutval = None for d in rdists: for u in revDists[d]: ind += 1 if not cutval: removals.remove(u) if ind < prunedGraphSize: pass else: cutval = sd[u] + td[u] elif sd[u] + td[u] < cutval: removals.remove(u) else: pass print "%d in removals" % (len(removals)) return removals
def doFindPathsAndIntermediates(g, tdir, net, sources, unsatisfied, target, ag, pairScores, npaths, tracedSourceAtoms, reactionDirConstraints, tracedAtoms): tempfn = "%s/temp_ksp%d.txt" % (tdir, random.randint(10**5, 10**6)) if npaths == 1: (ddist, dpaths) = dijkstra(g, SROOT, target) node = target path = [] while node in dpaths: path.insert(0, node) node = dpaths[node] path.insert(0, node) paths = [path] acyclics, cyclics = 1, 0 else: if ALGORITHM_KSP == "REA": # REA was utilized to check amount of cyclic paths in preliminary tests cyclicpaths = rea.REAksp(g, npaths, SROOT, target) elif ALGORITHM_KSP == "SimpleYen": cyclicpaths = kspyen.kspSimpleYen(g, npaths, SROOT, target) else: print "Unknown algorithm: " % (ALGORITHM_KSP) assert(0) paths = kspyen.removeCyclicPaths(cyclicpaths) acyclics, cyclics = len(paths), len(cyclicpaths) - len(paths) print "%d/%d acyclic paths" % (len(paths), len(cyclicpaths)) results = [] if paths == None: return results, 0, 0 for path in paths: if len(path) < 4: # This should not happen but if it happens, lets not abort print "Warning: short path (assertion failed):", path continue # Collect the set of atom nodes which need to be # covered by subsequent path queries intermediates = set() # Traverse path backwards from each unsatisfied atom, # check which atoms cannot be traced back to sources for u in unsatisfied: tgt, ta = u if ag.atomTypes[tgt][ta] not in tracedAtoms: continue pathend = path[-2].split("-")[0] if tgt != pathend: intermediates.add((tgt, ta)) continue newIntermediates = determineRequiredIntermediates(path, ta, ag, net) intermediates.update(newIntermediates) # Extract rpairs from path and return them as result res = set() for i in range(3, len(path) - 1, 2): rpair, stuff = path[i].split("_") dir = stuff.split("-")[0] == "f" # True: rpair in forward direction res.add((rpair, dir)) results.append((res, intermediates)) return results, acyclics, cyclics