// Traverse from node0 -> node1 static void traverse_5pflank(BreakpointCaller *caller, GraphCrawler *crawler, dBNode node0, dBNode node1) { const dBGraph *db_graph = crawler->cache.db_graph; dBNode next_nodes[4]; Nucleotide next_nucs[4]; size_t i, num_next; BinaryKmer bkmer0 = db_node_get_bkmer(db_graph, node0.key); num_next = db_graph_next_nodes(db_graph, bkmer0, node0.orient, db_node_edges(db_graph, node0.key, 0), next_nodes, next_nucs); // Find index of previous node for(i = 0; i < num_next && !db_nodes_are_equal(next_nodes[i],node1); i++) {} ctx_assert(i < num_next && db_nodes_are_equal(next_nodes[i],node1)); kmer_run_buf_reset(&caller->koruns_5p); kmer_run_buf_reset(&caller->koruns_5p_ended); kmer_run_buf_reset(&caller->flank5p_run_buf); // Go backwards to get 5p flank // NULL means loop from 0..(ncols-1) graph_crawler_fetch(crawler, node0, next_nodes, next_nucs, i, num_next, NULL, db_graph->num_of_cols, gcrawler_flank5p_stop_at_ref_covg, gcrawler_flank5p_finish_ref_covg, caller); }
/** * @param right_edge is true iff we this kmer is the last in a unitig */ static inline void _print_edge(hkey_t node, bool right_edge, BinaryKmer bkey, Edges edges, UnitigEnd uend0, UnitigPrinter *p) { // DOT: leave from east end if +, west end if - // connect to west end if +, east end if - const char dot_exit[2] = "ew", dot_join[2] = "we", gfa_orient[2] = "+-"; size_t i, n; dBNode next_nodes[4]; Nucleotide next_nucs[4]; Orientation orient = right_edge ? uend0.rorient : !uend0.lorient; // Unitig orientations Orientation ut_or0 = right_edge ? FORWARD : REVERSE, ut_or1; n = db_graph_next_nodes(p->db_graph, bkey, orient, edges, next_nodes, next_nucs); for(i = 0; i < n; i++) { UnitigEnd uend1 = p->ugraph.unitig_ends[next_nodes[i].key]; char tmpstr[100]; db_node_to_str(p->db_graph, next_nodes[i], tmpstr); if(!uend1.assigned) status(" -> node %zu [%s]", uend1.unitigid, tmpstr); ctx_assert(next_nodes[i].key != HASH_NOT_FOUND); ctx_assert(uend1.assigned); ut_or1 = next_nodes[i].orient == uend1.lorient ? FORWARD : REVERSE; // Don't do reverse-to-reverse links when node links to itself, // these are duplicates of forward-to-forward if(node < next_nodes[i].key || (node == next_nodes[i].key && ut_or0 + ut_or1 < 2)) { pthread_mutex_lock(&p->outlock); switch(p->syntax) { case PRINT_DOT: fprintf(p->fout, " node%zu:%c -> node%zu:%c\n", (size_t)uend0.unitigid, dot_exit[ut_or0], (size_t)uend1.unitigid, dot_join[ut_or1]); break; case PRINT_GFA: fprintf(p->fout, "L\tnode%zu\t%c\tnode%zu\t%c\t%zuM\n", (size_t)uend0.unitigid, gfa_orient[ut_or0], (size_t)uend1.unitigid, gfa_orient[ut_or1], p->db_graph->kmer_size - 1); break; default: die("Bad syntax: %i", p->syntax); } pthread_mutex_unlock(&p->outlock); } } }
// Edges restricted to this colour, only in one direction (node.orient) Edges db_node_edges_in_col(dBNode node, size_t col, const dBGraph *db_graph) { if(db_graph->node_in_cols == NULL && db_graph->col_covgs == NULL) { Edges edges = db_node_get_edges(db_graph, node.key, col); return edges_mask_orientation(edges, node.orient); } // Edges are merged into one colour ctx_assert(db_graph->num_edge_cols == 1); ctx_assert(db_graph->node_in_cols != NULL || db_graph->col_covgs != NULL); Edges edges = db_node_get_edges(db_graph, node.key, 0); // Check which next nodes are in the given colour BinaryKmer bkmer = db_node_get_bkmer(db_graph, node.key); dBNode nodes[4]; Nucleotide nucs[4]; size_t i, n; n = db_graph_next_nodes(db_graph, bkmer, node.orient, edges, nodes, nucs); edges = 0; if(db_graph->node_in_cols != NULL) { for(i = 0; i < n; i++) if(db_node_has_col(db_graph, nodes[i].key, col)) edges = edges_set_edge(edges, nucs[i], node.orient); } else if(db_graph->col_covgs != NULL) { for(i = 0; i < n; i++) if(db_node_col_covg(db_graph, nodes[i].key, col) > 0) edges = edges_set_edge(edges, nucs[i], node.orient); } else ctx_assert(0); return edges; }
void test_graph_crawler() { test_status("Testing graph crawler..."); // Construct 1 colour graph with kmer-size=11 dBGraph graph; const size_t kmer_size = 11, ncols = 3; db_graph_alloc(&graph, kmer_size, ncols, 1, 2048, DBG_ALLOC_EDGES | DBG_ALLOC_NODE_IN_COL | DBG_ALLOC_BKTLOCKS); char graphseq[3][77] = // < X X X............... {"GTTCCAGAGCGGAGGTCTCCCAACAACATGGTATAAGTTGTCTAGCCCCGGTTCGCGCGGGTACTTCTTACAGCGC", "GTTCCAGAGCGGAGGTCTCCCAACAACTTGGTATAAGTTGTCTAGTCCCGGTTCGCGCGGCATTTCAGCATTGTTA", "GTTCCAGAGCGCGACAGAGTGCATATCACGCTAAGCACAGCCCTCTTCTATCTGCTTTTAAATGGATCAATAATCG"}; build_graph_from_str_mt(&graph, 0, graphseq[0], strlen(graphseq[0])); build_graph_from_str_mt(&graph, 1, graphseq[1], strlen(graphseq[1])); build_graph_from_str_mt(&graph, 2, graphseq[2], strlen(graphseq[2])); // Crawl graph GraphCrawler crawler; graph_crawler_alloc(&crawler, &graph); dBNode node = db_graph_find_str(&graph, graphseq[0]); dBNode next_node = db_graph_find_str(&graph, graphseq[0]+1); TASSERT(node.key != HASH_NOT_FOUND); TASSERT(next_node.key != HASH_NOT_FOUND); BinaryKmer bkey = db_node_get_bkmer(&graph, node.key); Edges edges = db_node_get_edges(&graph, node.key, 0); dBNode next_nodes[4]; Nucleotide next_nucs[4]; size_t i, p, num_next, next_idx; num_next = db_graph_next_nodes(&graph, bkey, node.orient, edges, next_nodes, next_nucs); next_idx = 0; while(next_idx < num_next && !db_nodes_are_equal(next_nodes[next_idx],next_node)) next_idx++; TASSERT(next_idx < num_next && db_nodes_are_equal(next_nodes[next_idx],next_node)); // Crawl in all colours graph_crawler_fetch(&crawler, node, next_nodes, next_idx, num_next, NULL, graph.num_of_cols, NULL, NULL, NULL); TASSERT2(crawler.num_paths == 2, "crawler.num_paths: %u", crawler.num_paths); // Fetch paths dBNodeBuffer nbuf; db_node_buf_alloc(&nbuf, 16); StrBuf sbuf; strbuf_alloc(&sbuf, 128); for(p = 0; p < crawler.num_paths; p++) { db_node_buf_reset(&nbuf); graph_crawler_get_path_nodes(&crawler, p, &nbuf); strbuf_ensure_capacity(&sbuf, nbuf.len+graph.kmer_size); sbuf.end = db_nodes_to_str(nbuf.b, nbuf.len, &graph, sbuf.b); for(i = 0; i < 3 && strcmp(graphseq[i]+1,sbuf.b) != 0; i++) {} TASSERT2(i < 3, "seq: %s", sbuf.b); TASSERT2(sbuf.end == 75, "sbuf.end: %zu", sbuf.end); TASSERT2(nbuf.len == 65, "nbuf.len: %zu", nbuf.len); } strbuf_dealloc(&sbuf); db_node_buf_dealloc(&nbuf); graph_crawler_dealloc(&crawler); db_graph_dealloc(&graph); }
// Walk the graph remembering the last time we met the ref // When traversal fails, dump sequence up to last meeting with the ref static void follow_break(BreakpointCaller *caller, dBNode node) { size_t i, j, k, num_next; dBNode next_nodes[4]; Nucleotide next_nucs[4]; size_t nonref_idx[4], num_nonref_next = 0; const dBGraph *db_graph = caller->db_graph; BinaryKmer bkey = db_node_get_bkmer(db_graph, node.key); Edges edges = db_node_get_edges(db_graph, node.key, 0); num_next = db_graph_next_nodes(db_graph, bkey, node.orient, edges, next_nodes, next_nucs); // Filter out next nodes in the reference for(i = 0; i < num_next; i++) { if(kograph_num(caller->kograph, next_nodes[i].key) == 0) { nonref_idx[num_nonref_next] = i; num_nonref_next++; } } // Abandon if all options are in ref or none are if(num_nonref_next == num_next || num_nonref_next == 0) return; // Follow all paths not in ref, in all colours GraphCrawler *fw_crawler = &caller->crawlers[node.orient]; GraphCrawler *rv_crawler = &caller->crawlers[!node.orient]; dBNodeBuffer *allelebuf = &caller->allelebuf, *flank5pbuf = &caller->flank5pbuf; GCMultiColPath *flank5p_multicolpath, *allele_multicolpath; KOccurRun *flank5p_runs, *flank3p_runs; size_t flank5p_pathid, allele_pathid; size_t num_flank5p_runs, num_flank3p_runs; // We fetch 5' flanks in all colours then merge matching paths // we stop fetching a single path if it stops tracking the reference // Alternatively, we could fetch the 5' flank in everyone and stop after a // given distance, then check for that set of paths how much it tracks the // reference. This has the advantage of scaling much better with number of // samples, but not so well as min_ref_nkmers increases (since we fetch // many flanks that can't be used) - I think this is less of a worry. // Loop over possible next nodes at this junction for(i = 0; i < num_nonref_next; i++) { size_t next_idx = nonref_idx[i]; // Go backwards to get 5p flank traverse_5pflank(caller, rv_crawler, db_node_reverse(next_nodes[next_idx]), db_node_reverse(node)); // Loop over the flanks we got for(j = 0; j < rv_crawler->num_paths; j++) { // Get 5p flank db_node_buf_reset(flank5pbuf); graph_crawler_get_path_nodes(rv_crawler, j, flank5pbuf); flank5p_multicolpath = &rv_crawler->multicol_paths[j]; flank5p_pathid = flank5p_multicolpath->pathid; // Fetch 3pflank ref position num_flank5p_runs = caller->flank5p_refs[flank5p_pathid].num_runs; flank5p_runs = fetch_ref_contact(&rv_crawler->cache, flank5p_pathid, caller->flank5p_refs, &caller->flank5p_run_buf); koruns_reverse(flank5p_runs, num_flank5p_runs, flank5pbuf->len); koruns_sort_by_qoffset(flank5p_runs, num_flank5p_runs); db_nodes_reverse_complement(flank5pbuf->data, flank5pbuf->len); if(num_flank5p_runs > 0) { // Reset caller kmer_run_buf_reset(&caller->koruns_3p); kmer_run_buf_reset(&caller->koruns_3p_ended); kmer_run_buf_reset(&caller->allele_run_buf); // functions gcrawler_path_stop_at_ref_covg(), // gcrawler_path_finish_ref_covg() // both fill koruns_3p, koruns_3p_ended and allele_run_buf // Only traverse in the colours we have a flank for graph_crawler_fetch(fw_crawler, node, next_nodes, next_nucs, next_idx, num_next, flank5p_multicolpath->cols, flank5p_multicolpath->num_cols, gcrawler_path_stop_at_ref_covg, gcrawler_path_finish_ref_covg, caller); // Assemble contigs - fetch forwards for each path for given 5p flank for(k = 0; k < fw_crawler->num_paths; k++) { // Fetch nodes db_node_buf_reset(allelebuf); graph_crawler_get_path_nodes(fw_crawler, k, allelebuf); ctx_assert(allelebuf->len > 0); allele_multicolpath = &fw_crawler->multicol_paths[k]; allele_pathid = allele_multicolpath->pathid; // Fetch 3pflank ref position num_flank3p_runs = caller->allele_refs[allele_pathid].num_runs; flank3p_runs = fetch_ref_contact(&fw_crawler->cache, allele_pathid, caller->allele_refs, &caller->allele_run_buf); process_contig(caller, allele_multicolpath->cols, allele_multicolpath->num_cols, flank5pbuf, allelebuf, flank5p_runs, num_flank5p_runs, flank3p_runs, num_flank3p_runs); } } } } }
// `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 }