hmmer-3.3/easel/miniapps/esl-alimanip.c
/* Function:  insert_x_diffmx()
 * Synopsis:  NxN insert difference matrix for N aligned digital seqs.         
 * Incept:    EPN, Wed Jun 25 10:25:01 2008
 *
 * Purpose:   For each pair of sequences calculates the fraction of number
 *            of inserts that are different between the two sequences.
 *            An 'insert' is present in sequence s for consensus column 
 *            (non-gap RF column) c if at least 1 residue exists between 
 *            consensus column c and c+1. If sequence t also has an insert
 *            between c and c+1, they share that insert. If that were the
 *            only insert in either of the two sequences, then they would
 *            share 1.0 fractional insert id, and 1.0 - 1.0 = 0.0 fractional 
 *            insert difference, thus the insert diff mx entry between 
 *            seq s and t would be 0.0.
 *
 * Args:      go - command-line options
 *            errbuf - for printing error messages
 *            msa   - aligned dsq's, [0..N-1][1..alen]                  
 *            do_length_weight - weight insert similarity by length of inserts
 *            do_only_internal_inserts - TRUE to only count inserts that are at positions
 *                                       internal to both seq i, j (don't count those truncated off in either seq)
 *            ret_D - RETURN: NxN matrix of fractional differences
 *            
 * Returns:   <eslOK> on success, and <ret_D> contains the difference
 *            matrix; caller is obligated to free <D> with 
 *            <esl_dmatrix_Destroy()>. 
 *
 * Throws:    <eslEINVAL> if a seq has a different
 *            length than others. On failure, <ret_D> is returned <NULL>
 *            and state of inputs is unchanged.
 */
insert_x_diffmx()
Synopsis:  NxN insert difference matrix for N aligned digital seqs.

For each pair of sequences calculates the fraction of number
of inserts that are different between the two sequences.
An 'insert' is present in sequence s for consensus column
(non-gap RF column) c if at least 1 residue exists between
consensus column c and c+1. If sequence t also has an insert
between c and c+1, they share that insert. If that were the
only insert in either of the two sequences, then they would
share 1.0 fractional insert id, and 1.0 - 1.0 = 0.0 fractional
insert difference, thus the insert diff mx entry between
seq s and t would be 0.0.



 on success, and  contains the difference
matrix; caller is obligated to free  with
.

 if a seq has a different
