third_party_software/hmmer-3.0-linux-intel-x86_64/easel/miniapps/esl-alimanip.c
/* reorder_msa
 *                   
 * Given an array specifying a new order for the sequences in
 * the MSA, reorder it by swapping pointers.
 */


Given an array specifying a new order for the sequences in
the MSA, reorder it by swapping pointers.
Given an array specifying a new order for the sequences in the MSA reorder it by swapping pointers .
static int
reorder_msa(ESL_MSA *msa, int *order, char *errbuf)
{
  int status;
  char **tmp; 
  ESL_ALLOC(tmp, sizeof(char *) * msa->nseq);
  int i, a;
  int *covered;
  ESL_ALLOC(covered, sizeof(int) * msa->nseq);
  esl_vec_ISet(covered, msa->nseq, 0);
  for(i = 0; i < msa->nseq; i++) { 
    if(covered[order[i]]) ESL_FAIL(eslEINVAL, errbuf, "reorder_msa() order array has duplicate entries for i: %d\n", i);
    covered[order[i]] = 1;
  }
  free(covered);
  if(msa->flags & eslMSA_DIGITAL) { 
    ESL_DSQ **tmp_dsq; 
    ESL_ALLOC(tmp_dsq, sizeof(ESL_DSQ *) * msa->nseq);
    for(i = 0; i < msa->nseq; i++) tmp_dsq[i] = msa->ax[i];
    for(i = 0; i < msa->nseq; i++) msa->ax[i] = tmp_dsq[order[i]];
    free(tmp_dsq);
  }
  else { 
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->aseq[i];
    for(i = 0; i < msa->nseq; i++) msa->aseq[i] = tmp[order[i]];
  }
  for(i = 0; i < msa->nseq; i++) tmp[i] = msa->sqname[i];
  for(i = 0; i < msa->nseq; i++) msa->sqname[i] = tmp[order[i]];
  if(msa->sqacc != NULL) { 
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->sqacc[i];
    for(i = 0; i < msa->nseq; i++) msa->sqacc[i] = tmp[order[i]];
  }
  if(msa->sqdesc != NULL) { 
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->sqdesc[i];
    for(i = 0; i < msa->nseq; i++) msa->sqdesc[i] = tmp[order[i]];
  }
  if(msa->ss != NULL) { 
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->ss[i];
    for(i = 0; i < msa->nseq; i++) msa->ss[i] = tmp[order[i]];
  }
  if(msa->sa != NULL) { 
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->sa[i];
    for(i = 0; i < msa->nseq; i++) msa->sa[i] = tmp[order[i]];
  }
  if(msa->pp != NULL) { 
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->pp[i];
    for(i = 0; i < msa->nseq; i++) msa->pp[i] = tmp[order[i]];
  }
  for(a = 0; a < msa->ngs; a++) {
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->gs[a][i];
    for(i = 0; i < msa->nseq; i++) msa->gs[a][i] = tmp[order[i]];
  }
  for(a = 0; a < msa->ngr; a++) {
    for(i = 0; i < msa->nseq; i++) tmp[i] = msa->gr[a][i];
    for(i = 0; i < msa->nseq; i++) msa->gr[a][i] = tmp[order[i]];
  }
  free(tmp);
  return eslOK;
 ERROR: 
  return status;
}
static int reorder_msa ( ESL_MSA * msa , int * order , char * errbuf ) { int status ; char * * tmp ; ESL_ALLOC ( tmp , sizeof ( char * ) * msa -> nseq ) ; int i , a ; int * covered ; ESL_ALLOC ( covered , sizeof ( int ) * msa -> nseq ) ; esl_vec_ISet ( covered , msa -> nseq , 0 ) ; for ( i = 0 ; i < msa -> nseq ; i ++ ) { if ( covered [ order [ i ] ] ) ESL_FAIL ( eslEINVAL , errbuf , " \n " , i ) ; covered [ order [ i ] ] = 1 ; } free ( covered ) ; if ( msa -> flags & eslMSA_DIGITAL ) { ESL_DSQ * * tmp_dsq ; ESL_ALLOC ( tmp_dsq , sizeof ( ESL_DSQ * ) * msa -> nseq ) ; for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp_dsq [ i ] = msa -> ax [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> ax [ i ] = tmp_dsq [ order [ i ] ] ; free ( tmp_dsq ) ; } else { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> aseq [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> aseq [ i ] = tmp [ order [ i ] ] ; } for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> sqname [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> sqname [ i ] = tmp [ order [ i ] ] ; if ( msa -> sqacc != NULL ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> sqacc [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> sqacc [ i ] = tmp [ order [ i ] ] ; } if ( msa -> sqdesc != NULL ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> sqdesc [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> sqdesc [ i ] = tmp [ order [ i ] ] ; } if ( msa -> ss != NULL ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> ss [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> ss [ i ] = tmp [ order [ i ] ] ; } if ( msa -> sa != NULL ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> sa [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> sa [ i ] = tmp [ order [ i ] ] ; } if ( msa -> pp != NULL ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> pp [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> pp [ i ] = tmp [ order [ i ] ] ; } for ( a = 0 ; a < msa -> ngs ; a ++ ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> gs [ a ] [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> gs [ a ] [ i ] = tmp [ order [ i ] ] ; } for ( a = 0 ; a < msa -> ngr ; a ++ ) { for ( i = 0 ; i < msa -> nseq ; i ++ ) tmp [ i ] = msa -> gr [ a ] [ i ] ; for ( i = 0 ; i < msa -> nseq ; i ++ ) msa -> gr [ a ] [ i ] = tmp [ order [ i ] ] ; } free ( tmp ) ; return eslOK ; ERROR : return status ; }
reorder_msa
reorder_msa
/* contract check */ /* 'order' must be have nseq elements, elements must be in range [0..nseq-1], no duplicates  */ /* printf("order[i:%4d]: %4d\n", i, order[i]);
       printf("covered[order[i:%4d]]: %4d\n", i, covered[order[i]]);
    */ /* swap aseq or ax (one or the other must be non-NULL) */ /* digital MSA */ /* text MSA */ /* swap sqnames (mandatory) */ /* swap sqacc, if they exist */ /* swap sqdesc, if they exist */ /* swap ss, if they exist */ /* swap sa, if they exist */ /* swap pp, if they exist */ /* swap gs annotation, if it exists */ /* swap gr annotation, if it exists */