NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sequence_mmap.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 
30 #include <nvbio/basic/console.h>
31 
32 namespace nvbio {
33 namespace io {
34 
35 // load a sequence from file
36 //
37 // \param alphabet the alphabet to use for encoding
38 // \param prefix prefix file name
39 // \param mapped_name memory mapped object name
41  const Alphabet alphabet,
42  const char* file_name,
43  const char* mapped_name,
44  const SequenceFlags load_flags,
45  const QualityEncoding qualities)
46 {
47  log_visible(stderr, "SequenceDataMMAPServer::loading... started\n");
48 
49  // TODO: check the extension; if there's no extension, assume it's a pac index
50  bool r = load_pac( alphabet, this, file_name, mapped_name, load_flags, qualities );
51 
52  log_visible(stderr, "SequenceDataMMAPServer::loading... done\n");
53  return r;
54 }
55 
56 std::string SequenceDataMMAPServer::info_file_name(const char* name) { return std::string("nvbio.") + std::string( name ) + ".seq_info";}
57 std::string SequenceDataMMAPServer::sequence_file_name(const char* name) { return std::string("nvbio.") + std::string( name ) + ".seq"; }
58 std::string SequenceDataMMAPServer::sequence_index_file_name(const char* name) { return std::string("nvbio.") + std::string( name ) + ".seq_index"; }
59 std::string SequenceDataMMAPServer::qual_file_name(const char* name) { return std::string("nvbio.") + std::string( name ) + ".qual"; }
60 std::string SequenceDataMMAPServer::name_file_name(const char* name) { return std::string("nvbio.") + std::string( name ) + ".name"; }
61 std::string SequenceDataMMAPServer::name_index_file_name(const char* name) { return std::string("nvbio.") + std::string( name ) + ".name_index"; }
62 
63 // load from a memory mapped object
64 //
65 // \param name memory mapped object name
66 bool SequenceDataMMAP::load(const char* file_name)
67 {
68  std::string infoName = SequenceDataMMAPServer::info_file_name( file_name );
69  std::string seqName = SequenceDataMMAPServer::sequence_file_name( file_name );
70  std::string seqIndexName = SequenceDataMMAPServer::sequence_index_file_name( file_name );
71  std::string qualName = SequenceDataMMAPServer::qual_file_name( file_name );
72  std::string nameName = SequenceDataMMAPServer::name_file_name( file_name );
73  std::string nameIndexName = SequenceDataMMAPServer::name_index_file_name( file_name );
74 
75  try {
76  const SequenceDataInfo* info = (const SequenceDataInfo*)m_info_file.init( infoName.c_str(), sizeof(SequenceDataInfo) );
77 
78  this->SequenceDataInfo::operator=( *info );
79 
80  const uint64 index_file_size = info->size() * sizeof(uint32);
81  const uint64 seq_file_size = info->words() * sizeof(uint32);
82  const uint64 qual_file_size = info->qs() * sizeof(char);
83  const uint64 name_file_size = info->m_name_stream_len * sizeof(char);
84 
85  m_sequence_ptr = (uint32*)m_sequence_file.init( seqName.c_str(), seq_file_size );
86  m_sequence_index_ptr = (uint32*)m_sequence_index_file.init( seqIndexName.c_str(), index_file_size );
87  m_qual_ptr = qual_file_size ? (char*)m_qual_file.init( qualName.c_str(), qual_file_size ) : NULL;
88  m_name_ptr = (char*)m_name_file.init( nameName.c_str(), name_file_size );
89  m_name_index_ptr = (uint32*)m_sequence_index_file.init( nameIndexName.c_str(), index_file_size );
90  }
91  catch (MappedFile::mapping_error error)
92  {
93  log_info(stderr, "SequenceDataMMAP: error mapping file \"%s\" (%d)!\n", error.m_file_name, error.m_code);
94  return false;
95  }
96  catch (MappedFile::view_error error)
97  {
98  log_info(stderr, "SequenceDataMMAP: error viewing file \"%s\" (%d)!\n", error.m_file_name, error.m_code);
99  return false;
100  }
101  catch (...)
102  {
103  log_info(stderr, "SequenceDataMMAP: error mapping file (unknown)!\n");
104  return false;
105  }
106  return true;
107 }
108 
109 // map a sequence file
110 //
111 // \param sequence_file_name the file to open
112 //
113 SequenceDataMMAP* map_sequence_file(const char* sequence_file_name)
114 {
116  if (ret->load( sequence_file_name ) == false)
117  {
118  delete ret;
119  return NULL;
120  }
121  return ret;
122 }
123 
124 } // namespace io
125 } // namespace nvbio