⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shuffle.c

📁 这是一个基于HMM 模型的生物多序列比对算法的linux实现版本。hmmer
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************** * HMMER - Biological sequence analysis with profile HMMs * Copyright (C) 1992-2003 Washington University School of Medicine * All Rights Reserved *  *     This source code is distributed under the terms of the *     GNU General Public License. See the files COPYING and LICENSE *     for details. *****************************************************************//* shuffle.c *  * Routines for randomizing sequences. *   * All routines are alphabet-independent (DNA, protein, RNA, whatever); * they assume that input strings are purely alphabetical [a-zA-Z], and * will return strings in all upper case [A-Z]. *   * All return 1 on success, and 0 on failure; 0 status invariably * means the input string was not alphabetical. *  * StrShuffle()   - shuffled string, preserve mono-symbol composition. * StrDPShuffle() - shuffled string, preserve mono- and di-symbol composition. *  * StrMarkov0()   - random string, same zeroth order Markov properties. * StrMarkov1()   - random string, same first order Markov properties. *  * StrReverse()   - simple reversal of string * StrRegionalShuffle() -  mono-symbol shuffled string in regional windows * * There are also similar routines for shuffling alignments: * * AlignmentShuffle()   - alignment version of StrShuffle(). * AlignmentBootstrap() - sample with replacement; a bootstrap dataset. * QRNAShuffle()        - shuffle a pairwise alignment, preserving all gap positions. *  * CVS $Id: shuffle.c,v 1.8 2003/04/14 16:00:16 eddy Exp $ */#include "squidconf.h"#include <string.h>#include <ctype.h>#include "squid.h"#include "sre_random.h"/* Function: StrShuffle() *  * Purpose:  Returns a shuffled version of s2, in s1. *           (s1 and s2 can be identical, to shuffle in place.) *   * Args:     s1 - allocated space for shuffled string. *           s2 - string to shuffle. *            * Return:   1 on success. */intStrShuffle(char *s1, char *s2){  int  len;  int  pos;  char c;    if (s1 != s2) strcpy(s1, s2);  for (len = strlen(s1); len > 1; len--)    {				      pos       = CHOOSE(len);      c         = s1[pos];      s1[pos]   = s1[len-1];      s1[len-1] = c;    }  return 1;}/* Function: StrDPShuffle() * Date:     SRE, Fri Oct 29 09:15:17 1999 [St. Louis] * * Purpose:  Returns a shuffled version of s2, in s1. *           (s1 and s2 may be identical; i.e. a string *           may be shuffled in place.) The shuffle is a   *           "doublet-preserving" (DP) shuffle. Both *           mono- and di-symbol composition are preserved. *            *           Done by searching for a random Eulerian  *           walk on a directed multigraph.  *           Reference: S.F. Altschul and B.W. Erickson, Mol. Biol. *           Evol. 2:526-538, 1985. Quoted bits in my comments *           are from Altschul's outline of the algorithm. * * Args:     s1   - RETURN: the string after it's been shuffled *                    (space for s1 allocated by caller) *           s2   - the string to be shuffled * * Returns:  0 if string can't be shuffled (it's not all [a-zA-z] *             alphabetic. *           1 on success.  */intStrDPShuffle(char *s1, char *s2){  int    len;  int    pos;	/* a position in s1 or s2 */  int    x,y;   /* indices of two characters */  char **E;     /* edge lists: E[0] is the edge list from vertex A */  int   *nE;    /* lengths of edge lists */  int   *iE;    /* positions in edge lists */  int    n;	/* tmp: remaining length of an edge list to be shuffled */  char   sf;    /* last character in s2 */  char   Z[26]; /* connectivity in last edge graph Z */   int    keep_connecting; /* flag used in Z connectivity algorithm */  int    is_eulerian;		/* flag used for when we've got a good Z */    /* First, verify that the string is entirely alphabetic.   */  len = strlen(s2);  for (pos = 0; pos < len; pos++)    if (! isalpha((int) s2[pos])) return 0;  /* "(1) Construct the doublet graph G and edge ordering E   *      corresponding to S."   *    * Note that these also imply the graph G; and note,   * for any list x with nE[x] = 0, vertex x is not part   * of G.   */  E  = MallocOrDie(sizeof(char *) * 26);  nE = MallocOrDie(sizeof(int)    * 26);  for (x = 0; x < 26; x++)    {      E[x]  = MallocOrDie(sizeof(char) * (len-1));      nE[x] = 0;     }  x = toupper((int) s2[0]) - 'A';  for (pos = 1; pos < len; pos++)    {      y = toupper((int) s2[pos]) - 'A';      E[x][nE[x]] = y;      nE[x]++;      x = y;    }    /* Now we have to find a random Eulerian edge ordering.   */  sf = toupper((int) s2[len-1]) - 'A';   is_eulerian = 0;  while (! is_eulerian)    {      /* "(2) For each vertex s in G except s_f, randomly select       *      one edge from the s edge list of E(S) to be the       *      last edge of the s list in a new edge ordering."       *       * select random edges and move them to the end of each        * edge list.       */      for (x = 0; x < 26; x++)	{	  if (nE[x] == 0 || x == sf) continue;	  	  pos           = CHOOSE(nE[x]);	  y             = E[x][pos];			  E[x][pos]     = E[x][nE[x]-1];	  E[x][nE[x]-1] = y;	}      /* "(3) From this last set of edges, construct the last-edge       *      graph Z and determine whether or not all of its       *      vertices are connected to s_f."       *        * a probably stupid algorithm for looking at the       * connectivity in Z: iteratively sweep through the       * edges in Z, and build up an array (confusing called Z[x])       * whose elements are 1 if x is connected to sf, else 0.       */      for (x = 0; x < 26; x++) Z[x] = 0;      Z[(int) sf] = keep_connecting = 1;      while (keep_connecting) {	keep_connecting = 0;	for (x = 0; x < 26; x++)	  {	    y = E[x][nE[x]-1];            /* xy is an edge in Z */	    if (Z[x] == 0 && Z[y] == 1)   /* x is connected to sf in Z */	      {		Z[x] = 1;		keep_connecting = 1;	      }	  }      }      /* if any vertex in Z is tagged with a 0, it's       * not connected to sf, and we won't have a Eulerian       * walk.       */      is_eulerian = 1;      for (x = 0; x < 26; x++)	{	  if (nE[x] == 0 || x == sf) continue;	  if (Z[x] == 0) {	    is_eulerian = 0;	    break;	  }	}      /* "(4) If any vertex is not connected in Z to s_f, the       *      new edge ordering will not be Eulerian, so return to       *      (2). If all vertices are connected in Z to s_f,        *      the new edge ordering will be Eulerian, so       *      continue to (5)."       *             * e.g. note infinite loop while is_eulerian is FALSE.       */    }  /* "(5) For each vertex s in G, randomly permute the remaining   *      edges of the s edge list of E(S) to generate the s   *      edge list of the new edge ordering E(S')."   *         * Essentially a StrShuffle() on the remaining nE[x]-1 elements   * of each edge list; unfortunately our edge lists are arrays,   * not strings, so we can't just call out to StrShuffle().   */  for (x = 0; x < 26; x++)    for (n = nE[x] - 1; n > 1; n--)      {	pos       = CHOOSE(n);	y         = E[x][pos];	E[x][pos] = E[x][n-1];	E[x][n-1] = y;      }  /* "(6) Construct sequence S', a random DP permutation of   *      S, from E(S') as follows. Start at the s_1 edge list.   *      At each s_i edge list, add s_i to S', delete the   *      first edge s_i,s_j of the edge list, and move to   *      the s_j edge list. Continue this process until   *      all edge lists are exhausted."   */   iE = MallocOrDie(sizeof(int) * 26);  for (x = 0; x < 26; x++) iE[x] = 0;   pos = 0;   x = toupper((int) s2[0]) - 'A';  while (1)     {      s1[pos++] = 'A' + x;	/* add s_i to S' */            y = E[x][iE[x]];      iE[x]++;			/* "delete" s_i,s_j from edge list */        x = y;			/* move to s_j edge list. */      if (iE[x] == nE[x])	break;			/* the edge list is exhausted. */    }  s1[pos++] = 'A' + sf;  s1[pos]   = '\0';    /* Reality checks.   */  if (x   != sf)  Die("hey, you didn't end on s_f.");  if (pos != len) Die("hey, pos (%d) != len (%d).", pos, len);    /* Free and return.   */  Free2DArray((void **) E, 26);  free(nE);  free(iE);  return 1;}  /* Function: StrMarkov0() * Date:     SRE, Fri Oct 29 11:08:31 1999 [St. Louis] * * Purpose:  Returns a random string s1 with the same *           length and zero-th order Markov properties *           as s2.  *            *           s1 and s2 may be identical, to randomize s2 *           in place. * * Args:     s1 - allocated space for random string *           s2 - string to base s1's properties on. * * Returns:  1 on success; 0 if s2 doesn't look alphabetical. */int StrMarkov0(char *s1, char *s2){  int   len;  int   pos;   float p[26];			/* symbol probabilities */  /* First, verify that the string is entirely alphabetic.   */  len = strlen(s2);  for (pos = 0; pos < len; pos++)    if (! isalpha((int) s2[pos])) return 0;  /* Collect zeroth order counts and convert to frequencies.   */  FSet(p, 26, 0.);  for (pos = 0; pos < len; pos++)    p[(int)(toupper((int) s2[pos]) - 'A')] += 1.0;  FNorm(p, 26);  /* Generate a random string using those p's.   */  for (pos = 0; pos < len; pos++)    s1[pos] = FChoose(p, 26) + 'A';  s1[pos] = '\0';  return 1;}/* Function: StrMarkov1() * Date:     SRE, Fri Oct 29 11:22:20 1999 [St. Louis] *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -