/* insertions move set*/ PRIVATE int insertions(Encoded *Enc, struct_en *str, struct_en *minim){ int cnt = 0; short *pt = str->structure; int len = pt[0]; int i,j; for (i=1; i<=len; i++) { if (pt[i]==0) { for (j=i+1; j<=len; j++) { /* end if found closing bracket*/ if (pt[j]!=0 && pt[j]<j) break; /*')'*/ if (pt[j]!=0 && pt[j]>j) { /*'('*/ j = pt[j]; continue; } /* if conditions are met, do insert*/ if (try_insert(pt, Enc->seq, i, j)) { Enc->bp_left=i; Enc->bp_right=j; if (Enc->noLP) { /* if lone bases occur, try inserting one another base*/ if (lone_base(pt, i) || lone_base(pt, j)) { /* inside*/ if (try_insert(pt, Enc->seq, i+1, j-1)) { Enc->bp_left2=i+1; Enc->bp_right2=j-1; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } else /*outside*/ if (try_insert(pt, Enc->seq, i-1, j+1)) { Enc->bp_left2=i-1; Enc->bp_right2=j+1; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } else { cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } else { cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } } } } return cnt; }
// insertions move set int insertions(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose) { int cnt = 0; short *pt = enc.pt; int len = pt[0]; for (int i=1; i<=len; i++) { if (pt[i]==0) { for (int j=i+1; j<=len; j++) { // end if found closing bracket if (pt[j]!=0 && pt[j]<j) break; //')' if (pt[j]!=0 && pt[j]>j) { //'(' j = pt[j]; continue; } // if conditions are met, do insert if (try_insert(pt, enc.seq, i, j)) { enc.bp_left=i; enc.bp_right=j; if (deg.opt->noLP) { // if lone bases occur, try inserting one another base if (lone_base(pt, i) || lone_base(pt, j)) { // inside if (try_insert(pt, enc.seq, i+1, j-1)) { enc.bp_left2=i+1; enc.bp_right2=j-1; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } else //outside if (try_insert(pt, enc.seq, i-1, j+1)) { enc.bp_left2=i-1; enc.bp_right2=j+1; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } else { cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } else { cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } } } } return cnt; }
PRIVATE int move_rset(Encoded *Enc, struct_en *str){ /* count better neighbours*/ int cnt = 0; /* deepest descent*/ struct_en min; min.structure = allocopy(str->structure); min.energy = str->energy; Enc->current_en = str->energy; if (Enc->verbose_lvl>0) { fprintf(stderr, " start of MR:\n "); print_str(stderr, str->structure); fprintf(stderr, " %d\n\n", str->energy); } // construct and permute possible moves construct_moves(Enc, str->structure); /* find first lower one*/ int i; for (i=0; i<Enc->num_moves; i++) { Enc->bp_left = Enc->moves_from[i]; Enc->bp_right = Enc->moves_to[i]; cnt = update_deepest(Enc, str, &min); if (cnt) break; } /* if degeneracy occurs, solve it!*/ if (deal_deg && !cnt && (Enc->end_unpr - Enc->begin_unpr)>0) { Enc->processed[Enc->end_pr] = str->structure; Enc->end_pr++; str->structure = Enc->unprocessed[Enc->begin_unpr]; Enc->unprocessed[Enc->begin_unpr]=NULL; Enc->begin_unpr++; cnt += move_rset(Enc, str); } else { /* write output to str*/ copy_arr(str->structure, min.structure); str->energy = min.energy; } /* release minimal*/ free(min.structure); /* resolve degeneracy in local minima*/ if (deal_deg && (Enc->end_pr - Enc->begin_pr)>0) { Enc->processed[Enc->end_pr]=str->structure; Enc->end_pr++; int min = find_min(Enc->processed, Enc->begin_pr, Enc->end_pr); short *tmp = Enc->processed[min]; Enc->processed[min] = Enc->processed[Enc->begin_pr]; Enc->processed[Enc->begin_pr] = tmp; str->structure = Enc->processed[Enc->begin_pr]; Enc->begin_pr++; free_degen(Enc); } return cnt; }
/* deletions move set*/ PRIVATE int deletions(Encoded *Enc, struct_en *str, struct_en *minim){ int cnt = 0; short *pt = str->structure; int len = pt[0]; int i; for (i=1; i<=len; i++) { if (pt[i]>pt[pt[i]]) { /* '('*/ Enc->bp_left=-i; Enc->bp_right=-pt[i]; /*if nolp enabled, make (maybe) 2nd delete*/ if (Enc->noLP) { int lone = -1; if (lone_base(pt, i-1)) lone=i-1; else if (lone_base(pt, i+1)) lone=i+1; else if (lone_base(pt, pt[i]-1)) lone=pt[i]-1; else if (lone_base(pt, pt[i]+1)) lone=pt[i]+1; /* check*/ if (lone != -1 && (pt[lone]==0 || pt[pt[lone]]==0)) { fprintf(stderr, "WARNING: pt[%d(or %d)]!=\'.\'", lone, pt[lone]); } if (lone != -1) { Enc->bp_left2=-lone-1; Enc->bp_right2=-pt[lone]-1; } if (!lone_base(pt, pt[lone]-1) && !lone_base(pt, pt[lone]+1)) { cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } else { /* nolp not enabled*/ cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } } return cnt; }
// deletions move set int deletions(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose) { int cnt = 0; short *pt = enc.pt; int len = pt[0]; for (int i=1; i<=len; i++) { if (pt[i]>pt[pt[i]]) { // '(' enc.bp_left=-i; enc.bp_right=-pt[i]; //if nolp enabled, make (maybe) 2nd delete if (deg.opt->noLP) { int lone = -1; if (lone_base(pt, i-1)) lone=i-1; else if (lone_base(pt, i+1)) lone=i+1; else if (lone_base(pt, pt[i]-1)) lone=pt[i]-1; else if (lone_base(pt, pt[i]+1)) lone=pt[i]+1; // check if (lone != -1 && (pt[lone]==0 || pt[pt[lone]]==0)) { fprintf(stderr, "WARNING: pt[%d(or %d)]!=\'.\'", lone, pt[lone]); } if (lone != -1) { enc.bp_left2=-lone-1; enc.bp_right2=-pt[lone]-1; } if (!lone_base(pt, pt[lone]-1) && !lone_base(pt, pt[lone]+1)) { cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } else { // nolp not enabled cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } } return cnt; }
/* deletions move set*/ PRIVATE int deletions(Encoded *Enc, struct_en *str, struct_en *minim){ int cnt = 0; short *pt = str->structure; int len = pt[0]; int i; if (Enc->verbose_lvl>1) { fprintf(stderr, " "); print_str(stderr, str->structure); fprintf(stderr, " %5d Deletions:\n", str->energy); } for (i=1; i<=len; i++) { if (pt[i]>pt[pt[i]]) { /* '('*/ Enc->bp_left=-i; Enc->bp_right=-pt[i]; /*if nolp enabled, make (maybe) 2nd delete*/ if (Enc->noLP) { /* is there a pair around? */ bool inside_pair = exists_base(pt, i+1, pt[i]-1); bool outside_pair = exists_base(pt, i-1, pt[i]+1); /* we would destroy 1/2 bpairs from inside a stack*/ if (inside_pair && outside_pair) continue; /* check if we are in noLP space */ if (!inside_pair && !outside_pair) { fprintf(stderr, "WRONG!!!! %d %d\n", i, pt[i]); } /* and make a delete or double delete if necessary, double delete do only from one side*/ if (inside_pair) { if (!exists_base(pt, i+2, pt[i]-2)) { Enc->bp_left2=-(i+1); Enc->bp_right2=-(pt[i]-1); } } else if (outside_pair) { if (!exists_base(pt, i-2, pt[i]+2)) { continue; } } } /* finally the delete */ cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } return cnt; }
/* insertions move set*/ PRIVATE int insertions(Encoded *Enc, struct_en *str, struct_en *minim){ int cnt = 0; short *pt = str->structure; int len = pt[0]; int i,j; if (Enc->verbose_lvl>1) { fprintf(stderr, " "); print_str(stderr, str->structure); fprintf(stderr, " %5d Insertions:\n", str->energy); } for (i=1; i<=len; i++) { if (pt[i]==0) { for (j=i+1; j<=len; j++) { /* end if found closing bracket*/ if (pt[j]!=0 && pt[j]<j) break; /*')'*/ if (pt[j]!=0 && pt[j]>j) { /*'('*/ j = pt[j]; continue; } /* if conditions are met, do insert*/ if (try_insert(pt, Enc->seq, i, j)) { Enc->bp_left=i; Enc->bp_right=j; if (Enc->noLP) { /* if lone bases occur, try inserting one another base*/ if (lone_base(pt, i, j)) { /* try only inside insert -- not to repeat structures*/ if (try_insert(pt, Enc->seq, i+1, j-1)) { /* but now, check if this second insert does not extend the stem */ if (pt[i+2] != 0 && pt[i+2] == pt[j-2]) continue; Enc->bp_left2=i+1; Enc->bp_right2=j-1; } else continue; } } // finally insert: cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } } } return cnt; }
/*shift move set*/ PRIVATE int shifts(Encoded *Enc, struct_en *str, struct_en *minim){ int cnt = 0; int brack_num = 0; short *pt = str->structure; int len = pt[0]; int i, k; for (i=1; i<=len; i++) { if (pt[i]!=0 && pt[i]>i) { /*'('*/ int j=pt[i]; /* outer switch left*/ if (Enc->verbose_lvl>1) fprintf(stderr, "%2d bracket %2d position, outer switch left\n", brack_num+1, i); for (k=i-1; k>0; k--) { if (pt[k]!=0 && pt[k]>k/*'('*/) break; if (pt[k]!=0 && pt[k]<k/*')'*/) { k = pt[k]; continue; } /* checks*/ if (pt[k]!=0) { fprintf(stderr, "WARNING: \'%c\'should be \'.\' at pos %d!\n", pt[k], k); } /* switch (i,j) to (k,j)*/ if (j-k>MINGAP && compat(Enc->seq[k-1], Enc->seq[j-1])) { Enc->bp_left=-i; Enc->bp_right=-j; Enc->bp_left2=k; Enc->bp_right2=j; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } /* switch (i,j) to (k,i)*/ if (i-k>MINGAP && compat(Enc->seq[i-1], Enc->seq[k-1])) { Enc->bp_left=-i; Enc->bp_right=-j; Enc->bp_left2=k; Enc->bp_right2=i; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } /* outer switch right*/ if (Enc->verbose_lvl>1) fprintf(stderr, "%2d bracket %2d position, outer switch right\n", brack_num+1, i); for (k=j+1; k<=len; k++) { if (pt[k]!=0 && pt[k]<k/*')'*/) break; if (pt[k]!=0 && pt[k]>k/*'('*/) { k = pt[k]; continue; } /* check*/ if (pt[k]!=0) { fprintf(stderr, "WARNING: \'%c\'should be \'.\' at pos %d!\n", pt[k], k); } /* switch (i,j) to (i,k)*/ if (k-i>MINGAP && compat(Enc->seq[i-1], Enc->seq[k-1])) { Enc->bp_left=-i; Enc->bp_right=-j; Enc->bp_left2=i; Enc->bp_right2=k; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } /* switch (i,j) to (j,k)*/ if (k-j>MINGAP && compat(Enc->seq[j-1], Enc->seq[k-1])) { Enc->bp_left=-i; Enc->bp_right=-j; Enc->bp_left2=j; Enc->bp_right2=k; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } if (Enc->verbose_lvl>1) fprintf(stderr, "%2d bracket %2d position, inner switch\n", brack_num+1, i); /* inner switch*/ for (k=i+1; k<j; k++) { /* jump to end of the sub-bracketing*/ if (pt[k]!=0 && pt[k]>k/*'('*/) { k=pt[k]; continue; } /* left switch (i,j) to (k,j)*/ if (j-k>MINGAP && compat(Enc->seq[k-1], Enc->seq[j-1])) { Enc->bp_left=-i; Enc->bp_right=-j; Enc->bp_left2=k; Enc->bp_right2=j; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } /* right switch (i,j) to (i,k)*/ if (k-i>MINGAP && compat(Enc->seq[i-1], Enc->seq[k-1])) { Enc->bp_left=-i; Enc->bp_right=-j; Enc->bp_left2=i; Enc->bp_right2=k; cnt += update_deepest(Enc, str, minim); /* in case useFirst is on and structure is found, end*/ if (Enc->first && cnt > 0) return cnt; } } /* end inner switch for*/ brack_num++; } /* end if (pt[i]=='(')*/ } /* end for in switches*/ return cnt; }
//shift move set int shifts(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose) { int cnt = 0; int brack_num = 0; short *pt = enc.pt; int len = pt[0]; for (int i=1; i<=len; i++) { if (pt[i]!=0 && pt[i]>i) { //'(' int j=pt[i]; // outer switch left if (verbose) fprintf(stderr, "%2d bracket %2d position, outer switch left\n", brack_num+1, i); for (int k=i-1; k>0; k--) { if (pt[k]!=0 && pt[k]>k/*'('*/) break; if (pt[k]!=0 && pt[k]<k/*')'*/) { k = pt[k]; continue; } // checks if (pt[k]!=0) { fprintf(stderr, "WARNING: \'%c\'should be \'.\' at pos %d!\n", pt[k], k); } // switch (i,j) to (k,j) if (j-k>MINGAP && compat(enc.seq[k-1], enc.seq[j-1])) { enc.bp_left=-i; enc.bp_right=-j; enc.bp_left2=k; enc.bp_right2=j; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } // switch (i,j) to (k,i) if (i-k>MINGAP && compat(enc.seq[i-1], enc.seq[k-1])) { enc.bp_left=-i; enc.bp_right=-j; enc.bp_left2=k; enc.bp_right2=i; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } // outer switch right if (verbose) fprintf(stderr, "%2d bracket %2d position, outer switch right\n", brack_num+1, i); for (int k=j+1; k<=len; k++) { if (pt[k]!=0 && pt[k]<k/*')'*/) break; if (pt[k]!=0 && pt[k]>k/*'('*/) { k = pt[k]; continue; } // check if (pt[k]!=0) { fprintf(stderr, "WARNING: \'%c\'should be \'.\' at pos %d!\n", pt[k], k); } // switch (i,j) to (i,k) if (k-i>MINGAP && compat(enc.seq[i-1], enc.seq[k-1])) { enc.bp_left=-i; enc.bp_right=-j; enc.bp_left2=i; enc.bp_right2=k; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } // switch (i,j) to (j,k) if (k-j>MINGAP && compat(enc.seq[j-1], enc.seq[k-1])) { enc.bp_left=-i; enc.bp_right=-j; enc.bp_left2=j; enc.bp_right2=k; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } if (verbose) fprintf(stderr, "%2d bracket %2d position, inner switch\n", brack_num+1, i); // inner switch for (int k=i+1; k<j; k++) { // jump to end of the sub-bracketing if (pt[k]!=0 && pt[k]>k/*'('*/) { k=pt[k]; continue; } // left switch (i,j) to (k,j) if (j-k>MINGAP && compat(enc.seq[k-1], enc.seq[j-1])) { enc.bp_left=-i; enc.bp_right=-j; enc.bp_left2=k; enc.bp_right2=j; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } // right switch (i,j) to (i,k) if (k-i>MINGAP && compat(enc.seq[i-1], enc.seq[k-1])) { enc.bp_left=-i; enc.bp_right=-j; enc.bp_left2=i; enc.bp_right2=k; cnt += update_deepest(enc, deepest, min_pt, deg, verbose); // in case useFirst is on and structure is found, end if (deg.opt->first && cnt > 0) return cnt; } } // end inner switch for brack_num++; } // end if (pt[i]=='(') } // end for in switches return cnt; }