将核碱基表示从ASCII转换为UCSC .2bit

ymzxtsji  于 2024-01-06  发布在  其他
关注(0)|答案(3)|浏览(292)

非抗生素DNA序列仅由核碱基腺嘌呤(A)、胞嘧啶(C)、鸟嘌呤(G)、胸腺嘧啶(T)组成。对于人类消费,碱基可以用相应的char表示,在charchar中:ACGTacgt。然而,当需要存储长序列时,这种表示是低效的。由于只需要存储四个符号,每个符号都可以分配一个2位代码。2 UCSC规定的常用.2bit正是这样做的,使用以下编码:T = 0b00,C = 0b01,A = 0b10,G = 0b11
下面的C代码显示了为清晰起见而编写的参考实现。各种转换以char序列表示的基因组序列的开源软件通常使用由序列中的每个char索引的256条目查找表。这也与char的内部表示隔离。然而,存储器访问在能量上是昂贵的,即使访问的是片上缓存,并且通用表查找难以SIMD化。因此,如果转换可以通过简单的整数运算来完成,这将是有利的。考虑到ASCII是主要的char编码,可以限制于此。
什么是有效的计算方法来转换作为ASCII字符的核碱基到它们的.2bit-表示?

  1. /* Convert nucleobases A, C, G, T represented as either uppercase or lowercase
  2. ASCII characters into UCSC .2bit-presentation. Passing any values other than
  3. those corresponding to 'A', 'C', 'G', 'T', 'a', 'c', 'g', 't' results in an
  4. indeterminate response.
  5. */
  6. unsigned int ascii_to_2bit (unsigned int a)
  7. {
  8. const unsigned int UCSC_2BIT_A = 2;
  9. const unsigned int UCSC_2BIT_C = 1;
  10. const unsigned int UCSC_2BIT_G = 3;
  11. const unsigned int UCSC_2BIT_T = 0;
  12. switch (a) {
  13. case 'A':
  14. case 'a':
  15. return UCSC_2BIT_A;
  16. break;
  17. case 'C':
  18. case 'c':
  19. return UCSC_2BIT_C;
  20. break;
  21. case 'G':
  22. case 'g':
  23. return UCSC_2BIT_G;
  24. break;
  25. case 'T':
  26. case 't':
  27. default:
  28. return UCSC_2BIT_T;
  29. break;
  30. }
  31. }

字符串

kmynzznz

kmynzznz1#

如果我们仔细观察碱基的ASCII字符的二进制代码,就会发现第1位和第2位提供了一个唯一的两位代码:A = 0b01000001 -> 0b00C = 0b01000011 -> 0b01G = 0b01000111 -> 0b11T = 0b01010100-> 0b10。类似于ASCII字符,仅在第5位不同。不幸的是,这种简单的Map并不完全匹配.2bit编码,其中A和T的代码被交换。解决这个问题的一种方法是用一个简单的四项排列表存储在一个变量中,可能在优化后分配给一个寄存器(“寄存器内查找表”):

  1. unsigned int ascii_to_2bit_perm (unsigned int a)
  2. {
  3. unsigned int perm = (2 << 0) | (1 << 2) | (0 << 4) | (3 << 6);
  4. return (perm >> (a & 6)) & 3;
  5. }

字符串
另一种方法通过观察AT的位1为0,但CG为1。因此,我们可以通过对 inverse 进行XOR来交换A和T的编码。将输入的位1与初始代码的位1进行比较:

  1. unsigned int ascii_to_2bit_twiddle (unsigned int a)
  2. {
  3. return ((a >> 1) & 3) ^ (~a & 2);
  4. }


这个版本在具有快速位域提取指令的处理器和没有桶形移位器的低端处理器上很有优势,因为只需要右移一个位位置。在乱序处理器上,这种方法提供了比置换表更高的并行度。它似乎更容易适应SIMD实现,因为所有字节通道都使用相同的移位计数。
在我仔细研究相关ASCII字符的二进制编码之前,我研究了一个简单的数学计算,对较小的乘数和除数进行简单的蛮力搜索,得到:

  1. unsigned int ascii_to_2bit_math (unsigned int a)
  2. {
  3. return ((18 * (a & 31)) % 41) & 3;
  4. }


