hmmer-3.3/easel/esl_sqio_ncbi.c
/* Function:  sqncbi_ReadBlock()
 * Synopsis:  Read the next block of sequences from a file.
 * Incept:    MSF, Mon Dec 10, 2009 [Janelia]
 *
 * Purpose:   Reads a block of sequences from open sequence file <sqfp> into 
 *            <sqBlock>.
 *
 *            In the case that <long_target> is false, the sequences are
 *            expected to be protein - individual sequences won't be long
 *            so read them in one-whole-sequence at a time. If <max_sequences>
 *            is set to a number > 0 read <max_sequences> sequences.
 *
 *            If <long_target> is true, the sequences are expected to be DNA.
 *            Because sequences in a DNA database can exceed MAX_RESIDUE_COUNT,
 *            this function uses ReadWindow to read chunks of sequence no
 *            larger than <max_residues>, and must allow for the possibility that
 *            a request will be made to continue reading a partly-read
 *            sequence.
 *
 * Returns:   <eslOK> on success; the new sequence is stored in <sqBlock>.
 * 
 *            Returns <eslEOF> when there is no sequence left in the
 *            file (including first attempt to read an empty file).
 * 
 *            Returns <eslEFORMAT> if there's a problem with the format,
 *            such as an illegal character;
 *
 * Throws:    <eslEMEM> on allocation failure;
 *            <eslEINCONCEIVABLE> on internal error.
 */
sqncbi_ReadBlock()
Synopsis:  Read the next block of sequences from a file.
Incept:    MSF, Mon Dec 10, 2009 [Janelia]

Reads a block of sequences from open sequence file  into
.

In the case that  is false, the sequences are
expected to be protein - individual sequences won't be long
so read them in one-whole-sequence at a time. If 
is set to a number > 0 read  sequences.

If  is true, the sequences are expected to be DNA.
Because sequences in a DNA database can exceed MAX_RESIDUE_COUNT,
this function uses ReadWindow to read chunks of sequence no
larger than , and must allow for the possibility that
a request will be made to continue reading a partly-read
sequence.

 on success; the new sequence is stored in .

Returns  when there is no sequence left in the
file (including first attempt to read an empty file).

Returns  if there's a problem with the format,
such as an illegal character.

 on allocation failure;
 on internal error.
