static Alignment *HPair_refine_alignment(HPair *hpair, Alignment *alignment){ register Alignment *refined_alignment = NULL; register Region *region; register gint query_region_start, target_region_start; g_assert(hpair->heuristic->optimal); if(hpair->verbosity > 1) g_message("Refining alignment ... (%d)", alignment->score); switch(hpair->heuristic->has->refinement){ case Heuristic_Refinement_FULL: region = Region_create(0, 0, hpair->query_length, hpair->target_length); refined_alignment = Optimal_find_path( hpair->heuristic->optimal, region, hpair->user_data, alignment->score, hpair->subopt); g_assert(refined_alignment); Region_destroy(region); break; case Heuristic_Refinement_REGION: query_region_start = MAX(0, alignment->region->query_start - hpair->heuristic->has->refinement_boundary); target_region_start = MAX(0, alignment->region->target_start - hpair->heuristic->has->refinement_boundary); region = Region_create(query_region_start, target_region_start, MIN(hpair->query_length, Region_query_end(alignment->region) + hpair->heuristic->has->refinement_boundary) - query_region_start, MIN(hpair->target_length, Region_target_end(alignment->region) + hpair->heuristic->has->refinement_boundary) - target_region_start); refined_alignment = Optimal_find_path( hpair->heuristic->optimal, region, hpair->user_data, alignment->score, hpair->subopt); g_assert(refined_alignment); Region_destroy(region); break; default: g_error("Bad type for refinement [%s]", Heuristic_Refinement_to_string( hpair->heuristic->has->refinement)); break; } g_assert(refined_alignment); g_assert(refined_alignment->score >= alignment->score); return refined_alignment; }
static void test_alignment(C4_Model *model, Sequence *query, Sequence *target, Protein2Genome_Data *p2gd){ register C4_Score score; register Alignment *alignment; register Optimal *optimal = Optimal_create(model, NULL, Optimal_Type_SCORE |Optimal_Type_PATH, FALSE); register Region *region = Region_create(0, 0, query->len, target->len); score = Optimal_find_score(optimal, region, p2gd, NULL); g_message("Score is [%d] (expect 131)", score); g_assert(score == 129); alignment = Optimal_find_path(optimal, region, p2gd, C4_IMPOSSIBLY_LOW_SCORE, NULL); g_message("Alignment score is [%d]", alignment->score); Alignment_display(alignment, query, target, NULL, Protein2Genome_Data_get_submat(p2gd), Protein2Genome_Data_get_translate(p2gd), stdout); g_assert(score == alignment->score); Alignment_destroy(alignment); Optimal_destroy(optimal); Region_destroy(region); return; }
int KDTree_neighbor_search(struct KDTree* tree, float neighbor_radius, struct Neighbor** neighbors) { long int i; int ok; Region_dim=tree->dim; if(tree->_neighbor_list) { free(tree->_neighbor_list); tree->_neighbor_list = NULL; } tree->_neighbor_count=0; /* note the use of r^2 to avoid use of sqrt */ tree->_neighbor_radius=neighbor_radius; tree->_neighbor_radius_sq=neighbor_radius*neighbor_radius; if (Node_is_leaf(tree->_root)) { /* this is a boundary condition */ /* bucket_size>nr of points */ ok = KDTree_search_neighbors_in_bucket(tree, tree->_root); } else { /* "normal" situation */ struct Region *region; /* start with [-INF, INF] */ region= Region_create(NULL, NULL); if (!region) return 0; ok = KDTree__neighbor_search(tree, tree->_root, region, 0); Region_destroy(region); } if (!ok) return 0; *neighbors = NULL; for (i = 0; i < tree->_neighbor_count; i++) { struct Neighbor* neighbor = malloc(sizeof(struct Neighbor)); if (!neighbor) { while(1) { neighbor = *neighbors; if (!neighbor) return 0; *neighbors = neighbor->next; free(neighbor); } } *neighbor = tree->_neighbor_list[i]; neighbor->next = *neighbors; *neighbors = neighbor; } return 1; }
static struct Region* Region_create_intersect_right(struct Region* region, float split_coord, int current_dim) { struct Region* p; const float value = region->_left[current_dim]; region->_left[current_dim]=split_coord; p = Region_create(region->_left, region->_right); region->_left[current_dim]=value; return p; }
OPair *OPair_create(Optimal *optimal, SubOpt *subopt, gint query_length, gint target_length, gpointer user_data){ register OPair *opair = g_new(OPair, 1); g_assert(optimal); g_assert(query_length >= 0); g_assert(target_length >= 0); opair->optimal = Optimal_share(optimal); opair->user_data = user_data; opair->subopt = SubOpt_share(subopt); opair->region = Region_create(0, 0, query_length, target_length); opair->prev_score = C4_IMPOSSIBLY_HIGH_SCORE; return opair; }
int KDTree_search_center_radius(struct KDTree* tree, float *coord, float radius) { int i; int dim = tree->dim; float* left = malloc(dim*sizeof(float)); float* right = malloc(dim*sizeof(float)); if (left==NULL || right==NULL) { if (left) free(left); if (right) free(right); return 0; } Region_dim=tree->dim; if (tree->_radius_list) { free(tree->_radius_list); tree->_radius_list = NULL; } tree->_count=0; tree->_radius=radius; /* use of r^2 to avoid sqrt use */ tree->_radius_sq=radius*radius; for (i=0; i<tree->dim; i++) { left[i]=coord[i]-radius; right[i]=coord[i]+radius; /* set center of query */ tree->_center_coord[i]=coord[i]; } /* clean up! */ if (coord) free(coord); Region_destroy(tree->_query_region); tree->_query_region= Region_create(left, right); free(left); free(right); if (!tree->_query_region) return 0; return KDTree_search(tree, NULL, NULL, 0); }
int Argument_main(Argument *arg){ register C4_Model *est2genome; register C4_Score score; register Alignment *alignment; register Optimal *optimal; register gchar *query_seq = "CGATCGATCGNATCGATCGATC" "CATCTATCTAGCGAGCGATCTA", *target_seq = "CGATCGATCGATCGATCGATC" "GTNNNNNNNNNNNNNNNNNNNN" "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN" "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN" "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN" "NNNNNNNNNNNNNNNNNNNNNNNNNNNAG" /* "CTNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAC" */ "CATCTATCTANNNGCGAGCGATCTA"; register Alphabet *dna_alphabet = Alphabet_create( Alphabet_Type_DNA, FALSE); register Sequence *query = Sequence_create("query", NULL, query_seq, 0, Sequence_Strand_UNKNOWN, dna_alphabet), *target = Sequence_create("target", NULL, target_seq, 0, Sequence_Strand_UNKNOWN, dna_alphabet); /**/ register Region *region = Region_create(0, 0, query->len, target->len); register EST2Genome_Data *e2gd; Match_ArgumentSet_create(arg); Affine_ArgumentSet_create(arg); Intron_ArgumentSet_create(arg); Viterbi_ArgumentSet_create(arg); Argument_process(arg, "est2genome.test", NULL, NULL); est2genome = EST2Genome_create(); g_message("Has [%d] shadows", est2genome->shadow_list->len); optimal = Optimal_create(est2genome, NULL, Optimal_Type_SCORE|Optimal_Type_PATH, FALSE); e2gd = EST2Genome_Data_create(query, target); score = Optimal_find_score(optimal, region, e2gd, NULL); g_message("Score is [%d]", score); g_assert(score == 159); /**/ alignment = Optimal_find_path(optimal, region, e2gd, C4_IMPOSSIBLY_LOW_SCORE, NULL); g_message("Alignment score is [%d]", alignment->score); Alignment_display(alignment, query, target, EST2Genome_Data_get_submat(e2gd), EST2Genome_Data_get_submat(e2gd), NULL, stdout); g_assert(score == alignment->score); /**/ Alphabet_destroy(dna_alphabet); C4_Model_destroy(est2genome); Alignment_destroy(alignment); Optimal_destroy(optimal); EST2Genome_Data_destroy(e2gd); Sequence_destroy(query); Sequence_destroy(target); Region_destroy(region); return 0; }
static int KDTree__neighbor_search(struct KDTree* tree, struct Node *node, struct Region *region, int depth) { struct Node *left, *right; struct Region *left_region = NULL; struct Region *right_region = NULL; int localdim; int intersect; float cut_value; int ok = 1; localdim=depth%tree->dim; left=node->_left; right=node->_right; cut_value = node->_cut_value; /* planes of left and right nodes */ intersect=Region_test_intersect_left(region, cut_value, localdim); if(intersect==1) { left_region = Region_create(region->_left, region->_right); if (!left_region) ok = 0; } else if(intersect==0) { left_region = Region_create_intersect_left(region, cut_value, localdim); if (!left_region) ok = 0; } else if(intersect==-1) /* intersect is -1 if no overlap */ { left_region = NULL; } intersect=Region_test_intersect_right(region, cut_value, localdim); if(intersect==-1) { right_region = Region_create(region->_left, region->_right); if (!right_region) ok = 0; } else if(intersect==0) { right_region = Region_create_intersect_right(region, cut_value, localdim); if (!right_region) ok = 0; } else if(intersect==+1) /* intersect is +1 if no overlap */ { right_region = NULL; } if (ok) { if (!Node_is_leaf(left)) { /* search for pairs in this half plane */ ok = KDTree__neighbor_search(tree, left, left_region, depth+1); } else { ok = KDTree_search_neighbors_in_bucket(tree, left); } } if (ok) { if (!Node_is_leaf(right)) { /* search for pairs in this half plane */ ok = KDTree__neighbor_search(tree, right, right_region, depth+1); } else { ok = KDTree_search_neighbors_in_bucket(tree, right); } } /* search for pairs between the half planes */ if (ok) { ok = KDTree_neighbor_search_pairs(tree, left, left_region, right, right_region, depth+1); } /* cleanup */ Region_destroy(left_region); Region_destroy(right_region); return ok; }
static int KDTree_neighbor_search_pairs(struct KDTree* tree, struct Node *down, struct Region *down_region, struct Node *up, struct Region *up_region, int depth) { int down_is_leaf, up_is_leaf; int localdim; int ok = 1; /* if regions do not overlap - STOP */ if (!down || !up || !down_region || !up_region) { /* STOP */ return ok; } if (Region_test_intersection(down_region, up_region, tree->_neighbor_radius)==0) { /* regions cannot contain neighbors */ return ok; } /* dim */ localdim=depth%tree->dim; /* are they leaves? */ up_is_leaf=Node_is_leaf(up); down_is_leaf=Node_is_leaf(down); if (up_is_leaf && down_is_leaf) { /* two leaf nodes */ ok = KDTree_search_neighbors_between_buckets(tree, down, up); } else { /* one or no leaf nodes */ struct Node *up_right, *up_left, *down_left, *down_right; struct Region *up_left_region = NULL; struct Region *up_right_region = NULL; struct Region *down_left_region = NULL; struct Region *down_right_region = NULL; if (down_is_leaf) { down_left=down; /* make a copy of down_region */ down_left_region= Region_create(down_region->_left, down_region->_right); if (down_left_region==NULL) ok = 0; down_right=NULL; down_right_region=NULL; } else { float cut_value; int intersect; cut_value=down->_cut_value; down_left=down->_left; down_right=down->_right; intersect=Region_test_intersect_left(down_region, cut_value, localdim); if(intersect==1) { down_left_region = Region_create(down_region->_left, down_region->_right); if (down_left_region==NULL) ok = 0; } else if(intersect==0) { down_left_region = Region_create_intersect_left(down_region, cut_value, localdim); if (down_left_region==NULL) ok = 0; } else if(intersect==-1) /* intersect is -1 if no overlap */ { down_left_region = NULL; } intersect=Region_test_intersect_right(down_region, cut_value, localdim); if(intersect==-1) { down_right_region = Region_create(down_region->_left, down_region->_right); if (down_right_region==NULL) ok = 0; } else if(intersect==0) { down_right_region = Region_create_intersect_right(down_region, cut_value, localdim); if (down_right_region==NULL) ok = 0; } else if(intersect==+1) { down_right_region = NULL; } } if (up_is_leaf) { up_left=up; /* make a copy of up_region */ up_left_region= Region_create(up_region->_left, up_region->_right); if (up_left_region==NULL) ok = 0; up_right=NULL; up_right_region=NULL; } else { float cut_value; int intersect; cut_value=up->_cut_value; up_left=up->_left; up_right=up->_right; intersect=Region_test_intersect_left(up_region, cut_value, localdim); if(intersect==1) { up_left_region = Region_create(up_region->_left, up_region->_right); if (up_left_region==NULL) ok = 0; } else if(intersect==0) { up_left_region = Region_create_intersect_left(up_region, cut_value, localdim); if (up_left_region==NULL) ok = 0; } else if(intersect==-1) /* intersect is -1 if no overlap */ { up_left_region = NULL; } intersect=Region_test_intersect_right(up_region, cut_value, localdim); if(intersect==-1) { up_right_region = Region_create(up_region->_left, up_region->_right); if (up_right_region==NULL) ok = 0; } else if(intersect==0) { up_right_region = Region_create_intersect_right(up_region, cut_value, localdim); if (up_right_region==NULL) ok = 0; } else if(intersect==+1) /* intersect is +1 if no overlap */ { up_right_region = NULL; } } if (ok) ok = KDTree_neighbor_search_pairs(tree, up_left, up_left_region, down_left, down_left_region, depth+1); if (ok) ok = KDTree_neighbor_search_pairs(tree, up_left, up_left_region, down_right, down_right_region, depth+1); if (ok) ok = KDTree_neighbor_search_pairs(tree, up_right, up_right_region, down_left, down_left_region, depth+1); if (ok) ok = KDTree_neighbor_search_pairs(tree, up_right, up_right_region, down_right, down_right_region, depth+1); Region_destroy(down_left_region); Region_destroy(down_right_region); Region_destroy(up_left_region); Region_destroy(up_right_region); } return ok; }
static int KDTree_search(struct KDTree* tree, struct Region *region, struct Node *node, int depth) { int current_dim; int ok = 1; if(depth==0) { /* start with [-INF, INF] region */ region = Region_create(NULL, NULL); if (region==NULL) return 0; /* start with root node */ node=tree->_root; } current_dim=depth%tree->dim; if(Node_is_leaf(node)) { long int i; for (i=node->_start; i<node->_end; i++) { struct DataPoint data_point; data_point=tree->_data_point_list[i]; if (Region_encloses(tree->_query_region, data_point._coord)) { /* point is enclosed in query region - report & stop */ ok = KDTree_report_point(tree, data_point._index, data_point._coord); } } } else { struct Node *left_node, *right_node; struct Region *left_region, *right_region; int intersect_left, intersect_right; left_node=node->_left; /* LEFT HALF PLANE */ /* new region */ intersect_left=Region_test_intersect_left(region, node->_cut_value, current_dim); if(intersect_left==1) { left_region = Region_create(region->_left, region->_right); if (left_region) ok = KDTree_test_region(tree, left_node, left_region, depth); else ok = 0; } else if (intersect_left==0) { left_region = Region_create_intersect_left(region, node->_cut_value, current_dim); if (left_region) ok = KDTree_test_region(tree, left_node, left_region, depth); else ok = 0; } /* intersect_left is -1 if no overlap */ /* RIGHT HALF PLANE */ right_node=node->_right; /* new region */ intersect_right=Region_test_intersect_right(region, node->_cut_value, current_dim); if (intersect_right==-1) { right_region = Region_create(region->_left, region->_right); /* test for overlap/inside/outside & do recursion/report/stop */ if (right_region) ok = KDTree_test_region(tree, right_node, right_region, depth); else ok = 0; } else if (intersect_right==0) { right_region = Region_create_intersect_right(region, node->_cut_value, current_dim); /* test for overlap/inside/outside & do recursion/report/stop */ if (right_region) ok = KDTree_test_region(tree, right_node, right_region, depth); else ok = 0; } /* intersect_right is +1 if no overlap */ } Region_destroy(region); return ok; }
Region *Region_copy(Region *region) { g_assert(region); return Region_create(region->query_start, region->target_start, region->query_length, region->target_length); }