乘法器18对于没有快速整数乘法器的处理器来说是友好的。现代编译器可以有效地处理带有编译时常数除数的模计算,并且不需要除法。即使如此,我注意到即使是最好的编译器也很难利用非常有限的输入和输出范围,所以我不得不手动修改它来简化代码:

  1. unsigned int ascii_to_2bit_math (unsigned int a)
  2. {
  3. unsigned int t = 18 * (a & 31);
  4. return (t - ((t * 25) >> 10)) & 3;
  5. }


然而,在64位平台上,这整个计算可以用64位、32条目的查找表来代替,如果这个64位表可以有效地放入寄存器中,则该查找表可以给予有竞争力的性能,这是x86-64的情况,其中它被作为立即数加载。

  1. unsigned int ascii_to_2bit_tab (unsigned int a)
  2. {
  3. uint64_t tab = ((0ULL << 0) | (2ULL << 2) | (0ULL << 4) | (1ULL << 6) |
  4. (3ULL << 8) | (0ULL << 10) | (2ULL << 12) | (3ULL << 14) |
  5. (1ULL << 16) | (3ULL << 18) | (0ULL << 20) | (2ULL << 22) |
  6. (3ULL << 24) | (1ULL << 26) | (2ULL << 28) | (0ULL << 30) |
  7. (1ULL << 32) | (3ULL << 34) | (1ULL << 36) | (2ULL << 38) |
  8. (0ULL << 40) | (1ULL << 42) | (3ULL << 44) | (0ULL << 46) |
  9. (2ULL << 48) | (0ULL << 50) | (1ULL << 52) | (3ULL << 54) |
  10. (0ULL << 56) | (2ULL << 58) | (3ULL << 60) | (1ULL << 62));
  11. return (tab >> (2 * (a & 31))) & 3;
  12. }


