hmmer-3.3/easel/esl_sq.c
/* Function:  esl_sq_Digitize()
 * Synopsis:  Convert an <ESL_SQ> to digital mode.
 * Incept:    EPN, Mon Feb 12 11:09:06 2007
 *
 * Purpose:   Given a sequence <sq> in text mode, convert it to
 *            digital mode, using alphabet <abc>.
 *            
 *            Internally, the <dsq> digital sequence field is filled,
 *            the <seq> text alignment field is destroyed and free'd,
 *            a copy of the alphabet pointer is kept in the sq's
 *            <abc> reference.
 *
 * Args:      abc    - digital alphabet
 *            sq     - sequence to digitize
 *
 * Returns:   <eslOK> on success.
 *            Returns <eslEINVAL> if the sequence contains invalid characters
 *            that can't be digitized. If this happens, the <sq> is returned
 *            unaltered - left in text mode, with <seq> as it was. (This is
 *            a normal error, because <sq->seq> may be user input that we 
 *            haven't validated yet.)
 *
 * Throws:    <eslEMEM> on allocation failure; in this case, state of <sq> may be 
 *            wedged, and it should only be destroyed, not used.
 */
esl_sq_Digitize()
Synopsis:  Convert an  to digital mode.

Given a sequence  in text mode, convert it to
digital mode, using alphabet .

Internally, the  digital sequence field is filled,
the  text alignment field is destroyed and free'd,
a copy of the alphabet pointer is kept in the sq's
 reference.

abc    - digital alphabet
sq     - sequence to digitize

 on success.
Returns  if the sequence contains invalid characters
that can't be digitized. If this happens, the  is returned
unaltered - left in text mode, with  as it was. (This is
a normal error, because seq> may be user input that we
haven't validated yet.)

 on allocation failure; in this case, state of  may be
wedged, and it should only be destroyed, not used.
esl_sq_Digitize () Synopsis : Convert an to digital mode . Given a sequence in text mode convert it to digital mode using alphabet . Internally the digital sequence field is filled the text alignment field is destroyed and free ' d a copy of the alphabet pointer is kept in the sq ' s reference . abc - digital alphabet sq - sequence to digitize on success . Returns if the sequence contains invalid characters that can ' t be digitized . If this happens the is returned unaltered - left in text mode with as it was . ( This is a normal error because seq > may be user input that we haven ' t validated yet . ) on allocation failure ; in this case state of may be wedged and it should only be destroyed not used .
int
esl_sq_Digitize(const ESL_ALPHABET *abc, ESL_SQ *sq)
{
  int   x;        
  int status;
  if (sq->dsq   != NULL) return eslOK;
  if (sq->seq   == NULL) ESL_EXCEPTION(eslEINVAL, "sq has no text sequence");
  if (esl_abc_ValidateSeq(abc, sq->seq, sq->n, NULL) != eslOK) return eslEINVAL;
  if (sq->salloc < sq->n+2) {	
    sq->salloc = sq->n+2;
    if (sq->ss != NULL) {
      void *tmp;
      ESL_RALLOC(sq->ss, tmp, sizeof(char) * sq->salloc);
    }
    for (x = 0; x < sq->nxr; x++) 
      if (sq->xr[x] != NULL) {
	void *tmp;
	ESL_RALLOC(sq->xr[x], tmp, sizeof(char) * sq->salloc);
      }
  }
  ESL_ALLOC(sq->dsq, (sq->salloc) * sizeof(ESL_DSQ));
  if ((status = esl_abc_Digitize(abc, sq->seq, sq->dsq)) != eslOK) goto ERROR;
  if (sq->ss != NULL) {
    memmove(sq->ss+1, sq->ss, sq->n+1);
    sq->ss[0] = '\0';
  }
  for (x = 0; x < sq->nxr; x++) 
    if (sq->xr[x] != NULL) {
      memmove(sq->xr[x]+1, sq->xr[x], sq->n+1);
      sq->xr[x][0] = '\0';
    }
  free(sq->seq);
  sq->seq = NULL;
  sq->abc = abc;
  return eslOK;
 ERROR:
  if (sq->dsq != NULL) free(sq->dsq);
  return status;
}
int esl_sq_Digitize ( const ESL_ALPHABET * abc , ESL_SQ * sq ) { int x ; int status ; if ( sq -> dsq != NULL ) return eslOK ; if ( sq -> seq == NULL ) ESL_EXCEPTION ( eslEINVAL , " " ) ; if ( esl_abc_ValidateSeq ( abc , sq -> seq , sq -> n , NULL ) != eslOK ) return eslEINVAL ; if ( sq -> salloc < sq -> n + 2 ) { sq -> salloc = sq -> n + 2 ; if ( sq -> ss != NULL ) { void * tmp ; ESL_RALLOC ( sq -> ss , tmp , sizeof ( char ) * sq -> salloc ) ; } for ( x = 0 ; x < sq -> nxr ; x ++ ) if ( sq -> xr [ x ] != NULL ) { void * tmp ; ESL_RALLOC ( sq -> xr [ x ] , tmp , sizeof ( char ) * sq -> salloc ) ; } } ESL_ALLOC ( sq -> dsq , ( sq -> salloc ) * sizeof ( ESL_DSQ ) ) ; if ( ( status = esl_abc_Digitize ( abc , sq -> seq , sq -> dsq ) ) != eslOK ) goto ERROR ; if ( sq -> ss != NULL ) { memmove ( sq -> ss + 1 , sq -> ss , sq -> n + 1 ) ; sq -> ss [ 0 ] = ' \0 ' ; } for ( x = 0 ; x < sq -> nxr ; x ++ ) if ( sq -> xr [ x ] != NULL ) { memmove ( sq -> xr [ x ] + 1 , sq -> xr [ x ] , sq -> n + 1 ) ; sq -> xr [ x ] [ 0 ] = ' \0 ' ; } free ( sq -> seq ) ; sq -> seq = NULL ; sq -> abc = abc ; return eslOK ; ERROR : if ( sq -> dsq != NULL ) free ( sq -> dsq ) ; return status ; }
Function:  esl_sq_Digitize()
 Synopsis:  Convert an <ESL_SQ> to digital mode.
Function : esl_sq_Digitize () Synopsis : Convert an <ESL_SQ > to digital mode .
/* index for optional extra residue markups */ /* Contract checks */ /* Validate before we convert, so we leave <seq> untouched if it's bad. */ /* Allocate dsq, ss properly; these are our last failure points. */ /* You can't just call Grow() here, because it would grow for old text mode, not new digital */ /* it's possible (though unlikely) for salloc to be 1 residue too small */ /* optional extra residue markups follow same convenctions as ss */ /* Now convert. */