NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
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 
28 #pragma once
29 
30 #include <nvbio/basic/types.h>
31 #include <nvbio/basic/numbers.h>
32 #include <nvbio/basic/console.h>
33 #include <string>
34 
35 namespace nvbio {
36 namespace alndiff {
37 
39 {
40  BooleanStats() : L(0), R(0), L_not_R(0), R_not_L(0), L_and_R(0), n(0) {}
41 
42  void push(const bool l, const bool r)
43  {
44  L += (l == true) ? 1u : 0u;
45  R += (r == true) ? 1u : 0u;
46  L_not_R += (l == true) && (r == false) ? 1u : 0u;
47  R_not_L += (r == true) && (l == false) ? 1u : 0u;
48  L_and_R += (l == true) && (r == true) ? 1u : 0u;
49  ++n;
50  }
51 
52  float avg_L() const { return n ? float(L) / float(n) : 0.0f; }
53  float avg_R() const { return n ? float(R) / float(n) : 0.0f; }
54  float avg_L_not_R() const { return n ? float(L_not_R) / float(n) : 0.0f; }
55  float avg_R_not_L() const { return n ? float(R_not_L) / float(n) : 0.0f; }
56  float avg_L_and_R() const { return n ? float(L_and_R) / float(n) : 0.0f; }
57 
64 };
65 
66 template <uint32 X>
67 struct Histogram
68 {
70  {
71  for (uint32 i = 0; i < 2*X; ++i)
72  bins[i] = 0;
73  }
74 
75  uint32 all_but(const uint32 i) const { return count - bins[i+X]; }
76 
77  uint32 operator[] (const int32 i) const { return bins[i + X]; }
78 
79  void push(const int32 i)
80  {
81  const int32 bin = nvbio::min(nvbio::max(int32(i + X),0),int32(2*X-1));
82  ++bins[bin];
83  ++count;
84  }
85 
87  uint32 bins[2*X];
88 };
89 
90 // accumulate histogram values bottom-up
91 template <uint32 X>
93 {
95  H.count = I.count;
96  H.bins[0] = I.bins[0];
97  for (int32 i = 1; i < 2*X; ++i)
98  H.bins[i] = I.bins[i] + H.bins[i-1];
99  return H;
100 }
101 // accumulate histogram values top-down
102 template <uint32 X>
104 {
105  Histogram<X> H;
106  H.count = I.count;
107  H.bins[2*X-1] = I.bins[2*X-1];
108  for (int32 i = 2*X-2; i >= 0; --i)
109  H.bins[i] = I.bins[i] + H.bins[i+1];
110  return H;
111 }
112 
113 template <uint32 X, uint32 Y>
115 {
117  {
118  for (uint32 i = 0; i < 2*X; ++i)
119  for (uint32 j = 0; j < 2*Y; ++j)
120  bins[i][j] = 0;
121  }
122 
123  void push(const int32 x, const int32 y)
124  {
125  const int32 bin_x = nvbio::min(nvbio::max(int32(x + X),0),int32(2*X-1));
126  const int32 bin_y = nvbio::min(nvbio::max(int32(y + Y),0),int32(2*Y-1));
127  ++bins[bin_x][bin_y];
128  ++count;
129  }
130  uint32 operator() (const int32 i, const int32 j) const { return bins[i + X][j + Y]; }
131 
133  uint32 bins[2*X][2*Y];
134 };
135 
136 inline
138 {
139  switch (bin)
140  {
141  case 0:
142  return 16;
143  case 1:
144  return 36;
145  case 2:
146  return 100;
147  case 3:
148  return 150;
149  case 4:
150  return 200;
151  case 5:
152  return 250;
153  case 6:
154  return 300;
155  case 7:
156  return 350;
157  case 8:
158  return 400;
159  case 9:
160  return 450;
161  case 10:
162  return 500;
163  default:
164  return 1000;
165  }
166 }
167 
168 inline
170 {
171  if (read_len <= 16)
172  return 0;
173  else if (read_len <= 36)
174  return 1;
175  else if (read_len <= 100)
176  return 2;
177  else if (read_len <= 150)
178  return 3;
179  else if (read_len <= 200)
180  return 4;
181  else if (read_len <= 250)
182  return 5;
183  else if (read_len <= 300)
184  return 6;
185  else if (read_len <= 350)
186  return 7;
187  else if (read_len <= 400)
188  return 8;
189  else if (read_len <= 450)
190  return 9;
191  else if (read_len <= 500)
192  return 10;
193  else
194  return 11;
195 }
196 inline int32 log_bin(const int32 x)
197 {
198  return x == 0 ? 0u :
199  x < 0 ? -int32(1u + nvbio::log2(-x)) :
200  int32(1u + nvbio::log2(x));
201 }
202 inline int32 log_bin_range(const int32 bin)
203 {
204  return bin == 0 ? 0 :
205  bin < 0 ? -int32(1u << (-bin+1)) :
206  int32(1u << (bin-1));
207 }
208 
209 // return the local file name from a path
210 //
211 inline const char* local_file(const std::string& file_name)
212 {
213  #if WIN32
214  const size_t pos = file_name.find_last_of("/\\");
215  #else
216  const size_t pos = file_name.rfind('/');
217  #endif
218 
219  if (pos == std::string::npos)
220  return file_name.c_str();
221  else
222  return file_name.c_str() + pos + 1u;
223 }
224 
225 } // namespace alndiff
226 } // namespace nvbio