sqncbi_ReadBlock () Synopsis : Read the next block of sequences from a file . Incept : MSF Mon Dec 10 2009 [ Janelia ] Reads a block of sequences from open sequence file into . In the case that is false the sequences are expected to be protein - individual sequences won ' t be long so read them in one - whole - sequence at a time . If is set to a number > 0 read sequences . If is true the sequences are expected to be DNA . Because sequences in a DNA database can exceed MAX_RESIDUE_COUNT this function uses ReadWindow to read chunks of sequence no larger than and must allow for the possibility that a request will be made to continue reading a partly - read sequence . on success ; the new sequence is stored in . Returns when there is no sequence left in the file ( including first attempt to read an empty file ) . Returns if there ' s a problem with the format such as an illegal character . on allocation failure ; on internal error .
static int
sqncbi_ReadBlock(ESL_SQFILE *sqfp, ESL_SQ_BLOCK *sqBlock, int max_residues, int max_sequences, int long_target)
{
	  int     i = 0;
	  int     size = 0;
	  int     status = eslOK;
	  ESL_SQ *tmpsq  = NULL;
	  sqBlock->count = 0;
	  if ( !long_target  )
	  {  
	    if (max_sequences < 1 || max_sequences > sqBlock->listSize)
	      max_sequences = sqBlock->listSize;
		  for (i = 0; i < max_sequences && size < MAX_RESIDUE_COUNT; ++i)
		  {
			  status = sqncbi_Read(sqfp, sqBlock->list + i);
			  if (status != eslOK) break;
			  size += sqBlock->list[i].n;
			  ++sqBlock->count;
		  }
	  }
	  else
	  { 
          if (max_residues < 0)
            max_residues = MAX_RESIDUE_COUNT;
          tmpsq = esl_sq_CreateDigital(sqBlock->list->abc);
		  if (! sqBlock->complete )
		  {
			  status = sqncbi_ReadWindow(sqfp, sqBlock->list->C, max_residues, sqBlock->list);
			  if (status == eslOK)
			  {
				  sqBlock->count = i = 1;
				  size = sqBlock->list->n;
				  sqBlock->list[i].L = sqfp->data.ncbi.seq_L;
				  if (sqBlock->list->n >= max_residues)
				  { 
				    if ( sqBlock->list->n == sqfp->data.ncbi.seq_L) {
				      sqBlock->complete = TRUE;
				      esl_sq_Reuse(tmpsq);
				      tmpsq->start =  sqBlock->list->start ;
				      tmpsq->C = 0;
				      status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq); 
              if (status == eslEOD) 
                status = eslOK;
				    } else {
				      sqBlock->complete = FALSE;
				    }
	          if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
	          return status;
				  }
				  else
				  {
					  esl_sq_Reuse(tmpsq);
					  tmpsq->start =  sqBlock->list->start ;
					  tmpsq->C = 0;
					  status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq);
					  if (status != eslEOD) {
					    if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
					    return status; 
					  }
	          sqBlock->list->L = tmpsq->L;
				  }
			  }
			  else if (status == eslEOD)
			  { 
			  }
			  else
			  {
			    if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
				  return status;
			  }
		  } 
		  for (  ; i < sqBlock->listSize && size < max_residues; ++i)
		  {
		    int request_size = ESL_MAX(max_residues-size, max_residues * .05);
			  esl_sq_Reuse(tmpsq);
			  esl_sq_Reuse(sqBlock->list + i);
			  status = sqncbi_ReadWindow(sqfp, 0, request_size, tmpsq);  
        esl_sq_Copy(tmpsq, sqBlock->list +i);
			  if (status != eslOK) break; 
			  size += sqBlock->list[i].n - sqBlock->list[i].C;
			  sqBlock->list[i].L = sqfp->data.ncbi.seq_L;
			  ++(sqBlock->count);
			  if (size >= max_residues)
		     { 
          if ( sqBlock->list[i].n == sqfp->data.ncbi.seq_L) {
             sqBlock->complete = TRUE;
             esl_sq_Reuse(tmpsq);
             tmpsq->start =  sqBlock->list->start ;
             tmpsq->C = 0;
             status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq); 
             if (status == eslEOD) 
                status = eslOK;
           } else {
             sqBlock->complete = FALSE;
           }
		       if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
		       return status;
			  }
			  else
			  {
				  esl_sq_Reuse(tmpsq);
				  tmpsq->start =  sqBlock->list[i].start ;
				  tmpsq->end   =  sqBlock->list[i].end ;
				  tmpsq->n     =  sqBlock->list[i].n ;
				  tmpsq->idx   =  sqBlock->list[i].idx ;
				  tmpsq->C = 0;
				  status = sqncbi_ReadWindow(sqfp, 0, max_residues, tmpsq);
		       if (status != eslEOD) {
		         if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
		         return status; 
		       }
				  status = eslOK;
			  }
		  }
	  }
	  if (status == eslEOF && i > 0) status = eslOK;
	  sqBlock->complete = TRUE;
	  if(tmpsq != NULL) esl_sq_Destroy(tmpsq);
	  return status;
}
static int sqncbi_ReadBlock ( ESL_SQFILE * sqfp , ESL_SQ_BLOCK * sqBlock , int max_residues , int max_sequences , int long_target ) { int i = 0 ; int size = 0 ; int status = eslOK ; ESL_SQ * tmpsq = NULL ; sqBlock -> count = 0 ; if ( ! long_target ) { if ( max_sequences < 1 || max_sequences > sqBlock -> listSize ) max_sequences = sqBlock -> listSize ; for ( i = 0 ; i < max_sequences && size < MAX_RESIDUE_COUNT ; ++ i ) { status = sqncbi_Read ( sqfp , sqBlock -> list + i ) ; if ( status != eslOK ) break ; size += sqBlock -> list [ i ] . n ; ++ sqBlock -> count ; } } else { if ( max_residues < 0 ) max_residues = MAX_RESIDUE_COUNT ; tmpsq = esl_sq_CreateDigital ( sqBlock -> list -> abc ) ; if ( ! sqBlock -> complete ) { status = sqncbi_ReadWindow ( sqfp , sqBlock -> list -> C , max_residues , sqBlock -> list ) ; if ( status == eslOK ) { sqBlock -> count = i = 1 ; size = sqBlock -> list -> n ; sqBlock -> list [ i ] . L = sqfp -> data . ncbi . seq_L ; if ( sqBlock -> list -> n >= max_residues ) { if ( sqBlock -> list -> n == sqfp -> data . ncbi . seq_L ) { sqBlock -> complete = TRUE ; esl_sq_Reuse ( tmpsq ) ; tmpsq -> start = sqBlock -> list -> start ; tmpsq -> C = 0 ; status = sqncbi_ReadWindow ( sqfp , 0 , max_residues , tmpsq ) ; if ( status == eslEOD ) status = eslOK ; } else { sqBlock -> complete = FALSE ; } if ( tmpsq != NULL ) esl_sq_Destroy ( tmpsq ) ; return status ; } else { esl_sq_Reuse ( tmpsq ) ; tmpsq -> start = sqBlock -> list -> start ; tmpsq -> C = 0 ; status = sqncbi_ReadWindow ( sqfp , 0 , max_residues , tmpsq ) ; if ( status != eslEOD ) { if ( tmpsq != NULL ) esl_sq_Destroy ( tmpsq ) ; return status ; } sqBlock -> list -> L = tmpsq -> L ; } } else if ( status == eslEOD ) { } else { if ( tmpsq != NULL ) esl_sq_Destroy ( tmpsq ) ; return status ; } } for ( ; i < sqBlock -> listSize && size < max_residues ; ++ i ) { int request_size = ESL_MAX ( max_residues - size , max_residues * .05 ) ; esl_sq_Reuse ( tmpsq ) ; esl_sq_Reuse ( sqBlock -> list + i ) ; status = sqncbi_ReadWindow ( sqfp , 0 , request_size , tmpsq ) ; esl_sq_Copy ( tmpsq , sqBlock -> list + i ) ; if ( status != eslOK ) break ; size += sqBlock -> list [ i ] . n - sqBlock -> list [ i ] . C ; sqBlock -> list [ i ] . L = sqfp -> data . ncbi . seq_L ; ++ ( sqBlock -> count ) ; if ( size >= max_residues ) { if ( sqBlock -> list [ i ] . n == sqfp -> data . ncbi . seq_L ) { sqBlock -> complete = TRUE ; esl_sq_Reuse ( tmpsq ) ; tmpsq -> start = sqBlock -> list -> start ; tmpsq -> C = 0 ; status = sqncbi_ReadWindow ( sqfp , 0 , max_residues , tmpsq ) ; if ( status == eslEOD ) status = eslOK ; } else { sqBlock -> complete = FALSE ; } if ( tmpsq != NULL ) esl_sq_Destroy ( tmpsq ) ; return status ; } else { esl_sq_Reuse ( tmpsq ) ; tmpsq -> start = sqBlock -> list [ i ] . start ; tmpsq -> end = sqBlock -> list [ i ] . end ; tmpsq -> n = sqBlock -> list [ i ] . n ; tmpsq -> idx = sqBlock -> list [ i ] . idx ; tmpsq -> C = 0 ; status = sqncbi_ReadWindow ( sqfp , 0 , max_residues , tmpsq ) ; if ( status != eslEOD ) { if ( tmpsq != NULL ) esl_sq_Destroy ( tmpsq ) ; return status ; } status = eslOK ; } } } if ( status == eslEOF && i > 0 ) status = eslOK ; sqBlock -> complete = TRUE ; if ( tmpsq != NULL ) esl_sq_Destroy ( tmpsq ) ; return status ; }
Function:  sqncbi_ReadBlock()
 Synopsis:  Read the next block of sequences from a file.
