utils.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * nvbio
  3. * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the NVIDIA CORPORATION nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <nvbio/basic/types.h>
  29. #include <nvbio/basic/numbers.h>
  30. #include <nvbio/basic/console.h>
  31. #include <string>
  32. namespace nvbio {
  33. namespace alndiff {
  34. struct BooleanStats
  35. {
  36. BooleanStats() : L(0), R(0), L_not_R(0), R_not_L(0), L_and_R(0), n(0) {}
  37. void push(const bool l, const bool r)
  38. {
  39. L += (l == true) ? 1u : 0u;
  40. R += (r == true) ? 1u : 0u;
  41. L_not_R += (l == true) && (r == false) ? 1u : 0u;
  42. R_not_L += (r == true) && (l == false) ? 1u : 0u;
  43. L_and_R += (l == true) && (r == true) ? 1u : 0u;
  44. ++n;
  45. }
  46. float avg_L() const { return n ? float(L) / float(n) : 0.0f; }
  47. float avg_R() const { return n ? float(R) / float(n) : 0.0f; }
  48. float avg_L_not_R() const { return n ? float(L_not_R) / float(n) : 0.0f; }
  49. float avg_R_not_L() const { return n ? float(R_not_L) / float(n) : 0.0f; }
  50. float avg_L_and_R() const { return n ? float(L_and_R) / float(n) : 0.0f; }
  51. uint32 L;
  52. uint32 R;
  53. uint32 L_not_R;
  54. uint32 R_not_L;
  55. uint32 L_and_R;
  56. uint32 n;
  57. };
  58. template <uint32 X>
  59. struct Histogram
  60. {
  61. Histogram() : count(0)
  62. {
  63. for (uint32 i = 0; i < 2*X; ++i)
  64. bins[i] = 0;
  65. }
  66. uint32 all_but(const uint32 i) const { return count - bins[i+X]; }
  67. uint32 operator[] (const int32 i) const { return bins[i + X]; }
  68. void push(const int32 i)
  69. {
  70. const int32 bin = nvbio::min(nvbio::max(int32(i + X),0),int32(2*X-1));
  71. ++bins[bin];
  72. ++count;
  73. }
  74. uint32 count;
  75. uint32 bins[2*X];
  76. };
  77. // accumulate histogram values bottom-up
  78. template <uint32 X>
  79. Histogram<X> cumulative(const Histogram<X>& I)
  80. {
  81. Histogram<X> H;
  82. H.count = I.count;
  83. H.bins[0] = I.bins[0];
  84. for (int32 i = 1; i < 2*X; ++i)
  85. H.bins[i] = I.bins[i] + H.bins[i-1];
  86. return H;
  87. }
  88. // accumulate histogram values top-down
  89. template <uint32 X>
  90. Histogram<X> reverse_cumulative(const Histogram<X>& I)
  91. {
  92. Histogram<X> H;
  93. H.count = I.count;
  94. H.bins[2*X-1] = I.bins[2*X-1];
  95. for (int32 i = 2*X-2; i >= 0; --i)
  96. H.bins[i] = I.bins[i] + H.bins[i+1];
  97. return H;
  98. }
  99. template <uint32 X, uint32 Y>
  100. struct Histogram2d
  101. {
  102. Histogram2d() : count(0)
  103. {
  104. for (uint32 i = 0; i < 2*X; ++i)
  105. for (uint32 j = 0; j < 2*Y; ++j)
  106. bins[i][j] = 0;
  107. }
  108. void push(const int32 x, const int32 y)
  109. {
  110. const int32 bin_x = nvbio::min(nvbio::max(int32(x + X),0),int32(2*X-1));
  111. const int32 bin_y = nvbio::min(nvbio::max(int32(y + Y),0),int32(2*Y-1));
  112. ++bins[bin_x][bin_y];
  113. ++count;
  114. }
  115. uint32 operator() (const int32 i, const int32 j) const { return bins[i + X][j + Y]; }
  116. uint32 count;
  117. uint32 bins[2*X][2*Y];
  118. };
  119. inline
  120. uint32 read_length_bin_range(const uint32 bin)
  121. {
  122. switch (bin)
  123. {
  124. case 0:
  125. return 16;
  126. case 1:
  127. return 36;
  128. case 2:
  129. return 100;
  130. case 3:
  131. return 150;
  132. case 4:
  133. return 200;
  134. case 5:
  135. return 250;
  136. case 6:
  137. return 300;
  138. case 7:
  139. return 350;
  140. case 8:
  141. return 400;
  142. case 9:
  143. return 450;
  144. case 10:
  145. return 500;
  146. default:
  147. return 1000;
  148. }
  149. }
  150. inline
  151. uint32 read_length_bin(const uint32 read_len)
  152. {
  153. if (read_len <= 16)
  154. return 0;
  155. else if (read_len <= 36)
  156. return 1;
  157. else if (read_len <= 100)
  158. return 2;
  159. else if (read_len <= 150)
  160. return 3;
  161. else if (read_len <= 200)
  162. return 4;
  163. else if (read_len <= 250)
  164. return 5;
  165. else if (read_len <= 300)
  166. return 6;
  167. else if (read_len <= 350)
  168. return 7;
  169. else if (read_len <= 400)
  170. return 8;
  171. else if (read_len <= 450)
  172. return 9;
  173. else if (read_len <= 500)
  174. return 10;
  175. else
  176. return 11;
  177. }
  178. inline int32 log_bin(const int32 x)
  179. {
  180. return x == 0 ? 0u :
  181. x < 0 ? -int32(1u + nvbio::log2(-x)) :
  182. int32(1u + nvbio::log2(x));
  183. }
  184. inline int32 log_bin_range(const int32 bin)
  185. {
  186. return bin == 0 ? 0 :
  187. bin < 0 ? -int32(1u << (-bin+1)) :
  188. int32(1u << (bin-1));
  189. }
  190. // return the local file name from a path
  191. //
  192. inline const char* local_file(const std::string& file_name)
  193. {
  194. #if WIN32
  195. const size_t pos = file_name.find_last_of("/\\");
  196. #else
  197. const size_t pos = file_name.rfind('/');
  198. #endif
  199. if (pos == std::string::npos)
  200. return file_name.c_str();
  201. else
  202. return file_name.c_str() + pos + 1u;
  203. }
  204. } // namespace alndiff
  205. } // namespace nvbio