NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
alignment.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 
33 namespace nvbio {
34 namespace alndiff {
35 
36 struct Alignment
37 {
38  enum { PAIRED = 1,
40  UNMAPPED = 4,
42  REVERSE = 16,
44  READ_1 = 64,
45  READ_2 = 128,
46  SECONDARY = 256,
47  QC_FAILED = 512,
48  DUPLICATE = 1024
49  };
50 
52  read_id( uint32(-1) ),
53  read_len( 0 ),
54  mate( 0 ),
55  pos( 0 ),
56  ref_id( uint32(-1) ),
57  flag( UNMAPPED ),
58  score( -65536 ),
59  mapQ( 0 ),
60  ed( 255 ),
61  subs( 0 ),
62  ins( 0 ),
63  dels( 0 ),
64  n_mm( 0 ),
65  n_gapo( 0 ),
66  n_gape( 0 ),
67  has_second( 0 ),
68  sec_score( -65536 ) {}
69 
70  bool is_mapped() const { return (pos != 0) && ((flag & UNMAPPED) == 0); }
71  bool is_rc() const { return (flag & REVERSE) != 0; }
72  bool is_unique() const { return is_mapped() && (has_second == false); }
73  bool is_ambiguous() const { return is_mapped() && has_second && (sec_score == score); }
74 
75  uint16 mapped_read_bases() const { return subs + ins; }
76  uint16 mapped_ref_bases() const { return subs + dels; }
77  uint16 trimmed() const { return read_len - subs - ins; }
78 
96 };
97 
98 inline
99 bool distant(const Alignment& a1, const Alignment& a2)
100 {
101  return (int64(a1.pos) < int64(a2.pos) - a1.read_len) ||
102  (int64(a1.pos) > int64(a2.pos) + a1.read_len);
103 }
104 
106 {
108  AlignmentPair(const Alignment m1, const Alignment m2) : mate1(m1), mate2(m2) {}
109 
110  uint32 read_id() const { return mate1.read_id; }
111  uint32 read_len() const { return mate1.read_len + mate2.read_len; }
112  int32 score() const { return mate1.score + mate2.score; }
113  uint8 mapQ() const { return mate1.mapQ; }
114  uint8 ed() const { return mate1.ed + mate2.ed; }
115  uint16 subs() const { return mate1.subs + mate2.subs; }
116  uint16 ins() const { return mate1.ins + mate2.ins; }
117  uint16 dels() const { return mate1.dels + mate2.dels; }
118  uint8 n_mm() const { return mate1.n_mm + mate2.n_mm; }
119  uint8 n_gapo() const { return mate1.n_gapo + mate2.n_gapo; }
120  uint8 n_gape() const { return mate1.n_gape + mate2.n_gape; }
121  bool has_second() const { return mate1.has_second && mate2.has_second; }
123 
124  bool is_mapped() const { return mate1.is_mapped() && mate2.is_mapped(); }
126  bool is_mapped_unpaired() const { return mate1.is_mapped() && mate2.is_mapped() && (((mate1.flag & Alignment::PROPER_PAIR) == 0) || ((mate2.flag & Alignment::PROPER_PAIR) == 0)); }
127  bool is_unique_paired() const { return is_mapped_paired() && mate1.is_unique() && mate2.is_unique(); }
128  bool is_ambiguous_paired()const { return is_mapped_paired() && has_second() && (score() == sec_score()); }
129 
130  uint16 mapped_read_bases() const { return subs() + ins(); }
131  uint16 mapped_ref_bases() const { return subs() + dels(); }
132  uint16 trimmed() const { return read_len() - subs() - ins(); }
133 
134  const Alignment& operator[] (const uint32 i) const { return *(&mate1 + i); }
135 
138 };
139 
141 {
144  virtual ~AlignmentStream() {}
145 
148  virtual bool is_ok() { return false; }
149 
153  const uint32 count,
154  Alignment* batch) { return 0; }
155 };
156 
159 AlignmentStream* open_alignment_file(const char* file_name);
160 
161 } // alndiff namespace
162 } // nvbio namespace