NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
alignments.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 //#include <nvBowtie/bowtie2/cuda/defs.h>
29 #include <nvbio/basic/cuda/arch.h>
32 #include <nvbio/basic/pod.h>
33 #include <algorithm>
34 
35 #pragma once
36 
37 namespace nvbio {
38 namespace io {
39 
42 
46 enum MDS_OP {
47  MDS_MATCH = 0,
52 };
53 
57 struct Cigar
58 {
61  enum Operation {
63  INSERTION = 1,
64  DELETION = 2,
66  };
67 
69  Cigar() {}
70 
72  Cigar(const uint8 type, const uint16 len) : m_type( type ), m_len( len ) {}
73 
75 };
76 
80 struct Alignment
81 {
83  static uint32 max_ed() { return 255u; }
84 
86  static int32 max_score() { return (1 << 17) - 1; }
87 
89  static int32 min_score() { return -((1 << 17) - 1); }
90 
93  const uint32 pos,
94  const uint32 ed,
95  const int32 score,
96  const uint32 rc,
97  const uint32 mate = 0,
98  const bool paired = false,
99  const bool discordant = false)
100  {
101  //NVBIO_CUDA_ASSERT( ed < 1024u );
102  //NVBIO_CUDA_ASSERT( score >= min_score() && score <= max_score() );
103  m_align = pos;
104  m_ed = ed;
105  m_score = score < 0 ? uint32( -score ) : uint32( score );
106  m_score_sgn = score < 0 ? 1u : 0u;
107  m_rc = rc;
108  m_mate = mate;
109  m_paired = paired ? 1u : 0u;
110  m_discordant = discordant ? 1u : 0u;
111  }
112 
120  NVBIO_HOST_DEVICE NVBIO_FORCEINLINE bool is_paired() const { return (m_paired == true) && is_aligned(); }
121  NVBIO_HOST_DEVICE NVBIO_FORCEINLINE bool is_unpaired() const { return (m_paired == false) && is_aligned(); }
122  NVBIO_HOST_DEVICE NVBIO_FORCEINLINE bool is_concordant() const { return (m_paired == true) && (m_discordant == false); }
123  NVBIO_HOST_DEVICE NVBIO_FORCEINLINE bool is_discordant() const { return (m_paired == true) && (m_discordant == true); }
124 
126  static Alignment invalid() { return Alignment( uint32(-1), max_ed(), max_score(), 0u, 0u, false ); }
127 
130 };
131 
136 {
138  bool operator() (const Alignment f, const Alignment s) const { return (f.m_score > s.m_score); }
139 };
140 
145 {
148 
150  BestAlignments(const Alignment& a1, const Alignment& a2) : m_a1( a1 ), m_a2( a2 ) {}
151 
160 
163 
164  template <uint32 I>
166  const Alignment& alignment() const { return I == 0 ? m_a1 : m_a2; }
167 
168  template <uint32 I>
170  Alignment& alignment() { return I == 0 ? m_a1 : m_a2; }
171 
172  template <uint32 I>
175 
176  template <uint32 I>
178  int32 score() const { return I == 0 ? best_score() : second_score(); }
179 
180  template <uint32 I>
182  uint32 ed() const { return I == 0? best_ed() : second_ed(); }
183 
184  template <uint32 I>
186  bool is_aligned() const { return I == 0 ? m_a1.is_aligned() : m_a2.is_aligned(); }
187 
188  template <uint32 I>
190  bool is_rc() const { return I == 0 ? m_a1.is_rc() : m_a2.is_rc(); }
191 
194 };
195 
200 {
203 
205  PairedAlignments(const Alignment& a, const Alignment& o) : m_a( a ), m_o( o ) {}
206 
213 
215  Alignment& mate(const uint32 m) { return m == m_a.mate() ? m_a : m_o; }
216 
218  Alignment mate(const uint32 m) const { return m == m_a.mate() ? m_a : m_o; }
219 
222 };
223 
228 {
231 
233  BestPairedAlignments(const BestAlignments& a, const BestAlignments& o) : m_a1( a.m_a1 ), m_a2( a.m_a2 ), m_o1( o.m_a1 ), m_o2( o.m_a2 ) {}
234 
236  BestPairedAlignments(const BestAlignments& a) : m_a1( a.m_a1 ), m_a2( a.m_a2 ), m_o1( Alignment::invalid() ), m_o2( Alignment::invalid() ) {}
237 
239  NVBIO_HOST_DEVICE NVBIO_FORCEINLINE bool is_aligned() const { return m_a1.is_aligned() /*&& m_o1.is_aligned()*/; }
244  NVBIO_HOST_DEVICE NVBIO_FORCEINLINE uint32 best_ed() const { return m_a1.ed() + (is_paired() ? m_o1.ed() : 0u); }
249 
252 
253  template <uint32 I>
256 
257  template <uint32 I>
259  const Alignment& alignment() const { return I == 0 ? m_a1 : m_a2; }
260 
261  template <uint32 I>
263  Alignment& alignment() { return I == 0 ? m_a1 : m_a2; }
264 
265  template <uint32 I>
267  const Alignment& opposite_alignment() const { return I == 0 ? m_o1 : m_o2; }
268 
269  template <uint32 I>
271  Alignment& opposite_alignment() { return I == 0 ? m_o1 : m_o2; }
272 
273  template <uint32 I>
276 
277  template <uint32 I>
279  uint32 opposite_alignment_pos() const { return I == 0 ? m_o1.alignment() : m_o2.alignment(); }
280 
281  template <uint32 I>
283  int32 score() const { return I == 0 ? best_score() : second_score(); }
284 
285  template <uint32 I>
287  uint32 ed() const { return I == 0? best_ed() : second_ed(); }
288 
289  template <uint32 I>
291  bool is_aligned() const { return I == 0 ? m_a1.is_aligned() : m_a2.is_aligned(); }
292 
293  template <uint32 I>
295  bool is_rc() const { return I == 0 ? m_a1.is_rc() : m_a2.is_rc(); }
296 
297  template <uint32 I>
299  bool is_opposite_rc() const { return I == 0 ? m_o1.is_rc() : m_o2.is_rc(); }
300 
301  template <uint32 I>
303  uint32 anchor_mate() const { return I == 0 ? m_a1.mate() : m_a2.mate(); }
304 
305  template <uint32 I>
307  uint32 opposite_mate() const { return I == 0 ? m_o1.mate() : m_o2.mate(); }
308 
309  template <uint32 I>
311  int32 anchor_score() const { return I == 0 ? m_a1.score() : m_a2.score(); }
312 
313  template <uint32 I>
315  uint32 anchor_ed() const { return I == 0? m_a1.ed() : m_a2.ed(); }
316 
317  template <uint32 I>
319  int32 opposite_score() const { return I == 0 ? m_o1.score() : m_o2.score(); }
320 
321  template <uint32 I>
323  uint32 opposite_ed() const { return I == 0 ? m_o1.ed() : m_o2.ed(); }
324 
326  BestAlignments anchor() const { return BestAlignments( m_a1, m_a2 ); }
327 
330 
332  BestAlignments mate(const uint32 m) const
333  {
334  return BestAlignments(
335  m == m_a1.mate() ? m_a1 : m_o1,
336  m == m_a2.mate() ? m_a2 : m_o2 );
337  }
338 
343 };
344 
346 {
348  bool operator() (const BestAlignments& op) { return op.has_second(); }
349 
351  bool operator() (const BestPairedAlignments& op) { return op.has_second(); }
352 };
353 
355 {
357  bool operator() (const BestAlignments& op) { return op.has_second() && op.alignment<1>().is_paired(); }
358 
360  bool operator() (const BestPairedAlignments& op) { return op.has_second_paired(); }
361 };
362 
364 {
366  bool operator() (const BestAlignments& op) { return op.has_second() && op.alignment<1>().is_unpaired() ; }
367 
369  bool operator() (const BestPairedAlignments& op) { return op.has_second_unpaired(); }
370 };
371 
372 struct is_paired
373 {
375  bool operator() (const BestAlignments& op) { return op.alignment<0>().is_paired(); }
376 
378  bool operator() (const Alignment& op) { return op.is_paired(); }
379 };
381 {
383  bool operator() (const BestAlignments& op) { return op.alignment<0>().is_unpaired(); }
384 
386  bool operator() (const Alignment& op) { return op.is_unpaired(); }
387 };
389 {
391  bool operator() (const BestAlignments& op) { return op.alignment<0>().is_concordant(); }
392 
394  bool operator() (const Alignment& op) { return op.is_concordant(); }
395 };
397 {
399  bool operator() (const BestAlignments& op) { return op.alignment<0>().is_discordant(); }
400 
402  bool operator() (const Alignment& op) { return op.is_discordant(); }
403 };
404 struct is_not_concordant // note: !concordant = discordant | unpaired - i.e. this is different from is_discordant
405 {
407  bool operator() (const BestAlignments& op) { return op.alignment<0>().is_concordant() == false; }
408 
410  bool operator() (const Alignment& op) { return op.is_concordant() == false; }
411 };
413 {
415  bool operator() (const BestAlignments& op) { return op.alignment<0>().is_aligned(); }
416 
418  bool operator() (const Alignment& op) { return op.is_aligned(); }
419 };
420 
421 // check whether two alignments are distinct
422 //
424  const uint32 pos1,
425  const bool rc1,
426  const uint32 pos2,
427  const bool rc2,
428  const uint32 dist);
429 
430 // check whether two paired-end alignments are distinct
431 //
433  const uint32 apos1,
434  const uint32 opos1,
435  const bool arc1,
436  const bool orc1,
437  const uint32 apos2,
438  const uint32 opos2,
439  const bool arc2,
440  const bool orc2,
441  const uint32 dist);
442 
443 // check whether two alignments are distinct
444 //
445 // NOTE: this uses Alignment::sink() which is only valid during alignment,
446 // as sink() is unioned with ed().
447 //
449  const Alignment& p1,
450  const Alignment& p2,
451  const uint32 dist = 1);
452 
453 // check whether two paired-end alignments are distinct
454 //
455 // NOTE: this uses Alignment::sink() which is only valid during alignment,
456 // as sink() is unioned with ed().
457 //
459  const PairedAlignments& p1,
460  const PairedAlignments& p2);
461 
462 // check whether two paired-end alignments are distinct
463 //
464 // NOTE: this uses Alignment::sink() which is only valid during alignment,
465 // as sink() is unioned with ed().
466 //
468  const PairedAlignments& p1,
469  const PairedAlignments& p2,
470  const uint32 dist);
471 
473 
474 } // namespace io
475 
476 // pod_type specializations
477 template <> struct pod_type<io::Alignment> { typedef uint2 type; };
478 template <> struct pod_type<io::BestAlignments> { typedef uint4 type; };
479 
480 } // namespace nvbio
481 
482 #include <nvbio/io/alignments_inl.h>