我添加了我的测试框架以供参考:

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #define ORIGINAL_MATH (1)
  5. unsigned int ascii_to_2bit_perm (unsigned int a)
  6. {
  7. unsigned int perm = (2 << 0) | (1 << 2) | (0 << 4) | (3 << 6);
  8. return (perm >> (a & 6)) & 3;
  9. }
  10. unsigned int ascii_to_2bit_twiddle (unsigned int a)
  11. {
  12. return ((a >> 1) & 3) ^ (~a & 2);
  13. }
  14. unsigned int ascii_to_2bit_math (unsigned int a)
  15. {
  16. #if ORIGINAL_MATH
  17. return ((18 * (a & 31)) % 41) & 3;
  18. #else // ORIGINAL_MATH
  19. unsigned int t = 18 * (a & 31);
  20. return (t - ((t * 25) >> 10)) & 3;
  21. #endif // ORIGINAL_MATH
  22. }
  23. unsigned int ascii_to_2bit_tab (unsigned int a)
  24. {
  25. uint64_t tab = ((0ULL << 0) | (2ULL << 2) | (0ULL << 4) | (1ULL << 6) |
  26. (3ULL << 8) | (0ULL << 10) | (2ULL << 12) | (3ULL << 14) |
  27. (1ULL << 16) | (3ULL << 18) | (0ULL << 20) | (2ULL << 22) |
  28. (3ULL << 24) | (1ULL << 26) | (2ULL << 28) | (0ULL << 30) |
  29. (1ULL << 32) | (3ULL << 34) | (1ULL << 36) | (2ULL << 38) |
  30. (0ULL << 40) | (1ULL << 42) | (3ULL << 44) | (0ULL << 46) |
  31. (2ULL << 48) | (0ULL << 50) | (1ULL << 52) | (3ULL << 54) |
  32. (0ULL << 56) | (2ULL << 58) | (3ULL << 60) | (1ULL << 62));
  33. return (tab >> (2 * (a & 31))) & 3;
  34. }
  35. /* Convert nucleobases A, C, G, T represented as either uppercase or lowercase
  36. ASCII characters into UCSC .2bit-presentation. Passing any values other than
  37. those corresponding to 'A', 'C', 'G', 'T', 'a', 'c', 'g', 't' results in an
  38. inderminate response.
  39. */
  40. unsigned int ascii_to_2bit (unsigned int a)
  41. {
  42. const unsigned int UCSC_2BIT_A = 2;
  43. const unsigned int UCSC_2BIT_C = 1;
  44. const unsigned int UCSC_2BIT_G = 3;
  45. const unsigned int UCSC_2BIT_T = 0;
  46. switch (a) {
  47. case 'A':
  48. case 'a':
  49. return UCSC_2BIT_A;
  50. break;
  51. case 'C':
  52. case 'c':
  53. return UCSC_2BIT_C;
  54. break;
  55. case 'G':
  56. case 'g':
  57. return UCSC_2BIT_G;
  58. break;
  59. case 'T':
  60. case 't':
  61. default:
  62. return UCSC_2BIT_T;
  63. break;
  64. }
  65. }
  66. int main (void)
  67. {
  68. char nucleobase[8] = {'A', 'C', 'G', 'T', 'a', 'c', 'g', 't'};
  69. printf ("Testing permutation variant:\n");
  70. for (unsigned int i = 0; i < sizeof nucleobase; i++) {
  71. unsigned int ref = ascii_to_2bit (nucleobase[i]);
  72. unsigned int res = ascii_to_2bit_perm (nucleobase[i]);
  73. printf ("i=%2u %c res=%u ref=%u %c\n",
  74. i, nucleobase[i], res, ref, (res == ref) ? 'p' : 'F');
  75. }
  76. printf ("Testing bit-twiddling variant:\n");
  77. for (unsigned int i = 0; i < sizeof nucleobase; i++) {
  78. unsigned int ref = ascii_to_2bit (nucleobase[i]);
  79. unsigned int res = ascii_to_2bit_twiddle (nucleobase[i]);
  80. printf ("i=%2u %c res=%u ref=%u %c\n",
  81. i, nucleobase[i], res, ref, (res == ref) ? 'p' : 'F');
  82. }
  83. printf ("Testing math-based variant:\n");
  84. for (unsigned int i = 0; i < sizeof nucleobase; i++) {
  85. unsigned int ref = ascii_to_2bit (nucleobase[i]);
  86. unsigned int res = ascii_to_2bit_math (nucleobase[i]);
  87. printf ("i=%2u %c res=%u ref=%u %c\n",
  88. i, nucleobase[i], res, ref, (res == ref) ? 'p' : 'F');
  89. }
  90. printf ("Testing table-based variant:\n");
  91. for (unsigned int i = 0; i < sizeof nucleobase; i++) {
  92. unsigned int ref = ascii_to_2bit (nucleobase[i]);
  93. unsigned int res = ascii_to_2bit_tab (nucleobase[i]);
  94. printf ("i=%2u %c res=%u ref=%u %c\n",
  95. i, nucleobase[i], res, ref, (res == ref) ? 'p' : 'F');
  96. }
  97. return EXIT_SUCCESS;
  98. }

展开查看全部
zbq4xfa0

zbq4xfa02#

一种选择是:

  1. unsigned int ascii_to_2bit (unsigned int a)
  2. {
  3. return ((0xad - a) & 6) >> 1;
  4. }

字符串
其优点在于,它只需要8位,不会溢出,并且不包含非常数移位,因此即使没有特定的SIMD指令,例如,在64位字中输入8个字符,也可以立即用于并行化

  1. unsigned int ascii_to_2bit_8bytes (uint64_t a)
  2. {
  3. return ((0xadadadadadadadad - a) & 0x0606060606060606) >> 1;
  4. }


在每个字节的底部留下两个输出位。

展开查看全部
wz8daaqr

wz8daaqr3#

