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