Exemplo n.º 1
0
Arquivo: brot.c Projeto: marcom/corb
int
brot_main(const char *cmdline)
{
   struct brot_args_info brot_args;
   SeqMatrix* sm               = NULL;
   int retval                  = 0;
   Scmf_Rna_Opt_data* sim_data = NULL;
   GFile* entropy_file = NULL;

   /* command line parsing */
   brot_cmdline_parser_init (&brot_args);

   retval = brot_cmdline_parser_string (cmdline, &brot_args, get_progname());

   if (retval == 0)
   {
      retval = brot_cmdline_parser_required (&brot_args, get_progname());
   }

   /* postprocess arguments */
   if (retval == 0)
   {
      retval = brot_cmdline_parser_postprocess (&brot_args);
   }

   /* init simulation data */
   if (retval == 0)
   {
      sim_data = SCMF_RNA_OPT_DATA_NEW_INIT(brot_args.inputs[1], 
                                            strlen (brot_args.inputs[1]),
                                            RNA_ALPHABET,
                                            strlen(RNA_ALPHABET)/2,
            ((-1) * ((logf (1 / 0.000001f)) / (strlen (brot_args.inputs[1])))));
      if (sim_data == NULL)
      {
         retval = 1;
      }
   }

   /* init matrix */
   if (retval == 0)
   {
      sm = SEQMATRIX_NEW;
      if (sm != NULL)
      {
         retval = SEQMATRIX_INIT (
            alphabet_size (scmf_rna_opt_data_get_alphabet (sim_data)),
                                  strlen (brot_args.inputs[1]),
                                  sm);
         /*seqmatrix_print_2_stdout (6, sm);*/
      }
      else
      {
         retval = 1;
      }
   }

   /* fix certain sites in the matrix */
   if (retval == 0)
   {
      retval = adopt_site_presettings (&brot_args,
                                      scmf_rna_opt_data_get_alphabet (sim_data),
                                       sm);
   }

   /* open entropy file if name given */
   if (brot_args.entropy_output_given)
   {
      entropy_file = GFILE_OPEN (brot_args.entropy_output_arg,
                                 strlen (brot_args.entropy_output_arg),
                                 GFILE_VOID, "a");
      if (entropy_file == NULL)
      {
         retval = 1;
      }
      else
      {
         if (gfile_printf (entropy_file, "# bROT settings:\n") < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# steps: %lu\n",
                                brot_args.steps_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# temp:              %f\n",
                                brot_args.temp_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# beta-short:        %f\n",
                                brot_args.beta_short_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# beta-long:         %f\n",
                                brot_args.beta_long_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# speedup-threshold: %f\n",
                                brot_args.speedup_threshold_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# min-cool:          %f\n",
                                brot_args.min_cool_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# scale-cool:        %f\n",
                                brot_args.scale_cool_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# lambda:            %f\n",
                                brot_args.lambda_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# sm-entropy:        %f\n",
                                brot_args.sm_entropy_arg) < 0)
         {
            retval = 1;
         }
      }
   }

   if (retval == 0)
   {
      if (brot_args.scoring_arg == scoring_arg_simpleNN)
      {
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_simplenn_scoring (&brot_args,
                                                      sm,
                                                      sim_data,
                                                      entropy_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
      else if (brot_args.scoring_arg == scoring_arg_nussinov)
      {
         retval = simulate_using_nussinov_scoring (&brot_args,
                                                   sm,
                                                   sim_data,
                                                   entropy_file);
      }
      else if (brot_args.scoring_arg == scoring_arg_NN)
      {
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_nn_scoring (&brot_args,
                                                sm,
                                                sim_data,
                                                entropy_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
   }

   /* close entropy file */
   if (retval == 0)
   {
      retval = gfile_close (entropy_file);
   }
   else
   {
      gfile_close (entropy_file);
   }

   if (retval == 0)
   {
      /* seqmatrix_print_2_stdout (2, sm); */
      mprintf ("%s\n", scmf_rna_opt_data_get_seq(sim_data));
   }   

   /* finalise */
   brot_cmdline_parser_free (&brot_args);
   seqmatrix_delete (sm);
   scmf_rna_opt_data_delete (sim_data);

   if (retval == 0)
   {
      return EXIT_SUCCESS;
   }
   else
   {
      return EXIT_FAILURE;
   }
}
Exemplo n.º 2
0
Arquivo: brot.c Projeto: marcom/corb
static int
simulate_using_simplenn_scoring (struct brot_args_info* brot_args,
                                 SeqMatrix* sm,
                                 Scmf_Rna_Opt_data* data,
                                 GFile* entropy_file)
{
   int error = 0;
   char** bp_allowed = NULL;
   char bi, bj;
   unsigned long i, j, k;
   unsigned long alpha_size
      = alphabet_size (scmf_rna_opt_data_get_alphabet(data));
   unsigned long allowed_bp = 0;
   NN_scores* scores =
      NN_SCORES_NEW_INIT(0, scmf_rna_opt_data_get_alphabet (data));

   if (scores == NULL)
   {
      error = 1;
   }

   /* prepare index of allowed base pairs */
   if (!error)
   {
      bp_allowed = XMALLOC(alpha_size * sizeof (*bp_allowed));
      if (bp_allowed == NULL)
      {
         error = 1;
      }
   }

   if (!error)
   {

      allowed_bp = nn_scores_no_allowed_basepairs (scores);

      /* we need 1 byte for each possible pair + 1byte for the NULL byte for
         each letter in the alphabet */
      bp_allowed[0] = XCALLOC (allowed_bp + alpha_size,
                               sizeof (**(bp_allowed)));
      if (bp_allowed[0] == NULL)
      {
         error = 1;
      }
   }

   if (!error)
   {
      i = 0;
      k = 0;
      while (i < alpha_size)
      {
         bp_allowed[i] = bp_allowed[0] + (k * sizeof (**(bp_allowed)));
         
         for (j = 0; j < allowed_bp; j++)
         {
            nn_scores_get_allowed_basepair (j, &bi, &bj, scores);
            if (i == (unsigned) bi)
            {
               bp_allowed[0][k] = bj + 1;
               k++;
            }
         }
         
         k++;
         i++;
      }
   }

   /* simulate */
   if (!error)
   {
      scmf_rna_opt_data_set_scores (scores, data);
      scmf_rna_opt_data_set_bp_allowed (bp_allowed, data);

      seqmatrix_set_gas_constant (8.314472, sm);

      seqmatrix_set_func_calc_cell_energy (scmf_rna_opt_calc_simplenn, sm);
      /*scmf_rna_opt_data_init_negative_design_energies (data, sm);*/
      seqmatrix_set_pre_col_iter_hook (
        scmf_rna_opt_data_init_negative_design_energies_alt, sm);
      error = seqmatrix_simulate_scmf (brot_args->steps_arg,
                                       brot_args->temp_arg,
                                       brot_args->beta_long_arg,
                                       brot_args->beta_short_arg,
                                       brot_args->speedup_threshold_arg,
                                       brot_args->min_cool_arg,
                                       brot_args->scale_cool_arg,
                                       brot_args->lambda_arg,
                                       brot_args->sm_entropy_arg,
                                       entropy_file,
                                       sm,
                                       data);
   }

   /* collate */
   if (!error)
   {
      seqmatrix_set_transform_row (scmf_rna_opt_data_transform_row_2_base, sm);
      seqmatrix_set_fixed_site_hook (scmf_rna_opt_data_update_neg_design_energy,
                                     sm);

      error = seqmatrix_collate_is (0.99,
                                    brot_args->steps_arg / 2,
                                    brot_args->temp_arg, 
                                    brot_args->beta_long_arg,
                                    brot_args->beta_short_arg,
                                    brot_args->speedup_threshold_arg,
                                    brot_args->min_cool_arg,
                                    brot_args->scale_cool_arg,
                                    brot_args->lambda_arg,
                                    brot_args->sm_entropy_arg,
                                    sm,
                                    data);
      /*error = seqmatrix_collate_mv (sm, data);*/
   }

   nn_scores_delete (scores);
   scmf_rna_opt_data_set_scores (NULL, data);
   scmf_rna_opt_data_set_bp_allowed (NULL, data);

   XFREE (bp_allowed[0]);
   XFREE (bp_allowed);

   return error;
}
Exemplo n.º 3
0
Arquivo: brot.c Projeto: marcom/corb
static int
simulate_using_nn_scoring (struct brot_args_info* brot_args,
                           SeqMatrix* sm,
                           Scmf_Rna_Opt_data* data,
                           GFile* entropy_file)
{
   int error = 0;
   char** bp_allowed = NULL;
   char bi, bj;
   unsigned long i, j, k;
   unsigned long alpha_size
      = alphabet_size (scmf_rna_opt_data_get_alphabet(data));
   unsigned long allowed_bp = 0;
   NN_scores* scores =
      NN_SCORES_NEW_INIT(50.0f, scmf_rna_opt_data_get_alphabet (data));

   if (scores == NULL)
   {
      error = 1;
   }

   /* prepare index of allowed base pairs */
   if (!error)
   {
      /* "randomise" scoring function */
      mfprintf (stdout, "Using seed: %ld\n", brot_args->seed_arg);
      nn_scores_add_thermal_noise (alpha_size, brot_args->seed_arg, scores);

      bp_allowed = XMALLOC(alpha_size * sizeof (*bp_allowed));
      if (bp_allowed == NULL)
      {
         error = 1;
      }
   }

   if (!error)
   {
      allowed_bp = nn_scores_no_allowed_basepairs (scores);

      /* we need 1 byte for each possible pair + 1byte for the NULL byte for
         each letter in the alphabet */
      bp_allowed[0] = XCALLOC (allowed_bp + alpha_size,
                               sizeof (**(bp_allowed)));
      if (bp_allowed[0] == NULL)
      {
         error = 1;
      }
   }

   if (!error)
   {
      i = 0;
      k = 0;
      while (i < alpha_size)
      {
         bp_allowed[i] = bp_allowed[0] + (k * sizeof (**(bp_allowed)));
         
         for (j = 0; j < allowed_bp; j++)
         {
            nn_scores_get_allowed_basepair (j, &bi, &bj, scores);
            if (i == (unsigned) bi)
            {
               bp_allowed[0][k] = bj + 1;
               k++;
            }
         }
         
         k++;
         i++;
      }
   }

   /* decompose secondary structure */
   if (!error)
   {
      error = scmf_rna_opt_data_secstruct_init (data);
   }

   mfprintf (stdout, "TREATMENT of fixed sites: If both sites of a pair are "
             "fixed, delete from list? Verbose info!!!\n");

   /* set our special function for calc. cols.: Iterate over sec.struct., not
      sequence matrix! */
   if (!error)
   {
      scmf_rna_opt_data_set_scores (scores, data);
      scmf_rna_opt_data_set_bp_allowed (bp_allowed, data);
      scmf_rna_opt_data_set_scales (brot_args->negative_design_scaling_arg,
                                    brot_args->heterogenity_term_scaling_arg,
                                    data);
      scmf_rna_opt_data_set_het_window (brot_args->window_size_arg, data);

      seqmatrix_set_func_calc_eeff_col (scmf_rna_opt_calc_col_nn, sm);
      seqmatrix_set_gas_constant (8.314472, sm);

      error = seqmatrix_simulate_scmf (brot_args->steps_arg,
                                       brot_args->temp_arg,
                                       brot_args->beta_long_arg,
                                       brot_args->beta_short_arg,
                                       brot_args->speedup_threshold_arg,
                                       brot_args->min_cool_arg,
                                       brot_args->scale_cool_arg,
                                       brot_args->lambda_arg,
                                       brot_args->sm_entropy_arg,
                                       entropy_file,
                                       sm,
                                       data);
   }

   /* collate */
   if (!error)
   {
     /*  seqmatrix_print_2_stdout (2, sm); */
      seqmatrix_set_transform_row (scmf_rna_opt_data_transform_row_2_base, sm);

      error = seqmatrix_collate_is (0.99,
                                    brot_args->steps_arg / 2,
                                    brot_args->temp_arg,
                                    brot_args->beta_long_arg,
                                    brot_args->beta_short_arg,
                                    brot_args->speedup_threshold_arg,
                                    brot_args->min_cool_arg,
                                    brot_args->scale_cool_arg,
                                    brot_args->lambda_arg,
                                    brot_args->sm_entropy_arg,
                                    sm,
                                    data);
      /*error = seqmatrix_collate_mv (sm, data);*/
   }

   /* first: iterate scmf on secstruct, not sm! */

   nn_scores_delete (scores);
   scmf_rna_opt_data_set_scores (NULL, data);
   scmf_rna_opt_data_set_bp_allowed (NULL, data);

   XFREE (bp_allowed[0]);
   XFREE (bp_allowed);

   return error;
}
Exemplo n.º 4
0
Arquivo: brot.c Projeto: bienchen/corb
int
brot_main(const char *cmdline)
{
   struct brot_args_info brot_args;
   SeqMatrix* sm               = NULL;
   int retval                  = 0;
   Scmf_Rna_Opt_data* sim_data = NULL;
   GFile* entropy_file = NULL;
   GFile* simulation_file = NULL;

   /* command line parsing */
   brot_cmdline_parser_init (&brot_args);

   retval = brot_cmdline_parser_string (cmdline, &brot_args, get_progname());

   if (retval == 0)
   {
      retval = brot_cmdline_parser_required (&brot_args, get_progname());
   }

   /* postprocess arguments */
   if (retval == 0)
   {
      retval = brot_cmdline_parser_postprocess (&brot_args, cmdline);
   }

   /* init simulation data */
   if (retval == 0)
   {
      sim_data = SCMF_RNA_OPT_DATA_NEW_INIT(brot_args.inputs[1], 
                                            strlen (brot_args.inputs[1]),
                                            RNA_ALPHABET,
                                            strlen(RNA_ALPHABET)/2,
             ((-1) * ((logf (1 / 0.000001f)) / (strlen (brot_args.inputs[1])))),
                                            brot_args.file_given);
      if (sim_data == NULL)
      {
         retval = 1;
      }
   }

   /* init matrix */
   if (retval == 0)
   {
      sm = SEQMATRIX_NEW;
      if (sm != NULL)
      {
         retval = SEQMATRIX_INIT (
            alphabet_size (scmf_rna_opt_data_get_alphabet (sim_data)),
                                      scmf_rna_opt_data_get_rna_size(sim_data),
                                      /* strlen (brot_args.inputs[1]), */
                                  sm);
         /*seqmatrix_print_2_stdout (6, sm);*/
      }
      else
      {
         retval = 1;
      }
   }

   /* fix certain sites in the matrix */
   if (retval == 0)
   {
      retval = adopt_site_presettings (&brot_args,
                                      scmf_rna_opt_data_get_alphabet (sim_data),
                                       sim_data,
                                       sm);
   }

   /* open entropy file if name given */
   if (retval == 0)
   {
      print_verbose ("# Entropy file            (-p): ");
      if (brot_args.entropy_output_given)
      {
         print_verbose ("%s", brot_args.entropy_output_arg);
         entropy_file = GFILE_OPEN (brot_args.entropy_output_arg,
                                    strlen (brot_args.entropy_output_arg),
                                    GFILE_VOID, "a");
         if (entropy_file == NULL)
         {
            retval = 1;
         }
         else
         {
            retval = brot_settings_2_file (entropy_file, cmdline);
         }
      }
      /* open simulation/ matrix file if name given */
      print_verbose ("\n# Simulation/ Matrix file (-m): ");
      if (brot_args.simulation_output_given)
      {
         print_verbose ("%s", brot_args.simulation_output_arg);
         simulation_file = GFILE_OPEN (brot_args.simulation_output_arg,
                                       strlen (brot_args.simulation_output_arg),
                                       GFILE_VOID, "a");
         
         if (simulation_file == NULL)
         {
            retval = 1;
         }
         else
         {
            retval = brot_settings_2_file (simulation_file, cmdline);
            if (retval == 0)
            {
               if (gfile_printf (simulation_file, "START\n") < 0)
               {
                  retval = 1;
               }
            }
         }
      }
   }
   
   if (retval == 0)
   {
      print_verbose ("\n# Scoring scheme          (-c): ");

      if (brot_args.scoring_arg == scoring_arg_simpleNN)
      {
         print_verbose ("simpleNN\n");
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_simplenn_scoring (&brot_args,
                                                      sm,
                                                      sim_data,
                                                      entropy_file,
                                                      simulation_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
      else if (brot_args.scoring_arg == scoring_arg_nussinov)
      {
         print_verbose ("nussinov\n");
         retval = simulate_using_nussinov_scoring (&brot_args,
                                                   sm,
                                                   sim_data,
                                                   entropy_file,
                                                   simulation_file);
      }
      else if (brot_args.scoring_arg == scoring_arg_NN)
      {
         print_verbose ("NN\n");
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_nn_scoring (&brot_args,
                                                sm,
                                                sim_data,
                                                entropy_file,
                                                simulation_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
   }

   /* close files */
   if (retval == 0)
   {
      retval = gfile_close (entropy_file);
   }
   else
   {
      gfile_close (entropy_file);
   }

   if (simulation_file != NULL)
   {
      if (gfile_printf (simulation_file, "END\n") < 0)
      {
         retval = 1;
      }
   }

   if (retval == 0)
   {
      retval = gfile_close (simulation_file);
   }
   else
   {
      gfile_close (simulation_file);
   }

   if (retval == 0)
   {
      /*seqmatrix_print_2_stdout (2, sm);*/
      mprintf ("%s\n", scmf_rna_opt_data_get_seq(sim_data));
   }

   /* finalise */
   brot_cmdline_parser_free (&brot_args);
   seqmatrix_delete (sm);
   scmf_rna_opt_data_delete (sim_data);

   if (retval == 0)
   {
      return EXIT_SUCCESS;
   }
   else
   {
      return EXIT_FAILURE;
   }
}