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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// 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: Enrico Siragusa <enrico.siragusa@fu-berlin.de>
// ==========================================================================
// This file contains database type definitions.
// ==========================================================================
#ifndef SEQAN_APPS_SEARCHJOIN_DB_H_
#define SEQAN_APPS_SEARCHJOIN_DB_H_
#include <numeric>
#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/index.h>
#define SEQAN_DEBUG
using namespace seqan2;
// ============================================================================
// Database Type Definitions
// ============================================================================
// ----------------------------------------------------------------------------
// Database Basic Types
// ----------------------------------------------------------------------------
struct SmallDb_;
typedef Tag<SmallDb_> SmallDb;
struct HugeDb_;
typedef Tag<HugeDb_> HugeDb;
struct StringOfMaxSize256_;
typedef Tag<StringOfMaxSize256_> StringOfMaxSize256;
typedef Owner<ConcatDirect<> > TDbSSetSpec;
typedef Alloc<StringOfMaxSize256> TDbStringSpec;
typedef String<Dna5, TDbStringSpec> TDbDnaString;
typedef StringSet<TDbDnaString, TDbSSetSpec> TDbDna;
typedef String<char, TDbStringSpec> TDbGeoString;
typedef StringSet<TDbGeoString, TDbSSetSpec> TDbGeo;
// ----------------------------------------------------------------------------
// Database Basic Metafunctions
// ----------------------------------------------------------------------------
namespace seqan2
{
//template <>
//struct Size<TDbDnaString>
//{
// typedef unsigned char Type;
//};
template <>
struct Size<TDbDna>
{
typedef unsigned int Type;
};
template <>
struct StringSetLimits<TDbDna>
{
typedef String<Size<TDbDna>::Type> Type;
};
//template <>
//struct Size<TDbGeoString>
//{
// typedef unsigned char Type;
//};
template <>
struct Size<TDbGeo>
{
typedef unsigned int Type;
};
template <>
struct StringSetLimits<TDbGeo>
{
typedef String<Size<TDbGeo>::Type> Type;
};
}
// ============================================================================
// Database Index Type Definitions
// ============================================================================
// ----------------------------------------------------------------------------
// Database Suffix Array Type Definitions
// ----------------------------------------------------------------------------
typedef Index<TDbDna, IndexSa<SmallDb> > TDbDnaSaSmall;
typedef Index<TDbGeo, IndexSa<SmallDb> > TDbGeoSaSmall;
typedef Index<TDbDna, IndexSa<HugeDb> > TDbDnaSaHuge;
typedef Index<TDbGeo, IndexSa<HugeDb> > TDbGeoSaHuge;
namespace seqan2
{
template <>
struct Fibre<TDbDnaSaSmall, FibreSA>
{
typedef String<Pair<unsigned int, unsigned char, BitPacked<24, 8> >, DefaultIndexStringSpec<TDbDnaSaSmall>::Type> Type;
};
template <>
struct Fibre<TDbGeoSaSmall, FibreSA>
{
typedef String<Pair<unsigned int, unsigned char, BitPacked<24, 8> >, DefaultIndexStringSpec<TDbGeoSaSmall>::Type> Type;
};
template <>
struct Fibre<TDbDnaSaHuge, FibreSA>
{
typedef String<Pair<unsigned int, unsigned char, Pack>, DefaultIndexStringSpec<TDbDnaSaHuge>::Type> Type;
};
template <>
struct Fibre<TDbGeoSaHuge, FibreSA>
{
typedef String<Pair<unsigned int, unsigned char, Pack>, DefaultIndexStringSpec<TDbDnaSaHuge>::Type> Type;
};
}
// ----------------------------------------------------------------------------
// Database Shape Length Definitions
// ----------------------------------------------------------------------------
//template <typename TText>
//struct ShapeLength
//{
// static const unsigned VALUE = std::numeric_limits<typename Size<TText>::Type>::max();
//};
//
//template <>
//struct ShapeLength<TDbDna>
//{
// static const unsigned VALUE = 10u;
//};
//
//template <>
//struct ShapeLength<TDbGeo>
//{
// static const unsigned VALUE = 3u;
//};
// ----------------------------------------------------------------------------
// Database QGram Index with Bucket Refinement Type Definitions
// ----------------------------------------------------------------------------
//typedef UngappedShape<10> TShapeDna;
//typedef UngappedShape<3> TShapeGeo;
//
//typedef IndexQGram<TShapeDna, BucketRefinement> TDbDnaQGramSpec;
//typedef IndexQGram<TShapeGeo, BucketRefinement> TDbGeoQGramSpec;
//
//typedef Index<TDbDna, TDbDnaQGramSpec> TDbDnaQGram;
//typedef Index<TDbGeo, TDbGeoQGramSpec> TDbGeoQGram;
//namespace seqan2
//{
//template <>
//struct Fibre<TDbDnaQGram, FibreDir>
//{
// typedef String<unsigned int, DefaultIndexStringSpec<TDbDnaQGram>::Type> Type;
//};
//
//template <>
//struct Fibre<TDbGeoQGram, FibreDir>
//{
// typedef String<unsigned int, DefaultIndexStringSpec<TDbGeoQGram>::Type> Type;
//};
//}
// ============================================================================
// Tags, Classes, Enums
// ============================================================================
// ----------------------------------------------------------------------------
// Tag Query
// ----------------------------------------------------------------------------
struct Query_;
typedef Tag<Query_> Query;
// ----------------------------------------------------------------------------
// Class DbParser
// ----------------------------------------------------------------------------
template <typename TDb, typename TSpec = void>
struct DbParser
{
TDb & db;
CharString _id;
CharString _text;
EqualsChar<','> delim;
// AssertFunctor<TFunctor, ParseError> asserter(functor);
DbParser(TDb & db) :
db(db)
{}
};
template <typename TDb>
struct DbParser<TDb, Query>
{
TDb & db;
CharString _id;
CharString _text;
CharString _errors;
EqualsChar<':'> delim;
EqualsChar<','> delim2;
DbParser(TDb & db) :
db(db)
{}
};
// ----------------------------------------------------------------------------
// Class Db
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec = void>
struct Db
{
typedef Owner<ConcatDirect<> > TIdsSpec;
typedef StringSet<CharString, TIdsSpec> TIds;
typedef typename Size<TText>::Type TTextSize;
typedef TTextSize TErrors;
TText text;
TIds ids;
TTextSize minLength;
TTextSize maxLength;
TTextSize avgLength;
TErrors errors;
Db() :
minLength(0),
maxLength(0),
avgLength(0),
errors(0)
{}
};
template <typename TText>
struct Db<TText, Query>
{
typedef Owner<ConcatDirect<> > TIdsSpec;
typedef StringSet<CharString, TIdsSpec> TIds;
typedef typename Size<TText>::Type TTextSize;
typedef TTextSize TErrors;
TText text;
TIds ids;
TTextSize minLength;
TTextSize maxLength;
TTextSize avgLength;
String<TErrors> errors;
TErrors minErrors;
TErrors maxErrors;
TErrors avgErrors;
Db() :
minLength(0),
maxLength(0),
avgLength(0),
minErrors(0),
maxErrors(0),
avgErrors(0)
{}
};
// ----------------------------------------------------------------------------
// Class DbIndex
// ----------------------------------------------------------------------------
template <typename TIndex, typename TSpec = void>
struct DbIndex
{
typedef typename Size<TIndex>::Type TSize;
TIndex index;
String<TSize> dir;
TSize sortedPrefix;
};
template <typename TIndex>
struct DbIndex<TIndex, Query>
{
typedef typename Size<TIndex>::Type TSize;
typedef String<TIndex> TIndices;
typedef String<TSize> TErrors;
TIndices index;
TErrors errors;
TSize seedLength;
DbIndex() :
seedLength(0)
{}
};
// ============================================================================
// Metafunctions
// ============================================================================
// ----------------------------------------------------------------------------
// Metafunction Size<Db>::Type [Db]
// ----------------------------------------------------------------------------
namespace seqan2
{
template <typename TText, typename TSpec>
struct Size<Db<TText, TSpec> >
{
typedef typename Size<TText>::Type Type;
};
template <typename TIndex, typename TSpec>
struct Size<DbIndex<TIndex, TSpec> >
{
typedef typename DbIndex<TIndex, TSpec>::TSize Type;
};
}
// ----------------------------------------------------------------------------
// Metafunction Host<Db>::Type [Db]
// ----------------------------------------------------------------------------
namespace seqan2
{
template <typename TText, typename TSpec>
struct Host<Db<TText, TSpec> >
{
typedef TText Type;
};
}
// ============================================================================
// Functions
// ============================================================================
// ----------------------------------------------------------------------------
// Function parseLine() [DbParser]
// ----------------------------------------------------------------------------
template <typename TDb, typename TSpec, typename TInputIt>
inline void
parseLine(DbParser<TDb, TSpec> & parser, TInputIt & inputIt)
{
_clearBuffers(parser);
_parseId(parser, inputIt);
_parseText(parser, inputIt);
}
template <typename TDb, typename TInputIt>
inline void
parseLine(DbParser<TDb, Query> & parser, TInputIt & inputIt)
{
_clearBuffers(parser);
_parseId(parser, inputIt);
_parseText(parser, inputIt);
_parseErrors(parser, inputIt);
}
// ----------------------------------------------------------------------------
// Function _clearBuffers() [DbParser]
// ----------------------------------------------------------------------------
template <typename TDb, typename TSpec>
inline void
_clearBuffers(DbParser<TDb, TSpec> & parser)
{
clear(parser._id);
clear(parser._text);
}
template <typename TDb>
inline void
_clearBuffers(DbParser<TDb, Query> & parser)
{
clear(parser._id);
clear(parser._text);
clear(parser._errors);
}
// ----------------------------------------------------------------------------
// Function _parseId() [DbParser]
// ----------------------------------------------------------------------------
template <typename TDb, typename TSpec, typename TInputIt>
inline void
_parseId(DbParser<TDb, TSpec> & parser, TInputIt & inputIt)
{
// Read id.
readUntil(parser._id, inputIt, parser.delim);
// Add id to database.
appendValue(parser.db.ids, parser._id);
// Skip delim.
skipOne(inputIt);
}
// ----------------------------------------------------------------------------
// Function _parseText() [DbParser]
// ----------------------------------------------------------------------------
template <typename TDb, typename TSpec, typename TInputIt>
inline void
_parseText(DbParser<TDb, TSpec> & parser, TInputIt & inputIt)
{
// Read text.
readLine(parser._text, inputIt);
// Add text to database.
appendValue(parser.db.text, parser._text);
}
template <typename TDb, typename TInputIt>
inline void
_parseText(DbParser<TDb, Query> & parser, TInputIt & inputIt)
{
// Read text.
readUntil(parser._text, inputIt, parser.delim2);
// Add text to database.
appendValue(parser.db.text, parser._text);
// Skip delim.
skipOne(inputIt);
}
// ----------------------------------------------------------------------------
// Function _parseErrors() [DbParser]
// ----------------------------------------------------------------------------
template <typename TDb, typename TInputIt>
inline void
_parseErrors(DbParser<TDb, Query> & parser, TInputIt & inputIt)
{
// Read errors.
readLine(parser._errors, inputIt);
// Add errors to database.
appendValue(parser.db.errors, std::atoi(toCString(parser._errors)));
}
// ----------------------------------------------------------------------------
// Function load() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec, typename TFileName>
bool load(Db<TText, TSpec> & db, TFileName const & fileName)
{
typedef Db<TText, TSpec> TDb;
typedef DbParser<TDb, TSpec> TDbParser;
typedef typename Size<TText>::Type TTextSize;
typedef std::ifstream TInputStream;
typedef typename DirectionIterator<TInputStream, Input>::Type TInputIt;
TInputStream inputFile;
// Open file.
if (!open(inputFile, toCString(fileName), OPEN_RDONLY))
return false;
// Instantiate an input iterator on the file.
TInputIt inputIt = directionIterator(inputFile, Input());
// Instantiate a parser.
TDbParser parser(db);
// Initialize min/max text length.
db.minLength = std::numeric_limits<TTextSize>::max();
db.maxLength = std::numeric_limits<TTextSize>::min();
// Read the file.
while (!atEnd(inputIt))
{
parseLine(parser, inputIt);
// Update min/max text length.
TTextSize textLength = length(back(db.text));
db.minLength = _min(db.minLength, textLength);
db.maxLength = _max(db.maxLength, textLength);
}
// Compute average text length.
db.avgLength = length(db.text) ? length(db.text.concat) / length(db.text) : 0;
_updateErrors(db);
return true;
}
// ----------------------------------------------------------------------------
// Function _updateErrors() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec>
void _updateErrors(Db<TText, TSpec> const & /* db */)
{}
template <typename TText>
void _updateErrors(Db<TText, Query> & db)
{
if (length(db.text) == 0)
{
db.avgErrors = 0;
db.minErrors = 0;
db.maxErrors = 0;
}
else
{
db.avgErrors = std::accumulate(begin(db.errors, Standard()), end(db.errors, Standard()), 0) / length(db.text);
db.minErrors = value(std::min_element(begin(db.errors, Standard()), end(db.errors, Standard())));
db.maxErrors = value(std::max_element(begin(db.errors, Standard()), end(db.errors, Standard())));
}
}
// ----------------------------------------------------------------------------
// Function split() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSeedLength>
void split(Db<TText, Query> & dbShort, Db<TText, Query> & dbLong, Db<TText, Query> /*const*/ & db, TSeedLength seedLength)
{
typedef Db<TText, Query> TDb;
typedef typename Size<TDb>::Type TDbSize;
typedef typename Value<TText>::Type TTextReference;
typedef typename Size<TText>::Type TTextSize;
TDbSize dbSize = length(db.text);
// Initialize min/max text length.
dbShort.minLength = std::numeric_limits<TTextSize>::max();
dbShort.maxLength = std::numeric_limits<TTextSize>::min();
dbLong.minLength = std::numeric_limits<TTextSize>::max();
dbLong.maxLength = std::numeric_limits<TTextSize>::min();
for (TDbSize dbId = 0; dbId < dbSize; ++dbId)
{
TTextReference text = db.text[dbId];
TTextSize textLength = length(text);
TDb *dbOut = (textLength < seedLength) ? &dbShort : &dbLong;
(*dbOut).minLength = _min((*dbOut).minLength, textLength);
(*dbOut).maxLength = _max((*dbOut).maxLength, textLength);
appendValue((*dbOut).text, text);
appendValue((*dbOut).ids, db.ids[dbId]);
appendValue((*dbOut).errors, getErrors(db, dbId));
}
// Compute average text length.
if (length(dbShort.text) == 0)
{
dbShort.minLength = 0;
dbShort.maxLength = 0;
dbShort.avgLength = 0;
}
else
{
dbShort.avgLength = length(dbShort.text.concat) / length(dbShort.text);
}
if (length(dbLong.text) == 0)
{
dbLong.minLength = 0;
dbLong.maxLength = 0;
dbLong.avgLength = 0;
}
else
{
dbLong.avgLength = length(dbLong.text.concat) / length(dbLong.text);
}
_updateErrors(dbShort);
_updateErrors(dbLong);
}
// ----------------------------------------------------------------------------
// Function getErrors() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec>
inline typename Size<TText>::Type
getErrors(Db<TText, TSpec> const & db, typename Size<Db<TText, TSpec> >::Type /* dbId */)
{
return db.errors;
}
template <typename TText>
inline typename Size<TText>::Type
getErrors(Db<TText, Query> const & db, typename Size<Db<TText, Query> >::Type dbId)
{
return db.errors[dbId];
}
// ----------------------------------------------------------------------------
// Function getMinErrors() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec>
inline typename Size<TText>::Type
getMinErrors(Db<TText, TSpec> const & db)
{
return db.errors;
}
template <typename TText>
inline typename Size<TText>::Type
getMinErrors(Db<TText, Query> const & db)
{
return db.minErrors;
}
// ----------------------------------------------------------------------------
// Function getMaxErrors() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec>
inline typename Size<TText>::Type
getMaxErrors(Db<TText, TSpec> const & db)
{
return db.errors;
}
template <typename TText>
inline typename Size<TText>::Type
getMaxErrors(Db<TText, Query> const & db)
{
return db.maxErrors;
}
// ----------------------------------------------------------------------------
// Function getAvgErrors() [Db]
// ----------------------------------------------------------------------------
template <typename TText, typename TSpec>
inline typename Size<TText>::Type
getAvgErrors(Db<TText, TSpec> const & db)
{
return db.errors;
}
template <typename TText>
inline typename Size<TText>::Type
getAvgErrors(Db<TText, Query> const & db)
{
return db.avgErrors;
}
// ----------------------------------------------------------------------------
// Function countSeeds() [Db]
// ----------------------------------------------------------------------------
template <typename TCounts, typename TText, typename TSpec, typename TSeedLength>
TSeedLength countSeeds(TCounts & seedCounts,
Db<TText, TSpec> /* const */ & db,
TSeedLength minSeedLength)
{
typedef Db<TText, TSpec> const TDb;
typedef typename Size<TDb>::Type TDbSize;
typedef typename Value<TText>::Type TTextReference;
typedef typename Size<TText>::Type TTextSize;
typedef TTextSize TErrors;
TDbSize dbSize = length(db.text);
TTextSize exactSeedLength = db.avgLength / (getAvgErrors(db) + 1);
TTextSize seedLength = _max(exactSeedLength, minSeedLength);
TSeedLength maxSeedErrors = 0;
resize(seedCounts, getMaxErrors(db) + 1, 0, Exact());
for (TDbSize dbId = 0; dbId < dbSize; ++dbId)
{
TTextReference text = db.text[dbId];
TTextSize textLength = length(text);
TErrors errors = getErrors(db, dbId);
TTextSize seedCount = _max(textLength / seedLength, 1u);
TErrors seedErrors = errors / seedCount;
TTextSize seedCountHigh = (errors % seedCount) + 1;
TTextSize seedCountLow = seedCount - seedCountHigh;
seedCounts[seedErrors] += seedCountHigh;
if (seedErrors > 0) seedCounts[seedErrors - 1] += seedCountLow;
maxSeedErrors = _max(maxSeedErrors, seedErrors);
}
resize(seedCounts, maxSeedErrors + 1);
return seedLength;
}
// ----------------------------------------------------------------------------
// Function build() [DbIndex]
// ----------------------------------------------------------------------------
template <typename TText, typename TIndexSpec, typename TDbIndexSpec, typename TDbSpec, typename TSpec>
void build(DbIndex<Index<TText, TIndexSpec>, TDbIndexSpec> & dbIndex,
Db<TText, TDbSpec> /* const */ & db,
TSpec)
{
//typedef Db<TText, TDbSpec> TDb;
//typedef typename Value<TText>::Type TTextReference;
typedef typename Size<TText>::Type TTextSize;
typedef Index<TText, TIndexSpec> TIndex;
typedef typename Value<TIndex>::Type TIndexAlphabet;
typedef typename Size<TIndex>::Type TIndexSize;
typedef typename Fibre<TIndex, FibreSA>::Type TIndexSAFibre;
typedef typename Value<TIndexSAFibre>::Type TIndexSAPos;
typedef typename Iterator<TIndex, TopDown<> >::Type TIterator;
// we don't know the maximal seed length in advance as it depends on the query
// hence we choose a sufficiently large number
unsigned maxSeedLength = 10000;
dbIndex.index = TIndex(db.text);
// 1. create and sort q-gram buckets
TIndexSAFibre &sa = indexSA(dbIndex.index);
TText const &text = db.text;
Shape<TIndexAlphabet, SimpleShape> shape;
String<TIndexSize> &dir = dbIndex.dir;
unsigned shapeLength;
if (ValueSize<TIndexAlphabet>::VALUE <= 5)
shapeLength = _min(maxSeedLength, 10u);
else
shapeLength = _min(maxSeedLength, 3u);
dbIndex.sortedPrefix = shapeLength;
TTextSize stepSize = 1;
if (IsSameType<TSpec, Exact>::VALUE)
stepSize = maxSeedLength;
resize(shape, shapeLength);
resize(sa, _qgramQGramCount(text, shape, stepSize), Exact());
resize(dir, _fullDirLength(shape), Exact());
Nothing nothing;
createQGramIndex(sa, dir, nothing, text, shape, stepSize);
// 2. refine q-gram buckets and sort up to their maxSeedLength prefix
if (shapeLength < maxSeedLength)
{
maxSeedLength -= shapeLength; // delta between maxSeedLength and shapeLength is used for QGramLessOffset_
typename Iterator<TIndexSAFibre, Standard>::Type saBegin = begin(sa, Standard());
SEQAN_OMP_PRAGMA(parallel for schedule(dynamic,1))
for (int i = 1; i < (int)length(dir); ++i)
{
if (dir[i - 1] + 1 < dir[i])
{
auto infixSA = infix(sa, saBegin + dir[i - 1], saBegin + dir[i]);
sort(
infixSA,
QGramLessOffset_<TIndexSAPos, TText const>(text, maxSeedLength, shapeLength),
Parallel());
}
// typename Infix<TIndexSAFibre>::Type saInf = infix(sa, dir[i - 1], dir[i]);
// _refineQGramIndexBucket(
// saInf,
// text,
// shapeLength,
// maxSeedLength);
}
}
TIterator it(dbIndex.index);
// TIndexSAFibre sa = indexSA(dbIndex.index);
// resize(sa, lengthSum(db.text));
//
// TDbSize dbSize = length(db.text);
// for (TDbSize dbId = 0; dbId < dbSize; ++dbId)
// {
// TTextReference text = db.text[dbId];
// TTextSize textLength = length(text);
//
// TIndexSAPos saPos;
// assignValueI1(saPos, dbId);
// for (TTextSize pos = 0; pos < textLength; ++pos)
// {
// assignValueI2(saPos, pos);
// appendValue(sa, saPos);
// }
// }
//
// QGramLess_<TIndexSAPos, TText const> less(db.text, std::numeric_limits<TTextSize>::max());
// sort(sa, less, Parallel());
}
// ----------------------------------------------------------------------------
// Function build() [DbIndex<Query>]
// ----------------------------------------------------------------------------
template <typename TText, typename TIndexSpec, typename TDbSpec, typename TSeedLength>
void buildQuery(DbIndex<Index<TText, IndexSa<TIndexSpec> >, Query> & dbIndex,
Db<TText, TDbSpec> /* const */ & db,
TSeedLength minSeedLength)
{
typedef Db<TText, TDbSpec> TDb;
typedef typename Size<TDb>::Type TDbSize;
typedef DbIndex<Index<TText, IndexSa<TIndexSpec> >, Query> TDbIndex;
typedef typename Size<TDbIndex>::Type TDbIndexSize;
typedef Index<TText, IndexSa<TIndexSpec> > TIndex;
//typedef typename Fibre<TIndex, FibreSA>::Type TIndexSAFibre;
typedef typename Size<TText>::Type TTextSize;
typedef TTextSize TErrors;
String<TDbSize> seedCounts;
// Count seeds.
dbIndex.seedLength = countSeeds(seedCounts, db, minSeedLength);
// Get non-zero seed counts.
TErrors maxSeedErrors = length(seedCounts);
for (TErrors seedErrors = 0; seedErrors < maxSeedErrors; ++seedErrors)
if (seedCounts[seedErrors] > 0)
appendValue(dbIndex.errors, seedErrors);
std::cout << "Seed length:\t\t\t\t" << dbIndex.seedLength << std::endl;
std::cout << "Seed counts:\t\t\t\t";
std::copy(begin(seedCounts, Standard()),
end(seedCounts, Standard()),
std::ostream_iterator<TDbSize>(std::cout, ", "));
std::cout << std::endl;
std::cout << "Seed errors:\t\t\t\t";
std::copy(begin(dbIndex.errors, Standard()),
end(dbIndex.errors, Standard()),
std::ostream_iterator<TDbIndexSize>(std::cout, ", "));
std::cout << std::endl;
// Resize indices.
TErrors seedSetsCount = length(dbIndex.errors);
resize(dbIndex.index, seedSetsCount, Exact());
for (TErrors seedSet = 0; seedSet < seedSetsCount; ++seedSet)
{
TErrors seedErrors = dbIndex.errors[seedSet];
dbIndex.index[seedSet] = TIndex(db.text);
reserve(indexSA(dbIndex.index[seedSet]), seedCounts[seedErrors], Exact());
}
// Build SA fibres.
SEQAN_OMP_PRAGMA(parallel for schedule(dynamic))
for (int seedSet = 0; seedSet < (int)seedSetsCount; ++seedSet)
{
TErrors seedErrors = dbIndex.errors[seedSet];
_buildSA(indexSA(dbIndex.index[seedSet]), db, dbIndex.seedLength, seedErrors);
}
}
// ----------------------------------------------------------------------------
// Function _buildSA() [DbIndex<Query>]
// ----------------------------------------------------------------------------
template <typename TIndexSAFibre, typename TText, typename TDbSpec, typename TSeedErrors, typename TSeedLength>
void _buildSA(TIndexSAFibre & sa,
Db<TText, TDbSpec> /* const */ & db,
TSeedLength seedLength,
TSeedErrors seedErrors)
{
typedef Db<TText, TDbSpec> TDb;
typedef typename Size<TDb>::Type TDbSize;
typedef typename Value<TIndexSAFibre>::Type TIndexSAPos;
typedef typename Value<TText>::Type TTextReference;
typedef typename Size<TText>::Type TTextSize;
typedef TTextSize TErrors;
TDbSize dbSize = length(db.text);
for (TDbSize dbId = 0; dbId < dbSize; ++dbId)
{
TTextReference text = db.text[dbId];
TTextSize textLength = length(text);
TErrors errors = getErrors(db, dbId);
TTextSize seedCount = _max(textLength / seedLength, 1u);
TSeedErrors seedErrors_ = errors / seedCount;
TTextSize seedCountHigh = (errors % seedCount) + 1;
TIndexSAPos seed;
assignValueI1(seed, dbId);
if (seedErrors_ == seedErrors)
{
TTextSize seedCounter = 0;
TTextSize seedPos = 0;
for (; seedCounter < seedCountHigh; ++seedCounter, seedPos += seedLength)
{
assignValueI2(seed, seedPos);
appendValue(sa, seed);
}
}
else if (seedErrors_ > 0 && seedErrors_ - 1 == seedErrors)
{
TTextSize seedCounter = seedCountHigh;
TTextSize seedPos = seedCountHigh * seedLength;
for (; seedCounter < seedCount; ++seedCounter, seedPos += seedLength)
{
assignValueI2(seed, seedPos);
appendValue(sa, seed);
}
}
}
// Construct index using quicksort.
QGramLess_<TIndexSAPos, TText const> less(db.text, seedLength);
sort(sa, less, Parallel());
}
// ----------------------------------------------------------------------------
// Function build() [DbIndex]
// ----------------------------------------------------------------------------
// NOTE(esiragusa): Build a trie of Db using QGram bucket sort.
//template <typename TDb, typename TIndexText, typename TShape, typename TIndexSpec, typename TSpec>
//void build(DbIndex<TDb, Index<TIndexText, IndexQGram<TShape, TIndexSpec> >, TSpec> & dbIndex)
//{
// typedef Index<TIndexText, IndexQGram<TShape, TIndexSpec> > TIndex;
// typedef typename Fibre<TIndex, QGramSA>::Type TIndexSAFibre;
// typedef typename Fibre<TIndex, QGramDir>::Type TIndexDirFibre;
// typedef typename Fibre<TIndex, QGramShape>::Type TIndexShape;
// typedef typename Fibre<TIndex, QGramBucketMap>::Type TIndexBucketMap;
// typedef typename Value<TIndexSAFibre>::Type TSAPos;
//
// typedef typename Size<TDb>::Type TDbSize;
// typedef typename Host<TDb>::Type const TText;
// typedef typename Value<TText>::Type const TText;
// typedef typename Iterator<TText, Standard>::Type TTextIterator;
//
// // NOTE(esiragusa): This is done in DbIndex constructor.
//// dbIndex.index = TIndex(dbIndex.db.text);
//
// // NOTE(esiragusa): This is to index whole strings.
//// setStepSize(dbIndex.index, 256u);
//
// TIndexSAFibre & sa = indexSA(dbIndex.index);
// TIndexDirFibre & dir = indexDir(dbIndex.index);
// TIndexShape & shape = indexShape(dbIndex.index);
// TIndexBucketMap & bucketMap = indexBucketMap(dbIndex.index);
//
// // Resize suffix array and directory.
// TDbSize dbSize = length(dbIndex.db.text);
// resize(sa, dbSize, Exact());
// resize(dir, _fullDirLength(dbIndex.index), Exact());
//
// // Clear directory.
// _qgramClearDir(dir, bucketMap);
//
// // Count qgrams.
// for (TDbSize dbId = 0; dbId < dbSize; ++dbId)
// {
// TText & text = dbIndex.db.text[dbId];
// TTextIterator textIt = begin(text, Standard());
// ++dir[requestBucket(bucketMap, hash(shape, textIt))];
// }
//
// // Compute cumulative sum.
// _qgramCummulativeSum(dir, False());
//
// // Fill suffix array.
// for (TDbSize dbId = 0; dbId < dbSize; ++dbId)
// {
// TText & text = dbIndex.db.text[dbId];
// TTextIterator textIt = begin(text, Standard());
//
// TSAPos saPos;
// assignValueI1(saPos, dbId);
// assignValueI2(saPos, 0);
// sa[dir[getBucket(bucketMap, hash(shape, textIt)) + 1]++] = saPos;
// }
//
// // Refine buckets.
// _refineQGramIndex(sa, dir, indexText(dbIndex.index), weight(shape), 256);
// _setHost(dbIndex.index);
//}
#endif // #ifndef SEQAN_APPS_SEARCHJOIN_DB_H_
|