hmmer-3.3/easel/miniapps/esl-sfetch.c
/* onefetch():
 * Given one <key> (a seq name or accession), retrieve the corresponding sequence.
 * In SSI mode, we can do this quickly by positioning the file, then regurgitating
 * every line until the end-of-record marker; we don't even have to parse.
 * Without an SSI index, we have to parse the file sequentially 'til we find
 * the one we're after.
 */
Given one  (a seq name or accession), retrieve the corresponding sequence.
In SSI mode, we can do this quickly by positioning the file, then regurgitating
every line until the end-of-record marker; we don't even have to parse.
Without an SSI index, we have to parse the file sequentially 'til we find
the one we're after.
Given one ( a seq name or accession ) retrieve the corresponding sequence . In SSI mode we can do this quickly by positioning the file then regurgitating every line until the end - of - record marker ; we don ' t even have to parse . Without an SSI index we have to parse the file sequentially ' til we find the one we ' re after .
static void
onefetch(ESL_GETOPTS *go, FILE *ofp, char *key, ESL_SQFILE *sqfp)
{
  ESL_SQ  *sq            = esl_sq_Create();
  int      do_revcomp    = esl_opt_GetBoolean(go, "-r");
  char    *newname       = esl_opt_GetString(go, "-n");
  int      status;
  if (sqfp->data.ascii.ssi != NULL)	
    {
      status = esl_sqfile_PositionByKey(sqfp, key);
      if      (status == eslENOTFOUND) esl_fatal("seq %s not found in SSI index for file %s\n", key, sqfp->filename);
      else if (status == eslEFORMAT)   esl_fatal("Failed to parse SSI index for %s\n", sqfp->filename);
      else if (status != eslOK)        esl_fatal("Failed to look up location of seq %s in SSI index of file %s\n", key, sqfp->filename);
      status = esl_sqio_Read(sqfp, sq);
      if      (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
					       sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
      else if (status == eslEOF)     esl_fatal("Unexpected EOF reading sequence file %s",
					       status, sqfp->filename);
      else if (status != eslOK)      esl_fatal("Unexpected error %d reading sequence file %s",
					       status, sqfp->filename);
      if (strcmp(key, sq->name) != 0 && strcmp(key, sq->acc) != 0) 
	esl_fatal("whoa, internal error; found the wrong sequence %s, not %s", sq->name, key);
    }  
  else 
    { 
      while ((status = esl_sqio_Read(sqfp, sq)) != eslEOF) {
	if      (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
						 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
	else if (status != eslOK)      esl_fatal("Unexpected error %d reading sequence file %s",
						 status, sqfp->filename);
	if (strcmp(key, sq->name) == 0 || strcmp(key, sq->acc) == 0) break;
	esl_sq_Reuse(sq);
      }
      if (status == eslEOF) esl_fatal("Failed to find sequence %s in file %s\n", key, sqfp->filename);
    }
  if (do_revcomp == FALSE && newname == NULL && ! esl_sqio_IsAlignment(sqfp->format)) 
    { 
      if (esl_sqio_Echo(sqfp, sq, ofp) != eslOK) esl_fatal("Echo failed: %s\n", esl_sqfile_GetErrorBuf(sqfp));
    }
  else
    { 
      if (do_revcomp && esl_sq_ReverseComplement(sq) != eslOK) esl_fatal("Failed to reverse complement %s; is it a protein?\n", sq->name);
      if (newname != NULL) esl_sq_SetName(sq, newname);
      esl_sqio_Write(ofp, sq, eslSQFILE_FASTA, FALSE);
    }
  esl_sq_Destroy(sq);
}
static void onefetch ( ESL_GETOPTS * go , FILE * ofp , char * key , ESL_SQFILE * sqfp ) { ESL_SQ * sq = esl_sq_Create ( ) ; int do_revcomp = esl_opt_GetBoolean ( go , " " ) ; char * newname = esl_opt_GetString ( go , " " ) ; int status ; if ( sqfp -> data . ascii . ssi != NULL ) { status = esl_sqfile_PositionByKey ( sqfp , key ) ; if ( status == eslENOTFOUND ) esl_fatal ( " \n " , key , sqfp -> filename ) ; else if ( status == eslEFORMAT ) esl_fatal ( " \n " , sqfp -> filename ) ; else if ( status != eslOK ) esl_fatal ( " \n " , key , sqfp -> filename ) ; status = esl_sqio_Read ( sqfp , sq ) ; if ( status == eslEFORMAT ) esl_fatal ( " \n \n " , sqfp -> filename , esl_sqfile_GetErrorBuf ( sqfp ) ) ; else if ( status == eslEOF ) esl_fatal ( " " , status , sqfp -> filename ) ; else if ( status != eslOK ) esl_fatal ( " " , status , sqfp -> filename ) ; if ( strcmp ( key , sq -> name ) != 0 && strcmp ( key , sq -> acc ) != 0 ) esl_fatal ( " " , sq -> name , key ) ; } else { while ( ( status = esl_sqio_Read ( sqfp , sq ) ) != eslEOF ) { if ( status == eslEFORMAT ) esl_fatal ( " \n \n " , sqfp -> filename , esl_sqfile_GetErrorBuf ( sqfp ) ) ; else if ( status != eslOK ) esl_fatal ( " " , status , sqfp -> filename ) ; if ( strcmp ( key , sq -> name ) == 0 || strcmp ( key , sq -> acc ) == 0 ) break ; esl_sq_Reuse ( sq ) ; } if ( status == eslEOF ) esl_fatal ( " \n " , key , sqfp -> filename ) ; } if ( do_revcomp == FALSE && newname == NULL && ! esl_sqio_IsAlignment ( sqfp -> format ) ) { if ( esl_sqio_Echo ( sqfp , sq , ofp ) != eslOK ) esl_fatal ( " \n " , esl_sqfile_GetErrorBuf ( sqfp ) ) ; } else { if ( do_revcomp && esl_sq_ReverseComplement ( sq ) != eslOK ) esl_fatal ( " \n " , sq -> name ) ; if ( newname != NULL ) esl_sq_SetName ( sq , newname ) ; esl_sqio_Write ( ofp , sq , eslSQFILE_FASTA , FALSE ) ; } esl_sq_Destroy ( sq ) ; }
onefetch():
 Given one <key> (a seq name or accession), retrieve the corresponding sequence.
onefetch () : Given one <key > ( a seq name or accession ) retrieve the corresponding sequence .
/* Try to position the file at the desired sequence with SSI. */ /* Else, we have to read the whole damn file sequentially until we find the seq */ /* If we're not manipulating the sequence in any way, and it's not from an alignment file, we can Echo() it. */ /* Otherwise we Write() the parsed version. */