1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// Copyright (c) 2013 NVIDIA Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Jochen Singer <jochen.singer@fu-berlin.de>
// Author: Enrico Siragusa <enrico.siragusa@fu-berlin.de>
// ==========================================================================
//SEQAN_NO_DDDOC:do not generate documentation for this file
#ifndef INDEX_FM_SPARSE_STRING_H_
#define INDEX_FM_SPARSE_STRING_H_
namespace seqan2 {
// ==========================================================================
// Forwards
// ==========================================================================
template <typename TString, typename TSpec>
struct SparseString;
// ==========================================================================
// Tags
// ==========================================================================
/*!
* @defgroup SparseStringFibres Sparse String Fibres
* @brief Tag to select a specific fibre of a @link FMIndex @endlink.
*
* These tags can be used to get @link Fibre Fibres @endlink of a FM index.
*
* @see Fibre
* @see SparseString#getFibre
*
* @tag SparseStringFibres#FibreValueString
* @brief The String containing the stored values.
*
* @tag SparseStringFibres#FibreIndicatorString
* @brief The string storing for each position if a value different from a default value is stored.
*/
// ----------------------------------------------------------------------------
// Tag FibreValues
// ----------------------------------------------------------------------------
struct FibreValues_;
typedef Tag<FibreValues_> const FibreValues;
// ----------------------------------------------------------------------------
// Tag FibreIndicators
// ----------------------------------------------------------------------------
struct FibreIndicators_;
typedef Tag<FibreIndicators_> const FibreIndicators;
// ==========================================================================
// Metafunctions
// ==========================================================================
// ----------------------------------------------------------------------------
// Metafunction Size
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec>
struct Size<SparseString<TFibreValues, TSpec> > : Size<TFibreValues> {};
// ----------------------------------------------------------------------------
// Metafunction Value
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec>
struct Value<SparseString<TFibreValues, TSpec> >
{
typedef typename Value<TFibreValues>::Type Type;
};
template <typename TFibreValues, typename TSpec>
struct Value<SparseString<TFibreValues, TSpec> const> :
Value<SparseString<TFibreValues, TSpec> > {};
// ----------------------------------------------------------------------------
// Metafunction GetValue
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec>
struct GetValue<SparseString<TFibreValues, TSpec> > :
Value<SparseString<TFibreValues, TSpec> > {};
template <typename TFibreValues, typename TSpec>
struct GetValue<SparseString<TFibreValues, TSpec> const> :
Value<SparseString<TFibreValues, TSpec> const> {};
// ----------------------------------------------------------------------------
// Metafunction Reference
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec>
struct Reference<SparseString<TFibreValues, TSpec> >
{
typedef typename Value<SparseString<TFibreValues, TSpec> >::Type Type;
};
// ----------------------------------------------------------------------------
// Metafunction DefaultValue
// ----------------------------------------------------------------------------
template <typename TSpec>
struct DefaultValue;
template <typename TSpec>
struct DefaultValue<TSpec const> :
DefaultValue<TSpec> {};
template <typename TFibreValues, typename TSpec>
struct DefaultValue<SparseString<TFibreValues, TSpec> >
{
typedef typename GetValue<SparseString<TFibreValues, TSpec> const>::Type Type;
static const Type VALUE = -1;
};
// ----------------------------------------------------------------------------
// Metafunction Fibre
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec>
struct Fibre<SparseString<TFibreValues, TSpec>, FibreValues>
{
typedef TFibreValues Type;
};
template <typename TFibreValues, typename TSpec>
struct Fibre<SparseString<TFibreValues, TSpec>, FibreIndicators>
{
// NOTE(esiragusa): the CSA TConfig is not passed to the RD.
typedef RankDictionary<bool, Levels<TSpec> > Type;
};
// ----------------------------------------------------------------------------
// Metafunction Iterator
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec>
struct Iterator<SparseString<TFibreValues, TSpec>, Standard>
{
typedef Iter<SparseString<TFibreValues, TSpec>, PositionIterator> Type;
};
template <typename TFibreValues, typename TSpec>
struct Iterator<SparseString<TFibreValues, TSpec> const, Standard>
{
typedef Iter<SparseString<TFibreValues, TSpec> const, PositionIterator> Type;
};
template <typename TFibreValues, typename TSpec>
struct Iterator<SparseString<TFibreValues, TSpec>, Rooted>:
Iterator<SparseString<TFibreValues, TSpec>, Standard>{};
template <typename TFibreValues, typename TSpec>
struct Iterator<SparseString<TFibreValues, TSpec> const, Rooted>:
Iterator<SparseString<TFibreValues, TSpec> const, Standard>{};
// ==========================================================================
// Classes
// ==========================================================================
// ----------------------------------------------------------------------------
// Class SparseString
// ----------------------------------------------------------------------------
// NOTE(esiragusa): Why is SparseString not a specialization of String?
/*!
* @class SparseString
* @headerfile <seqan/index.h>
* @brief A string storing only a fraction of the values of the original string.
*
* @signature template <typename TValueString, typename TSpec>
* class SparseString;
*
* @tparam TSpec The specialisation tag. Default: void.
* @tparam TValueString The type of the @link String string @endlink containing the values.
*/
template <typename TValueString, typename TSpec = void>
struct SparseString
{
typedef typename Fibre<SparseString, FibreValues>::Type TFibreValues_;
typedef typename Fibre<SparseString, FibreIndicators>::Type TFibreIndicators_;
typedef typename Size<SparseString>::Type TSize;
TFibreValues_ values;
TFibreIndicators_ indicators;
TSize _length;
SparseString() :
_length(0)
{}
};
// ==========================================================================
// Functions
// ==========================================================================
template <typename TFibreValues, typename TSpec, typename TPos, typename TValue>
inline void _assignValueInValueString(SparseString<TFibreValues, TSpec> & string, TPos pos, TValue value)
{
getFibre(string, FibreValues())[pos] = value;
}
// ----------------------------------------------------------------------------
// Function clear()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#clear
* @headerfile <seqan/index.h>
* @brief Resets the @link SparseString @endlink.
*
* @signature void clear(sparseString);
*
* @param[in,out] sparseString The @link SparseString @endlink to be cleared.
*/
template <typename TFibreValues, typename TSpec>
inline void clear(SparseString<TFibreValues, TSpec> & string)
{
string._length = 0;
clear(getFibre(string, FibreValues()));
clear(getFibre(string, FibreIndicators()));
}
// ----------------------------------------------------------------------------
// Function empty()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#empty
* @headerfile <seqan/index.h>
* @brief Returns whether or not the @link SparseString @endlink is empty.
*
* @signature bool empty(sparseString);
*
* @param[in] sparseString The SparseString to be checked.
*
* @return bool <tt>true</tt> if there are no elements in the sparse string and <tt>false</tt> otherwise.
*/
template <typename TFibreValues, typename TSpec>
inline bool empty(SparseString<TFibreValues, TSpec> const & string)
{
// return empty(getFibre(string, FibreIndicators()));
return length(string) == 0;
}
template <typename TFibreValues, typename TSpec, typename TPos>
inline bool _isContained(SparseString<TFibreValues, TSpec> const & string, TPos const & pos)
{
return getValue(getFibre(string, FibreIndicators()), pos);
}
// ----------------------------------------------------------------------------
// Function assignValue()
// ----------------------------------------------------------------------------
template <typename TFibreValues, typename TSpec, typename TPos, typename TValue>
inline void
assignValue(SparseString<TFibreValues, TSpec> & string, TPos pos, TValue value)
{
if (!_isContained(string, pos))
setValue(getFibre(string, FibreIndicators()), pos, false);
getFibre(string, FibreValues())[getRank(getFibre(string, FibreIndicators()), pos) - 1] = value;
}
// ----------------------------------------------------------------------------
// Function getValue()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#getValue
* @headerfile <seqan/index.h>
* @brief Returns the value of a SparseString.
*
* @signature TValue getValue(sparseString, pos);
*
* @param[in] sparseString The @link SparseString @endlink.
* @param[in] pos The position at which a value should be assign to the sparse string.
* Types: @link UnsignedIntegerConcept @endlink
*
* @return TValue The type @link GetValue @endlink of @link SparseString @endlink is returned.
*/
template <typename TFibreValues, typename TSpec, typename TPos>
inline typename GetValue<SparseString<TFibreValues, TSpec> >::Type
getValue(SparseString<TFibreValues, TSpec> & string, TPos pos)
{
if (_isContained(string, pos))
return getValue(getFibre(string, FibreValues()), getRank(getFibre(string, FibreIndicators()), pos) - 1);
else
return DefaultValue<SparseString<TFibreValues, TSpec> >::VALUE;
}
template <typename TFibreValues, typename TSpec, typename TPos>
inline typename GetValue<SparseString<TFibreValues, TSpec> const>::Type
getValue(SparseString<TFibreValues, TSpec> const & string, TPos pos)
{
if (_isContained(string, pos))
return getValue(getFibre(string, FibreValues()), getRank(getFibre(string, FibreIndicators()), pos) - 1);
else
return DefaultValue<SparseString<TFibreValues, TSpec> const>::VALUE;
}
// ----------------------------------------------------------------------------
// Function value()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#value
* @headerfile <seqan/index.h>
* @brief Returns the value of a @link SparseString @endlink.
*
* @signature TReference value(sparseString, pos);
*
* @param[in] sparseString The @link SparseString @endlink.
* @param[in] pos The position at which a value should be assign to the sparse string.
* Types: @link UnsignedIntegerConcept @endlink
*
* @return TReference The type @link Reference @endlink of @link SparseString @endlink is returned.
*/
template <typename TFibreValues, typename TSpec, typename TPos>
inline typename Reference<SparseString<TFibreValues, TSpec> >::Type
value(SparseString<TFibreValues, TSpec> & string, TPos pos)
{
return getValue(string, pos);
}
template <typename TFibreValues, typename TSpec, typename TPos>
inline typename Reference<SparseString<TFibreValues, TSpec> >::Type
value(SparseString<TFibreValues, TSpec> const & string, TPos pos)
{
return getValue(string, pos);
}
// ----------------------------------------------------------------------------
// Function getFibre()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#getFibre
* @headerfile <seqan/index.h>
* @brief Returns a specific fibre of a @link SparseString @endlink.
*
* @signature TFibre getFibre(sparseString, fibreTag);
*
* @param[in] sparseString The sparseString holding the fibre.
* @param[in] fibreTag A tag that identifies the @link Fibre @endlink. Types:
* @link SparseStringFibres SparseString Fibres @endlink
*
* @return TFibre A reference to the @link Fibre @endlink object.
*/
template <typename TFibreValues, typename TSpec>
inline typename Fibre<SparseString<TFibreValues, TSpec>, FibreValues>::Type const &
getFibre(SparseString<TFibreValues, TSpec> const & sparseString, FibreValues)
{
return sparseString.values;
}
template <typename TFibreValues, typename TSpec>
inline typename Fibre<SparseString<TFibreValues, TSpec>, FibreValues>::Type &
getFibre(SparseString<TFibreValues, TSpec> & sparseString, FibreValues)
{
return sparseString.values;
}
template <typename TFibreValues, typename TSpec>
inline typename Fibre<SparseString<TFibreValues, TSpec>, FibreIndicators>::Type const &
getFibre(SparseString<TFibreValues, TSpec> const & sparseString, FibreIndicators)
{
return sparseString.indicators;
}
template <typename TFibreValues, typename TSpec>
inline typename Fibre<SparseString<TFibreValues, TSpec>, FibreIndicators>::Type &
getFibre(SparseString<TFibreValues, TSpec> & sparseString, FibreIndicators)
{
return sparseString.indicators;
}
// ----------------------------------------------------------------------------
// Function length()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#length
* @headerfile <seqan/index.h>
* @brief Returns the number of elements in the @link SparseString @endlink.
*
* @signature TSize length(sparseString);
*
* @param[in] sparseString The sparse string suffix array.
*
* @return TSize The number of elements in the sparse string array. Types: The result of @link Size @endlink of the
* sparse string.
*/
template <typename TFibreValues, typename TSpec>
inline typename Size<SparseString<TFibreValues, TSpec> const>::Type
length(SparseString<TFibreValues, TSpec> const & string)
{
return string._length;
}
// ----------------------------------------------------------------------------
// Function resize()
// ----------------------------------------------------------------------------
// NOTE(esiragusa): This version of resize() was now working, therefore it was commented out.
//template <typename TFibreValues, typename TSpec, typename TSize, typename TValue, typename TExpand>
//inline typename Size<typename Fibre<SparseString<TFibreValues, TSpec>, FibreValues>::Type>::Type
//resize(SparseString<TFibreValues, TSpec> & string, TSize size, TValue value, Tag<TExpand> const tag)
//{
// if (value != DefaultValue<SparseString<TFibreValues, TSpec> >::VALUE)
// {
// if (length(string) < size)
// resize(getFibre(string, FibreValues()), length(getFibre(string, FibreValues())) + (size - length(string)), value, tag);
// else
// resize(getFibre(string, FibreValues()), getRank(getFibre(string, FibreIndicators()), size), value, tag);
//
// resize(getFibre(string, FibreIndicators()), size, true, tag);
// }
//
// string._length = size;
//
// return resize(getFibre(string, FibreIndicators()), size, false);
//}
/*!
* @fn SparseString#resize
* @headerfile <seqan/index.h>
* @brief Resets the number of elements in the compressed suffix array.
*
* @signature TSize resize(sparseString, newLength);
*
* @param[in,out] sparseString The sparse string.
* @param[in] newLength The number of elements which should be stored in the sparse string. Types:
* @link UnsignedIntegerConcept @endlink.
*
* @return TSize The number of elements in the sparse string. Types: The result of @link Size @endlink of the
* sparse string.
*
* @note If the new length is smaller than the actual one then the last <tt>x<tt> items of the compressed suffix array
* are deleted with <tt>x = oldLength - newLength</tt>.
*/
template <typename TFibreValues, typename TSpec, typename TSize, typename TExpand>
inline typename Size<typename Fibre<SparseString<TFibreValues, TSpec>, FibreValues>::Type>::Type
resize(SparseString<TFibreValues, TSpec> & string, TSize size, Tag<TExpand> tag)
{
// return resize(string, size, DefaultValue<SparseString<TFibreValues, TSpec> >::VALUE, tag);
string._length = size;
return resize(getFibre(string, FibreIndicators()), size, tag);
}
// ----------------------------------------------------------------------------
// Function open()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#open
* @headerfile <seqan/index.h>
* @brief This functions open a sparse string from disk.
*
* @signature bool open(string, fileName[, openMode]);
*
* @param[in] string The string to be opened. Types: SparseString
* @param[in] fileName C-style character string containing the file name.
* @param[in] openMode The combination of flags defining how the file should be
* opened. To open a file read-only, write-only or to read and
* write use <tt>OPEN_RDONLY</tt>, <tt>OPEN_WRONLY</tt>, or
* <tt>OPEN_RDWR</tt>.To create or overwrite a file add
* <tt>OPEN_CREATE</tt>. To append a file if existing add
* <tt>OPEN_APPEND</tt>. To circumvent problems, files are always
* opened in binary mode. Default: <tt>OPEN_RDWR | OPEN_CREATE |
* OPEN_APPEND</tt>
*
* @return bool <tt>true</tt> on success.
*/
template <typename TFibreValues, typename TSpec>
inline bool open(SparseString<TFibreValues, TSpec> & sparseString, const char * fileName, int openMode)
{
String<char> name;
// Length saved inside a .len file.
name = fileName;
append(name, ".len");
if (!open(sparseString._length, toCString(name), openMode)) return false;
// Values saved inside a .val file.
name = fileName;
append(name, ".val");
if (!open(getFibre(sparseString, FibreValues()), toCString(name), openMode)) return false;
// Indicators saved inside a .ind file.
name = fileName;
append(name, ".ind");
if (!open(getFibre(sparseString, FibreIndicators()), toCString(name), openMode)) return false;
return true;
}
template <typename TFibreValues, typename TSpec>
inline bool open(SparseString<TFibreValues, TSpec> & sparseString, const char * fileName)
{
return open(sparseString, fileName, DefaultOpenMode<SparseString<TFibreValues, TSpec> >::VALUE);
}
// ----------------------------------------------------------------------------
// Function save()
// ----------------------------------------------------------------------------
/*!
* @fn SparseString#save
*
* @headerfile <seqan/index.h>
*
* @brief This functions saves a sparse string to disk.
*
* @signature bool save(string, fileName[, openMode]);
*
* @param[in] string The string to be saved. Types: SparseString
* @param[in] fileName C-style character string containing the file name.
* @param[in] openMode The combination of flags defining how the file should be
* opened. To open a file read-only, write-only or to read and
* write use <tt>OPEN_RDONLY</tt>, <tt>OPEN_WRONLY</tt>, or
* <tt>OPEN_RDWR</tt>.To create or overwrite a file add
* <tt>OPEN_CREATE</tt>. To append a file if existing add
* <tt>OPEN_APPEND</tt>. To circumvent problems, files are always
* opened in binary mode. Default: <tt>OPEN_RDWR | OPEN_CREATE |
* OPEN_APPEND</tt>
*
* @return bool <tt>true</tt> on success.
*/
template <typename TFibreValues, typename TSpec>
inline bool save(SparseString<TFibreValues, TSpec> const & sparseString, const char * fileName)
{
return save(sparseString, fileName, DefaultOpenMode<SparseString<TFibreValues, TSpec> >::VALUE);
}
template <typename TFibreValues, typename TSpec>
inline bool save(SparseString<TFibreValues, TSpec> const & sparseString, const char * fileName, int openMode)
{
String<char> name;
name = fileName;
append(name, ".len");
if (!save(length(sparseString), toCString(name), openMode)) return false;
name = fileName;
append(name, ".val");
if (!save(getFibre(sparseString, FibreValues()), toCString(name), openMode)) return false;
name = fileName;
append(name, ".ind");
if (!save(getFibre(sparseString, FibreIndicators()), toCString(name), openMode)) return false;
return true;
}
// TODO(singer): setValue function
}
#endif // INDEX_FM_SPARSE_STRING_H_
|