static bool read_touches_graph(const read_t *r, const dBGraph *db_graph, LoadingStats *stats) { bool found = false; BinaryKmer bkmer; Nucleotide nuc; dBNode node; const size_t kmer_size = db_graph->kmer_size; size_t i, num_contigs = 0, num_kmers_loaded = 0; size_t search_pos = 0, start, end = 0, contig_len; if(r->seq.end >= kmer_size) { while((start = seq_contig_start(r, search_pos, kmer_size, 0,0)) < r->seq.end && !found) { end = seq_contig_end(r, start, kmer_size, 0, 0, &search_pos); contig_len = end - start; __sync_fetch_and_add((volatile size_t*)&stats->total_bases_loaded, contig_len); num_contigs++; bkmer = binary_kmer_from_str(r->seq.b + start, kmer_size); num_kmers_loaded++; node = db_graph_find(db_graph, bkmer); if(node.key != HASH_NOT_FOUND) { found = true; break; } for(i = start+kmer_size; i < end; i++) { nuc = dna_char_to_nuc(r->seq.b[i]); bkmer = binary_kmer_left_shift_add(bkmer, kmer_size, nuc); num_kmers_loaded++; node = db_graph_find(db_graph, bkmer); if(node.key != HASH_NOT_FOUND) { found = true; break; } } } } // Update stats __sync_fetch_and_add((volatile size_t*)&stats->total_bases_read, r->seq.end); __sync_fetch_and_add((volatile size_t*)&stats->num_kmers_loaded, num_kmers_loaded); __sync_fetch_and_add((volatile size_t*)&stats->num_kmers_novel, num_kmers_loaded - found); __sync_fetch_and_add((volatile size_t*)&stats->num_good_reads, num_contigs > 0); __sync_fetch_and_add((volatile size_t*)&stats->num_bad_reads, num_contigs == 0); return found; }
// // Integrity checks // // Check an array of nodes denote a contigous path bool db_node_check_nodes(const dBNode *nodes, size_t num, const dBGraph *db_graph) { if(num == 0) return true; const size_t kmer_size = db_graph->kmer_size; BinaryKmer bkmer0, bkmer1, tmp; Nucleotide nuc; size_t i; bkmer0 = db_node_oriented_bkmer(db_graph, nodes[0]); for(i = 0; i+1 < num; i++) { bkmer1 = db_node_oriented_bkmer(db_graph, nodes[i+1]); nuc = binary_kmer_last_nuc(bkmer1); tmp = binary_kmer_left_shift_add(bkmer0, kmer_size, nuc); ctx_assert_ret(binary_kmers_are_equal(tmp, bkmer1)); bkmer0 = bkmer1; } return true; }
// Extend a supernode, nlist[offset] must already be set // Walk along nodes starting from node/or, storing the supernode in nlist // Returns the number of nodes added, adds no more than `limit` // return false if out of space and limit > 0 bool supernode_extend(dBNodeBuffer *nbuf, size_t limit, const dBGraph *db_graph) { ctx_assert(nbuf->len > 0); const size_t kmer_size = db_graph->kmer_size; dBNode node0 = nbuf->data[0], node1 = nbuf->data[nbuf->len-1], node = node1; BinaryKmer bkmer = db_node_oriented_bkmer(db_graph, node); Edges edges = db_node_get_edges_union(db_graph, node.key); Nucleotide nuc; while(edges_has_precisely_one_edge(edges, node.orient, &nuc)) { bkmer = binary_kmer_left_shift_add(bkmer, kmer_size, nuc); node = db_graph_find(db_graph, bkmer); edges = db_node_get_edges_union(db_graph, node.key); ctx_assert(node.key != HASH_NOT_FOUND); if(edges_has_precisely_one_edge(edges, rev_orient(node.orient), &nuc)) { if(node.key == node0.key || node.key == nbuf->data[nbuf->len-1].key) { // don't create a loop A->B->A or a->b->B->A break; } if(limit && nbuf->len >= limit) return false; db_node_buf_add(nbuf, node); } else break; } return true; }
// if colour is -1 aligns to all colours, otherwise aligns to given colour only // Returns number of kmers lost from the end static size_t db_alignment_from_read(dBAlignment *aln, const read_t *r, uint8_t qcutoff, uint8_t hp_cutoff, const dBGraph *db_graph, int colour) { size_t contig_start, contig_end = 0, search_start = 0; const size_t kmer_size = db_graph->kmer_size; BinaryKmer bkmer, tmp_key; Nucleotide nuc; hkey_t node; size_t i, offset, nxtbse; dBNodeBuffer *nodes = &aln->nodes; Int32Buffer *rpos = &aln->rpos; ctx_assert(nodes->len == rpos->len); size_t n = nodes->len, init_len = n; db_node_buf_capacity(nodes, n + r->seq.end); int32_buf_capacity(rpos, n + r->seq.end); while((contig_start = seq_contig_start(r, search_start, kmer_size, qcutoff, hp_cutoff)) < r->seq.end) { contig_end = seq_contig_end(r, contig_start, kmer_size, qcutoff, hp_cutoff, &search_start); const char *contig = r->seq.b + contig_start; size_t contig_len = contig_end - contig_start; bkmer = binary_kmer_from_str(contig, kmer_size); bkmer = binary_kmer_right_shift_one_base(bkmer); for(offset=contig_start, nxtbse=kmer_size-1; nxtbse < contig_len; nxtbse++,offset++) { nuc = dna_char_to_nuc(contig[nxtbse]); bkmer = binary_kmer_left_shift_add(bkmer, kmer_size, nuc); tmp_key = binary_kmer_get_key(bkmer, kmer_size); node = hash_table_find(&db_graph->ht, tmp_key); if(node != HASH_NOT_FOUND && (colour == -1 || db_node_has_col(db_graph, node, colour))) { nodes->b[n].key = node; nodes->b[n].orient = bkmer_get_orientation(bkmer, tmp_key); rpos->b[n] = offset; n++; } } } // Return number of bases from the last kmer found until read end size_t ret = (n == init_len ? r->seq.end /* No kmers found */ : r->seq.end - (rpos->b[n-1] + kmer_size)); nodes->len = rpos->len = n; // Check for sequence gaps for(i = init_len; i+1 < nodes->len; i++) { if(rpos->b[i]+1 < rpos->b[i+1]) { aln->seq_gaps = true; break; } } return ret; }