NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
score_best_inl.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 
32 
33 namespace nvbio {
34 namespace bowtie2 {
35 namespace cuda {
36 
37 namespace detail {
38 
41 
44 
47 
53 template <typename AlignerType, typename PipelineType>
54 struct BestScoreStream : public AlignmentStreamBase<SCORE_STREAM,AlignerType,PipelineType>
55 {
57  typedef typename base_type::context_type context_type;
59 
71  const uint32 _band_len,
72  const PipelineType _pipeline,
73  const AlignerType _aligner,
74  const ParamsPOD _params) :
75  base_type( _pipeline, _aligner, _params ), m_band_len( _band_len ) {}
76 
80  uint32 max_pattern_length() const { return base_type::m_pipeline.reads.max_read_len(); }
81 
85  uint32 max_text_length() const { return base_type::m_pipeline.reads.max_read_len() + m_band_len; }
86 
90  uint32 size() const { return base_type::m_pipeline.hits_queue_size; }
91 
96  const uint32 i,
97  context_type* context) const
98  {
99  context->idx = base_type::m_pipeline.idx_queue[i];
100 
101  // fetch the hit to process
102  HitReference<HitQueuesDeviceView> hit = base_type::m_pipeline.scoring_queues.hits[ context->idx ];
103 
104  // setup the read info
105  context->mate = 0u;
106  context->read_rc = hit.seed.rc;
107  context->read_id = hit.read_id;
108  context->read_range = base_type::m_pipeline.reads.get_range( context->read_id );
109 
110  // setup the genome range
111  const uint32 g_pos = hit.loc;
112 
113  const uint32 read_len = context->read_range.y - context->read_range.x;
114  context->genome_begin = g_pos > m_band_len/2 ? g_pos - m_band_len/2 : 0u;
115  context->genome_end = nvbio::min( context->genome_begin + m_band_len + read_len, base_type::m_pipeline.genome_length );
116 
117  // initialize the sink
118  context->sink = aln::BestSink<int32>();
119 
120  // setup the minimum score
121  const io::Alignment second_best = base_type::m_pipeline.best_alignments[ context->read_id + base_type::m_pipeline.best_stride ];
122  context->min_score = nvbio::max( second_best.score(), base_type::m_pipeline.score_limit );
123 
124  NVBIO_CUDA_DEBUG_PRINT_IF( base_type::m_params.debug.show_score_info( context->read_id ), "score-min: %d (rc[%u], pos[%u], [qid %u])]\n", context->min_score, context->read_rc, context->genome_begin, i );
125  return true;
126  }
127 
131  void output(
132  const uint32 i,
133  const context_type* context) const
134  {
135  // write the final hit.score and hit.sink attributes
136  HitReference<HitQueuesDeviceView> hit = base_type::m_pipeline.scoring_queues.hits[ context->idx ];
137 
138  const aln::BestSink<int32> sink = context->sink;
139  hit.score = nvbio::max( sink.score, scheme_type::worst_score );
140  hit.sink = context->genome_begin + sink.sink.x;
141 
142  // TODO: rewrite hit.loc
143  // hit.loc = context->genome_begin;
144  NVBIO_CUDA_DEBUG_PRINT_IF( base_type::m_params.debug.show_score( context->read_id, (sink.score >= context->min_score) ), "score: %d (rc[%u], pos[%u], [qid %u])]\n", sink.score, context->read_rc, context->genome_begin, i );
145  }
146 
148 };
149 
153 template <typename aligner_type, typename pipeline_type>
155  const uint32 band_len,
156  const pipeline_type& pipeline,
157  const aligner_type aligner,
158  const ParamsPOD params)
159 {
160  const uint32 static_band_len =
161  (band_len < 4) ? 3u :
162  (band_len < 8) ? 7u :
163  (band_len < 16) ? 15u :
164  31u;
165 
167 
168  stream_type stream(
169  static_band_len,
170  pipeline,
171  aligner,
172  params );
173 
174  typedef aln::DeviceThreadScheduler scheduler_type;
175  //typedef aln::DeviceStagedThreadScheduler scheduler_type;
176 
177  if (band_len < 4)
178  {
180 
181  batch.enact( stream, pipeline.dp_buffer_size, pipeline.dp_buffer );
182  }
183  else if (band_len < 8)
184  {
186 
187  batch.enact( stream, pipeline.dp_buffer_size, pipeline.dp_buffer );
188  }
189  else if (band_len < 16)
190  {
192 
193  batch.enact( stream, pipeline.dp_buffer_size, pipeline.dp_buffer );
194  }
195  else
196  {
198 
199  batch.enact( stream, pipeline.dp_buffer_size, pipeline.dp_buffer );
200  }
201 }
202 
206 
207 } // namespace detail
208 
209 //
210 // execute a batch of single-ended banded-alignment score calculations, best mapping
211 //
212 template <typename scheme_type>
214  const uint32 band_len,
216  const ParamsPOD params)
217 {
218  if (params.alignment_type == LocalAlignment)
219  {
221  band_len,
222  pipeline,
223  pipeline.scoring_scheme.local_aligner(),
224  params );
225  }
226  else
227  {
229  band_len,
230  pipeline,
231  pipeline.scoring_scheme.end_to_end_aligner(),
232  params );
233  }
234 }
235 
236 } // namespace cuda
237 } // namespace bowtie2
238 } // namespace nvbio