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 #include <nvbio/basic/cuda/arch.h>
34 
35 #pragma once
36 
37 namespace nvbio {
38 
41 
44 
45 enum ReadType { STANDARD = 0u, COMPLEMENT = 1u };
46 enum DirType { FORWARD = 0u, REVERSE = 1u };
47 
51 template<typename IndexType>
53 {
54  typedef IndexType index_type;
57 
61  ReverseXform() : pos(0) { }
62 
66  ReverseXform(const index_type n) : pos(n-1) { }
67 
71  index_type operator() (const index_type i) const { return pos-i; }
72 
73  const index_type pos;
74 };
75 
79 template<typename IndexType>
81 {
82  typedef IndexType index_type;
85 
89  OffsetXform() : pos(0) { }
90 
94  OffsetXform(const index_type n) : pos(n) { }
95 
99  index_type operator() (const index_type i) const { return pos+i; }
100 
102 };
103 
104 struct quality_nop {};
105 
110 template <typename ReadStreamType>
112 {
113  static const uint32 SYMBOL_SIZE = 8u;
114 
117  typedef uint8 value_type;
119  typedef const value_type* pointer;
120  typedef typename ReadStreamType::difference_type difference_type;
121  typedef typename ReadStreamType::index_type index_type;
125 
130 
134  ReadStreamQualities(const ReadStreamType& read) : m_read( &read ) {}
135 
139  uint8 operator[] (const uint32 pos) const { return m_read->quality(pos); }
140 
144  iterator begin() { return iterator( *this ); }
145 
149  iterator end() { return iterator( *this, m_read->length() ); }
150 
154  const_iterator begin() const { return const_iterator( *this ); }
155 
159  const_iterator end() const { return const_iterator( *this, m_read->length() ); }
160 
161  const ReadStreamType* m_read;
162 };
163 
168 template< typename StreamType, typename QualType = quality_nop >
170 {
171  static const uint32 SYMBOL_SIZE = StreamType::SYMBOL_SIZE;
172 
173  typedef typename StreamType::symbol_type value_type;
174  typedef typename StreamType::index_type index_type;
177 
180  typedef const value_type* pointer;
185 
190 
194  ReadStream(const StreamType& s, const uint2 range)
195  : stream(s), first(range.x), last(range.y-1), rev(false), comp(false) { }
196 
200  ReadStream(const StreamType& s, const QualType q, const uint2 range)
201  : stream(s), qual(q), first(range.x), last(range.y-1), rev(false), comp(false) { }
202 
206  void set_flags(const DirType d, const ReadType t) { rev = (d == REVERSE); comp = (t == COMPLEMENT); }
207 
211  uint32 length() const { return 1+last-first; }
212 
217  {
218  const uint32 index = rev ? last-pos : first+pos;
219  const value_type c = stream[ index ];
220 
221  return (comp) ? (c < 4 ? 3-c : c) : c;
222  }
223 
227  uint8 quality(const uint32 pos) const
228  {
229  const uint32 index = rev ? last-pos : first+pos;
230  return qual[ index ];
231  }
232 
236  qual_string_type qualities() const { return qual_string_type(*this); }
237 
241  iterator begin() { return iterator( *this ); }
242 
246  iterator end() { return iterator( *this, length() ); }
247 
251  const_iterator begin() const { return const_iterator( *this ); }
252 
256  const_iterator end() const { return const_iterator( *this, length() ); }
257 
258 
262  StreamType stream;
263  QualType qual;
264 };
265 
268 template< typename StreamType, typename QualType>
271 
276 template <typename SequenceDataT, typename Tag>
278 {
279  typedef typename SequenceDataT::sequence_storage_iterator read_storage;
280  typedef typename SequenceDataT::qual_storage_iterator qual_iterator;
282  typedef typename loader_type::iterator read_iterator;
284 
288  string_type load(const SequenceDataT& batch, const uint2 range, const DirType dir, const ReadType op)
289  {
290  const qual_iterator quals( batch.qual_stream() + range.x );
291 
293  loader.load( batch.sequence_stream() + range.x, range.y - range.x ),
294  quals,
295  make_uint2( 0, range.y - range.x ) );
296 
297  read.set_flags( dir, op );
298  return read;
299  }
303  string_type load(const SequenceDataT& batch, const uint2 range, const DirType dir, const ReadType op, const uint2 subrange)
304  {
305  const qual_iterator quals( batch.qual_stream() + range.x );
306 
308  loader.load( batch.sequence_stream() + range.x, range.y - range.x, subrange, dir == REVERSE ? true : false ),
309  quals,
310  make_uint2( 0, range.y - range.x ) );
311 
312  read.set_flags( dir, op );
313  return read;
314  }
315 
317 };
318 
323 template <typename SequenceDataT, typename Tag>
325 {
326  typedef typename SequenceDataT::sequence_storage_iterator stream_storage;
328  typedef typename loader_type::iterator stream_iterator;
330 
334  string_type load(const SequenceDataT& batch, const uint2 range)
335  {
336  return string_type(
337  range.y - range.x,
338  loader.load( batch.sequence_stream() + range.x, range.y - range.x ) );
339  }
340 
342 };
343 
346 
347 } // namespace nvbio