length than others. On failure,  is returned 
and state of inputs is unchanged.
insert_x_diffmx () Synopsis : NxN insert difference matrix for N aligned digital seqs . For each pair of sequences calculates the fraction of number of inserts that are different between the two sequences . An ' insert ' is present in sequence s for consensus column ( non - gap RF column ) c if at least 1 residue exists between consensus column c and c + 1 . If sequence t also has an insert between c and c + 1 they share that insert . If that were the only insert in either of the two sequences then they would share 1 . 0 fractional insert id and 1 . 0 - 1 . 0 = 0 . 0 fractional insert difference thus the insert diff mx entry between seq s and t would be 0 . 0 . on success and contains the difference matrix ; caller is obligated to free with . if a seq has a different length than others . On failure is returned and state of inputs is unchanged .
int
insert_x_diffmx(const ESL_GETOPTS *go, char *errbuf, ESL_MSA *msa, int rflen, int *i_am_rf, int do_length_weight, int do_only_internal_inserts, ESL_DMATRIX **ret_D)
{
  int status;
  ESL_DMATRIX *D = NULL;
  int i,j;
  int N = msa->nseq;
  int nshared;
  double nshared_len;
  int nins;
  int *firstA, *lastA; 
  int ifirst, ilast, jfirst, jlast;
  if(msa->rf == NULL)                  ESL_FAIL(eslEINVAL, errbuf, "No #=GC RF markup in alignment.");
  if(! (msa->flags & eslMSA_DIGITAL))  ESL_FAIL(eslEINVAL, errbuf, "insert_x_diffmx() MSA is not digitized.\n");
  if (( D = esl_dmatrix_Create(N,N) ) == NULL) esl_fatal(errbuf);
  if ((status = determine_first_last_consensus_columns(msa, errbuf, i_am_rf, rflen, &firstA, &lastA)) != eslOK) return status;
  for (i = 0; i < N; i++)
    {
      D->mx[i][i] = 0.;
      ifirst = do_only_internal_inserts ? firstA[i] : 0;
      ilast  = do_only_internal_inserts ? lastA[i]  : rflen;
      for (j = i+1; j < N; j++)
	{
	  jfirst = do_only_internal_inserts ? firstA[j] : 0;
	  jlast  = do_only_internal_inserts ? lastA[j]  : rflen;
	  if(do_length_weight) { 
	    status = insert_x_pair_shared_length(msa, i_am_rf, i, j, ESL_MAX(ifirst, jfirst), ESL_MIN(ilast, jlast), &(D->mx[i][j]), &nshared_len, &nins);
	  }
	  else { 
	    status = insert_x_pair_shared(msa, i_am_rf, i, j, ESL_MAX(ifirst, jfirst), ESL_MIN(ilast, jlast), &(D->mx[i][j]), &nshared, &nins);
	  }
	  D->mx[i][j] = 1. - D->mx[i][j]; 
	  if (status != eslOK) ESL_XEXCEPTION(status, "Pairwise insert identity calculation failed at seqs %d,%d\n", i,j);
	  D->mx[j][i] =  D->mx[i][j];
	}
    }
  if (ret_D != NULL) *ret_D = D; else esl_dmatrix_Destroy(D);
  return eslOK;
 ERROR:
  if (D     != NULL)  esl_dmatrix_Destroy(D);
  if (ret_D != NULL) *ret_D = NULL;
  return status;
}
int insert_x_diffmx ( const ESL_GETOPTS * go , char * errbuf , ESL_MSA * msa , int rflen , int * i_am_rf , int do_length_weight , int do_only_internal_inserts , ESL_DMATRIX * * ret_D ) { int status ; ESL_DMATRIX * D = NULL ; int i , j ; int N = msa -> nseq ; int nshared ; double nshared_len ; int nins ; int * firstA , * lastA ; int ifirst , ilast , jfirst , jlast ; if ( msa -> rf == NULL ) ESL_FAIL ( eslEINVAL , errbuf , " " ) ; if ( ! ( msa -> flags & eslMSA_DIGITAL ) ) ESL_FAIL ( eslEINVAL , errbuf , " \n " ) ; if ( ( D = esl_dmatrix_Create ( N , N ) ) == NULL ) esl_fatal ( errbuf ) ; if ( ( status = determine_first_last_consensus_columns ( msa , errbuf , i_am_rf , rflen , & firstA , & lastA ) ) != eslOK ) return status ; for ( i = 0 ; i < N ; i ++ ) { D -> mx [ i ] [ i ] = 0. ; ifirst = do_only_internal_inserts ? firstA [ i ] : 0 ; ilast = do_only_internal_inserts ? lastA [ i ] : rflen ; for ( j = i + 1 ; j < N ; j ++ ) { jfirst = do_only_internal_inserts ? firstA [ j ] : 0 ; jlast = do_only_internal_inserts ? lastA [ j ] : rflen ; if ( do_length_weight ) { status = insert_x_pair_shared_length ( msa , i_am_rf , i , j , ESL_MAX ( ifirst , jfirst ) , ESL_MIN ( ilast , jlast ) , & ( D -> mx [ i ] [ j ] ) , & nshared_len , & nins ) ; } else { status = insert_x_pair_shared ( msa , i_am_rf , i , j , ESL_MAX ( ifirst , jfirst ) , ESL_MIN ( ilast , jlast ) , & ( D -> mx [ i ] [ j ] ) , & nshared , & nins ) ; } D -> mx [ i ] [ j ] = 1. - D -> mx [ i ] [ j ] ; if ( status != eslOK ) ESL_XEXCEPTION ( status , " \n " , i , j ) ; D -> mx [ j ] [ i ] = D -> mx [ i ] [ j ] ; } } if ( ret_D != NULL ) * ret_D = D ; else esl_dmatrix_Destroy ( D ) ; return eslOK ; ERROR : if ( D != NULL ) esl_dmatrix_Destroy ( D ) ; if ( ret_D != NULL ) * ret_D = NULL ; return status ; }
Function:  insert_x_diffmx()
 Synopsis:  NxN insert difference matrix for N aligned digital seqs.
Function : insert_x_diffmx () Synopsis : NxN insert difference matrix for N aligned digital seqs .
/* [0..i..nseq-1] first and last consensus column occupied by seq i, only used if do_only_internal_inserts == TRUE */ /* TEMP  for (i = 0; i < N; i++) printf("i: %4d %4d %4d\n", i, firstA[i], lastA[i]); */ /* if(esl_opt_GetBoolean(go, "--verbose")) printf("D %4d %4d %.3f %8.3f of %4d\n", i, j, 1. - D->mx[i][j], nshared_len, nins); */ /* if(esl_opt_GetBoolean(go, "--verbose")) printf("D %4d %4d %.3f %4d of %4d\n", i, j, 1. - D->mx[i][j], nshared, nins); */ /* convert from id to distance */ /* if(esl_opt_GetBoolean(go, "--verbose")) printf("\n"); */