alignment.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // alignment.h
  28. //
  29. #pragma once
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <nvbio/alignment/alignment.h>
  33. #include <nvbio/alignment/batched.h>
  34. #include <nvbio/strings/string_set.h>
  35. #include <nvbio/io/sequence/sequence.h>
  36. using namespace nvbio;
  37. // a functor to extract the read infixes from the hit diagonals
  38. //
  39. struct read_infixes
  40. {
  41. // constructor
  42. NVBIO_HOST_DEVICE
  43. read_infixes(const io::ConstSequenceDataView reads) :
  44. m_reads( reads ) {}
  45. // functor operator
  46. NVBIO_HOST_DEVICE
  47. string_infix_coord_type operator() (const uint2 diagonal) const
  48. {
  49. const io::SequenceDataAccess<DNA_N> reads( m_reads );
  50. const uint32 read_id = diagonal.y;
  51. // fetch the read range
  52. return reads.get_range( read_id );
  53. }
  54. const io::ConstSequenceDataView m_reads;
  55. };
  56. // a functor to extract the genome infixes from the hit diagonals
  57. //
  58. template <uint32 BAND_LEN>
  59. struct genome_infixes
  60. {
  61. // constructor
  62. NVBIO_HOST_DEVICE
  63. genome_infixes(const uint32 genome_len, const io::ConstSequenceDataView reads) :
  64. m_genome_len( genome_len ),
  65. m_reads( reads ) {}
  66. // functor operator
  67. NVBIO_HOST_DEVICE
  68. string_infix_coord_type operator() (const uint2 diagonal) const
  69. {
  70. const io::SequenceDataAccess<DNA_N> reads( m_reads );
  71. const uint32 read_id = diagonal.y;
  72. const uint32 text_pos = diagonal.x;
  73. // fetch the read range
  74. const uint2 read_range = reads.get_range( read_id );
  75. const uint32 read_len = read_range.y - read_range.x;
  76. // compute the segment of text to align to
  77. const uint32 genome_begin = text_pos > BAND_LEN/2 ? text_pos - BAND_LEN/2 : 0u;
  78. const uint32 genome_end = nvbio::min( genome_begin + read_len + BAND_LEN, m_genome_len );
  79. return make_uint2( genome_begin, genome_end );
  80. }
  81. const uint32 m_genome_len;
  82. const io::ConstSequenceDataView m_reads;
  83. };
  84. // a functor to extract the score from a sink
  85. //
  86. struct sink_score
  87. {
  88. typedef aln::BestSink<int16> argument_type;
  89. typedef int16 result_type;
  90. // functor operator
  91. NVBIO_HOST_DEVICE
  92. int16 operator() (const aln::BestSink<int16>& sink) const { return sink.score; }
  93. };