//Gets all the paths from a given node. int perfect_path_get_all_paths_from(dBNode * node, Orientation orientation, PathArray * pa, int limit, dBGraph * db_graph) { int found = 0; pathStep ps; ps.orientation = orientation; ps.node = node; ps.flags = 0; ps.path = NULL; Nucleotide n; for (n = 0; n < 4; n++) { ps.label = n; path_reset(pa->paths[n]); path_set_limit(limit,pa->paths[n] ); if (db_node_edge_exist_any_colour(node, n, orientation)) { perfect_path_get_path_from_step_with_callback_with_args(ps, NULL, NULL, &pp_copy_path,pa->paths[n], db_graph); } } return found; }
/*----------------------------------------------------------------------* * Function: * * Purpose: * * Params: * * Returns: * *----------------------------------------------------------------------*/ Nucleotide coverage_walk_get_best_label(dBNode* node, Orientation orientation, dBGraph* db_graph) { Nucleotide label = Undefined; int highest_coverage = 0; //void check_edge(Nucleotide nucleotide) { Nucleotide nucleotide; for (nucleotide = Adenine; nucleotide < Undefined; nucleotide++) { //if (debugme) log_printf(" Trying nucleotide %c\n", binary_nucleotide_to_char(nucleotide)); if (db_node_edge_exist_any_colour(node, nucleotide, orientation)) { pathStep step, reverse_step, next_step; int coverage; step.node = node; step.label = nucleotide; step.orientation = orientation; db_graph_get_next_step(&step, &next_step, &reverse_step, db_graph); coverage = element_get_coverage_all_colours(next_step.node); //if (debugme) log_printf(" Coverage %i\n", coverage); if (coverage > highest_coverage) { label = nucleotide; highest_coverage = coverage; } } } // nucleotide_iterator(&check_edge); return label; }
/*----------------------------------------------------------------------* * Function: * * Purpose: * * Params: * * Returns: * *----------------------------------------------------------------------*/ int grow_graph_from_node(dBNode* start_node, dBNode** best_node, dBGraph* graph) { Queue* nodes_to_walk; dBNode* node; int orientation; int depth; int current_graph_size = 0; int best_coverage = 0; int best_edges = 0; *best_node = 0; // Nucleotide iterator, used to walk all possible paths from a node void walk_if_exists(Nucleotide n) { //if (debug) printf("Trying nucleotide %i\n", n); // If there is an edge in any colour for this nucleotide... if (db_node_edge_exist_any_colour(node, n, orientation)) { //if (debug) printf(" Edge exists\n"); // Get first node along this edge and check we've not already visited it... Orientation next_orientation; Nucleotide reverse_nucleotide; dBNode * next_node; next_node = db_graph_get_next_node(node, orientation, &next_orientation, n, &reverse_nucleotide, graph); if (!next_node) { log_and_screen_printf("Error: Something went wrong with db_graph_get_next_node\n"); exit(-1); } // If not already visited the first node, walk it... if (!db_node_check_flag_visited(next_node)) { pathStep first_step; Path * new_path; dBNode* end_node; int i = 0; // Get path first_step.node = node; first_step.orientation = orientation; first_step.label = n; new_path = path_new(MAX_EXPLORE_NODES, graph->kmer_size); if (!new_path) { log_and_screen_printf("ERROR: Not enough memory to allocate new path.\n"); exit(-1); } db_graph_get_perfect_path_with_first_edge_all_colours(&first_step, &db_node_action_do_nothing, new_path, graph); // Add end node to list of nodes to visit end_node = new_path->nodes[new_path->length-1]; if (!db_node_check_flag_visited(end_node)) { if (!db_node_is_blunt_end_all_colours(end_node, new_path->orientations[new_path->length-1])) { if (queue_push_node(nodes_to_walk, end_node, depth+1) == NULL) { log_and_screen_printf("Queue too large. Ending.\n"); exit(1); } } } // Now go through all nodes, look for best and mark all as visited for (i=0; i<new_path->length; i++) { if (!db_node_check_flag_visited(new_path->nodes[i])) { int this_coverage = element_get_coverage_all_colours(new_path->nodes[i]); int this_edges = db_node_edges_count_all_colours(new_path->nodes[i], forward) + db_node_edges_count_all_colours(new_path->nodes[i], reverse); if ((best_node == 0) || (this_coverage > best_coverage) || ((this_coverage == best_coverage) && (this_edges < best_edges))) { best_coverage = this_coverage; best_edges = this_edges; *best_node = new_path->nodes[i]; } db_node_action_set_flag_visited(new_path->nodes[i]); current_graph_size++; } } // Clean up path_destroy(new_path); } } } // Start a queue of nodes to walk //log_and_screen_printf("Allocating %d Mb to store queue information (max %d nodes, when full each node could be %d)...\n", ((METACORTEX_QUEUE_SIZE * sizeof(QueueItem*)) / 1024) / 1024, METACORTEX_QUEUE_SIZE, sizeof(QueueItem)); nodes_to_walk = queue_new(METACORTEX_QUEUE_SIZE); if (!nodes_to_walk) { log_and_screen_printf("Couldn't get memory for node queue.\n"); exit(-1); } // Add start node to list of nodes to visit if (queue_push_node(nodes_to_walk, start_node, 0) == NULL) { log_and_screen_printf("Queue too large. Ending.\n"); exit(-1); } if (!db_node_check_flag_visited(start_node)) { db_node_action_set_flag_visited(start_node); current_graph_size++; } // Now keep visiting nodes and walking paths while (nodes_to_walk->number_of_items > 0) { // Take top node from list node = queue_pop_node(nodes_to_walk, &depth); // Look at all paths out from here orientation = forward; nucleotide_iterator(&walk_if_exists); orientation = reverse; nucleotide_iterator(&walk_if_exists); } queue_free(nodes_to_walk); // If we didn't find a start node, presumably this is a singleton? if (*best_node == 0) { printf("Note: didn't find a best node, setting to start node\n"); *best_node = start_node; } return current_graph_size; }