hmmer-3.3/easel/esl_sq.c
/* Function:  esl_sq_Sample()
 * Synopsis:  Sample a random, ugly <ESL_SQ> for test purposes.
 * Incept:    SRE, Tue Feb 23 08:32:54 2016 [H1/83]
 *
 * Purpose:   Sample a random sequence with random annotation, with a
 *            sequence length of <0..maxL> (note 0 is included!),
 *            using the random number generator <rng>. Return the
 *            newly allocated sequence in <*ret_sq>. Caller is 
 *            responsible for free'ing it with <esl_sq_Destroy()>.
 *            
 *            If <abc> is <NULL>, a text mode sequence is sampled.  If
 *            a digital alphabet <abc> is provided, a digital mode
 *            sequence is sampled.
 *            
 *            This routine is intended for producing test cases. The
 *            sequence object contains randomized contents. If you
 *            want to synthesize random sequences (as opposed to
 *            sampling an entirely synthetic <ESL_SQ> object), see
 *            the <randomseq> module.
 *
 * Args:      rng    : random number generator
 *            abc    : digital alphabet; or <NULL> for text mode
 *            maxL   : sequence length sampled is 0..maxL 
 *            ret_sq : RESULT: new <ESL_SQ>
 *
 * Returns:   <eslOK> on success
 *
 * Throws:    <eslEMEM> on allocation failure.
 */
esl_sq_Sample()
Synopsis:  Sample a random, ugly  for test purposes.

Sample a random sequence with random annotation, with a
sequence length of <0..maxL> (note 0 is included!),
using the random number generator . Return the
newly allocated sequence in <*ret_sq>. Caller is
responsible for free'ing it with .

If  is , a text mode sequence is sampled.  If
a digital alphabet  is provided, a digital mode
sequence is sampled.

This routine is intended for producing test cases. The
sequence object contains randomized contents. If you
want to synthesize random sequences (as opposed to
sampling an entirely synthetic  object), see
the  module.



 on success

 on allocation failure.
