/* Last changed Time-stamp: <2008-10-09 16:23:36 ivo> */ #include #include #include #include #include "findpath.h" #include "fold.h" #include "fold_vars.h" #include "utils.h" #include "pair_mat.h" #ifdef _OPENMP #include #endif #define LOOP_EN static char rcsid[] = "$Id: findpath.c,v 1.2 2008/10/09 15:42:45 ivo Exp $"; /* ################################# # GLOBAL VARIABLES # ################################# */ /* ################################# # PRIVATE VARIABLES # ################################# */ PRIVATE const char *seq=NULL; PRIVATE short *S=NULL, *S1=NULL; PRIVATE int BP_dist; PRIVATE move_t *path=NULL; PRIVATE int path_fwd; /* 1: struc1->struc2, else struc2 -> struc1 */ #ifdef _OPENMP /* NOTE: all variables are assumed to be uninitialized if they are declared as threadprivate */ #pragma omp threadprivate(seq, S, S1, BP_dist, path, path_fwd) #endif /* ################################# # PRIVATE FUNCTION DECLARATIONS # ################################# */ PRIVATE int *pair_table_to_loop_index (short *pt); PRIVATE move_t *copy_moves(move_t *mvs); PRIVATE int compare_ptable(const void *A, const void *B); PRIVATE int compare_energy(const void *A, const void *B); PRIVATE int compare_moves_when(const void *A, const void *B); PRIVATE void free_intermediate(intermediate_t *i); PRIVATE int find_path_once(const char *struc1, const char *struc2, int maxE, int maxl); PRIVATE int try_moves(intermediate_t c, int maxE, intermediate_t *next, int dist); /* ################################# # BEGIN OF FUNCTION DEFINITIONS # ################################# */ PUBLIC void free_path(path_t *path){ path_t *tmp = path; if(tmp){ while(tmp->s){ free(tmp->s); tmp++;} free(path); } } PUBLIC int find_saddle(const char *sequence, const char *struc1, const char *struc2, int max) { int maxl, maxE, i; const char *tmp; move_t *bestpath=NULL; int dir; path_fwd = 0; maxE = INT_MAX - 1; seq = sequence; update_fold_params(); make_pair_matrix(); /* nummerically encode sequence */ S = encode_sequence(seq, 0); S1 = encode_sequence(seq, 1); maxl=1; do { int saddleE; path_fwd = !path_fwd; if (maxl>max) maxl=max; if(path) free(path); saddleE = find_path_once(struc1, struc2, maxE, maxl); if (saddleEi!=0; mv++) { int i,j; if (mv->when>0) continue; i = mv->i; j = mv->j; pt = (short *) space(sizeof(short)*(len+1)); memcpy(pt, c.pt,(len+1)*sizeof(short)); if (j<0) { /*it's a delete move */ pt[-i]=0; pt[-j]=0; } else { /* insert move */ if ((loopidx[i] == loopidx[j]) && /* i and j belong to same loop */ (pt[i] == 0) && (pt[j]==0) /* ... and are unpaired */ ) { pt[i]=j; pt[j]=i; } else { free(pt); continue; /* llegal move, try next; */ } } #ifdef LOOP_EN en = c.curr_en + energy_of_move_pt(c.pt, S, S1, i, j); #else en = energy_of_structure_pt(seq, pt, S, S1, 0); #endif if (enoldE)?en:oldE; next[num_next].curr_en = en; next[num_next].pt = pt; mv->when=dist; mv->E = en; next[num_next++].moves = copy_moves(c.moves); mv->when=0; } else free(pt); } free(loopidx); return num_next; } PRIVATE int find_path_once(const char *struc1, const char *struc2, int maxE, int maxl) { short *pt1, *pt2; move_t *mlist; int i, len, d, dist=0, result; intermediate_t *current, *next; pt1 = make_pair_table(struc1); pt2 = make_pair_table(struc2); len = (int) strlen(struc1); mlist = (move_t *) space(sizeof(move_t)*len); /* bp_dist < n */ for (i=1; i<=len; i++) { if (pt1[i] != pt2[i]) { if (ipt != NULL; cc++) free_intermediate(cc); current[0].Sen=INT_MAX; break; } /* remove duplicates via sort|uniq if this becomes a bottleneck we can use a hash instead */ qsort(next, num_next, sizeof(intermediate_t),compare_ptable); for (u=0,c=1; cpt != NULL; cc++) free_intermediate(cc); for (u=0; u pt[i])) { /* ) */ --hx; if (hx>0) l = loop[stack[hx-1]]; /* index of enclosing loop */ else l=0; /* external loop has index 0 */ if (hx<0) { nrerror("unbalanced brackets in make_pair_table"); } } } loop[0] = nl; free(stack); #ifdef _DEBUG_LOOPIDX fprintf(stderr,"begin loop index\n"); fprintf(stderr, "....,....1....,....2....,....3....,....4" "....,....5....,....6....,....7....,....8\n"); print_structure(pt, loop[0]); for (i=1; i<=length; i++) fprintf(stderr,"%2d ", loop[i]); fprintf(stderr,"\n"); fprintf(stderr, "end loop index\n"); fflush(stderr); #endif return (loop); } PRIVATE void free_intermediate(intermediate_t *i) { free(i->pt); free(i->moves); i->pt = NULL; i->moves = NULL; i->Sen = INT_MAX; } PRIVATE int compare_ptable(const void *A, const void *B) { intermediate_t *a, *b; int c; a = (intermediate_t *) A; b = (intermediate_t *) B; c = memcmp(a->pt, b->pt, a->pt[0]*sizeof(short)); if (c!=0) return c; if ((a->Sen - b->Sen) != 0) return (a->Sen - b->Sen); return (a->curr_en - b->curr_en); } PRIVATE int compare_energy(const void *A, const void *B) { intermediate_t *a, *b; a = (intermediate_t *) A; b = (intermediate_t *) B; if ((a->Sen - b->Sen) != 0) return (a->Sen - b->Sen); return (a->curr_en - b->curr_en); } PRIVATE int compare_moves_when(const void *A, const void *B) { move_t *a, *b; a = (move_t *) A; b = (move_t *) B; return(a->when - b->when); } PRIVATE move_t* copy_moves(move_t *mvs) { move_t *new; new = (move_t *) space(sizeof(move_t)*(BP_dist+1)); memcpy(new,mvs,sizeof(move_t)*(BP_dist+1)); return new; } #ifdef TEST_FINDPATH int main(int argc, char *argv[]) { char *seq, *s1, *s2; int E, maxkeep=10; int verbose=0, i; path_t *route, *r; for (i=1; is; r++) { printf("%s %6.2f - %6.2f\n", r->s, energy_of_struct(seq,r->s), r->en); free(r->s); } } free(seq); free(s1); free(s2); return(EXIT_SUCCESS); } #endif