Ejemplo n.º 1
0
/* Gera uma velocidade seguindo uma normal com media media e desvio
 * padrao desv_pad. */
double rand_velocity(double media, double desv_pad) {
     double z = gen_norm();
     return (desv_pad*z + media);
}
Ejemplo n.º 2
0
int generate(int count)
{
    FILE *ofp;
    int length;     /* length of seqence */
    double probs[20];   /* probability mass function for AAs */
    double cum_probs[20]; /* probability distribution
                 * (cumulative sum of probs)
             */
    int s;  /* counter for sequences */
    int p;  /* counter for position in sequence */
    int a;  /* counter for amino acid number */
    int alo, ahi, amid; /* variables for binary search to get amino acid */
    
    double sum; /* temporary for computing cum_probs */
    
    double x;   /* uniform random number */
    gen_dirch_mix_param comp_gen; /* composition generator */
    
    ofp = fopen("GeneratedSequences.txt", "w");
    if (ofp == NULL) {
        fprintf(stderr, "Can't open output file\n");
        return 1;
    }
    
    srandom(getpid());
    gen_dirch_mix_initialize(&comp_gen, 20, 6,  mix_coeff, 
        (const double **)comps);
    
    for (s=0; s<count; s++)
    {
        length = (int) exp(mean_log_length +stddev_log_length*gen_norm());
    gen_dirch_mix(&comp_gen, probs);
    
    sum = 0.;
    /* convert probs to cumulative probabilities */
    for (a=0; a<20; a++)
    {   sum += probs[a];
        cum_probs[a] =sum;
    }
    
    for (p=0; p< length; p++)
    {   x = DRAND();
       /* do binary search to determine amino acid */
        alo = -1;    ahi=19;
        while (alo<ahi-1)
        {   /* invariant: 
         *    cum_probs[alo] < x <=  cum_probs[ahi]
         */
        assert (x <= cum_probs[ahi]);
        amid = (alo+ahi+1)/2;
        if (x > cum_probs[amid]) alo=amid;
        else ahi=amid;
        }
        if  (  fputc(AA[ahi], ofp) == EOF ) return 1;
    }
    if  (  fputc('\n', ofp) == EOF ) return 1;
    
    }
    
    if  ( fclose(ofp) != EOF ) return 0;
    else return 1;
}