esl_sq_Sample () Synopsis : Sample a random ugly for test purposes . Sample a random sequence with random annotation with a sequence length of <0 .. maxL > ( note 0 is included! ) using the random number generator . Return the newly allocated sequence in < * ret_sq > . Caller is responsible for free ' ing it with . If is a text mode sequence is sampled . If a digital alphabet is provided a digital mode sequence is sampled . This routine is intended for producing test cases . The sequence object contains randomized contents . If you want to synthesize random sequences ( as opposed to sampling an entirely synthetic object ) see the module . on success on allocation failure .
int
esl_sq_Sample(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc, int maxL, ESL_SQ **ret_sq)
{
  ESL_SQ *sq   = *ret_sq;               
  int     maxn = eslSQ_NAMECHUNK *2;    
  int     maxa = eslSQ_ACCCHUNK  *2;
  int     maxd = eslSQ_DESCCHUNK *2;
  char   *buf  = NULL;
  int     n;
  int     status;
  n = ESL_MAX(maxn, ESL_MAX(maxa, maxd));
  ESL_ALLOC(buf, sizeof(char) * (n+1));
  if (! sq)
    {
      if (abc == NULL) { if (( sq = esl_sq_Create())           == NULL) { status = eslEMEM; goto ERROR; } }
      else             { if (( sq = esl_sq_CreateDigital(abc)) == NULL) { status = eslEMEM; goto ERROR; } }
    }
  do {
    n = 1 + esl_rnd_Roll(rng, maxn);                    
    esl_rsq_Sample(rng, eslRSQ_SAMPLE_GRAPH, n, &buf);  
  } while (ispunct(buf[0]));                            
  esl_sq_SetName(sq, buf);
  if (esl_rnd_Roll(rng, 2))                              
    {
      n = 1 + esl_rnd_Roll(rng, maxa);                   
      esl_rsq_Sample(rng, eslRSQ_SAMPLE_GRAPH, n, &buf); 
      esl_sq_SetAccession(sq, buf);
    }
  if (esl_rnd_Roll(rng, 2))                                
    {
      do {
	n = 1 + esl_rnd_Roll(rng, maxd);                     
	esl_rsq_Sample(rng, eslRSQ_SAMPLE_PRINT, n, &buf);   
      } while (isspace(buf[0]));                             
      esl_sq_SetDesc(sq, buf);
    }
  if (esl_rnd_Roll(rng, 2))                               
    {
      sq->tax_id = 1 + esl_rnd_Roll(rng, 2147483647);     
    }
  n = esl_rnd_Roll(rng, maxL+1);                                             
  esl_sq_GrowTo(sq, n);
  if (abc == NULL) esl_rsq_Sample(rng, eslRSQ_SAMPLE_ALPHA, n, &(sq->seq));
  else             esl_rsq_SampleDirty(rng, abc, NULL, n, sq->dsq);         
  esl_sq_SetCoordComplete(sq, n);
  free(buf);
  *ret_sq = sq;
  return eslOK;
 ERROR:
  if (buf)               free(buf);
  if (!(*ret_sq) && sq)  esl_sq_Destroy(sq);
  return status;
}
int esl_sq_Sample ( ESL_RANDOMNESS * rng , ESL_ALPHABET * abc , int maxL , ESL_SQ * * ret_sq ) { ESL_SQ * sq = * ret_sq ; int maxn = eslSQ_NAMECHUNK * 2 ; int maxa = eslSQ_ACCCHUNK * 2 ; int maxd = eslSQ_DESCCHUNK * 2 ; char * buf = NULL ; int n ; int status ; n = ESL_MAX ( maxn , ESL_MAX ( maxa , maxd ) ) ; ESL_ALLOC ( buf , sizeof ( char ) * ( n + 1 ) ) ; if ( ! sq ) { if ( abc == NULL ) { if ( ( sq = esl_sq_Create ( ) ) == NULL ) { status = eslEMEM ; goto ERROR ; } } else { if ( ( sq = esl_sq_CreateDigital ( abc ) ) == NULL ) { status = eslEMEM ; goto ERROR ; } } } do { n = 1 + esl_rnd_Roll ( rng , maxn ) ; esl_rsq_Sample ( rng , eslRSQ_SAMPLE_GRAPH , n , & buf ) ; } while ( ispunct ( buf [ 0 ] ) ) ; esl_sq_SetName ( sq , buf ) ; if ( esl_rnd_Roll ( rng , 2 ) ) { n = 1 + esl_rnd_Roll ( rng , maxa ) ; esl_rsq_Sample ( rng , eslRSQ_SAMPLE_GRAPH , n , & buf ) ; esl_sq_SetAccession ( sq , buf ) ; } if ( esl_rnd_Roll ( rng , 2 ) ) { do { n = 1 + esl_rnd_Roll ( rng , maxd ) ; esl_rsq_Sample ( rng , eslRSQ_SAMPLE_PRINT , n , & buf ) ; } while ( isspace ( buf [ 0 ] ) ) ; esl_sq_SetDesc ( sq , buf ) ; } if ( esl_rnd_Roll ( rng , 2 ) ) { sq -> tax_id = 1 + esl_rnd_Roll ( rng , 2147483647 ) ; } n = esl_rnd_Roll ( rng , maxL + 1 ) ; esl_sq_GrowTo ( sq , n ) ; if ( abc == NULL ) esl_rsq_Sample ( rng , eslRSQ_SAMPLE_ALPHA , n , & ( sq -> seq ) ) ; else esl_rsq_SampleDirty ( rng , abc , NULL , n , sq -> dsq ) ; esl_sq_SetCoordComplete ( sq , n ) ; free ( buf ) ; * ret_sq = sq ; return eslOK ; ERROR : if ( buf ) free ( buf ) ; if ( ! ( * ret_sq ) && sq ) esl_sq_Destroy ( sq ) ; return status ; }
Function:  esl_sq_Sample()
 Synopsis:  Sample a random, ugly <ESL_SQ> for test purposes.
Function : esl_sq_Sample () Synopsis : Sample a random ugly <ESL_SQ > for test purposes .
// caller may or may not provide an existing <sq> // by being bigger than the initial alloc size, we will exercise realloc /* Name */ // 1..maxn // one word: no space // #, // are bad things for names to start with in Stockholm  /* Optional accession */ // 50% chance of an accession // 1..maxa // one word: no space /* Optional description */ // 50% chance of a description // 1..maxa // include spaces in descriptions... // ... just not as the first char. /* Optional taxid.  */ // 50% chance of taxid // 1..2^31-1 /* Sequence, in text or digital mode */ //0..maxL; 0 len seqs happen // "dirty" = with ambig residues