nvSSA.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // cuFMIndex.cpp : Defines the entry point for the console application.
  28. //
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <string>
  33. #include <nvbio/basic/console.h>
  34. #include <nvbio/io/fmindex/fmindex.h>
  35. void crcInit();
  36. using namespace nvbio;
  37. int main(int argc, char* argv[])
  38. {
  39. cudaSetDeviceFlags( cudaDeviceMapHost );
  40. crcInit();
  41. if (argc == 1)
  42. {
  43. log_info(stderr,"nvSSA [-gpu] input-prefix [output-prefix]\n");
  44. exit(0);
  45. }
  46. int base_arg = 0;
  47. const char* input;
  48. const char* output;
  49. if (strcmp( argv[1], "-gpu" ) == 0)
  50. base_arg = 2;
  51. else
  52. base_arg = 1;
  53. input = argv[base_arg];
  54. if (argc == base_arg+2)
  55. output = argv[base_arg+1];
  56. else
  57. output = argv[base_arg];
  58. //
  59. // Save sampled suffix array in a format compatible with BWA's
  60. //
  61. nvbio::io::FMIndexDataHost driver_data;
  62. if (!driver_data.load( input ))
  63. return 1;
  64. nvbio::io::FMIndexData::ssa_storage_type ssa, rssa;
  65. if (strcmp( argv[1], "-gpu" ) == 0)
  66. {
  67. nvbio::io::FMIndexDataDevice driver_data_cuda(
  68. driver_data,
  69. nvbio::io::FMIndexDataDevice::FORWARD | nvbio::io::FMIndexDataDevice::REVERSE );
  70. nvbio::io::FMIndexDataDevice::ssa_storage_type ssa_cuda, rssa_cuda;
  71. init_ssa( driver_data_cuda, ssa_cuda, rssa_cuda );
  72. ssa = ssa_cuda;
  73. rssa = rssa_cuda;
  74. }
  75. else
  76. init_ssa( driver_data, ssa, rssa );
  77. const uint32 sa_intv = nvbio::io::FMIndexData::SA_INT;
  78. const uint32 ssa_len = (driver_data.m_seq_length + sa_intv) / sa_intv;
  79. log_info(stderr, "saving SSA... started\n");
  80. {
  81. std::string file_name = std::string( output ) + std::string(".sa");
  82. FILE* file = fopen( file_name.c_str(), "wb" );
  83. fwrite( &driver_data.m_primary, sizeof(uint32), 1u, file );
  84. fwrite( &driver_data.m_L2+1, sizeof(uint32), 4u, file );
  85. fwrite( &sa_intv, sizeof(uint32), 1u, file );
  86. fwrite( &driver_data.m_seq_length, sizeof(uint32), 1u, file );
  87. fwrite( &ssa.m_ssa[1], sizeof(uint32), ssa_len-1, file );
  88. fclose( file );
  89. }
  90. {
  91. std::string file_name = std::string( output ) + std::string(".rsa");
  92. FILE* file = fopen( file_name.c_str(), "wb" );
  93. fwrite( &driver_data.m_rprimary, sizeof(uint32), 1u, file );
  94. fwrite( &driver_data.m_L2+1, sizeof(uint32), 4u, file );
  95. fwrite( &sa_intv, sizeof(uint32), 1u, file );
  96. fwrite( &driver_data.m_seq_length, sizeof(uint32), 1u, file );
  97. fwrite( &rssa.m_ssa[1], sizeof(uint32), ssa_len-1, file );
  98. fclose( file );
  99. }
  100. log_info(stderr, "saving SSA... done\n");
  101. return 0;
  102. }