Function : sqncbi_ReadBlock () Synopsis : Read the next block of sequences from a file .
/* in these cases, an individual sequence won't ever be really long,
			     so just read in a sequence at a time  */ /* DNA, not an alignment.  Might be really long sequences */ /*this variable was used instead of the MAX_RESIDUE_COUNT macro because old H3 impl_dummy could've required shorter sequences to fit in memory*/ //if complete flag set to FALSE, then the prior block must have ended with a window that was a possibly //incomplete part of it's full sequence. Read another overlapping window. //overloading C as indicator of how big C should be for this window reading action // Filled the block with a single very long window. // burn off the EOD // otherwise, the unexpected status will be returned // Burn off EOD (see notes for similar entry ~25 lines below), then go fetch the next sequence //surprising // turns out there isn't any more of the sequence to read, after all // otherwise, just start at the beginning /* restricted request_size is used to ensure that all blocks are pretty close to the
	       * same size. Without it, we may either naively keep asking for max_residue windows,
	       * which can result in a window with ~2*max_residues ... or we can end up with absurdly
	       * short fragments at the end of blocks
	       */ // end of sequences // a full window worth of sequence was read // burn off the EOD // otherwise, the unexpected status will be returned /* Sequence was finished before filling a full window. Need to burn off the EOD value that will be
				     returned by the next ReadWindow call. Can just use a tmp sq, after setting a couple
				     values ReadWindow needs to see for correct processing.
				   */ //tmpsq->doff  =  sqBlock->list[i].doff ; //surprising //sqBlock->list[i].L = tmpsq->L; /* EOF will be returned only in the case were no sequences were read */