// `node1` should be the first node of a supernode // `node0` should be the previous node // `next_base` is the last base of `node1` // `jmpfunc` is called with each supernode traversed and if it returns true // we continue crawling, otherwise we stop // `endfunc` is a function called at the end of traversal void graph_crawler_fetch(GraphCrawler *crawler, dBNode node0, dBNode next_nodes[4], size_t take_idx, size_t num_next, uint32_t *cols, size_t ncols, bool (*jmpfunc)(GraphCache *_cache, GCacheStep *_step, void *_arg), void (*endfunc)(GraphCache *_cache, uint32_t _pathid, void *_arg), void *arg) { const dBGraph *db_graph = crawler->cache.db_graph; GraphCache *cache = &crawler->cache; GraphWalker *wlk = &crawler->wlk; RepeatWalker *rptwlk = &crawler->rptwlk; GCUniColPath *unipaths = crawler->unicol_paths; ctx_assert(take_idx < num_next); ctx_assert(!db_nodes_are_equal(node0, next_nodes[take_idx])); // Fetch all paths in all colours dBNode node1 = next_nodes[take_idx]; bool is_fork; size_t i, c, col, nedges_cols, num_unicol_paths = 0; int pathid; for(c = 0; c < ncols; c++) { col = (cols != NULL ? cols[c] : c); if(db_node_has_col(db_graph, node0.key, col) && db_node_has_col(db_graph, node1.key, col)) { // Determine if this fork is a fork in the current colour for(nedges_cols = 0, i = 0; i < num_next && nedges_cols <= 1; i++) nedges_cols += db_node_has_col(db_graph, next_nodes[i].key, col); is_fork = (nedges_cols > 1); graph_walker_setup(wlk, true, col, col, db_graph); graph_walker_start(wlk, node0); graph_walker_force(wlk, node1, is_fork); pathid = graph_crawler_load_path(cache, node1, wlk, rptwlk, jmpfunc, arg); if(endfunc != NULL) endfunc(cache, pathid, arg); graph_walker_finish(wlk); graph_crawler_reset_rpt_walker(rptwlk, cache, pathid); unipaths[num_unicol_paths++] = (GCUniColPath){.colour = col, .pathid = pathid}; } else pathid = -1; crawler->col_paths[col] = pathid; }
// Check we can walk along a set of nodes through the graph // If @allow_extend is true, traverse past the end of the buffer and add nodes static inline int confirm_seq(size_t startidx, bool allow_extend, GraphWalker *wlk, RepeatWalker *rpt, dBNodeBuffer *nbuf, size_t colour, const dBGraph *db_graph) { ctx_assert(startidx < nbuf->len); size_t i, init_len = nbuf->len; graph_walker_setup(wlk, true, colour, colour, db_graph); graph_walker_start(wlk, nbuf->b[startidx]); for(i = startidx+1; graph_walker_next(wlk); i++) { if(!rpt_walker_attempt_traverse(rpt, wlk)) { reset(wlk,rpt,nbuf); return CONFIRM_REPEAT; } if(i < init_len) { if(!db_nodes_are_equal(nbuf->b[i], wlk->node)) { reset(wlk,rpt,nbuf); return CONFIRM_WRONG; } } else { db_node_buf_add(nbuf, wlk->node); if(!allow_extend) { reset(wlk,rpt,nbuf); nbuf->len--; // Remove node we added return CONFIRM_OVERSHOT; } } } // printf("stopped %zu / %zu %zu\n", i, init_len, nbuf->len); reset(wlk,rpt,nbuf); return i < init_len ? CONFIRM_SHORT : CONFIRM_SUCCESS; }
// `fork_node` is a node with outdegree > 1 void find_bubbles(BubbleCaller *caller, dBNode fork_node) { graph_cache_reset(&caller->cache); const dBGraph *db_graph = caller->db_graph; GraphCache *cache = &caller->cache; GraphWalker *wlk = &caller->wlk; RepeatWalker *rptwlk = &caller->rptwlk; // char tmpstr[MAX_KMER_SIZE+3]; // db_node_to_str(db_graph, fork_node, tmpstr); // status("Calling from %s", tmpstr); dBNode nodes[4]; Nucleotide bases[4]; size_t i, num_next, num_edges_in_col; BinaryKmer fork_bkmer = db_node_get_bkmer(db_graph, fork_node.key); num_next = db_graph_next_nodes(db_graph, fork_bkmer, fork_node.orient, db_node_edges(db_graph, fork_node.key, 0), nodes, bases); // loop over alleles, then colours Colour colour, colours_loaded = db_graph->num_of_cols; bool node_has_col[4]; uint32_t pathid; for(colour = 0; colour < colours_loaded; colour++) { if(!db_node_has_col(db_graph, fork_node.key, colour)) continue; // Determine if this fork is a fork in the current colour num_edges_in_col = 0; for(i = 0; i < num_next; i++) { node_has_col[i] = (db_node_has_col(db_graph, nodes[i].key, colour) > 0); num_edges_in_col += node_has_col[i]; } graph_walker_setup(wlk, true, colour, colour, db_graph); for(i = 0; i < num_next; i++) { if(node_has_col[i]) { graph_walker_start(wlk, fork_node); graph_walker_force(wlk, nodes[i], num_edges_in_col > 1); pathid = graph_crawler_load_path_limit(cache, nodes[i], wlk, rptwlk, caller->prefs.max_allele_len); graph_walker_finish(wlk); graph_crawler_reset_rpt_walker(rptwlk, cache, pathid); } } } // Set up 5p flank caller->flank5p.b[0] = db_node_reverse(fork_node); caller->flank5p.len = 0; // set to one to signify we haven't fetched flank yet }
static inline int test_statement_node(dBNode node, ExpABCWorker *wrkr) { const dBGraph *db_graph = wrkr->db_graph; dBNodeBuffer *nbuf = &wrkr->nbuf; GraphWalker *wlk = &wrkr->gwlk; RepeatWalker *rpt = &wrkr->rptwlk; size_t b_idx, col = wrkr->colour; // rpt_walker_clear(rpt); db_node_buf_reset(nbuf); db_node_buf_add(nbuf, node); // size_t AB_limit = wrkr->prime_AB ? SIZE_MAX : wrkr->max_AB_dist; size_t walk_limit = wrkr->max_AB_dist; // status("walk_limit: %zu", walk_limit); // Walk from B to find A graph_walker_setup(wlk, true, col, col, db_graph); graph_walker_start(wlk, nbuf->b[0]); while(graph_walker_next(wlk) && nbuf->len < walk_limit) { if(!rpt_walker_attempt_traverse(rpt, wlk)) { reset(wlk,rpt,nbuf); return RES_LOST_IN_RPT; } db_node_buf_add(nbuf, wlk->node); } reset(wlk,rpt,nbuf); if(nbuf->len == 1) return RES_NO_TRAVERSAL; // Traverse A->B db_nodes_reverse_complement(nbuf->b, nbuf->len); b_idx = nbuf->len - 1; if(wrkr->prime_AB) { // Prime A->B without attempting to cross graph_walker_prime(wlk, nbuf->b, nbuf->len, nbuf->len, true); while(graph_walker_next(wlk)) { if(!rpt_walker_attempt_traverse(rpt, wlk)) { reset(wlk,rpt,nbuf); return RES_LOST_IN_RPT; } db_node_buf_add(nbuf, wlk->node); } } else { // Attempt to traverse A->B then extend past B int r = confirm_seq(0, true, wlk, rpt, nbuf, col, db_graph); switch(r) { case CONFIRM_REPEAT: return RES_LOST_IN_RPT; case CONFIRM_OVERSHOT: ctx_assert2(0,"Can't 'overshoot' when extending"); case CONFIRM_WRONG: return RES_AB_WRONG; case CONFIRM_SHORT: if(wrkr->print_failed_contigs) print_failed(node, nbuf, db_graph, true, wrkr->prime_AB); wrkr->ab_fail_state[wlk->last_step.status]++; return RES_AB_FAILED; } } reset(wlk,rpt,nbuf); if(nbuf->len == b_idx+1) return RES_NO_TRAVERSAL; // Couldn't get past B // Last node is now C // Walk from B... record whether or not we reach C ctx_assert(db_nodes_are_equal(nbuf->b[b_idx], db_node_reverse(node))); int r = confirm_seq(b_idx, false, wlk, rpt, nbuf, col, db_graph); switch(r) { case CONFIRM_REPEAT: return RES_LOST_IN_RPT; case CONFIRM_OVERSHOT: return RES_BC_OVERSHOT; case CONFIRM_WRONG: return RES_BC_WRONG; case CONFIRM_SHORT: if(wrkr->print_failed_contigs) print_failed(node, nbuf, db_graph, false, wrkr->prime_AB); wrkr->bc_fail_state[wlk->last_step.status]++; return RES_BC_FAILED; case CONFIRM_SUCCESS: return RES_ABC_SUCCESS; } die("Shouldn't reach here: r=%i", r); return -1; }