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 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
|
/* $Id: writedb_impl.cpp 518254 2016-11-01 17:36:44Z ivanov $
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Kevin Bealer
*
*/
/// @file writedb_impl.cpp
/// Implementation for the CWriteDB_Impl class.
/// class for WriteDB.
#include <ncbi_pch.hpp>
#include <objtools/blast/seqdb_writer/writedb_error.hpp>
#include <objtools/blast/seqdb_reader/seqdbexpert.hpp>
#include <objtools/blast/seqdb_reader/seqdbcommon.hpp>
#include <objects/general/general__.hpp>
#include <objects/seqfeat/seqfeat__.hpp>
#include <util/sequtil/sequtil_convert.hpp>
#include <objects/blastdb/defline_extra.hpp> // for kAsnDeflineObjLabel
#include <serial/typeinfo.hpp>
#include <corelib/ncbi_bswap.hpp>
#include "writedb_impl.hpp"
#include <objtools/blast/seqdb_writer/writedb_convert.hpp>
#include <iostream>
#include <sstream>
BEGIN_NCBI_SCOPE
/// Import C++ std namespace.
USING_SCOPE(std);
CWriteDB_Impl::CWriteDB_Impl(const string & dbname,
bool protein,
const string & title,
EIndexType indices,
bool parse_ids,
bool long_ids,
bool use_gi_mask)
: m_Dbname (dbname),
m_Protein (protein),
m_Title (title),
m_MaxFileSize (0),
m_MaxVolumeLetters (0),
m_Indices (indices),
m_Closed (false),
m_MaskDataColumn (-1),
m_ParseIDs (parse_ids),
m_UseGiMask (use_gi_mask),
m_Pig (0),
m_Hash (0),
m_SeqLength (0),
m_HaveSequence (false),
m_LongSeqId (long_ids)
{
CTime now(CTime::eCurrent);
m_Date = now.AsString("b d, Y ");
string t = now.AsString("H:m P");
if (t[0] == '0') {
t.assign(t, 1, t.size() - 1);
}
m_Date += t;
}
CWriteDB_Impl::~CWriteDB_Impl()
{
try {
Close();
} catch (const CWriteDBException& e) {
LOG_POST(Error << "BLAST Database creation error: " << e.GetMsg());
}
}
void CWriteDB_Impl::x_ResetSequenceData()
{
m_Bioseq.Reset();
m_SeqVector = CSeqVector();
m_Deflines.Reset();
m_Ids.clear();
m_Linkouts.clear();
m_Memberships.clear();
m_Pig = 0;
m_Hash = 0;
m_SeqLength = 0;
m_Sequence.erase();
m_Ambig.erase();
m_BinHdr.erase();
NON_CONST_ITERATE(vector<int>, iter, m_HaveBlob) {
*iter = 0;
}
#if ((!defined(NCBI_COMPILER_WORKSHOP) || (NCBI_COMPILER_VERSION > 550)) && \
(!defined(NCBI_COMPILER_MIPSPRO)) )
NON_CONST_ITERATE(vector< CRef<CBlastDbBlob> >, iter, m_Blobs) {
(**iter).Clear();
}
#endif
}
void CWriteDB_Impl::AddSequence(const CTempString & seq,
const CTempString & ambig)
{
// Publish previous sequence (if any)
x_Publish();
// Blank slate for new sequence.
x_ResetSequenceData();
m_Sequence.assign(seq.data(), seq.length());
m_Ambig.assign(ambig.data(), ambig.length());
if (m_Indices & CWriteDB::eAddHash) {
x_ComputeHash(seq, ambig);
}
x_SetHaveSequence();
}
void CWriteDB_Impl::AddSequence(const CBioseq & bs)
{
// Publish previous sequence
x_Publish();
// Blank slate for new sequence.
x_ResetSequenceData();
m_Bioseq.Reset(& bs);
if (m_Bioseq->GetInst().CanGetMol() && (m_Bioseq->IsAa() != m_Protein)) {
CNcbiOstrstream msg;
msg << "Invalid molecule type of sequence added ("
<< (m_Bioseq->IsAa() ? "protein" : "nucleotide")
<< "); expected " << (m_Protein ? "protein" : "nucleotide");
NCBI_THROW(CWriteDBException, eArgErr, CNcbiOstrstreamToString(msg));
}
if (m_Indices & CWriteDB::eAddHash) {
x_ComputeHash(bs);
}
x_SetHaveSequence();
}
void CWriteDB_Impl::AddSequence(const CBioseq & bs, CSeqVector & sv)
{
AddSequence(bs);
m_SeqVector = sv;
}
void CWriteDB_Impl::AddSequence(const CBioseq_Handle & bsh)
{
CSeqVector sv(bsh);
AddSequence(*bsh.GetCompleteBioseq(), sv);
}
/// class to support searching for duplicate isam keys
template <class T>
class CWriteDB_IsamKey {
public:
// data member
T key;
CNcbiIfstream * source;
// constructor
CWriteDB_IsamKey(const string &fn) {
source = new CNcbiIfstream(fn.c_str(),
IOS_BASE::in | IOS_BASE::binary);
key = x_GetNextKey();
};
~CWriteDB_IsamKey() {
delete source;
};
// advance key to catch up other
bool AdvanceKey(const CWriteDB_IsamKey & other) {
while (!source->eof()) {
T next_key = x_GetNextKey();
if (next_key >= other.key) {
key = next_key;
return true;
}
}
return false;
};
// less_than, used for sorting
bool operator <(const CWriteDB_IsamKey &other) const {
return (key < other.key);
};
private:
// read in the next key, for numeric id
T x_GetNextKey() {
#define INT4_SIZE 4
char s[INT4_SIZE] = { '\0' };
source->read(s, INT4_SIZE);
if ((source->gcount() != INT4_SIZE) || source->eof()) {
return T();
}
source->seekg(INT4_SIZE, ios_base::cur);
#ifdef WORDS_BIGENDIAN
Int4 next_key = (Int4) *((Int4 *) s);
#else
Int4 next_key = CByteSwap::GetInt4((const unsigned char *)s);
#endif
return next_key;
};
};
// customized string file reading
template <> inline string
CWriteDB_IsamKey<string>::x_GetNextKey() {
#define CHAR_BUFFER_SIZE 256
char s[CHAR_BUFFER_SIZE] = { '\0' };
source->getline(s, CHAR_BUFFER_SIZE);
if ((source->gcount() == 0) || source->eof()) {
return kEmptyStr;
}
char * p = s;
while (*p != 0x02) ++p;
string in(s, p);
// check if the current key is PDB-like,
// if so, advance for the next
// PDB key must be [0-9]...
if ( (in.size() == 4)
&& ((in[0] - '0') * (in[0] - '9') <= 0) ) {
// probing the next key to make sure this is pdb id
char next_token[4];
source->read(next_token, 4);
source->seekg(-4, ios_base::cur);
string next_key(next_token, 4);
if (next_key == in) {
// automatically advance to next key
return x_GetNextKey();
}
}
return in;
};
/// Comparison function for set<CWriteDB_IsamKey<T> *>
template <class T>
struct CWriteDB_IsamKey_Compare {
bool operator() (const CWriteDB_IsamKey<T> * lhs,
const CWriteDB_IsamKey<T> * rhs) const {
return (*lhs < *rhs);
}
};
/// Check for duplicate ids across volumes
template <class T>
static void s_CheckDuplicateIds(set<CWriteDB_IsamKey<T> *,
CWriteDB_IsamKey_Compare<T> > & keys) {
while (!keys.empty()) {
// pick the smallest key
CWriteDB_IsamKey<T> * key = *(keys.begin());
keys.erase(key);
if (keys.empty()) {
delete key;
return;
}
const CWriteDB_IsamKey<T> * next = *(keys.begin());
if (key->AdvanceKey(*next)) {
if (keys.find(key) != keys.end()) {
CNcbiOstrstream msg;
msg << "Error: Duplicate seq_id <"
<< key->key
<< "> is found multiple times across volumes.";
NCBI_THROW(CWriteDBException, eArgErr, CNcbiOstrstreamToString(msg));
}
keys.insert(key);
} else {
delete key;
}
}
};
void CWriteDB_Impl::Close()
{
if (m_Closed)
return;
m_Closed = true;
x_Publish();
m_Sequence.erase();
m_Ambig.erase();
if (! m_Volume.Empty()) {
m_Volume->Close();
if (m_UseGiMask) {
for (unsigned int i=0; i<m_GiMasks.size(); ++i) {
m_GiMasks[i]->Close();
}
}
if (m_VolumeList.size() == 1) {
m_Volume->RenameSingle();
}
// disable the check for duplicate ids across volumes
/*
else if (m_Indices != CWriteDB::eNoIndex) {
set<CWriteDB_IsamKey<string> *, CWriteDB_IsamKey_Compare<string> > sids;
ITERATE(vector< CRef<CWriteDB_Volume> >, iter, m_VolumeList) {
string fn = (*iter)->GetVolumeName() + (m_Protein ? ".psd" : ".nsd");
if (CFile(fn).Exists()) {
sids.insert(new CWriteDB_IsamKey<string>(fn));
}
}
s_CheckDuplicateIds(sids);
set<CWriteDB_IsamKey<Int4> *, CWriteDB_IsamKey_Compare<Int4> > nids;
ITERATE(vector< CRef<CWriteDB_Volume> >, iter, m_VolumeList) {
string fn = (*iter)->GetVolumeName() + (m_Protein ? ".pnd" : ".nnd");
if (CFile(fn).Exists()) {
nids.insert(new CWriteDB_IsamKey<Int4>(fn));
}
}
s_CheckDuplicateIds(nids);
} */
if (m_VolumeList.size() > 1 || m_UseGiMask) {
x_MakeAlias();
}
m_Volume.Reset();
}
}
string CWriteDB_Impl::x_MakeAliasName()
{
return m_Dbname + (m_Protein ? ".pal" : ".nal");
}
void CWriteDB_Impl::x_MakeAlias()
{
string dblist;
if (m_VolumeList.size() > 1) {
for(unsigned i = 0; i < m_VolumeList.size(); i++) {
if (dblist.size())
dblist += " ";
dblist += CDirEntry(CWriteDB_File::MakeShortName(m_Dbname, i)).GetName();
}
} else {
dblist = m_Dbname;
}
string masklist("");
if (m_UseGiMask) {
for (unsigned i = 0; i < m_GiMasks.size(); i++) {
const string & x = m_GiMasks[i]->GetName();
if (x != "") {
masklist += x + " ";
}
}
}
string nm = x_MakeAliasName();
ofstream alias(nm.c_str());
alias << "#\n# Alias file created: " << m_Date << "\n#\n"
<< "TITLE " << m_Title << "\n"
<< "DBLIST " << dblist << "\n";
if (masklist != "") {
alias << "MASKLIST " << masklist << "\n";
}
}
void CWriteDB_Impl::x_GetBioseqBinaryHeader(const CBioseq & bioseq,
string & bin_hdr)
{
if (! bin_hdr.empty()) {
return;
}
if (! bioseq.CanGetDescr()) {
return;
}
// Getting the binary headers, when they exist, is probably faster
// than building new deflines from the 'visible' CBioseq parts.
vector< vector< char >* > bindata;
ITERATE(list< CRef< CSeqdesc > >, iter, bioseq.GetDescr().Get()) {
if ((**iter).IsUser()) {
const CUser_object & uo = (**iter).GetUser();
const CObject_id & oi = uo.GetType();
if (oi.IsStr() && oi.GetStr() == kAsnDeflineObjLabel) {
if (uo.CanGetData()) {
const vector< CRef< CUser_field > > & D = uo.GetData();
if (D.size() &&
D[0].NotEmpty() &&
D[0]->CanGetLabel() &&
D[0]->GetLabel().IsStr() &&
D[0]->GetLabel().GetStr() == kAsnDeflineObjLabel &&
D[0]->CanGetData() &&
D[0]->GetData().IsOss()) {
bindata = D[0]->GetData().GetOss();
break;
}
}
}
}
}
if (! bindata.empty()) {
if (bindata[0] && (! bindata[0]->empty())) {
vector<char> & b = *bindata[0];
bin_hdr.assign(& b[0], b.size());
}
}
}
static void
s_CheckEmptyLists(CRef<CBlast_def_line_set> & deflines, bool owner);
static CRef<CBlast_def_line_set>
s_EditDeflineSet(CConstRef<CBlast_def_line_set> & deflines)
{
CRef<CBlast_def_line_set> bdls(new CBlast_def_line_set);
SerialAssign(*bdls, *deflines);
s_CheckEmptyLists(bdls, true);
return bdls;
}
static void
s_CheckEmptyLists(CRef<CBlast_def_line_set> & deflines, bool owner)
{
CBlast_def_line_set * bdls = 0;
CConstRef<CBlast_def_line_set> here(&*deflines);
if (! owner) {
here = s_EditDeflineSet(here);
return;
}
bdls = const_cast<CBlast_def_line_set*>(here.GetPointer());
NON_CONST_ITERATE(list< CRef< CBlast_def_line > >, iter, bdls->Set()) {
CRef<CBlast_def_line> defline = *iter;
if (defline->CanGetMemberships() &&
defline->GetMemberships().size() == 0) {
defline->ResetMemberships();
}
if (defline->CanGetLinks() &&
defline->GetLinks().size() == 0) {
defline->ResetLinks();
}
}
deflines.Reset(bdls);
}
void
CWriteDB_Impl::x_BuildDeflinesFromBioseq(const CBioseq & bioseq,
CConstRef<CBlast_def_line_set> & deflines,
const vector< vector<int> > & membbits,
const vector< vector<int> > & linkouts,
int pig)
{
if (! (bioseq.CanGetDescr() && bioseq.CanGetId())) {
return;
}
vector<int> taxids;
string titles;
// Scan the CBioseq for taxids and the title string.
ITERATE(list< CRef< CSeqdesc > >, iter, bioseq.GetDescr().Get()) {
const CSeqdesc & desc = **iter;
if (desc.IsTitle()) {
//defline->SetTitle((**iter)->GetTitle());
titles = (**iter).GetTitle();
}
else {
const COrg_ref * org_pt = NULL;
if (desc.IsSource()) {
org_pt = &(desc.GetSource().GetOrg());
}
else if( desc.IsOrg()) {
org_pt = &(desc.GetOrg());
}
if((NULL != org_pt) && org_pt->CanGetDb()) {
ITERATE(vector< CRef< CDbtag > >,
dbiter,
org_pt->GetDb()) {
if ((**dbiter).CanGetDb() &&
(**dbiter).GetDb() == "taxon") {
const CObject_id & oi = (**dbiter).GetTag();
if (oi.IsId()) {
//defline->SetTaxid(oi.GetId());
taxids.push_back(oi.GetId());
}
}
}
}
}
}
// The bioseq has a field contianing the ids for the first
// defline. The title string contains the title for the first
// defline, plus all the other defline titles and ids. This code
// unpacks them and builds a normal blast defline set.
list< CRef<CSeq_id> > ids = bioseq.GetId();
unsigned taxid_i(0), mship_i(0), links_i(0);
bool used_pig(false);
// Build the deflines.
CRef<CBlast_def_line_set> bdls(new CBlast_def_line_set);
CRef<CBlast_def_line> defline;
while(! ids.empty()) {
defline.Reset(new CBlast_def_line);
defline->SetSeqid() = ids;
ids.clear();
/*
size_t pos = titles.find(" >");
string T;
if (pos != titles.npos) {
T.assign(titles, 0, pos);
titles.erase(0, pos + 2);
pos = titles.find(" ");
string nextid;
if (pos != titles.npos) {
nextid.assign(titles, 0, pos);
titles.erase(0, pos + 1);
} else {
nextid.swap(titles);
}
// Parse '|' seperated ids.
if ( nextid.find('|') == NPOS
|| !isalpha((unsigned char)(nextid[0]))) {
ids.push_back(CRef<CSeq_id> (new CSeq_id(CSeq_id::e_Local, nextid)));
} else {
CSeq_id::ParseFastaIds(ids, nextid);
}
} else {
T = titles;
}
*/
defline->SetTitle(titles);
if (taxid_i < taxids.size()) {
defline->SetTaxid(taxids[taxid_i++]);
}
if (mship_i < membbits.size()) {
const vector<int> & V = membbits[mship_i++];
defline->SetMemberships().assign(V.begin(), V.end());
}
if (links_i < linkouts.size()) {
const vector<int> & V = linkouts[mship_i++];
defline->SetLinks().assign(V.begin(), V.end());
}
if ((! used_pig) && pig) {
defline->SetOther_info().push_back(pig);
used_pig = true;
}
bdls->Set().push_back(defline);
}
s_CheckEmptyLists(bdls, true);
deflines = bdls;
}
void CWriteDB_Impl::
x_SetDeflinesFromBinary(const string & bin_hdr,
CConstRef<CBlast_def_line_set> & deflines)
{
CRef<CBlast_def_line_set> bdls(new CBlast_def_line_set);
istringstream iss(bin_hdr);
iss >> MSerial_AsnBinary >> *bdls;
s_CheckEmptyLists(bdls, true);
deflines.Reset(&* bdls);
}
static bool s_UseFastaReaderDeflines(CConstRef<CBioseq> & bioseq, CConstRef<CBlast_def_line_set> & deflines, bool long_seqid)
{
if(deflines.Empty())
return false;
const CSeq_id * bioseq_id = bioseq->GetNonLocalId();
if(bioseq_id == NULL ||
// For bare pir and prf ids go with the one from defline.
// This is to parse bare ids as local ones. The bare pdb ids are pdb in
// bioseq (parsed by CFastaReader), but local in deflines (parsed by
// CSeq_id).
(!long_seqid && (bioseq_id->IsPrf() || bioseq_id->IsPir()))) {
return true;
}
// Bioseq has non-local id, make sure at least one id is non-local from CFastaReader
// defline
ITERATE(list< CRef<CBlast_def_line> >, iter, deflines->Get()) {
CRef<CSeq_id> id = FindBestChoice((**iter).GetSeqid(), &CSeq_id::BestRank);
if (id.NotEmpty() && !id->IsLocal()) {
return true;
}
}
return false;
}
void
CWriteDB_Impl::x_ExtractDeflines(CConstRef<CBioseq> & bioseq,
CConstRef<CBlast_def_line_set> & deflines,
string & bin_hdr,
const vector< vector<int> > & membbits,
const vector< vector<int> > & linkouts,
int pig,
int OID,
bool parse_ids,
bool long_ids)
{
bool use_bin = (deflines.Empty() && pig == 0);
if (! bin_hdr.empty() && OID<0) {
return;
}
if (deflines.Empty()) {
// Use bioseq if deflines are not provided.
if (bioseq.Empty()) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Error: Cannot find CBioseq or deflines.");
}
// CBioseq objects from SeqDB have binary headers embedded in
// them. If these are found, we try to use them. However,
// using binary headers may not help us much if we also want
// lists of sequence identifiers (for building ISAM files).
if (use_bin) {
x_GetBioseqBinaryHeader(*bioseq, bin_hdr);
}
if (bin_hdr.empty()) {
x_GetFastaReaderDeflines(*bioseq,
deflines,
membbits,
linkouts,
pig,
false,
parse_ids,
long_ids);
}
if(!s_UseFastaReaderDeflines(bioseq, deflines, long_ids)) {
deflines.Reset();
}
if (bin_hdr.empty() && deflines.Empty()) {
x_BuildDeflinesFromBioseq(*bioseq,
deflines,
membbits,
linkouts,
pig);
}
}
if (bin_hdr.empty() &&
(deflines.Empty() || deflines->Get().empty())) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Error: No deflines provided.");
}
if (pig != 0) {
const list<int> * L = 0;
if (deflines->Get().front()->CanGetOther_info()) {
L = & deflines->Get().front()->GetOther_info();
}
// If the pig does not agree with the current value, set the
// new value and force a rebuild of the binary headers. If
// there is more than one value in the list, leave the others
// in place.
if ((L == 0) || L->empty()) {
CRef<CBlast_def_line_set> bdls = s_EditDeflineSet(deflines);
bdls->Set().front()->SetOther_info().push_back(pig);
deflines.Reset(&* bdls);
bin_hdr.erase();
} else if (L->front() != pig) {
CRef<CBlast_def_line_set> bdls = s_EditDeflineSet(deflines);
bdls->Set().front()->SetOther_info().front() = pig;
deflines.Reset(&* bdls);
bin_hdr.erase();
}
}
if (OID>=0) {
// Re-inject the BL_ORD_ID
CRef<CSeq_id> gnl_id(new CSeq_id);
gnl_id->SetGeneral().SetDb("BL_ORD_ID");
gnl_id->SetGeneral().SetTag().SetId(OID);
CRef<CBlast_def_line_set> bdls = s_EditDeflineSet(deflines);
bdls->Set().front()->SetSeqid().front() = gnl_id;
deflines.Reset(&* bdls);
}
if (bin_hdr.empty() || OID>=0) {
// Compress the deflines to binary.
CNcbiOstrstream oss;
oss << MSerial_AsnBinary << *deflines;
bin_hdr = CNcbiOstrstreamToString(oss);
}
if (deflines.Empty() && (! bin_hdr.empty())) {
// Uncompress the deflines from binary.
x_SetDeflinesFromBinary(bin_hdr, deflines);
}
}
void CWriteDB_Impl::x_CookHeader()
{
int OID = -1;
if (! m_ParseIDs) {
OID = (m_Volume ) ? m_Volume->GetOID() : 0;
}
x_ExtractDeflines(m_Bioseq,
m_Deflines,
m_BinHdr,
m_Memberships,
m_Linkouts,
m_Pig,
OID,
m_ParseIDs,
m_LongSeqId);
}
void CWriteDB_Impl::x_CookIds()
{
if (! m_Ids.empty()) {
return;
}
if (m_Deflines.Empty()) {
if (m_BinHdr.empty()) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Error: Cannot find IDs or deflines.");
}
x_SetDeflinesFromBinary(m_BinHdr, m_Deflines);
}
ITERATE(list< CRef<CBlast_def_line> >, iter, m_Deflines->Get()) {
const list< CRef<CSeq_id> > & ids = (**iter).GetSeqid();
// m_Ids.insert(m_Ids.end(), ids.begin(), ids.end());
// Spelled out for WorkShop. :-/
m_Ids.reserve(m_Ids.size() + ids.size());
ITERATE (list<CRef<CSeq_id> >, it, ids) {
m_Ids.push_back(*it);
}
}
}
void CWriteDB_Impl::x_MaskSequence()
{
// Scan and mask the sequence itself.
for(unsigned i = 0; i < m_Sequence.size(); i++) {
if (m_MaskLookup[m_Sequence[i] & 0xFF] != 0) {
m_Sequence[i] = m_MaskByte[0];
}
}
}
int CWriteDB_Impl::x_ComputeSeqLength()
{
if (! m_SeqLength) {
if (! m_Sequence.empty()) {
m_SeqLength = WriteDB_FindSequenceLength(m_Protein, m_Sequence);
} else if (m_SeqVector.size()) {
m_SeqLength = m_SeqVector.size();
} else if (! (m_Bioseq &&
m_Bioseq->CanGetInst() &&
m_Bioseq->GetInst().GetLength())) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Need sequence data.");
}
if (m_Bioseq.NotEmpty()) {
const CSeq_inst & si = m_Bioseq->GetInst();
m_SeqLength = si.GetLength();
}
}
return m_SeqLength;
}
void CWriteDB_Impl::x_CookSequence()
{
if (! m_Sequence.empty())
return;
if (! (m_Bioseq.NotEmpty() && m_Bioseq->CanGetInst())) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Need sequence data.");
}
const CSeq_inst & si = m_Bioseq->GetInst();
if (m_Bioseq->GetInst().CanGetSeq_data()) {
const CSeq_data & sd = si.GetSeq_data();
string msg;
switch(sd.Which()) {
case CSeq_data::e_Ncbistdaa:
WriteDB_StdaaToBinary(si, m_Sequence);
break;
case CSeq_data::e_Ncbieaa:
WriteDB_EaaToBinary(si, m_Sequence);
break;
case CSeq_data::e_Iupacaa:
WriteDB_IupacaaToBinary(si, m_Sequence);
break;
case CSeq_data::e_Ncbi2na:
WriteDB_Ncbi2naToBinary(si, m_Sequence);
break;
case CSeq_data::e_Ncbi4na:
WriteDB_Ncbi4naToBinary(si, m_Sequence, m_Ambig);
break;
case CSeq_data::e_Iupacna:
WriteDB_IupacnaToBinary(si, m_Sequence, m_Ambig);
break;
default:
msg = "Need to write conversion for data type [";
msg += NStr::IntToString((int) sd.Which());
msg += "].";
}
if (! msg.empty()) {
NCBI_THROW(CWriteDBException, eArgErr, msg);
}
} else {
int sz = m_SeqVector.size();
if (sz == 0) {
NCBI_THROW(CWriteDBException,
eArgErr,
"No sequence data in Bioseq, "
"and no Bioseq_Handle available.");
}
if (m_Protein) {
// I add one to the string length to allow the "i+1" in
// the loop to be done safely.
m_Sequence.reserve(sz);
m_SeqVector.GetSeqData(0, sz, m_Sequence);
} else {
// I add one to the string length to allow the "i+1" in the
// loop to be done safely.
string na8;
na8.reserve(sz + 1);
m_SeqVector.GetSeqData(0, sz, na8);
na8.resize(sz + 1);
string na4;
na4.resize((sz + 1) / 2);
for(int i = 0; i < sz; i += 2) {
na4[i/2] = (na8[i] << 4) + na8[i+1];
}
WriteDB_Ncbi4naToBinary(na4.data(),
(int) na4.size(),
(int) si.GetLength(),
m_Sequence,
m_Ambig);
}
}
}
void CWriteDB_Impl::x_CookColumns()
{
}
// The CPU should be kept at 190 degrees for 10 minutes.
void CWriteDB_Impl::x_CookData()
{
// We need sequence, ambiguity, and binary deflines. If any of
// these is missing, it is created from other data if possible.
// For now I am disabling binary headers, because in normal usage
// I would expect to see sequences from ID1 or similar, and the
// non-binary case is slightly more complex.
x_CookHeader();
x_CookIds();
x_CookSequence();
x_CookColumns();
if (m_Protein && m_MaskedLetters.size()) {
x_MaskSequence();
}
}
bool CWriteDB_Impl::x_HaveSequence() const
{
return m_HaveSequence;
}
void CWriteDB_Impl::x_SetHaveSequence()
{
_ASSERT(! m_HaveSequence);
m_HaveSequence = true;
}
void CWriteDB_Impl::x_ClearHaveSequence()
{
_ASSERT(m_HaveSequence);
m_HaveSequence = false;
}
void CWriteDB_Impl::x_Publish()
{
// This test should fail only on the first call, or if an
// exception was thrown.
if (x_HaveSequence()) {
_ASSERT(! (m_Bioseq.Empty() && m_Sequence.empty()));
x_ClearHaveSequence();
} else {
return;
}
x_CookData();
bool done = false;
if (! m_Volume.Empty()) {
done = m_Volume->WriteSequence(m_Sequence,
m_Ambig,
m_BinHdr,
m_Ids,
m_Pig,
m_Hash,
m_Blobs,
m_MaskDataColumn);
}
if (! done) {
int index = (int) m_VolumeList.size();
if (m_Volume.NotEmpty()) {
m_Volume->Close();
}
{
m_Volume.Reset(new CWriteDB_Volume(m_Dbname,
m_Protein,
m_Title,
m_Date,
index,
m_MaxFileSize,
m_MaxVolumeLetters,
m_Indices));
m_VolumeList.push_back(m_Volume);
#if ((!defined(NCBI_COMPILER_WORKSHOP) || (NCBI_COMPILER_VERSION > 550)) && \
(!defined(NCBI_COMPILER_MIPSPRO)) )
_ASSERT(m_Blobs.size() == m_ColumnTitles.size() * 2);
_ASSERT(m_Blobs.size() == m_ColumnMetas.size() * 2);
_ASSERT(m_Blobs.size() == m_HaveBlob.size() * 2);
for(size_t i = 0; i < m_ColumnTitles.size(); i++) {
m_Volume->CreateColumn(m_ColumnTitles[i],
m_ColumnMetas[i],
m_MaxFileSize);
}
#endif
}
// need to reset OID, hense recalculate the header and id
x_CookHeader();
x_CookIds();
done = m_Volume->WriteSequence(m_Sequence,
m_Ambig,
m_BinHdr,
m_Ids,
m_Pig,
m_Hash,
m_Blobs,
m_MaskDataColumn);
if (! done) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Cannot write sequence to volume.");
}
}
}
void CWriteDB_Impl::SetDeflines(const CBlast_def_line_set & deflines)
{
CRef<CBlast_def_line_set>
bdls(const_cast<CBlast_def_line_set*>(& deflines));
s_CheckEmptyLists(bdls, true);
m_Deflines = bdls;
}
inline int s_AbsMax(int a, int b)
{
return std::max(((a < 0) ? -a : a),
((b < 0) ? -b : b));
}
// Filtering data format on disk:
//
// Size of integer type for this blob (1, 2, or 4) (4 bytes).
//
// Array of filtering types:
// Filter-type (enumeration)
// Array of offsets:
// Start Offset
// End Offset
//
// The isize is one of 1, 2, or 4, written in the first byte, and
// followed by 0, 1, or 3 NUL bytes to align the data offset to a
// multiple of `isize'.
//
// All other integer values in this array use isize bytes, including
// array counts and the `type' enumerations. After all the offset is
// written, the blob is aligned to a multiple of 4 using the `eSimple'
// method.
//
// Each array is an element count followed by that many elements.
#if 0
// I think this is a better approach; but it needs more testing,
// particularly with regard to platform portability.
struct SWriteInt1 {
static void WriteInt(CBlastDbBlob & blob, int value)
{
blob.WriteInt1(value);
}
};
struct SWriteInt2 {
static void WriteInt(CBlastDbBlob & blob, int value)
{
blob.WriteInt2(value);
}
};
struct SWriteInt4 {
static void WriteInt(CBlastDbBlob & blob, int value)
{
blob.WriteInt4(value);
}
};
template<class TWriteSize, class TRanges>
void s_WriteRanges(CBlastDbBlob & blob,
int count,
const TRanges & ranges)
{
typedef vector< pair<TSeqPos, TSeqPos> > TPairVector;
Int4 num_written = 0;
TWriteSize::WriteInt(blob, count);
for ( typename TRanges::const_iterator r1 = (ranges).begin(),
r1_end = (ranges).end();
r1 != r1_end;
++r1 ) {
if (r1->offsets.size()) {
num_written ++;
TWriteSize::WriteInt(blob, r1->algorithm_id);
TWriteSize::WriteInt(blob, r1->offsets.size());
ITERATE(TPairVector, r2, r1->offsets) {
TWriteSize::WriteInt(blob, r2->first);
TWriteSize::WriteInt(blob, r2->second);
}
}
}
_ASSERT(num_written == count);
}
#endif
#if ((!defined(NCBI_COMPILER_WORKSHOP) || (NCBI_COMPILER_VERSION > 550)) && \
(!defined(NCBI_COMPILER_MIPSPRO)) )
void CWriteDB_Impl::SetMaskData(const CMaskedRangesVector & ranges,
const vector <TGi> & gis)
{
// No GI is found for the sequence
// TODO should we generate a warning?
if (m_UseGiMask && !gis.size()) {
return;
}
TSeqPos seq_length = x_ComputeSeqLength();
// Check validity of data and determine maximum integer value
// stored here before writing anything. The best numeric_size
// will be selected; this numeric size is applied uniformly to all
// integers in this blob (except for the first one, which is the
// integer size itself, and which is always a single byte.)
typedef vector< pair<TSeqPos, TSeqPos> > TPairVector;
int range_list_count = 0;
int offset_pairs_count = 0;
ITERATE(CMaskedRangesVector, r1, ranges) {
if (r1->empty()) {
continue;
}
range_list_count ++;
offset_pairs_count += r1->offsets.size();
if ( !m_MaskAlgoRegistry.IsRegistered(r1->algorithm_id) ) {
string msg("Error: Algorithm IDs must be registered before use.");
msg += " Unknown algorithm ID = " +
NStr::IntToString((int)r1->algorithm_id);
NCBI_THROW(CWriteDBException, eArgErr, msg);
}
ITERATE(TPairVector, r2, r1->offsets) {
if ((r2->first > r2->second) ||
(r2->second > seq_length)) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Error: Masked data offsets out of bounds.");
}
}
}
// We may be passed an empty list of ranges, or we might be passed
// several ranges whose lists of offsets are themself empty. No
// matter what is passed in, we should not emit empty lists and we
// should not emit any bytes at all if there are no elements.
if (offset_pairs_count == 0) {
return;
}
// Gi-based masks
if (m_UseGiMask) {
ITERATE(CMaskedRangesVector, r1, ranges) {
if (r1->offsets.size()) {
m_GiMasks[m_MaskAlgoMap[r1->algorithm_id]]
->AddGiMask(gis, r1->offsets);
}
}
return;
}
// OID-based masks
const int col_id = x_GetMaskDataColumnId();
CBlastDbBlob & blob = SetBlobData(col_id);
blob.Clear();
blob.WriteInt4(range_list_count);
CBlastDbBlob & blob2 = SetBlobData(col_id);
blob2.Clear();
blob2.WriteInt4(range_list_count);
ITERATE(CMaskedRangesVector, r1, ranges) {
if (r1->offsets.size()) {
blob.WriteInt4(r1->algorithm_id);
blob.WriteInt4(r1->offsets.size());
blob2.WriteInt4(r1->algorithm_id);
blob2.WriteInt4(r1->offsets.size());
ITERATE(TPairVector, r2, r1->offsets) {
blob.WriteInt4(r2->first);
blob.WriteInt4(r2->second);
blob2.WriteInt4_LE(r2->first);
blob2.WriteInt4_LE(r2->second);
}
}
}
blob.WritePadBytes(4, CBlastDbBlob::eSimple);
blob2.WritePadBytes(4, CBlastDbBlob::eSimple);
}
static const string s_EscapeColon(const string &in) {
const char l = 0x1;
return NStr::Replace(in, ":", string(l,1));
}
int CWriteDB_Impl::
RegisterMaskAlgorithm(EBlast_filter_program program,
const string & options,
const string & name)
{
int algorithm_id = m_MaskAlgoRegistry.Add(program, options);
string key = NStr::IntToString(algorithm_id);
string value = NStr::IntToString((int)program) + ":" +
s_EscapeColon(options);
if (m_UseGiMask) {
m_MaskAlgoMap[algorithm_id] = m_GiMasks.size();
m_GiMasks.push_back(CRef<CWriteDB_GiMask>
(new CWriteDB_GiMask(name, value, m_MaxFileSize)));
} else {
m_ColumnMetas[x_GetMaskDataColumnId()][key] = value;
}
return algorithm_id;
}
int CWriteDB_Impl::
RegisterMaskAlgorithm(const string &id,
const string &description,
const string &options)
{
int algorithm_id = m_MaskAlgoRegistry.Add(id);
string key = NStr::IntToString(algorithm_id);
string value = "100:" +
s_EscapeColon(options) + ":" +
s_EscapeColon(id) + ":" +
s_EscapeColon(description);
m_ColumnMetas[x_GetMaskDataColumnId()][key] = value;
return algorithm_id;
}
int CWriteDB_Impl::FindColumn(const string & title) const
{
for(int i = 0; i < (int) m_ColumnTitles.size(); i++) {
if (title == m_ColumnTitles[i]) {
return i;
}
}
return -1;
}
int CWriteDB_Impl::CreateColumn(const string & title, bool mbo)
{
_ASSERT(FindColumn(title) == -1);
size_t col_id = m_Blobs.size() / 2;
_ASSERT(m_HaveBlob.size() == col_id);
_ASSERT(m_ColumnTitles.size() == col_id);
_ASSERT(m_ColumnMetas.size() == col_id);
CRef<CBlastDbBlob> new_blob(new CBlastDbBlob);
CRef<CBlastDbBlob> new_blob2(new CBlastDbBlob);
m_Blobs .push_back(new_blob);
m_Blobs .push_back(new_blob2);
m_HaveBlob .push_back(0);
m_ColumnTitles.push_back(title);
m_ColumnMetas .push_back(TColumnMeta());
if (m_Volume.NotEmpty()) {
size_t id2 = m_Volume->CreateColumn(title, m_ColumnMetas.back(), mbo);
_ASSERT(id2 == col_id);
(void)id2; // get rid of compiler warning
}
return col_id;
}
void CWriteDB_Impl::AddColumnMetaData(int col_id,
const string & key,
const string & value)
{
if ((col_id < 0) || (col_id >= (int) m_ColumnMetas.size())) {
NCBI_THROW(CWriteDBException, eArgErr,
"Error: provided column ID is not valid");
}
m_ColumnMetas[col_id][key] = value;
if (m_Volume.NotEmpty()) {
m_Volume->AddColumnMetaData(col_id, key, value);
}
}
CBlastDbBlob & CWriteDB_Impl::SetBlobData(int col_id)
{
if ((col_id < 0) || (col_id * 2 >= (int) m_Blobs.size())) {
NCBI_THROW(CWriteDBException, eArgErr,
"Error: provided column ID is not valid");
}
if (m_HaveBlob[col_id] > 1) {
NCBI_THROW(CWriteDBException, eArgErr,
"Error: Already have blob for this sequence and column");
}
++m_HaveBlob[col_id];
// Blobs are reused to reduce buffer reallocation; a missing blob
// means the corresponding column does not exist.
return *m_Blobs[col_id * 2 + m_HaveBlob[col_id] - 1];
}
#endif
void CWriteDB_Impl::SetPig(int pig)
{
m_Pig = pig;
}
void CWriteDB_Impl::SetMaxFileSize(Uint8 sz)
{
m_MaxFileSize = sz;
}
void CWriteDB_Impl::SetMaxVolumeLetters(Uint8 sz)
{
m_MaxVolumeLetters = sz;
}
CRef<CBlast_def_line_set>
CWriteDB_Impl::ExtractBioseqDeflines(const CBioseq & bs, bool parse_ids,
bool long_seqids)
{
// Get information
CConstRef<CBlast_def_line_set> deflines;
string binary_header;
vector< vector<int> > v1, v2;
CConstRef<CBioseq> bsref(& bs);
x_ExtractDeflines(bsref, deflines, binary_header, v2, v2, 0, -1, parse_ids,
long_seqids);
// Convert to return type
CRef<CBlast_def_line_set> bdls;
bdls.Reset(const_cast<CBlast_def_line_set*>(&*deflines));
return bdls;
}
void CWriteDB_Impl::SetMaskedLetters(const string & masked)
{
// Only supported for protein.
if (! m_Protein) {
NCBI_THROW(CWriteDBException,
eArgErr,
"Error: Nucleotide masking not supported.");
}
m_MaskedLetters = masked;
if (masked.empty()) {
vector<char> none;
m_MaskLookup.swap(none);
return;
}
// Convert set of masked letters to stdaa, use the result to build
// a lookup table.
string mask_bytes;
CSeqConvert::Convert(m_MaskedLetters,
CSeqUtil::e_Iupacaa,
0,
(int) m_MaskedLetters.size(),
mask_bytes,
CSeqUtil::e_Ncbistdaa);
_ASSERT(mask_bytes.size() == m_MaskedLetters.size());
// Build a table of character-to-bool.
// (Bool is represented by char 0 and 1.)
m_MaskLookup.resize(256, (char)0);
for (unsigned i = 0; i < mask_bytes.size(); i++) {
int ch = ((int) mask_bytes[i]) & 0xFF;
m_MaskLookup[ch] = (char)1;
}
// Convert the masking character - always 'X' - to stdaa.
if (m_MaskByte.empty()) {
string mask_byte = "X";
CSeqConvert::Convert(mask_byte,
CSeqUtil::e_Iupacaa,
0,
1,
m_MaskByte,
CSeqUtil::e_Ncbistdaa);
_ASSERT(m_MaskByte.size() == 1);
}
}
void CWriteDB_Impl::ListVolumes(vector<string> & vols)
{
vols.clear();
ITERATE(vector< CRef<CWriteDB_Volume> >, iter, m_VolumeList) {
vols.push_back((**iter).GetVolumeName());
}
}
void CWriteDB_Impl::ListFiles(vector<string> & files)
{
files.clear();
ITERATE(vector< CRef<CWriteDB_Volume> >, iter, m_VolumeList) {
(**iter).ListFiles(files);
}
if (m_VolumeList.size() > 1) {
files.push_back(x_MakeAliasName());
}
}
/// Compute the hash of a (raw) sequence.
///
/// The hash of the provided sequence will be computed and assigned to
/// the m_Hash field. For protein, the sequence is in the Ncbistdaa
/// format. For nucleotide, the sequence and optional ambiguities are
/// in 'raw' format, meaning they are packed just as sequences are
/// packed in nsq files.
///
/// @param sequence The sequence data. [in]
/// @param ambiguities Nucleotide ambiguities are provided here. [in]
void CWriteDB_Impl::x_ComputeHash(const CTempString & sequence,
const CTempString & ambig)
{
if (m_Protein) {
m_Hash = SeqDB_SequenceHash(sequence.data(), sequence.size());
} else {
string na8;
SeqDB_UnpackAmbiguities(sequence, ambig, na8);
m_Hash = SeqDB_SequenceHash(na8.data(), na8.size());
}
}
/// Compute the hash of a (Bioseq) sequence.
///
/// The hash of the provided sequence will be computed and
/// assigned to the m_Hash member. The sequence is packed as a
/// CBioseq.
///
/// @param sequence The sequence as a CBioseq. [in]
void CWriteDB_Impl::x_ComputeHash(const CBioseq & sequence)
{
m_Hash = SeqDB_SequenceHash(sequence);
}
#define TAB_REPLACEMENT " "
void CWriteDB_Impl::
x_GetFastaReaderDeflines(const CBioseq & bioseq,
CConstRef<CBlast_def_line_set> & deflines,
const vector< vector<int> > & membits,
const vector< vector<int> > & linkout,
int pig,
bool accept_gt,
bool parse_ids,
bool long_seqids)
{
if (! bioseq.CanGetDescr()) {
return;
}
string fasta;
// Scan the CBioseq for the CFastaReader user object.
ITERATE(list< CRef< CSeqdesc > >, iter, bioseq.GetDescr().Get()) {
const CSeqdesc & desc = **iter;
if (desc.IsUser() &&
desc.GetUser().CanGetType() &&
desc.GetUser().GetType().IsStr() &&
desc.GetUser().GetType().GetStr() == "CFastaReader" &&
desc.GetUser().CanGetData()) {
const vector< CRef< CUser_field > > & D = desc.GetUser().GetData();
ITERATE(vector< CRef< CUser_field > >, iter, D) {
const CUser_field & f = **iter;
if (f.CanGetLabel() &&
f.GetLabel().IsStr() &&
f.GetLabel().GetStr() == "DefLine" &&
f.CanGetData() &&
f.GetData().IsStr()) {
fasta = NStr::Replace(f.GetData().GetStr(), "\\t", TAB_REPLACEMENT);
fasta = NStr::ParseEscapes(fasta);
break;
}
}
}
}
if (fasta.empty())
return;
// The bioseq has a field contianing the ids for the first
// defline. The title string contains the title for the first
// defline, plus all the other defline titles and ids. This code
// unpacks them and builds a normal blast defline set.
unsigned mship_i(0), links_i(0);
bool used_pig(false);
// Build the deflines.
CRef<CBlast_def_line_set> bdls(new CBlast_def_line_set);
CRef<CBlast_def_line> defline;
if (!parse_ids) {
// Generate an BL_ORD_ID in case no parse is needed
CRef<CSeq_id> gnl_id(new CSeq_id());
gnl_id->SetGeneral().SetDb("BL_ORD_ID");
gnl_id->SetGeneral().SetTag().SetId(0); // will be filled later
// Build the local defline.
defline.Reset(new CBlast_def_line);
defline->SetSeqid().push_back(gnl_id);
string title(fasta, 1, fasta.size());
// Replace ^A with space
NStr::ReplaceInPlace(title, "\001", " ");
// Replace tabs with three spaces
NStr::ReplaceInPlace(title, "\t", TAB_REPLACEMENT);
defline->SetTitle(title);
if (mship_i < membits.size()) {
const vector<int> & V = membits[mship_i++];
defline->SetMemberships().assign(V.begin(), V.end());
}
if (links_i < linkout.size()) {
const vector<int> & V = linkout[mship_i++];
defline->SetLinks().assign(V.begin(), V.end());
}
if ((! used_pig) && pig) {
defline->SetOther_info().push_back(pig);
used_pig = true;
}
bdls->Set().push_back(defline);
} else {
int skip = 1;
while(fasta.size()) {
size_t id_start = skip;
size_t pos_title = fasta.find(" ", skip);
size_t pos_next = fasta.find("\001", skip);
skip = 1;
if (pos_next == fasta.npos) {
if (accept_gt) {
pos_next = fasta.find(" >");
skip = 2;
}
} else {
// If there is a ^A, turn off GT checking.
accept_gt = false;
}
if (pos_next == fasta.npos) {
pos_next = fasta.size();
skip = 0;
}
if (pos_title == fasta.npos || pos_title >= pos_next) {
// title field is missing
pos_title = pos_next;
}
string ids(fasta, id_start, pos_title - id_start);
if (pos_title == pos_next) pos_title--;
string title(fasta, pos_title + 1, pos_next-pos_title - 1);
string remaining(fasta, pos_next, fasta.size() - pos_next);
fasta.swap(remaining);
// Parse '|' seperated ids.
list< CRef<CSeq_id> > seqids;
if ( (ids.find('|') == NPOS && long_seqids)
|| !isalpha((unsigned char)(ids[0]))) {
seqids.push_back(CRef<CSeq_id> (new CSeq_id(CSeq_id::e_Local, ids)));
} else {
CSeq_id::ParseFastaIds(seqids, ids);
if (!long_seqids) {
// If accession's molecule type is different than
// expected, change sequence id to local. CFastaReader
// cannot distingush between bare pir protein ids genbank
// nucleotide ids.
for (auto& it: seqids) {
CSeq_id::EAccessionInfo info = it->IdentifyAccession();
if (!it->IsLocal() && !it->IsGi() &&
(info & (CSeq_id::fAcc_prot | CSeq_id::fAcc_nuc)) &&
bioseq.IsAa() == !!(info & CSeq_id::fAcc_nuc)) {
string label = it->GetSeqIdString(true);
it.Reset(new CSeq_id(CSeq_id::e_Local, label));
}
}
}
}
// Build the actual defline.
defline.Reset(new CBlast_def_line);
defline->SetSeqid().swap(seqids);
defline->SetTitle(title);
if (mship_i < membits.size()) {
const vector<int> & V = membits[mship_i++];
defline->SetMemberships().assign(V.begin(), V.end());
}
if (links_i < linkout.size()) {
const vector<int> & V = linkout[mship_i++];
defline->SetLinks().assign(V.begin(), V.end());
}
if ((! used_pig) && pig) {
defline->SetOther_info().push_back(pig);
used_pig = true;
}
bdls->Set().push_back(defline);
}
}
s_CheckEmptyLists(bdls, true);
deflines = bdls;
}
#if ((!defined(NCBI_COMPILER_WORKSHOP) || (NCBI_COMPILER_VERSION > 550)) && \
(!defined(NCBI_COMPILER_MIPSPRO)) )
int CWriteDB_Impl::x_GetMaskDataColumnId()
{
if (m_MaskDataColumn == -1) {
m_MaskDataColumn = CreateColumn("BlastDb/MaskData", true);
}
return m_MaskDataColumn;
}
#endif
END_NCBI_SCOPE
|