Exemple #1
0
set<short*, setcomp> find_equal_energy(encoded &enc, int energy, degen &deg, bool direct, bool &lm)
{
  bool (*old_funct) (short *, int) = deg.opt->f_point;
  deg.opt->f_point = equal_energies;

  // count how many times called
  cnt_move++;

  // unused, just for necessity of functions
  short *min_pt = allocopy(enc.pt);

  // used
  deg.current = energy;

  // setup
  degen_energy = energy;
  degen_saddle = false;
  degen_done.clear();
  degen_undone.clear();
  degen_undone.insert(allocopy(enc.pt));

  //fprintf(stderr, " ins: %s\n", pt_to_str(enc.pt).c_str() );


  while (degen_undone.size()!=0) {

    // take first
    short *last = enc.pt;
    enc.pt = *degen_undone.begin();
    degen_undone.erase(degen_undone.begin());


    // move it
    insertions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
    deletions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);

    // shifts (only if enabled + noLP disabled)
    if (deg.opt->shift && !deg.opt->noLP) {
      shifts(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
    }

    degen_done.insert(enc.pt);
    //fprintf(stderr, "inserting(done): %s\n", pt_to_str(enc.pt).c_str());
    enc.pt = last;
  }

  free(min_pt);
  deg.opt->f_point = old_funct;

  lm = !degen_saddle;

  return degen_done;
}
Exemple #2
0
/*  sample usage: */
int main() {
  char seq[20] = "ACCCCCCTCTGTAGGGGGA";
  char str[20] = ".((.(.........).)).";

  /*  move to the local minimum and display it */
  int energy = move_standard(seq, str, GRADIENT, 0, 0, 0);
  fprintf(stdout, "%s %6.2f\n\n", str, energy/100.0);

  /* now create an array of every structure in neighbourhood of str structure */
  struct_en *list = NULL;
  int list_length = 0;

  int get_list(struct_en *new_one, struct_en *old_one)
  {
    /*  enlarge the list */
    list_length++;
    list = (struct_en*) realloc(list, list_length*sizeof(struct_en));

    /*  copy the structure */
    list[list_length-1].energy = new_one->energy;
    list[list_length-1].structure = allocopy(new_one->structure);

    /*  we want to continue -> return 0 */
    return 0;
  }
Exemple #3
0
// browse neighbours without degeneracy (almost the same as move_set) assume deg.first = true
bool browse_neighs(encoded &enc, int &energy, degen &deg, int &saddle_en)
{
  // count how many times called
  cnt_move++;

  // unused, just for necessity of functions
  short *min_pt = allocopy(enc.pt);

  // used
  deg.current = energy;

  // did we just find escape from basin?
  bool escape = false;

  //if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n  browse neighs:\n  %s %d\n\n", pt_to_str(enc.pt).c_str(), energy);

  escape = insertions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
  //if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n");

  // deletions
  if (!escape) escape = deletions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);

  // shifts (only if enabled + noLP disabled)
  if (deg.opt->shift && !deg.opt->noLP) {
    if (!escape) escape = shifts(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
  }

  if (escape) saddle_en = deg.current;

  free(min_pt);

  return escape;
}
Exemple #4
0
PUBLIC int
move_adaptive(char *string,
              short *ptable,
              short *s,
              short *s1,
              int verbosity_level){

  srand(time(NULL));

  Encoded enc;
  enc.seq = string;
  enc.s0 = s;
  enc.s1 = s1;

  /* moves*/
  enc.bp_left=0;
  enc.bp_right=0;
  enc.bp_left2=0;
  enc.bp_right2=0;

  /* options*/
  enc.noLP=0;
  enc.verbose_lvl=verbosity_level;
  enc.first=1;
  enc.shift=0;

  /* degeneracy*/
  enc.begin_unpr=0;
  enc.begin_pr=0;
  enc.end_unpr=0;
  enc.end_pr=0;
  enc.current_en=0;

  // function
  enc.funct=NULL;

  // allocate memory for moves
  enc.moves_from = (int*) space(ptable[0]*ptable[0]*sizeof(int));
  enc.moves_to = (int*) space(ptable[0]*ptable[0]*sizeof(int));

  int i;
  for (i=0; i<MAX_DEGEN; i++) enc.processed[i]=enc.unprocessed[i]=NULL;

  struct_en str;
  str.structure = allocopy(ptable);
  str.energy = energy_of_structure_pt(enc.seq, str.structure, enc.s0, enc.s1, 0);

  while (move_rset(&enc, &str)!=0) {
    free_degen(&enc);
  }
  free_degen(&enc);

  copy_arr(ptable, str.structure);
  free(str.structure);
  free(enc.moves_from);
  free(enc.moves_to);

  return str.energy;
}
Exemple #5
0
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;
}
Exemple #6
0
PUBLIC int
move_first( char *string,
            short *ptable,
            short *s,
            short *s1,
            int verbosity_level,
            int shifts,
            int noLP){

  cnt_move = 0;

  Encoded enc;
  enc.seq = string;
  enc.s0 = s;
  enc.s1 = s1;

  /* moves*/
  enc.bp_left=0;
  enc.bp_right=0;
  enc.bp_left2=0;
  enc.bp_right2=0;

  /* options*/
  enc.noLP=noLP;
  enc.verbose_lvl=verbosity_level;
  enc.first=1;
  enc.shift=shifts;

  /* degeneracy*/
  enc.begin_unpr=0;
  enc.begin_pr=0;
  enc.end_unpr=0;
  enc.end_pr=0;
  enc.current_en=0;

  /*  function */
  enc.funct=NULL;

  int i;
  for (i=0; i<MAX_DEGEN; i++) enc.processed[i]=enc.unprocessed[i]=NULL;

  struct_en str;
  str.structure = allocopy(ptable);
  str.energy = energy_of_structure_pt(enc.seq, str.structure, enc.s0, enc.s1, 0);

  while (move_set(&enc, &str)!=0) {
    free_degen(&enc);
  }
  free_degen(&enc);

  copy_arr(ptable, str.structure);
  free(str.structure);

  return str.energy;
}
Exemple #7
0
int DSU::AddLMtoTBD(short *tmp_str, int tmp_en, LMtype type, bool debug)
{
  RNAlocmin rna;
  rna.energy = tmp_en;
  rna.structure = allocopy(tmp_str);
  rna.str_ch = pt_to_chars_pk(tmp_str);
  //if (pknots) pt_to_str_pk(tmp_str, rna.str_ch);
  rna.type = type;

  // insert LM and return its number
  LM.push_back(rna);
  vertex_l[rna]=LM.size()-1;
  return LM.size()-1;
}
Exemple #8
0
bool equal_energies(short *str, int energy)
{
  if (energy == degen_energy) {
    if (degen_done.count(str)==0 && degen_undone.count(str)==0) {
      degen_undone.insert(allocopy(str));
      //if (DEBUGG) fprintf(stderr, "inserting(undn): %s\n", pt_to_str(str).c_str());
    }
  }
  if (energy < degen_energy) {
    degen_saddle = true;

    if (!direct) {    // collect info about same level structs
      unordered_map<short*, struct_info, hash_fncts, hash_eq>::iterator it = struct_map.find(str);
      // collect number sets (collect minima information)
      if (it!=struct_map.end()) {
        if (it->second.LM_nums.size()>0) numbers.insert(it->second.LM_nums);
        // collect saddle information
        saddles.insert(it->second.saddle_nums.begin(), it->second.saddle_nums.end());
      }
    }
  }

  return false;
}
Exemple #9
0
/* move to deepest (or first) neighbour*/
PRIVATE int
move_set(Encoded *Enc, struct_en *str){

  /* count how many times called*/
  cnt_move++;

  /* 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 MS:\n  "); print_str(stderr, str->structure); fprintf(stderr, " %d\n\n", str->energy); }

  /* if using first dont do all of them*/
  bool end = false;
  /* insertions*/
  if (!end) cnt += insertions(Enc, str, &min);
  if (Enc->first && cnt>0) end = true;
  if (Enc->verbose_lvl>1) fprintf(stderr, "\n");

  /* deletions*/
  if (!end) cnt += deletions(Enc, str, &min);
  if (Enc->first && cnt>0) end = true;

  /* shifts (only if enabled + noLP disabled)*/
  if (!end && Enc->shift && !Enc->noLP) {
    cnt += shifts(Enc, str, &min);
    if (Enc->first && cnt>0) end = true;
  }

  /* if degeneracy occurs, solve it!*/
  if (!end && (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_set(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 ((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);
  }

  if (Enc->verbose_lvl>1 && !(Enc->first)) { fprintf(stderr, "\n  end of MS:\n  "); print_str(stderr, str->structure); fprintf(stderr, " %d\n\n", str->energy); }

  return cnt;
}
Exemple #10
0
/* done with all structures along the way to deepest*/
PRIVATE int
update_deepest(Encoded *Enc, struct_en *str, struct_en *min){

  /* apply move + get its energy*/
  int tmp_en;
  tmp_en = str->energy + energy_of_move_pt(str->structure, Enc->s0, Enc->s1, Enc->bp_left, Enc->bp_right);
  do_move(str->structure, Enc->bp_left, Enc->bp_right);
  if (Enc->bp_left2 != 0) {
    tmp_en += energy_of_move_pt(str->structure, Enc->s0, Enc->s1, Enc->bp_left2, Enc->bp_right2);
    do_move(str->structure, Enc->bp_left2, Enc->bp_right2);
  }
  int last_en = str->energy;
  str->energy = tmp_en;


  /* use f_point if we have it */
  if (Enc->funct) {
    int end = Enc->funct(str, min);

    /*  undo moves */
    if (Enc->bp_left2!=0) do_move(str->structure, -Enc->bp_left2, -Enc->bp_right2);
    do_move(str->structure, -Enc->bp_left, -Enc->bp_right);
    str->energy = last_en;
    Enc->bp_left=0;
    Enc->bp_right=0;
    Enc->bp_left2=0;
    Enc->bp_right2=0;

    return (end?1:0);
  }

  if (Enc->verbose_lvl>1) { fprintf(stderr, "  "); print_str(stderr, str->structure); fprintf(stderr, " %d\n", tmp_en); }

  /* better deepest*/
  if (tmp_en < min->energy) {
    min->energy = tmp_en;
    copy_arr(min->structure, str->structure);

    /* delete degeneracy*/
    free_degen(Enc);

    /* undo moves*/
    if (Enc->bp_left2!=0) do_move(str->structure, -Enc->bp_left2, -Enc->bp_right2);
    do_move(str->structure, -Enc->bp_left, -Enc->bp_right);
    str->energy = last_en;
    Enc->bp_left=0;
    Enc->bp_right=0;
    Enc->bp_left2=0;
    Enc->bp_right2=0;
    return 1;
  }

  /* degeneracy*/
  if ((str->energy == min->energy) && (Enc->current_en == min->energy)) {
    int found = 0;
    int i;
    for (i=Enc->begin_pr; i<Enc->end_pr; i++) {
      if (equals(Enc->processed[i], str->structure)) {
        found = 1;
        break;
      }
    }
    for (i=Enc->begin_unpr; !found && i<Enc->end_unpr; i++) {
      if (equals(Enc->unprocessed[i], str->structure)) {
        found = 1;
        break;
      }
    }

    if (!found) {
      /* print_stren(stderr, str); // fprintf(stderr, " %6.2f\n", str->energy); */
      Enc->unprocessed[Enc->end_unpr]=allocopy(str->structure);
      Enc->end_unpr++;
    }
  }

  /* undo moves*/
  if (Enc->bp_left2!=0) do_move(str->structure, -Enc->bp_left2, -Enc->bp_right2);
  do_move(str->structure, -Enc->bp_left, -Enc->bp_right);
  str->energy = last_en;
  Enc->bp_left=0;
  Enc->bp_right=0;
  Enc->bp_left2=0;
  Enc->bp_right2=0;
  return 0;
}
Exemple #11
0
// move to deepest (or first) neighbour
int move_set(encoded &enc, int &deepest, degen &deg)
{
  // count how many times called
  cnt_move++;

  // count better neighbours
  int cnt = 0;

  // deepest descent
  deg.current = deepest;
  short *min_pt = allocopy(enc.pt);

  if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n  start of MS:\n  %s %d\n\n", pt_to_str(enc.pt).c_str(), deepest);

  // if using first dont do all of them
  bool end = false;
  // insertions
  if (!end) cnt += insertions(enc, deepest, min_pt, deg, deg.opt->verbose_lvl>3);
  if (deg.opt->first && cnt>0) end = true;
  fprintf(stderr, "\n");

  // deletions
  if (!end) cnt += deletions(enc, deepest, min_pt, deg, deg.opt->verbose_lvl>3);
  if (deg.opt->first && cnt>0) end = true;
  fprintf(stderr, "\n");

  // shifts (only if enabled + noLP disabled)
  if (!end && deg.opt->shift && !deg.opt->noLP) {
    cnt += shifts(enc, deepest, min_pt, deg, deg.opt->verbose_lvl>3);
    if (deg.opt->first && cnt>0) end = true;
  }

  if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n  %s\n  %s\n", enc.seq, pt_to_str(min_pt).c_str());

  // if degeneracy occurs, solve it!
  if (!end && deg.unprocessed.size()>0) {
    deg.processed.insert(allocopy(enc.pt));
    // take first
    if (enc.pt) free(enc.pt);
    enc.pt = (*deg.unprocessed.begin());
    deg.unprocessed.erase(deg.unprocessed.begin());
    if (deg.opt->minh>0.1001) {
      deepest = energy_of_structure_pt(enc.seq, enc.pt, enc.s0, enc.s1, 0);
    }
    cnt += move_set(enc, deepest, deg);
  } else {
    copy_arr(enc.pt, min_pt);
  }
  free(min_pt);

  // resolve degeneracy in local minima
  if (deg.processed.size()>0) {
    deg.processed.insert(enc.pt);
    enc.pt = *deg.processed.begin();
    deg.processed.erase(deg.processed.begin());

    erase_set(deg.processed);
  }

  return cnt;
}
Exemple #12
0
void DSU::ComputeTBD(TBD &pqueue, int maxkeep, int num_threshold, bool outer, bool noLP, bool shifts, bool debug, vector<RNAsaddle> *output_saddles, int conn_neighs, bool no_new)
{
  int cnt = 0;

  clock_t time_tbd = clock();

  // go through all pairs in queue
  while (pqueue.size()>0) {
    // check time:
    double time_secs = ((clock()  - time)/(double)CLOCKS_PER_SEC);
    if (stop_after && (time_secs > stop_after)) {
      fprintf(stderr, "Time threshold reached (%d secs.), processed %d/%d\n", stop_after, cnt, pqueue.size()+cnt);
      break;
    }

    // just visualisation
    if (!output_saddles && cnt%100==0) {
      double tim = (clock()  - time_tbd)/(double)CLOCKS_PER_SEC;
      std::pair<int, int> mem = getValue();
      //double one = ((sizeof(char)*strlen(seq) + sizeof(short)*strlen(seq)) + sizeof(RNAsaddle)) / 1024.0;
      fprintf(stderr, "Finding path: %7d/%7d; Time: %6.2f; Est.:%6.2f Mem.:%6.1fMB VM %6.1fMB PM\n", cnt, pqueue.size()+cnt, tim, tim/(double)cnt*pqueue.size(), mem.first/1024.0, mem.second/1024.0);
    }

    // apply threshold
    if (cnt>num_threshold) {
      fprintf(stderr, "Number threshold reached, processed %d/%d\n", cnt, pqueue.size()+cnt);
      break;
    } else {
      cnt++;
    }

    // get next
    TBDentry tbd = pqueue.get_first();
    if (tbd.i==-1) {
      fprintf(stderr, "Ending the path-finding -- i = %5d ; j = %5d ; fiber = %c ; type = %s \n", tbd.i, tbd.j, tbd.fiber?'Y':'N', type1_str[tbd.type_clust]);
      break;
    }

    // check no-conn
    if (conectivity.size() > 0 && !tbd.fiber && conectivity.joint(tbd.i, tbd.j)) continue;


    // get path
    if (debug) fprintf(stderr, "path between (%3d, %3d) type=%s fiber=%d:\n", tbd.i, tbd.j, type1_str[tbd.type_clust], tbd.fiber);
    //2fprintf(stderr, "depth: %d\n%s\n%s\n%s\n", maxkeep, seq, LM[tbd.i].str_ch, LM[tbd.j].str_ch);
    if (pknots) {
      path_pk *path = get_path_light_pk(seq, LM[tbd.i].structure, LM[tbd.j].structure, maxkeep);

      // variables for outer insertion
      double max_energy= -1e8;
      path_pk *max_path = path;

      // variables for inner loops and insertions

      // get the length of path for speed up
      int length = 0;
      for (path_pk *tmp = path; tmp && tmp->structure; tmp++) {
        if (max_path->en < tmp->en) max_path = tmp;
        length ++;
      }

      // create vector of known LM numbers on path (where 0 and length-1 are known)
      vector<int> lm_numbers(length, -1);
      lm_numbers[0] = tbd.i;
      lm_numbers[length-1] = tbd.j;

      // debug
      if (debug) {
        for (int i=0; i<length; i++) {
          fprintf(stderr, "path[%3d] %s %6.2f\n", i, pt_to_str_pk(path[i].structure).c_str(), path[i].en/100.0);
        }
      }

      // bisect the path and find new LMs:
      unsigned int old_size = LM.size();
      FindNumbers(0, length-1, path, lm_numbers, shifts, noLP, debug, no_new);

      // if we have found new minima and we want to do more than simple reevaluation of path (--conn-neighs>0)
      if (LM.size() - old_size > 0 && conn_neighs > 0 && !no_new) {
        for (unsigned int j=old_size; j<LM.size(); j++) {

          // sort 'em according to Hamming D. and take first "conn_neighs"
          multimap<int, int> distances;
          for (unsigned int i=0; i<old_size; i++) {
            distances.insert(make_pair(HammingDist(LM[i].structure, LM[j].structure), i));
          }
          int cnt = 0;
          int last_hd = -1;
          for (auto it=distances.begin(); it!=distances.end(); it++) {
            if (cnt > conn_neighs && last_hd != it->first) {
              break;
            }

            pqueue.insert(it->second, j, EXPERIM, false);

            cnt++;
            last_hd = it->first;
          }
        }

      }

      // debug
      if (debug) {
        int diff = 1;
        int last_num = lm_numbers[0];
        for (int i=0; i<length; i++) {
          fprintf(stderr, "path[%3d]= %4d (%s %6.2f)\n", i, lm_numbers[i], pt_to_str_pk(path[i].structure).c_str(), path[i].en/100.0);
          if (lm_numbers[i]!=last_num && lm_numbers[i]!=-1) {
            diff++;
            last_num=lm_numbers[i];
          }
        }
        histo[length][diff]++;
        histo[length][0]++;
      }

      // now process the array of found numbers:
      int last_num = lm_numbers[0];
      for (int i=1; i<length; i++) {
        if (lm_numbers[i]!=-1 && lm_numbers[i]!=last_num) {

          // get the highest saddle in case we traveled through many "-1" saddles:
          int j=i-1;
          int highest_num = i;
          while (j>0) {
            // check if not higher saddle:
            if (path[highest_num].en < path[j].en) highest_num = j;

            // we found first that is not -1
            if (lm_numbers[j]!=-1) break;
            j--;
          }

          // save saddle
          SDtype typ = (j==i-1?DIRECT:REDUCED);
          RNAsaddle saddle(last_num, lm_numbers[i], typ);
          saddle.energy = path[highest_num].en;
          saddle.str_ch = NULL;
          saddle.structure = allocopy(path[highest_num].structure);
          bool inserted = InsertUB(saddle, debug);

          // ???
          if (output_saddles && inserted) {
            output_saddles->push_back(saddle);
          }

          // try to insert new things into TBD:
          if ((lm_numbers[i]!=lm_numbers[length-1] || lm_numbers[i-1]!=lm_numbers[0]) && !no_new) {
            // check no-conn
            if (conectivity.size() > 0) conectivity.union_set(tbd.i, tbd.j);
            pqueue.insert(lm_numbers[i-1], lm_numbers[i], NEW_FOUND, true);


          }
          last_num = lm_numbers[i];
        }
      }

      // insert saddle between outer structures
      if (outer) {
        RNAsaddle tmp(tbd.i, tbd.j, NOT_SURE);
        tmp.energy = en_fltoi(max_energy);
        tmp.str_ch = NULL;
        tmp.structure = allocopy(max_path->structure);

        bool inserted = InsertUB(tmp, debug);

        if (output_saddles && inserted) {
          output_saddles->push_back(tmp);
        }
      }

      free_path_pk(path);
    } else {
      //fprintf(stderr, "%s\n%s\n%s\n", seq, LM[tbd.i].str_ch, LM[tbd.j].str_ch);
      path_t *path = get_path(seq, LM[tbd.i].str_ch, LM[tbd.j].str_ch, maxkeep);

      // variables for outer insertion
      double max_energy= -1e8;
      path_t *max_path = path;

      // variables for inner loops and insertions

      // get the length of path for speed up
      int length = 0;
      for (path_t *tmp = path; tmp && tmp->s; tmp++) {
        if (max_path->en < tmp->en) max_path = tmp;
        length ++;
      }

      // create vector of known LM numbers on path (where 0 and length-1 are known)
      vector<int> lm_numbers(length, -1);
      lm_numbers[0] = tbd.i;
      lm_numbers[length-1] = tbd.j;

      // bisect the path and find new LMs:
      unsigned int old_size = LM.size();
      FindNumbers(0, length-1, path, lm_numbers, shifts, noLP, debug, no_new);

      // if we have found new minima and we want to do more than simple reevaluation of path (--conn-neighs>0)
      if (LM.size() - old_size > 0 && conn_neighs > 0 && !no_new) {
        for (unsigned int j=old_size; j<LM.size(); j++) {

          // sort 'em according to Hamming D. and take first "conn_neighs"
          multimap<int, int> distances;
          for (unsigned int i=0; i<old_size; i++) {
            distances.insert(make_pair(HammingDist(LM[i].structure, LM[j].structure), i));
          }
          int cnt = 0;
          int last_hd = -1;
          for (auto it=distances.begin(); it!=distances.end(); it++) {
            if (cnt > conn_neighs && last_hd != it->first) {
              break;
            }

            pqueue.insert(it->second, j, EXPERIM, false);

            cnt++;
            last_hd = it->first;
          }
        }

      }

      // debug
      if (debug) {
        int diff = 1;
        int last_num = lm_numbers[0];
        for (int i=0; i<length; i++) {
          fprintf(stderr, "path[%3d]= %4d (%s %6.2f)\n", i, lm_numbers[i], path[i].s, path[i].en);
          if (lm_numbers[i]!=last_num && lm_numbers[i]!=-1) {
            diff++;
            last_num=lm_numbers[i];
          }
        }
        histo[length][diff]++;
        histo[length][0]++;
      }

      // now process the array of found numbers:
      int last_num = lm_numbers[0];
      for (int i=1; i<length; i++) {
        if (lm_numbers[i]!=-1 && lm_numbers[i]!=last_num) {

          // get the highest saddle in case we traveled through many "-1" saddles:
          int j=i-1;
          int highest_num = i;
          while (j>0) {
            // check if not higher saddle:
            if (path[highest_num].en < path[j].en) highest_num = j;

            // we found first that is not -1
            if (lm_numbers[j]!=-1) break;
            j--;
          }

          // save saddle
          SDtype typ = (j==i-1?DIRECT:REDUCED);
          RNAsaddle saddle(last_num, lm_numbers[i], typ);
          saddle.energy = en_fltoi(path[highest_num].en);
          saddle.str_ch = NULL;
          saddle.structure = make_pair_table(path[highest_num].s);
          bool inserted = InsertUB(saddle, debug);

          // ???
          if (output_saddles && inserted) {
            output_saddles->push_back(saddle);
          }

          // try to insert new things into TBD:
          if ((lm_numbers[i]!=lm_numbers[length-1] || lm_numbers[i-1]!=lm_numbers[0]) && !no_new) {
            // check no-conn
            if (conectivity.size() > 0) conectivity.union_set(tbd.i, tbd.j);
            pqueue.insert(lm_numbers[i-1], lm_numbers[i], NEW_FOUND, true);
          }
          last_num = lm_numbers[i];
        }
      }

      // insert saddle between outer structures
      if (outer) {
        RNAsaddle tmp(tbd.i, tbd.j, NOT_SURE);
        tmp.energy = en_fltoi(max_energy);
        tmp.str_ch = NULL;
        tmp.structure = make_pair_table(max_path->s);

        bool inserted = InsertUB(tmp, debug);

        if (output_saddles && inserted) {
          output_saddles->push_back(tmp);
        }
      }

      free_path(path);
    }

    // free stuff
    //if (last_str) free(last_str);

  } // all doing while
  fprintf(stderr, "The end of finding paths(%d). Size of pqueue = %d\n", cnt, (int)pqueue.size());
}
Exemple #13
0
void DSU::FindNumbers(int begin, int end, path_pk *path, vector<int> &lm_numbers, bool shifts, bool noLP, bool debug, bool no_new)
{
  // first resolve small case:
  if (end-begin<4) {
    bool begins = true;
    for (int i=begin+1; i<end; i++) {

      // get the minimum
      short *tmp_str = allocopy(path[i].structure);
      int tmp_en;
      if (pknots) {
        Structure str(seq, tmp_str, s0, s1);
        tmp_en = move_gradient_pk(seq, &str, s0, s1, shifts, 0);
        copy_arr(tmp_str, str.str);
      } else {
        tmp_en = move_gradient(seq, tmp_str, s0, s1, 0, shifts, noLP);
      }
      // speedup
      if (lm_numbers[begin] != -1 && begins && tmp_en == LM[lm_numbers[begin]].energy && str_eq(LM[lm_numbers[begin]].structure, tmp_str)) {
        lm_numbers[i] = lm_numbers[begin];
      } else {
        begins = false;
        if (lm_numbers[end] != -1 && tmp_en == LM[lm_numbers[end]].energy && str_eq(LM[lm_numbers[end]].structure, tmp_str)) {
          lm_numbers[i] = lm_numbers[end];
        }
      }

      if (lm_numbers[i]==-1) {
        lm_numbers[i] = FindNum(tmp_en, tmp_str);

        // update UBlist
        if (lm_numbers[i]==-1 && !no_new) {
          if (gl_maxen < tmp_en) {
            //fprintf(stderr, "exceeds en.: %s %6.2f\n", pt_to_str(tmp_str).c_str(), tmp_en/100.0);
            lm_numbers[i] = AddLMtoTBD(tmp_str, tmp_en, EE_DSU, debug);

          } else {
            if (debug) fprintf(stderr, "cannot find: %s %6.2f\n", pt_to_str_pk(tmp_str).c_str(), tmp_en/100.0);
            // add to list of minima and count with them later...
            lm_numbers[i] = AddLMtoTBD(tmp_str, tmp_en, NORM_CF, debug);
          }
        }
      } else debug_c++;

      free(tmp_str);
    }
    return ;
  }

  // da middle one
  int pivot = (end+begin)/2;

  short *tmp_str = allocopy(path[pivot].structure);
  //fprintf(stderr, "%s\n", pt_to_str(tmp_str).c_str());
  int tmp_en;
  if (pknots) {
    Structure str(seq, tmp_str, s0, s1);
    tmp_en = move_gradient_pk(seq, &str, s0, s1, shifts, 0);
    copy_arr(tmp_str, str.str);
  } else {
    tmp_en = move_gradient(seq, tmp_str, s0, s1, 0, shifts, noLP);
  }
  //fprintf(stderr, "%s\n", pt_to_str(tmp_str).c_str());

  // speed up:
  if (lm_numbers[begin] != -1 && tmp_en == LM[lm_numbers[begin]].energy && str_eq(LM[lm_numbers[begin]].structure, tmp_str)) {
    lm_numbers[pivot] = lm_numbers[begin];
  } else {
    if (lm_numbers[end] != -1 && tmp_en == LM[lm_numbers[end]].energy && str_eq(LM[lm_numbers[end]].structure, tmp_str)) {
      lm_numbers[pivot] = lm_numbers[end];
    }
  }

  // normal behaviour
  if (lm_numbers[pivot]==-1) {
    lm_numbers[pivot] = FindNum(tmp_en, tmp_str);

    // update UBlist
    if (lm_numbers[pivot]==-1 && !no_new) {
      if (gl_maxen < tmp_en) {
        //fprintf(stderr, "exceeds en.: %s %6.2f\n", pt_to_str(tmp_str).c_str(), tmp_en/100.0);
        lm_numbers[pivot] = AddLMtoTBD(tmp_str, tmp_en, EE_DSU, debug);

      } else {
        if (debug) fprintf(stderr, "cannot find: %s %6.2f\n", pt_to_str_pk(tmp_str).c_str(), tmp_en/100.0);
        // add to list of minima and count with them later...
        lm_numbers[pivot] = AddLMtoTBD(tmp_str, tmp_en, NORM_CF, debug);
      }
    }
  } else debug_c++;

  free(tmp_str);

  // continue recursion:
  if ((lm_numbers[pivot]!=lm_numbers[begin] || lm_numbers[pivot]==-1) && pivot-begin>1) FindNumbers(begin, pivot, path, lm_numbers, shifts, noLP, debug, no_new);
  if ((lm_numbers[pivot]!=lm_numbers[end]   || lm_numbers[pivot]==-1)  && end-pivot>1) FindNumbers(pivot, end, path, lm_numbers, shifts, noLP, debug, no_new);

  // return maximal energy
  return ;
}