下面的例程将用ASCII字符串的内容填充uint32_t值的数组,该ASCII字符串由您指定的字符填充,并将保存状态,以便能够附加第二个,第三个,使用它的方法通过一个main()例程来说明,该例程从命令行获取字符串参数,并将它们传递给TOTAL元素。例程的参数在它上面的注解中描述。

  1. #include <ctype.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define UCSC_2BIT_T (0)
  7. #define UCSC_2BIT_C (1)
  8. #define UCSC_2BIT_A (2)
  9. #define UCSC_2BIT_G (3)
  10. #define PAIRS_PER_INTEGER 16
  11. /**
  12. * Converts a string of nucleobases in ASCII form into an array
  13. * of 32bit integers in 2bitform. As it should be possible to
  14. * continue the array of integers, a status reference is
  15. * maintained in order to determine determine where in the
  16. * integer to start putting the two bit sequences. For this,
  17. * a (*state) variable is maintained (initialize it to 0) to
  18. * remember where to start putting bitpairs in the array.
  19. *
  20. * @param state_ref reference to the state variable to be maintained
  21. * with the position on which to put the base in the
  22. * array entry.
  23. * @param dna string with the ASCII chain of bases.
  24. * @param twobit array reference to start filling.
  25. * @param sz size of the array ***in array entries***.
  26. * @return the number of array elements written. This allows to
  27. * use a pointer and advance it at each function call
  28. * with the number of entries consumed on each call.
  29. */
  30. ssize_t
  31. dna2twobit(
  32. int *state_ref,
  33. char *dna,
  34. uint32_t twobit[],
  35. size_t sz)
  36. {
  37. /* local copy so we only change the state in case of
  38. * successful execution */
  39. int state = 30 - *state_ref;
  40. if (state == 30) *twobit = 0;
  41. ssize_t total_nb = 0; /* total number of array elements consumed */
  42. while (*dna && sz) {
  43. char base = toupper(*dna++);
  44. uint32_t tb;
  45. switch (base) {
  46. case 'A': tb = UCSC_2BIT_A; break;
  47. case 'C': tb = UCSC_2BIT_C; break;
  48. case 'T': tb = UCSC_2BIT_T; break;
  49. case 'G': tb = UCSC_2BIT_G; break;
  50. default: return -1;
  51. }
  52. *twobit |= tb << state;
  53. state -= 2;
  54. if (state < 0) {
  55. --sz; ++twobit;
  56. state += 32;
  57. *twobit = 0;
  58. total_nb++;
  59. }
  60. }
  61. *state_ref = 30 - state;
  62. return total_nb;
  63. }

字符串
这个函数可以单独链接到任何程序中,但我提供了一个main()代码来说明函数的使用。您可以在命令行参数中使用ASCII编码的实际链来调用程序。程序将一个接一个地对它们进行编码,以演示多重转换(16个碱基适合一个32位整数,正如您在问题中发布的格式定义所述,因此,如果没有编码16的倍数,则状态反映了最后一个中覆盖了多少位。
代码继续执行下面的main函数:

  1. #define TOTAL 16
  2. int main(int argc, char **argv)
  3. {
  4. int state = 0, i;
  5. uint32_t twobit_array[TOTAL], *p = twobit_array;
  6. size_t twobit_cap = TOTAL;
  7. for (i = 1; i < argc; ++i) {
  8. printf("Adding the following chain: [%s]\n", argv[i]);
  9. ssize_t n = dna2twobit(
  10. &state,
  11. argv[i],
  12. p,
  13. twobit_cap);
  14. if (n < 0) {
  15. fprintf(stderr,
  16. "argument invalid: %s\n"
  17. "continuing to next\n",
  18. argv[i]);
  19. continue;
  20. }
  21. twobit_cap -= n;
  22. p += n;
  23. }
  24. if (!state) --p;
  25. uint32_t *q = twobit_array;
  26. size_t off = 0;
  27. for (int j = 0; q <= p; j++) {
  28. char *sep = "";
  29. printf("%09zd: ", off);
  30. for (int k = 0; k < 4 && q <= p; k++) {
  31. printf("%s%08x", sep, *q);
  32. sep = "-";
  33. q++;
  34. off += 16;
  35. }
  36. printf("\n");
  37. }
  38. return 0;
  39. }

展开查看全部

相关问题