hmmer-3.3/src/p7_spensemble.c
/* Function:  p7_spsensemble_Add()
 * Synopsis:  Add a new segment pair to a growing ensemble.
 * Incept:    SRE, Wed Jan  9 10:28:04 2008 [Janelia]
 *
 * Purpose:   Adds a new segment pair to a growing ensemble <sp>.
 *            The segment pair is defined by start/end positions
 *            <i>,<j> on a target sequence (1..L), and start/end
 *            positions <k>,<m> on a query model (1..M). 
 *            
 *            You also provide the index <sampleidx> of which sampled
 *            traceback this segment pair came from; each traceback
 *            contains one or more segment pairs. These <sampleidx>
 *            indices start at 0 and they must arrive sequentially:
 *            that is, the caller must <Add()> all the segment pairs
 *            from traceback sample 0, then <Add()> all the segment
 *            pairs from traceback sample 1, and so on.
 *            
 *            The reason to enforce sequential addition has to do with
 *            the internals of the ensemble clustering algorithm;
 *            specifically with how it calculates the posterior
 *            probability of a cluster in the ensemble. You can't
 *            calculate the posterior probability of a cluster simply
 *            by dividing the number of segment pairs in a cluster by
 *            the total number of traces, because you can get
 *            "probabilities" of greater than one: sometimes more than
 *            one pair from the same trace get clustered together
 *            (because one domain got split into two or more segment
 *            pairs). Rather, what it does is to count the total
 *            number of traces that have one or more segments in the
 *            cluster, divided by the total number of traces. An
 *            efficient way to implement this is, when counting
 *            segments that belong to a cluster, only increment the
 *            numerator if the segment has a different traceback index
 *            than the last segment we counted in this cluster. (We'd
 *            rather not have to keep track of a table of all the
 *            traceback indices we've seen so far.)
 *
 * Args:      sp        - ensemble to add this segment pair to
 *            sampleidx - index of traceback that this seg pair came from (0..nsamples-1)
 *            i,j       - start,end position on target sequence (1..L)
 *            k,m       - start,end position on query model (1..M)
 *
 * Returns:   <eslOK> on success.
 *
 * Throws:    <eslEINVAL> if the <sampleidx> is out of order.
 *            <eslEMEM> if a reallocation fails.
 */
p7_spsensemble_Add()
Synopsis:  Add a new segment pair to a growing ensemble.

Adds a new segment pair to a growing ensemble .
The segment pair is defined by start/end positions
, on a target sequence (1..L), and start/end
positions , on a query model (1..M).

You also provide the index  of which sampled
traceback this segment pair came from; each traceback
contains one or more segment pairs. These 
indices start at 0 and they must arrive sequentially:
that is, the caller must  all the segment pairs
from traceback sample 0, then  all the segment
pairs from traceback sample 1, and so on.

The reason to enforce sequential addition has to do with
the internals of the ensemble clustering algorithm;
specifically with how it calculates the posterior
probability of a cluster in the ensemble. You can't
calculate the posterior probability of a cluster simply
by dividing the number of segment pairs in a cluster by
the total number of traces, because you can get
"probabilities" of greater than one: sometimes more than
one pair from the same trace get clustered together
(because one domain got split into two or more segment
pairs). Rather, what it does is to count the total
number of traces that have one or more segments in the
cluster, divided by the total number of traces. An
efficient way to implement this is, when counting
segments that belong to a cluster, only increment the
numerator if the segment has a different traceback index
than the last segment we counted in this cluster. (We'd
rather not have to keep track of a table of all the
traceback indices we've seen so far.)



 on success.

 if the  is out of order.
 if a reallocation fails.
p7_spsensemble_Add () Synopsis : Add a new segment pair to a growing ensemble . Adds a new segment pair to a growing ensemble . The segment pair is defined by start / end positions on a target sequence ( 1 .. L ) and start / end positions on a query model ( 1 .. M ) . You also provide the index of which sampled traceback this segment pair came from ; each traceback contains one or more segment pairs . These indices start at 0 and they must arrive sequentially : that is the caller must all the segment pairs from traceback sample 0 then all the segment pairs from traceback sample 1 and so on . The reason to enforce sequential addition has to do with the internals of the ensemble clustering algorithm ; specifically with how it calculates the posterior probability of a cluster in the ensemble . You can ' t calculate the posterior probability of a cluster simply by dividing the number of segment pairs in a cluster by the total number of traces because you can get " probabilities " of greater than one : sometimes more than one pair from the same trace get clustered together ( because one domain got split into two or more segment pairs ) . Rather what it does is to count the total number of traces that have one or more segments in the cluster divided by the total number of traces . An efficient way to implement this is when counting segments that belong to a cluster only increment the numerator if the segment has a different traceback index than the last segment we counted in this cluster . ( We ' d rather not have to keep track of a table of all the traceback indices we ' ve seen so far . ) on success . if the is out of order . if a reallocation fails .
int
p7_spensemble_Add(P7_SPENSEMBLE *sp, int sampleidx, int i, int j, int k, int m)
{
  int status;
  if      (sampleidx > sp->nsamples)  ESL_EXCEPTION(eslEINVAL, "seg pair's <sampleidx> is out of order");
  else if (sampleidx == sp->nsamples) sp->nsamples++;
  if (sp->n >= sp->nalloc) {
    void *p;
    ESL_RALLOC(sp->sp,         p, sizeof(struct p7_spcoord_s)  * sp->nalloc * 2);
    ESL_RALLOC(sp->workspace,  p, sizeof(int)                  * sp->nalloc * 4); 
    ESL_RALLOC(sp->assignment, p, sizeof(int)                  * sp->nalloc * 2);
    sp->nalloc *= 2;
  }
  sp->sp[sp->n].idx = sampleidx;
  sp->sp[sp->n].i   = i;
  sp->sp[sp->n].j   = j;
  sp->sp[sp->n].k   = k;
  sp->sp[sp->n].m   = m;
  sp->n++;
  return eslOK;
 ERROR:
  return status;
}
int p7_spensemble_Add ( P7_SPENSEMBLE * sp , int sampleidx , int i , int j , int k , int m ) { int status ; if ( sampleidx > sp -> nsamples ) ESL_EXCEPTION ( eslEINVAL , " " ) ; else if ( sampleidx == sp -> nsamples ) sp -> nsamples ++ ; if ( sp -> n >= sp -> nalloc ) { void * p ; ESL_RALLOC ( sp -> sp , p , sizeof ( struct p7_spcoord_s ) * sp -> nalloc * 2 ) ; ESL_RALLOC ( sp -> workspace , p , sizeof ( int ) * sp -> nalloc * 4 ) ; ESL_RALLOC ( sp -> assignment , p , sizeof ( int ) * sp -> nalloc * 2 ) ; sp -> nalloc *= 2 ; } sp -> sp [ sp -> n ] . idx = sampleidx ; sp -> sp [ sp -> n ] . i = i ; sp -> sp [ sp -> n ] . j = j ; sp -> sp [ sp -> n ] . k = k ; sp -> sp [ sp -> n ] . m = m ; sp -> n ++ ; return eslOK ; ERROR : return status ; }
Function:  p7_spsensemble_Add()
 Synopsis:  Add a new segment pair to a growing ensemble.
Function : p7_spsensemble_Add () Synopsis : Add a new segment pair to a growing ensemble .
/* remember, workspace is 2n */