Ejemplo n.º 1
0
float fold(const char *string, char *structure) {
  int i, length, energy, bonus=0, bonus_cnt=0;

  circ = 0;
  length = (int) strlen(string);
  if (length>init_length) initialize_fold(length);
  if (fabs(P->temperature - temperature)>1e-6) update_fold_params();

  encode_seq(string);

  BP = (int *)space(sizeof(int)*(length+2));
  make_ptypes(S, structure);

  energy = fill_arrays(string);

  backtrack(string, 0);

#ifdef PAREN
  parenthesis_structure(structure, length);
#else
  letter_structure(structure, length);
#endif

  /* check constraints */
  for(i=1;i<=length;i++) {
    if((BP[i]<0)&&(BP[i]>-4)) {
      bonus_cnt++;
      if((BP[i]==-3)&&(structure[i-1]==')')) bonus++;
      if((BP[i]==-2)&&(structure[i-1]=='(')) bonus++;
      if((BP[i]==-1)&&(structure[i-1]!='.')) bonus++;
    }

    if(BP[i]>i) {
      int l;
      bonus_cnt++;
      for(l=1; l<=base_pair[0].i; l++)
	if((i==base_pair[l].i)&&(BP[i]==base_pair[l].j)) bonus++;
    }
  }

  if (bonus_cnt>bonus) fprintf(stderr,"\ncould not enforce all constraints\n");
  bonus*=BONUS;

  free(S); free(S1); free(BP);

  energy += bonus;      /*remove bonus energies from result */

  if (backtrack_type=='C')
    return (float) c[indx[length]+1]/100.;
  else if (backtrack_type=='M')
    return (float) fML[indx[length]+1]/100.;
  else
    return (float) energy/100.;
}
Ejemplo n.º 2
0
PRIVATE void heat_capacity(char *string, float T_min, float T_max,
                          float h, int m)
{
   int length, i;
   char *structure;
   float hc, kT, min_en;
   
   length = (int) strlen(string);
   
   do_backtrack = 0;   

   temperature = T_min -m*h;
   initialize_fold(length);
   structure = (char *) space((unsigned) length+1);
   min_en = fold(string, structure);
   free(structure); free_arrays();
   kT = (temperature+K0)*GASCONST/1000;    /* in kcal */
   pf_scale = exp(-(1.07*min_en)/kT/length );
   init_pf_fold(length);
   
   for (i=0; i<2*m+1; i++) {
      F[i] = pf_fold(string, NULL);   /* T_min -2h */
      temperature += h;
      kT = (temperature+K0)*GASCONST/1000;
      pf_scale=exp(-(F[i]/length +h*0.00727)/kT); /* try to extrapolate F */
      update_pf_params(length); 
   }
   while (temperature <= (T_max+m*h+h)) {
      
      hc = - ddiff(F,h,m)* (temperature +K0 - m*h -h); 
      printf("%g   %g\n", (temperature-m*h-h), hc);  
      
      for (i=0; i<2*m; i++)
         F[i] = F[i+1];
      F[2*m] = pf_fold(string, NULL); 
      temperature += h;
      kT = (temperature+K0)*GASCONST/1000;
      pf_scale=exp(-(F[i]/length +h*0.00727)/kT);
      update_pf_params(length); 
   }
   free_pf_arrays();
}
Ejemplo n.º 3
0
void ini_or_reset_rl(void) {

  /* if there is no ringList-tree make a new one */
  if (wurzl == NULL) {
    ini_ringlist();

    /* start structure */
    struc2tree(GAV.startform);
    GSV.currE = GSV.startE = energy_of_struct(GAV.farbe, GAV.startform);


    /* stop structure(s) */
    if ( GTV.stop )  {
      int i;

      qsort(GAV.stopform, GSV.maxS, sizeof(char *), comp_struc);
      for (i = 0; i< GSV.maxS; i++)
	GAV.sE[i] = energy_of_struct(GAV.farbe_full, GAV.stopform[i]);
    }
    else {
      if(GTV.noLP)
	noLonelyPairs=1;
      initialize_fold(GSV.len);
      /* fold sequence to get Minimum free energy structure (Mfe) */
      GAV.sE[0] = fold(GAV.farbe_full, GAV.stopform[0]);
      free_arrays();
      /* revaluate energy of Mfe (maye differ if --logML=logarthmic */
      GAV.sE[0] = energy_of_struct(GAV.farbe_full, GAV.stopform[0]);
    }
    GSV.stopE = GAV.sE[0];
    ini_nbList(strlen(GAV.farbe_full)*strlen(GAV.farbe_full));
  }
  else {
    /* reset ringlist-tree to start conditions */
    reset_ringlist();
    if(GTV.start) struc2tree(GAV.startform);
    else {
      GSV.currE = GSV.startE;
    }
  }
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
    char *start, *structure, *rstart, *str2, *line;
    char  *ParamFile=NULL;
    int   i,j, length, l, hd;
    double energy=0., kT;
    int   pf, mfe, istty;
    int   repeat, found;

    do_backtrack = 0;
    pf = 0;
    mfe = 1;
    repeat = 0;
    init_rand();
    for (i=1; i<argc; i++) {
        if (argv[i][0]=='-')
            switch ( argv[i][1] )
            {
            case 'a':
                symbolset = argv[++i];
                /* symbolset should only have uppercase characters */
                for (l = 0; l < (int)strlen(symbolset); l++)
                    symbolset[l] = toupper(symbolset[l]);
                break;
            case 'T':
                if (argv[i][2]!='\0') usage();
                if (sscanf(argv[++i], "%lf", &temperature)==0)
                    usage();
                break;
            case 'F':
                mfe = 0;
                pf = 0;
                for(j=2; j<(int)strlen(argv[i]); j++) {
                    switch( argv[i][j] ) {
                    case 'm' :
                        mfe = 1;
                        break;
                    case 'p' :
                        pf = 1; /* old version had dangles=0 here */
                        break;
                    default :
                        usage();
                    }
                }
                break;
            case 'R':
                repeat = REPEAT_DEFAULT;
                if(++i<argc)
                    if (sscanf(argv[i], "%d", &repeat)==0)
                        usage();
                break;
            case 'n':
                if (strcmp(argv[i], "-noGU" )==0) noGU=1;
                else if (strcmp(argv[i], "-noLP" )==0) noLonelyPairs=1;
                else usage();
                break;
            case '4':
                tetra_loop=0;
                break;
            case 'e':
                if (sscanf(argv[++i],"%d", &energy_set)==0)
                    usage();
                break;
            case 'd':
                dangles=0;
                if (argv[i][2]!='\0')
                    if (sscanf(argv[i]+2, "%d", &dangles)==0)
                        usage();
                break;
            case 'f': /* when to stop RNAfold -p */
                if (sscanf(argv[++i],"%f", &final_cost)==0)
                    usage();
                break;
            case 'P':
                if (++i<argc)
                    ParamFile = argv[i];
                else
                    usage();
                break;
            case 'v':
                inv_verbose = 1;
                break;
            default:
                usage();
            }
    }

    kT = (temperature+273.15)*1.98717/1000.0;

    istty = (isatty(fileno(stdout))&&isatty(fileno(stdin)));

    if (ParamFile!=NULL)
        read_parameter_file(ParamFile);

    give_up = (repeat<0);

    do {
        if (istty) {
            printf("\nInput structure & start string"
                   " (lower case letters for const positions)\n"
                   "    @ to quit, and 0 for random start string\n");
            printf("%s\n", scale);
        }

        if ((line = get_line(stdin))==NULL) break;

        /* read structure, skipping over comment lines */
        while ((*line=='*')||(*line=='\0')||(*line=='>')) {
            printf("%s\n", line);
            free(line);
            if ((line = get_line(stdin))==NULL) break;
        }
        /* stop at eof or '@' */
        if (line==NULL) break;
        if (strcmp(line, "@") == 0) {
            free(line);
            break;
        }

        structure = (char *) space(strlen(line)+1);
        (void) sscanf(line,"%s",structure); /* scanf gets rid of trailing junk */
        free(line);

        length = (int) strlen(structure);
        str2 = (char *) space((unsigned)length+1);

        if ((line = get_line(stdin))!=NULL)
            if (strcmp(line, "@") == 0) {
                free(line);
                break;
            }

        start = (char *) space((unsigned) length+1);
        if (line !=NULL) {
            (void) strncpy(start, line, length);
            free(line);
        }

        if (istty) printf("length = %d\n", length);

        if (repeat!=0) found = (repeat>0)? repeat : (-repeat);
        else found = 1;

        initialize_fold(length);

        rstart = (char *) space((unsigned)length+1);
        while(found>0) {
            char *string;
            string = (char *) space((unsigned)length+1);
            strcpy(string, start);
            for (i=0; i<length; i++) {
                /* lower case characters are kept fixed, any other character
                   not in symbolset is replaced by a random character */
                if (islower(string[i])) continue;

                if (string[i]=='\0' || (strchr(symbolset,string[i])==NULL))
                    string[i]=symbolset[int_urn(0,strlen(symbolset)-1)];
            }
            strcpy(rstart, string); /* remember start string */

            if (mfe) {
                energy = inverse_fold(string, structure);
                if( (repeat>=0) || (energy<=0.0) ) {
                    found--;
                    hd = hamming(rstart, string);
                    printf("%s  %3d", string, hd);
                    if (energy>0) { /* no solution found */
                        printf("   d= %g\n", energy);
                        if(istty) {
                            energy = fold(string,str2);
                            printf("%s\n", str2);
                        }
                    } else printf("\n");
                }
            }
            if (pf) {
                if (!(mfe && give_up && (energy>0))) {
                    /* unless we gave up in the mfe part */
                    double prob, min_en, sfact=1.07;

                    /* get a reasonable pf_scale */
                    min_en = fold(string,str2);
                    pf_scale = exp(-(sfact*min_en)/kT/length);
                    init_pf_fold(length);

                    energy = inverse_pf_fold(string, structure);
                    prob = exp(-energy/kT);
                    hd = hamming(rstart, string);
                    printf("%s  %3d  (%g)\n", string, hd, prob);
                    free_pf_arrays();
                }
                if (!mfe) found--;
            }
            (void) fflush(stdout);
            free(string);
        }
        free(rstart);
        free_arrays();

        free(structure);
        free(str2);
        free(start);
        (void) fflush(stdout);
    } while (1);
    return 0;
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
    char *start;
    char *structure;
    char *rstart;
    char *str2;
    char *line;
    int i;
    int length;
    int l;
    int hd;
    double energy = 0.;
    double kT;
    int   pf = 0;
    int   mfe = 0;
    int   istty;
    int   repeat; 
    int   found;
    
    AjPFile inf     = NULL;
    AjPSeq  seq = NULL;
    AjPFile paramfile = NULL;
    AjPFile outf = NULL;
    
    float eT = 0.;
    AjBool eGU;
    
    AjBool eclose;
    AjBool lonely;
    AjBool etloop;
    AjPStr eenergy = NULL;
    char ewt = '\0';
    AjPStr edangles = NULL;
    AjPStr method   = NULL;
    AjPStr ealpha    = NULL;
    AjBool showfails = ajFalse;
    AjBool succeed = ajFalse;
    
    char edangle = '\0';
    
    ajint len;
    FILE *fp;
    
    
    
    embInitPV("vrnainverse",argc,argv,"VIENNA",VERSION);
    
    
    inf        = ajAcdGetInfile("structuresfile");
    seq        = ajAcdGetSeq("sequence");
    paramfile  = ajAcdGetInfile("paramfile");
    eT         = ajAcdGetFloat("temperature");
    eGU        = ajAcdGetBoolean("gu");
    eclose     = ajAcdGetBoolean("closegu");
    lonely     = ajAcdGetBoolean("lp");
    etloop     = ajAcdGetBoolean("tetraloop");
    eenergy    = ajAcdGetListSingle("energy");
    edangles   = ajAcdGetListSingle("dangles");
    method     = ajAcdGetListSingle("folding");
    ealpha     = ajAcdGetString("alphabet");
    final_cost = ajAcdGetFloat("final");
    repeat     = ajAcdGetInt("repeats");
    showfails  = ajAcdGetBoolean("showfails");
    succeed    = ajAcdGetBoolean("succeed");
    outf       = ajAcdGetOutfile("outfile");
    
    
    do_backtrack = 0; 
    structure = NULL;
    istty = 0;
    
    temperature   = (double) eT;
    noGU          = (eGU) ? 0 : 1;
    no_closingGU  = (eclose) ? 0 : 1;
    noLonelyPairs = (lonely) ? 0 : 1;
    tetra_loop    = !!etloop;
    
    ewt = *ajStrGetPtr(eenergy);
    if(ewt == '0')
	energy_set = 0;
    else if(ewt == '1')
	energy_set = 1;
    else if(ewt == '2')
	energy_set = 2;
    
    edangle = *ajStrGetPtr(edangles);
    if(edangle == '0')
	dangles = 0;
    else if(edangle == '1')
	dangles = 1;
    else if(edangle == '2')
	dangles = 2;
    else if(edangle == '3')
	dangles = 3;
    
    if(ajStrMatchC(method,"mp"))
    {
	mfe = 1;
	pf  = 1;
    }
    else if(ajStrMatchC(method,"m"))
    {
	mfe = 1;
	pf  = 0;
    }
    else if(ajStrMatchC(method,"p"))
    {
	mfe = 0;
	pf  = 1;
    }
    
    len = ajStrGetLen(ealpha);
    symbolset = (char *) space(len + 1);
    strcpy(symbolset, ajStrGetPtr(ealpha));
    for (l = 0; l < len; l++)
	symbolset[l] = toupper(symbolset[l]);
    
    inv_verbose = !!showfails;
    fp = ajFileGetFileptr(inf);
    
    init_rand();
    kT = (temperature+273.15)*1.98717/1000.0;
    
    istty = (isatty(fileno(stdout))&&isatty(fileno(stdin)));
    
    if (paramfile)
	read_parameter_file(paramfile);
    
    give_up = succeed;
    
    do {
      
	if ((line = get_line(fp))==NULL) break;

	/* read structure, skipping over comment lines */
	while ((*line=='*')||(*line=='\0')||(*line=='>'))
	{
	    free(line);
	    if ((line = get_line(fp))==NULL)
		break;
	} 
	/* stop at eof or '@' */
	if (line==NULL) break;
	if (strcmp(line, "@") == 0)
	{
	    free(line);
	    break;
	}

	structure = (char *) space(strlen(line)+1);
	/* scanf gets rid of trailing junk */
	(void) sscanf(line,"%s",structure);
	free(line);
      
	length = (int) strlen(structure);
	str2 = (char *) space((unsigned)length+1);

/* now look for a sequence to match the structure */

/*
	if ((line = get_line(fp))!=NULL)
	    if (strcmp(line, "@") == 0)
	    {
		free(line);
		break;
	    }
*/

	start = (char *) space((unsigned) length+1);
	if(seq)
	    (void) strncpy(start, ajSeqGetSeqC(seq), length);

	if (repeat!=0)
	    found = repeat;
	else
	    found = 1;
      
	initialize_fold(length);

	rstart = (char *) space((unsigned)length+1);
	while(found>0)
	{
	    char *string;
	    string = (char *) space((unsigned)length+1);
	    strcpy(string, start);
	    for (i=0; i<length; i++)
	    {
		/* lower case characters are kept fixed, any other character
		   not in symbolset is replaced by a random character */
		if (islower(string[i]))
		    continue;

		if (string[i]=='\0' || (strchr(symbolset,string[i])==NULL))
		    string[i]=symbolset[int_urn(0,strlen(symbolset)-1)];
	    }
	    strcpy(rstart, string);	/* remember start string */
	
	    if (mfe)
	    {
		energy = inverse_fold(string, structure);
		if( (!succeed) || (energy<=0.0) ) {
		    found--;
		    hd = hamming(rstart, string);
		    ajFmtPrintF(outf,"%s  %3d", string, hd);
		    if (energy>0)
		    {			/* no solution found */
			ajFmtPrintF(outf,"   d = %f\n", energy);
		    }
		    else
			ajFmtPrintF(outf,"\n");
		}
	    }

	    if (pf)
	    {
		if (!(mfe && give_up && (energy>0)))
		{
		    /* unless we gave up in the mfe part */
		    double prob, min_en, sfact=1.07;
	    
		    /* get a reasonable pf_scale */
		    min_en = fold(string,str2); 
		    pf_scale = exp(-(sfact*min_en)/kT/length);
		    init_pf_fold(length);
	    
		    energy = inverse_pf_fold(string, structure);
		    prob = exp(-energy/kT);
		    hd = hamming(rstart, string);
		    ajFmtPrintF(outf,"%s  %3d  (%f)\n", string, hd, prob);
		    free_pf_arrays();
		}
		if (!mfe)
		    found--;
	    }

	    free(string);
	}
	free(rstart);
	free_arrays();
      
	free(structure);
	free(str2);
	free(start);

    } while (1);

    ajSeqDel(&seq);
    ajStrDel(&eenergy);
    ajStrDel(&edangles);
    ajStrDel(&method);
    ajStrDel(&ealpha);

    ajFileClose(&inf);
    ajFileClose(&paramfile);
    ajFileClose(&outf);
    AJFREE(symbolset);

    embExit();
    return 0;
}