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 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
|
/*******************************************************************************************
*
* Compressed data base module. Auxiliary routines to open and manipulate a data base for
* which the sequence and read information are separated into two separate files, and the
* sequence is compressed into 2-bits for each base. Support for tracks of additional
* information, and trimming according to the current partition.
*
* Author : Gene Myers
* Date : July 2013
* Revised: April 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <dirent.h>
#include "DB.h"
#ifdef HIDE_FILES
#define PATHSEP "/."
#else
#define PATHSEP "/"
#endif
/*******************************************************************************************
*
* GENERAL UTILITIES
*
********************************************************************************************/
char *Prog_Name;
#ifdef INTERACTIVE
char Ebuffer[1000];
#endif
void *Malloc(int64 size, char *mesg)
{ void *p;
if ((p = malloc(size)) == NULL)
{ if (mesg == NULL)
EPRINTF(EPLACE,"%s: Out of memory\n",Prog_Name);
else
EPRINTF(EPLACE,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (p);
}
void *Realloc(void *p, int64 size, char *mesg)
{ if (size <= 0)
size = 1;
if ((p = realloc(p,size)) == NULL)
{ if (mesg == NULL)
EPRINTF(EPLACE,"%s: Out of memory\n",Prog_Name);
else
EPRINTF(EPLACE,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (p);
}
char *Strdup(char *name, char *mesg)
{ char *s;
if (name == NULL)
return (NULL);
if ((s = strdup(name)) == NULL)
{ if (mesg == NULL)
EPRINTF(EPLACE,"%s: Out of memory\n",Prog_Name);
else
EPRINTF(EPLACE,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (s);
}
FILE *Fopen(char *name, char *mode)
{ FILE *f;
if (name == NULL || mode == NULL)
return (NULL);
if ((f = fopen(name,mode)) == NULL)
EPRINTF(EPLACE,"%s: Cannot open %s for '%s'\n",Prog_Name,name,mode);
return (f);
}
char *PathTo(char *name)
{ char *path, *find;
if (name == NULL)
return (NULL);
if ((find = rindex(name,'/')) != NULL)
{ *find = '\0';
path = Strdup(name,"Extracting path from");
*find = '/';
}
else
path = Strdup(".","Allocating default path");
return (path);
}
char *Root(char *name, char *suffix)
{ char *path, *find, *dot;
int epos;
if (name == NULL)
return (NULL);
find = rindex(name,'/');
if (find == NULL)
find = name;
else
find += 1;
if (suffix == NULL)
{ dot = strchr(find,'.');
if (dot != NULL)
*dot = '\0';
path = Strdup(find,"Extracting root from");
if (dot != NULL)
*dot = '.';
}
else
{ epos = strlen(find);
epos -= strlen(suffix);
if (epos > 0 && strcasecmp(find+epos,suffix) == 0)
{ find[epos] = '\0';
path = Strdup(find,"Extracting root from");
find[epos] = suffix[0];
}
else
path = Strdup(find,"Allocating root");
}
return (path);
}
char *Catenate(char *path, char *sep, char *root, char *suffix)
{ static char *cat = NULL;
static int max = -1;
int len;
if (path == NULL || root == NULL || sep == NULL || suffix == NULL)
return (NULL);
len = strlen(path);
len += strlen(sep);
len += strlen(root);
len += strlen(suffix);
if (len > max)
{ max = ((int) (1.2*len)) + 100;
if ((cat = (char *) realloc(cat,max+1)) == NULL)
{ EPRINTF(EPLACE,"%s: Out of memory (Making path name for %s)\n",Prog_Name,root);
return (NULL);
}
}
sprintf(cat,"%s%s%s%s",path,sep,root,suffix);
return (cat);
}
char *Numbered_Suffix(char *left, int num, char *right)
{ static char *suffix = NULL;
static int max = -1;
int len;
if (left == NULL || right == NULL)
return (NULL);
len = strlen(left);
len += strlen(right) + 40;
if (len > max)
{ max = ((int) (1.2*len)) + 100;
if ((suffix = (char *) realloc(suffix,max+1)) == NULL)
{ EPRINTF(EPLACE,"%s: Out of memory (Making number suffix for %d)\n",Prog_Name,num);
return (NULL);
}
}
sprintf(suffix,"%s%d%s",left,num,right);
return (suffix);
}
#define COMMA ','
// Print big integers with commas/periods for better readability
void Print_Number(int64 num, int width, FILE *out)
{ if (width == 0)
{ if (num < 1000ll)
fprintf(out,"%lld",num);
else if (num < 1000000ll)
fprintf(out,"%lld%c%03lld",num/1000ll,COMMA,num%1000ll);
else if (num < 1000000000ll)
fprintf(out,"%lld%c%03lld%c%03lld",num/1000000ll,
COMMA,(num%1000000ll)/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%lld%c%03lld%c%03lld%c%03lld",num/1000000000ll,
COMMA,(num%1000000000ll)/1000000ll,
COMMA,(num%1000000ll)/1000ll,COMMA,num%1000ll);
}
else
{ if (num < 1000ll)
fprintf(out,"%*lld",width,num);
else if (num < 1000000ll)
{ if (width <= 4)
fprintf(out,"%lld%c%03lld",num/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld",width-4,num/1000ll,COMMA,num%1000ll);
}
else if (num < 1000000000ll)
{ if (width <= 8)
fprintf(out,"%lld%c%03lld%c%03lld",num/1000000ll,COMMA,(num%1000000ll)/1000ll,
COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld%c%03lld",width-8,num/1000000ll,COMMA,(num%1000000ll)/1000ll,
COMMA,num%1000ll);
}
else
{ if (width <= 12)
fprintf(out,"%lld%c%03lld%c%03lld%c%03lld",num/1000000000ll,COMMA,
(num%1000000000ll)/1000000ll,COMMA,
(num%1000000ll)/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld%c%03lld%c%03lld",width-12,num/1000000000ll,COMMA,
(num%1000000000ll)/1000000ll,COMMA,
(num%1000000ll)/1000ll,COMMA,num%1000ll);
}
}
}
// Return the number of digits, base 10, of num
int Number_Digits(int64 num)
{ int digit;
digit = 0;
while (num >= 1)
{ num /= 10;
digit += 1;
}
return (digit);
}
/*******************************************************************************************
*
* READ COMPRESSION/DECOMPRESSION UTILITIES
*
********************************************************************************************/
// Compress read into 2-bits per base (from [0-3] per byte representation
void Compress_Read(int len, char *s)
{ int i;
char c, d;
char *s0, *s1, *s2, *s3;
s0 = s;
s1 = s0+1;
s2 = s1+1;
s3 = s2+1;
c = s1[len];
d = s2[len];
s0[len] = s1[len] = s2[len] = 0;
for (i = 0; i < len; i += 4)
*s++ = (char ) ((s0[i] << 6) | (s1[i] << 4) | (s2[i] << 2) | s3[i]);
s1[len] = c;
s2[len] = d;
}
// Uncompress read form 2-bits per base into [0-3] per byte representation
void Uncompress_Read(int len, char *s)
{ int i, tlen, byte;
char *s0, *s1, *s2, *s3;
char *t;
s0 = s;
s1 = s0+1;
s2 = s1+1;
s3 = s2+1;
tlen = (len-1)/4;
t = s+tlen;
for (i = tlen*4; i >= 0; i -= 4)
{ byte = *t--;
s0[i] = (char) ((byte >> 6) & 0x3);
s1[i] = (char) ((byte >> 4) & 0x3);
s2[i] = (char) ((byte >> 2) & 0x3);
s3[i] = (char) (byte & 0x3);
}
s[len] = 4;
}
// Convert read in [0-3] representation to ascii representation (end with '\n')
void Lower_Read(char *s)
{ static char letter[4] = { 'a', 'c', 'g', 't' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
void Upper_Read(char *s)
{ static char letter[4] = { 'A', 'C', 'G', 'T' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
void Letter_Arrow(char *s)
{ static char letter[4] = { '1', '2', '3', '4' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
// Convert read in ascii representation to [0-3] representation (end with 4)
void Number_Read(char *s)
{ static char number[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
for ( ; *s != '\0'; s++)
*s = number[(int) *s];
*s = 4;
}
void Number_Arrow(char *s)
{ static char arrow[128] =
{ 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 0, 1, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 2,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
};
for ( ; *s != '\0'; s++)
*s = arrow[(int) *s];
*s = 4;
}
/*******************************************************************************************
*
* DB OPEN, TRIM & CLOSE ROUTINES
*
********************************************************************************************/
// Open the given database or dam, "path" into the supplied HITS_DB record "db". If the name has
// a part # in it then just the part is opened. The index array is allocated (for all or
// just the part) and read in.
// Return status of routine:
// -1: The DB could not be opened for a reason reported by the routine to EPLACE
// 0: Open of DB proceeded without mishap
// 1: Open of DAM proceeded without mishap
int Open_DB(char* path, HITS_DB *db)
{ HITS_DB dbcopy;
char *root, *pwd, *bptr, *fptr, *cat;
int nreads;
FILE *index, *dbvis;
int status, plen, isdam;
int part, cutoff, all;
int ufirst, tfirst, ulast, tlast;
status = -1;
dbcopy = *db;
plen = strlen(path);
if (strcmp(path+(plen-4),".dam") == 0)
root = Root(path,".dam");
else
root = Root(path,".db");
pwd = PathTo(path);
bptr = rindex(root,'.');
if (bptr != NULL && bptr[1] != '\0' && bptr[1] != '-')
{ part = strtol(bptr+1,&fptr,10);
if (*fptr != '\0' || part == 0)
part = 0;
else
*bptr = '\0';
}
else
part = 0;
isdam = 0;
cat = Catenate(pwd,"/",root,".db");
if (cat == NULL)
return (-1);
if ((dbvis = fopen(cat,"r")) == NULL)
{ cat = Catenate(pwd,"/",root,".dam");
if (cat == NULL)
return (-1);
if ((dbvis = fopen(cat,"r")) == NULL)
{ EPRINTF(EPLACE,"%s: Could not open database %s\n",Prog_Name,path);
goto error;
}
isdam = 1;
}
if ((index = Fopen(Catenate(pwd,PATHSEP,root,".idx"),"r")) == NULL)
goto error1;
if (fread(db,sizeof(HITS_DB),1,index) != 1)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
goto error2;
}
{ int p, nblocks, nfiles;
int64 size;
char fname[MAX_NAME], prolog[MAX_NAME];
nblocks = 0;
if (fscanf(dbvis,DB_NFILE,&nfiles) != 1)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
for (p = 0; p < nfiles; p++)
if (fscanf(dbvis,DB_FDATA,&tlast,fname,prolog) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
if (fscanf(dbvis,DB_NBLOCK,&nblocks) != 1)
if (part == 0)
{ cutoff = 0;
all = DB_ALL;
}
else
{ EPRINTF(EPLACE,"%s: DB %s has not yet been partitioned, cannot request a block !\n",
Prog_Name,root);
goto error2;
}
else
{ if (fscanf(dbvis,DB_PARAMS,&size,&cutoff,&all) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
if (part > nblocks)
{ EPRINTF(EPLACE,"%s: DB %s has only %d blocks\n",Prog_Name,root,nblocks);
goto error2;
}
}
if (part > 0)
{ for (p = 1; p <= part; p++)
if (fscanf(dbvis,DB_BDATA,&ufirst,&tfirst) != 2)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
if (fscanf(dbvis,DB_BDATA,&ulast,&tlast) != 2)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
}
else
{ ufirst = tfirst = 0;
ulast = db->ureads;
tlast = db->treads;
}
}
db->trimmed = 0;
db->tracks = NULL;
db->part = part;
db->cutoff = cutoff;
db->allarr |= all;
db->ufirst = ufirst;
db->tfirst = tfirst;
nreads = ulast-ufirst;
if (part <= 0)
{ db->reads = (HITS_READ *) Malloc(sizeof(HITS_READ)*(nreads+2),"Allocating Open_DB index");
if (db->reads == NULL)
goto error2;
db->reads += 1;
if (fread(db->reads,sizeof(HITS_READ),nreads,index) != (size_t) nreads)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
free(db->reads-1);
goto error2;
}
}
else
{ HITS_READ *reads;
int i, r, maxlen;
int64 totlen;
reads = (HITS_READ *) Malloc(sizeof(HITS_READ)*(nreads+2),"Allocating Open_DB index");
if (reads == NULL)
goto error2;
reads += 1;
fseeko(index,sizeof(HITS_READ)*ufirst,SEEK_CUR);
if (fread(reads,sizeof(HITS_READ),nreads,index) != (size_t) nreads)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
free(reads-1);
goto error2;
}
totlen = 0;
maxlen = 0;
for (i = 0; i < nreads; i++)
{ r = reads[i].rlen;
totlen += r;
if (r > maxlen)
maxlen = r;
}
db->maxlen = maxlen;
db->totlen = totlen;
db->reads = reads;
}
((int *) (db->reads))[-1] = ulast - ufirst; // Kludge, need these for DB part
((int *) (db->reads))[-2] = tlast - tfirst;
db->nreads = nreads;
db->path = Strdup(Catenate(pwd,PATHSEP,root,""),"Allocating Open_DB path");
if (db->path == NULL)
goto error2;
db->bases = NULL;
db->loaded = 0;
status = isdam;
error2:
fclose(index);
error1:
fclose(dbvis);
error:
if (bptr != NULL)
*bptr = '.';
free(pwd);
free(root);
if (status < 0)
*db = dbcopy;
return (status);
}
// Trim the DB or part thereof and all loaded tracks according to the cuttof and all settings
// of the current DB partition. Reallocate smaller memory blocks for the information kept
// for the retained reads.
void Trim_DB(HITS_DB *db)
{ int i, j, r;
int allflag, cutoff;
int64 totlen;
int maxlen, nreads;
HITS_TRACK *record;
HITS_READ *reads;
if (db->trimmed) return;
if (db->cutoff <= 0 && (db->allarr & DB_ALL) != 0) return;
cutoff = db->cutoff;
if ((db->allarr & DB_ALL) != 0)
allflag = 0;
else
allflag = DB_BEST;
reads = db->reads;
nreads = db->nreads;
for (record = db->tracks; record != NULL; record = record->next)
if (strcmp(record->name,".@qvs") == 0)
{ uint16 *table = ((HITS_QV *) record)->table;
j = 0;
for (i = 0; i < db->nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
table[j++] = table[i];
}
else
{ int *anno4, size;
int64 *anno8;
char *anno, *data;
size = record->size;
data = (char *) record->data;
if (data == NULL)
{ anno = (char *) record->anno;
j = 0;
for (i = r = 0; i < db->nreads; i++, r += size)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
{ memmove(anno+j,anno+r,size);
j += size;
}
memmove(anno+j,anno+r,size);
}
else if (size == 4)
{ int ai;
anno4 = (int *) (record->anno);
j = anno4[0] = 0;
for (i = 0; i < db->nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
{ ai = anno4[i];
anno4[j+1] = anno4[j] + (anno4[i+1]-ai);
memmove(data+anno4[j],data+ai,anno4[i+1]-ai);
j += 1;
}
record->data = Realloc(record->data,anno4[j],NULL);
}
else // size == 8
{ int64 ai;
anno8 = (int64 *) (record->anno);
j = anno8[0] = 0;
for (i = 0; i < db->nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
{ ai = anno8[i];
anno8[j+1] = anno8[j] + (anno8[i+1]-ai);
memmove(data+anno8[j],data+ai,anno8[i+1]-ai);
j += 1;
}
record->data = Realloc(record->data,anno8[j],NULL);
}
record->anno = Realloc(record->anno,record->size*(j+1),NULL);
}
totlen = maxlen = 0;
for (j = i = 0; i < nreads; i++)
{ r = reads[i].rlen;
if ((reads[i].flags & DB_BEST) >= allflag && r >= cutoff)
{ totlen += r;
if (r > maxlen)
maxlen = r;
reads[j++] = reads[i];
}
}
db->totlen = totlen;
db->maxlen = maxlen;
db->nreads = j;
db->trimmed = 1;
if (j < nreads)
{ db->reads = Realloc(reads-1,sizeof(HITS_READ)*(j+2),NULL);
db->reads += 1;
}
}
// The DB has already been trimmed, but a track over the untrimmed DB needs to be loaded.
// Trim the track by rereading the untrimmed DB index from the file system.
static int Late_Track_Trim(HITS_DB *db, HITS_TRACK *track, int ispart)
{ int i, j, r;
int allflag, cutoff;
int ureads;
char *root;
HITS_READ read;
FILE *indx;
if (!db->trimmed) return (0);
if (db->cutoff <= 0 && (db->allarr & DB_ALL) != 0) return (0);
cutoff = db->cutoff;
if ((db->allarr & DB_ALL) != 0)
allflag = 0;
else
allflag = DB_BEST;
root = rindex(db->path,'/') + 2;
indx = Fopen(Catenate(db->path,"","",".idx"),"r");
fseeko(indx,sizeof(HITS_DB) + sizeof(HITS_READ)*db->ufirst,SEEK_SET);
if (ispart)
ureads = ((int *) (db->reads))[-1];
else
ureads = db->ureads;
if (strcmp(track->name,".@qvs") == 0)
{ EPRINTF(EPLACE,"%s: Cannot load QV track after trimming\n",Prog_Name);
fclose(indx);
EXIT(1);
}
{ int *anno4, size;
int64 *anno8;
char *anno, *data;
size = track->size;
data = (char *) track->data;
if (data == NULL)
{ anno = (char *) track->anno;
j = r = 0;
for (i = r = 0; i < ureads; i++, r += size)
{ if (fread(&read,sizeof(HITS_READ),1,indx) != 1)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
fclose(indx);
EXIT(1);
}
if ((read.flags & DB_BEST) >= allflag && read.rlen >= cutoff)
{ memmove(anno+j,anno+r,size);
j += size;
}
r += size;
}
memmove(anno+j,anno+r,size);
}
else if (size == 4)
{ int ai;
anno4 = (int *) (track->anno);
j = anno4[0] = 0;
for (i = 0; i < ureads; i++)
{ if (fread(&read,sizeof(HITS_READ),1,indx) != 1)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
fclose(indx);
EXIT(1);
}
if ((read.flags & DB_BEST) >= allflag && read.rlen >= cutoff)
{ ai = anno4[i];
anno4[j+1] = anno4[j] + (anno4[i+1]-ai);
memmove(data+anno4[j],data+ai,anno4[i+1]-ai);
j += 1;
}
}
track->data = Realloc(track->data,anno4[j],NULL);
}
else // size == 8
{ int64 ai;
anno8 = (int64 *) (track->anno);
j = anno8[0] = 0;
for (i = 0; i < ureads; i++)
{ if (fread(&read,sizeof(HITS_READ),1,indx) != 1)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
fclose(indx);
EXIT(1);
}
if ((read.flags & DB_BEST) >= allflag && read.rlen >= cutoff)
{ ai = anno8[i];
anno8[j+1] = anno8[j] + (anno8[i+1]-ai);
memmove(data+anno8[j],data+ai,anno8[i+1]-ai);
j += 1;
}
}
track->data = Realloc(track->data,anno8[j],NULL);
}
track->anno = Realloc(track->anno,track->size*(j+1),NULL);
}
fclose(indx);
return (0);
}
// Shut down an open 'db' by freeing all associated space, including tracks and QV structures,
// and any open file pointers. The record pointed at by db however remains (the user
// supplied it and so should free it).
void Close_DB(HITS_DB *db)
{ HITS_TRACK *t, *p;
if (db->loaded)
free(((char *) (db->bases)) - 1);
else if (db->bases != NULL)
fclose((FILE *) db->bases);
if (db->reads != NULL)
free(db->reads-1);
free(db->path);
Close_QVs(db);
for (t = db->tracks; t != NULL; t = p)
{ p = t->next;
free(t->anno);
free(t->data);
free(t);
}
}
// Return the size in bytes of the memory occupied by a given DB
int64 sizeof_DB(HITS_DB *db)
{ int64 s;
HITS_TRACK *t;
s = sizeof(HITS_DB)
+ sizeof(HITS_READ)*(db->nreads+2)
+ strlen(db->path)+1
+ (db->totlen+db->nreads+4);
t = db->tracks;
if (t != NULL && strcmp(t->name,".@qvs") == 0)
{ HITS_QV *q = (HITS_QV *) t;
s += sizeof(HITS_QV)
+ sizeof(uint16) * db->nreads
+ q->ncodes * sizeof(QVcoding)
+ 6;
t = t->next;
}
for (; t != NULL; t = t->next)
{ s += sizeof(HITS_TRACK)
+ strlen(t->name)+1
+ t->size * (db->nreads+1);
if (t->data != NULL)
{ if (t->size == 8)
s += sizeof(int)*((int64 *) t->anno)[db->nreads];
else // t->size == 4
s += sizeof(int)*((int *) t->anno)[db->nreads];
}
}
return (s);
}
/*******************************************************************************************
*
* QV LOAD & CLOSE ROUTINES
*
********************************************************************************************/
HITS_DB *Active_DB = NULL; // Last db/qv used by "Load_QVentry"
HITS_QV *Active_QV; // Becomes invalid after closing
int Load_QVs(HITS_DB *db)
{ FILE *quiva, *istub, *indx;
char *root;
uint16 *table;
HITS_QV *qvtrk;
QVcoding *coding, *nx;
int ncodes = 0;
if (db->tracks != NULL && strcmp(db->tracks->name,".@qvs") == 0)
return (0);
if (db->trimmed)
{ EPRINTF(EPLACE,"%s: Cannot load QVs after trimming the DB\n",Prog_Name);
EXIT(1);
}
if (db->reads[db->nreads-1].coff < 0)
{ if (db->part > 0)
{ EPRINTF(EPLACE,"%s: All QVs for this block have not been added to the DB!\n",Prog_Name);
EXIT(1);
}
else
{ EPRINTF(EPLACE,"%s: All QVs for this DB have not been added!\n",Prog_Name);
EXIT(1);
}
}
// Open .qvs, .idx, and .db files
quiva = Fopen(Catenate(db->path,"","",".qvs"),"r");
if (quiva == NULL)
return (-1);
istub = NULL;
indx = NULL;
table = NULL;
coding = NULL;
qvtrk = NULL;
root = rindex(db->path,'/');
if (root[1] == '.')
{ *root = '\0';
istub = Fopen(Catenate(db->path,"/",root+2,".db"),"r");
*root = '/';
}
else
istub = Fopen(Catenate(db->path,"","",".db"),"r");
if (istub == NULL)
goto error;
{ int first, last, nfiles;
char prolog[MAX_NAME], fname[MAX_NAME];
int i, j;
if (fscanf(istub,DB_NFILE,&nfiles) != 1)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error;
}
if (db->part > 0)
{ int pfirst, plast;
int fbeg, fend;
int n, k;
FILE *indx;
// Determine first how many and which files span the block (fbeg to fend)
pfirst = db->ufirst;
plast = pfirst + db->nreads;
first = 0;
for (fbeg = 0; fbeg < nfiles; fbeg++)
{ if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error;
}
if (last > pfirst)
break;
first = last;
}
for (fend = fbeg+1; fend <= nfiles; fend++)
{ if (last >= plast)
break;
if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error;
}
first = last;
}
indx = Fopen(Catenate(db->path,"","",".idx"),"r");
ncodes = fend-fbeg;
coding = (QVcoding *) Malloc(sizeof(QVcoding)*ncodes,"Allocating coding schemes");
table = (uint16 *) Malloc(sizeof(uint16)*db->nreads,"Allocating QV table indices");
if (indx == NULL || coding == NULL || table == NULL)
{ ncodes = 0;
goto error;
}
// Carefully get the first coding scheme (its offset is most likely in a HITS_RECORD
// in .idx that is *not* in memory). Get all the other coding schemes normally and
// assign the tables # for each read in the block in "tables".
rewind(istub);
(void) fscanf(istub,DB_NFILE,&nfiles);
first = 0;
for (n = 0; n < fbeg; n++)
{ (void) fscanf(istub,DB_FDATA,&last,fname,prolog);
first = last;
}
for (n = fbeg; n < fend; n++)
{ (void) fscanf(istub,DB_FDATA,&last,fname,prolog);
i = n-fbeg;
if (first < pfirst)
{ HITS_READ read;
fseeko(indx,sizeof(HITS_DB) + sizeof(HITS_READ)*first,SEEK_SET);
if (fread(&read,sizeof(HITS_READ),1,indx) != 1)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
ncodes = i;
goto error;
}
fseeko(quiva,read.coff,SEEK_SET);
nx = Read_QVcoding(quiva);
if (nx == NULL)
{ ncodes = i;
goto error;
}
coding[i] = *nx;
}
else
{ fseeko(quiva,db->reads[first-pfirst].coff,SEEK_SET);
nx = Read_QVcoding(quiva);
if (nx == NULL)
{ ncodes = i;
goto error;
}
coding[i] = *nx;
db->reads[first-pfirst].coff = ftello(quiva);
}
j = first-pfirst;
if (j < 0)
j = 0;
k = last-pfirst;
if (k > db->nreads)
k = db->nreads;
while (j < k)
table[j++] = (uint16) i;
first = last;
}
fclose(indx);
indx = NULL;
}
else
{ // Load in coding scheme for each file, adjust .coff of first read in the file, and
// record which table each read uses
ncodes = nfiles;
coding = (QVcoding *) Malloc(sizeof(QVcoding)*nfiles,"Allocating coding schemes");
table = (uint16 *) Malloc(sizeof(uint16)*db->nreads,"Allocating QV table indices");
if (coding == NULL || table == NULL)
goto error;
first = 0;
for (i = 0; i < nfiles; i++)
{ if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error;
}
fseeko(quiva,db->reads[first].coff,SEEK_SET);
nx = Read_QVcoding(quiva);
if (nx == NULL)
{ ncodes = i;
goto error;
}
coding[i] = *nx;
db->reads[first].coff = ftello(quiva);
for (j = first; j < last; j++)
table[j] = (uint16) i;
first = last;
}
}
// Allocate and fill in the HITS_QV record and add it to the front of the
// track list
qvtrk = (HITS_QV *) Malloc(sizeof(HITS_QV),"Allocating QV pseudo-track");
if (qvtrk == NULL)
goto error;
qvtrk->name = Strdup(".@qvs","Allocating QV pseudo-track name");
if (qvtrk->name == NULL)
goto error;
qvtrk->next = db->tracks;
db->tracks = (HITS_TRACK *) qvtrk;
qvtrk->ncodes = ncodes;
qvtrk->table = table;
qvtrk->coding = coding;
qvtrk->quiva = quiva;
}
fclose(istub);
return (0);
error:
if (qvtrk != NULL)
free(qvtrk);
if (table != NULL)
free(table);
if (coding != NULL)
{ int i;
for (i = 0; i < ncodes; i++)
Free_QVcoding(coding+i);
free(coding);
}
if (indx != NULL)
fclose(indx);
if (istub != NULL)
fclose(istub);
fclose(quiva);
EXIT(1);
}
// Close the QV stream, free the QV pseudo track and all associated memory
void Close_QVs(HITS_DB *db)
{ HITS_TRACK *track;
HITS_QV *qvtrk;
int i;
Active_DB = NULL;
track = db->tracks;
if (track != NULL && strcmp(track->name,".@qvs") == 0)
{ qvtrk = (HITS_QV *) track;
for (i = 0; i < qvtrk->ncodes; i++)
Free_QVcoding(qvtrk->coding+i);
free(qvtrk->coding);
free(qvtrk->table);
fclose(qvtrk->quiva);
db->tracks = track->next;
free(track);
}
return;
}
/*******************************************************************************************
*
* TRACK LOAD & CLOSE ROUTINES
*
********************************************************************************************/
// Return status of track:
// 1: Track is for trimmed DB
// 0: Track is for untrimmed DB
// -1: Track is not the right size of DB either trimmed or untrimmed
// -2: Could not find the track
int Check_Track(HITS_DB *db, char *track, int *kind)
{ FILE *afile;
int tracklen, size, ispart;
int ureads, treads;
afile = NULL;
if (db->part > 0)
{ afile = fopen(Catenate(db->path,Numbered_Suffix(".",db->part,"."),track,".anno"),"r");
ispart = 1;
}
if (afile == NULL)
{ afile = fopen(Catenate(db->path,".",track,".anno"),"r");
ispart = 0;
}
if (afile == NULL)
return (-2);
if (fread(&tracklen,sizeof(int),1,afile) != 1)
{ fprintf(stderr,"%s: track files for %s are corrupted\n",Prog_Name,track);
exit (1);
}
if (fread(&size,sizeof(int),1,afile) != 1)
{ fprintf(stderr,"%s: track files for %s are corrupted\n",Prog_Name,track);
exit (1);
}
if (size == 0)
*kind = MASK_TRACK;
else if (size > 0)
*kind = CUSTOM_TRACK;
else
{ fprintf(stderr,"%s: track files for %s are corrupted\n",Prog_Name,track);
exit (1);
}
fclose(afile);
if (ispart)
{ ureads = ((int *) (db->reads))[-1];
treads = ((int *) (db->reads))[-2];
}
else
{ ureads = db->ureads;
treads = db->treads;
}
if (tracklen == ureads)
return (0);
else if (tracklen == treads)
return (1);
else
return (-1);
}
// If track is not already in the db's track list, then allocate all the storage for it,
// read it in from the appropriate file, add it to the track list, and return a pointer
// to the newly created HITS_TRACK record. If the track does not exist or cannot be
// opened for some reason, then NULL is returned.
HITS_TRACK *Load_Track(HITS_DB *db, char *track)
{ FILE *afile, *dfile;
int tracklen, size;
int nreads, ispart;
int treads, ureads;
void *anno;
void *data;
char *name;
HITS_TRACK *record;
if (track[0] == '.')
{ EPRINTF(EPLACE,"%s: Track name, '%s', cannot begin with a .\n",Prog_Name,track);
EXIT(NULL);
}
for (record = db->tracks; record != NULL; record = record->next)
if (strcmp(record->name,track) == 0)
return (record);
afile = NULL;
if (db->part)
{ afile = fopen(Catenate(db->path,Numbered_Suffix(".",db->part,"."),track,".anno"),"r");
ispart = 1;
}
if (afile == NULL)
{ afile = fopen(Catenate(db->path,".",track,".anno"),"r");
ispart = 0;
}
if (afile == NULL)
{ EPRINTF(EPLACE,"%s: Track '%s' does not exist\n",Prog_Name,track);
return (NULL);
}
dfile = NULL;
anno = NULL;
data = NULL;
record = NULL;
if (ispart)
name = Catenate(db->path,Numbered_Suffix(".",db->part,"."),track,".data");
else
name = Catenate(db->path,".",track,".data");
if (name == NULL)
goto error;
dfile = fopen(name,"r");
if (fread(&tracklen,sizeof(int),1,afile) != 1)
{ EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
goto error;
}
if (fread(&size,sizeof(int),1,afile) != 1)
{ EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
goto error;
}
if (size < 0)
{ EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
goto error;
}
if (size == 0)
size = 8;
if (ispart)
{ ureads = ((int *) (db->reads))[-1];
treads = ((int *) (db->reads))[-2];
}
else
{ ureads = db->ureads;
treads = db->treads;
}
if (db->trimmed)
{ if (tracklen != treads && tracklen != ureads)
{ EPRINTF(EPLACE,"%s: Track '%s' not same size as database !\n",Prog_Name,track);
goto error;
}
if ( ! ispart && db->part > 0)
{ if (tracklen == treads)
fseeko(afile,size*db->tfirst,SEEK_CUR);
else
fseeko(afile,size*db->ufirst,SEEK_CUR);
}
}
else
{ if (tracklen != ureads)
{ if (tracklen == treads)
EPRINTF(EPLACE,"%s: Track '%s' is for a trimmed DB !\n",Prog_Name,track);
else
EPRINTF(EPLACE,"%s: Track '%s' not same size as database !\n",Prog_Name,track);
goto error;
}
if ( ! ispart && db->part > 0)
fseeko(afile,size*db->ufirst,SEEK_CUR);
}
if (tracklen == treads)
nreads = ((int *) (db->reads))[-2];
else
nreads = ((int *) (db->reads))[-1];
anno = (void *) Malloc(size*(nreads+1),"Allocating Track Anno Vector");
if (anno == NULL)
goto error;
if (dfile != NULL)
{ int64 *anno8, off8, dlen;
int *anno4, off4;
int i;
if (fread(anno,size,nreads+1,afile) != (size_t) (nreads+1))
{ EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
goto error;
}
if (size == 4)
{ anno4 = (int *) anno;
off4 = anno4[0];
if (off4 != 0)
{ for (i = 0; i <= nreads; i++)
anno4[i] -= off4;
fseeko(dfile,off4,SEEK_SET);
}
dlen = anno4[nreads];
data = (void *) Malloc(dlen,"Allocating Track Data Vector");
}
else
{ anno8 = (int64 *) anno;
off8 = anno8[0];
if (off8 != 0)
{ for (i = 0; i <= nreads; i++)
anno8[i] -= off8;
fseeko(dfile,off8,SEEK_SET);
}
dlen = anno8[nreads];
data = (void *) Malloc(dlen,"Allocating Track Data Vector");
}
if (data == NULL)
goto error;
if (dlen > 0)
{ if (fread(data,dlen,1,dfile) != 1)
{ EPRINTF(EPLACE,"%s: Track '%s' data file is junk\n",Prog_Name,track);
goto error;
}
}
fclose(dfile);
dfile = NULL;
}
else
{ if (fread(anno,size,nreads,afile) != (size_t) nreads)
{ EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
goto error;
}
data = NULL;
}
fclose(afile);
record = (HITS_TRACK *) Malloc(sizeof(HITS_TRACK),"Allocating Track Record");
if (record == NULL)
goto error;
record->name = Strdup(track,"Allocating Track Name");
if (record->name == NULL)
goto error;
record->data = data;
record->anno = anno;
record->size = size;
if (db->trimmed && tracklen != treads)
{ if (Late_Track_Trim(db,record,ispart))
goto error;
}
if (db->tracks != NULL && strcmp(db->tracks->name,".@qvs") == 0)
{ record->next = db->tracks->next;
db->tracks->next = record;
}
else
{ record->next = db->tracks;
db->tracks = record;
}
return (record);
error:
if (record != NULL)
free(record);
if (data != NULL)
free(data);
if (anno != NULL)
free(anno);
if (dfile != NULL)
fclose(dfile);
fclose(afile);
EXIT (NULL);
}
void Close_Track(HITS_DB *db, char *track)
{ HITS_TRACK *record, *prev;
prev = NULL;
for (record = db->tracks; record != NULL; record = record->next)
{ if (strcmp(record->name,track) == 0)
{ free(record->anno);
free(record->data);
free(record->name);
if (prev == NULL)
db->tracks = record->next;
else
prev->next = record->next;
free(record);
return;
}
prev = record;
}
return;
}
/*******************************************************************************************
*
* READ BUFFER ALLOCATION AND READ ACCESS
*
********************************************************************************************/
// Allocate and return a buffer big enough for the largest read in 'db', leaving room
// for an initial delimiter character
char *New_Read_Buffer(HITS_DB *db)
{ char *read;
read = (char *) Malloc(db->maxlen+4,"Allocating New Read Buffer");
if (read == NULL)
EXIT(NULL);
return (read+1);
}
// Load into 'read' the i'th read in 'db'. As an upper case ASCII string if ascii is 2, as a
// lower-case ASCII string is ascii is 1, and as a numeric string over 0(A), 1(C), 2(G), and
// 3(T) otherwise.
//
// **NB**, the byte before read will be set to a delimiter character!
int Load_Read(HITS_DB *db, int i, char *read, int ascii)
{ FILE *bases = (FILE *) db->bases;
int64 off;
int len, clen;
HITS_READ *r = db->reads;
if (i >= db->nreads)
{ EPRINTF(EPLACE,"%s: Index out of bounds (Load_Read)\n",Prog_Name);
EXIT(1);
}
if (bases == NULL)
{ bases = Fopen(Catenate(db->path,"","",".bps"),"r");
if (bases == NULL)
EXIT(1);
db->bases = (void *) bases;
}
off = r[i].boff;
len = r[i].rlen;
if (ftello(bases) != off)
fseeko(bases,off,SEEK_SET);
clen = COMPRESSED_LEN(len);
if (clen > 0)
{ if (fread(read,clen,1,bases) != 1)
{ EPRINTF(EPLACE,"%s: Failed read of .bps file (Load_Read)\n",Prog_Name);
EXIT(1);
}
}
Uncompress_Read(len,read);
if (ascii == 1)
{ Lower_Read(read);
read[-1] = '\0';
}
else if (ascii == 2)
{ Upper_Read(read);
read[-1] = '\0';
}
else
read[-1] = 4;
return (0);
}
// Load into 'read' the i'th arrow in 'db'. As an ASCII string if ascii is 1,
// and as a numeric string otherwise.
//
HITS_DB *Arrow_DB = NULL; // Last db/arw used by "Load_Arrow"
FILE *Arrow_File = NULL; // Becomes invalid after closing
int Load_Arrow(HITS_DB *db, int i, char *read, int ascii)
{ FILE *arrow;
int64 off;
int len, clen;
HITS_READ *r = db->reads;
if (i >= db->nreads)
{ EPRINTF(EPLACE,"%s: Index out of bounds (Load_Arrow)\n",Prog_Name);
EXIT(1);
}
if (Arrow_DB != db)
{ fclose(Arrow_File);
arrow = Fopen(Catenate(db->path,"","",".arw"),"r");
if (arrow == NULL)
EXIT(1);
Arrow_File = arrow;
Arrow_DB = db;
}
else
arrow = Arrow_File;
off = r[i].boff;
len = r[i].rlen;
if (ftello(arrow) != off)
fseeko(arrow,off,SEEK_SET);
clen = COMPRESSED_LEN(len);
if (clen > 0)
{ if (fread(read,clen,1,arrow) != 1)
{ EPRINTF(EPLACE,"%s: Failed read of .bps file (Load_Arrow)\n",Prog_Name);
EXIT(1);
}
}
Uncompress_Read(len,read);
if (ascii == 1)
{ Letter_Arrow(read);
read[-1] = '\0';
}
else
read[-1] = 4;
return (0);
}
char *Load_Subread(HITS_DB *db, int i, int beg, int end, char *read, int ascii)
{ FILE *bases = (FILE *) db->bases;
int64 off;
int len, clen;
int bbeg, bend;
HITS_READ *r = db->reads;
if (i >= db->nreads)
{ EPRINTF(EPLACE,"%s: Index out of bounds (Load_Read)\n",Prog_Name);
EXIT(NULL);
}
if (bases == NULL)
{ bases = Fopen(Catenate(db->path,"","",".bps"),"r");
if (bases == NULL)
EXIT(NULL);
db->bases = (void *) bases;
}
bbeg = beg/4;
bend = (end-1)/4+1;
off = r[i].boff + bbeg;
len = end - beg;
if (ftello(bases) != off)
fseeko(bases,off,SEEK_SET);
clen = bend-bbeg;
if (clen > 0)
{ if (fread(read,clen,1,bases) != 1)
{ EPRINTF(EPLACE,"%s: Failed read of .bps file (Load_Read)\n",Prog_Name);
EXIT(NULL);
}
}
Uncompress_Read(4*clen,read);
read += beg%4;
read[len] = 4;
if (ascii == 1)
{ Lower_Read(read);
read[-1] = '\0';
}
else if (ascii == 2)
{ Upper_Read(read);
read[-1] = '\0';
}
else
read[-1] = 4;
return (read);
}
/*******************************************************************************************
*
* QV BUFFER ALLOCATION QV READ ACCESS
*
********************************************************************************************/
// Allocate and return a buffer of 5 vectors big enough for the largest read in 'db'
char **New_QV_Buffer(HITS_DB *db)
{ char **entry;
char *qvs;
int i;
qvs = (char *) Malloc(db->maxlen*5,"Allocating New QV Buffer");
entry = (char **) Malloc(sizeof(char *)*5,"Allocating New QV Buffer");
if (qvs == NULL || entry == NULL)
EXIT(NULL);
for (i = 0; i < 5; i++)
entry[i] = qvs + i*db->maxlen;
return (entry);
}
// Load into entry the QV streams for the i'th read from db. The parameter ascii applies to
// the DELTAG stream as described for Load_Read.
int Load_QVentry(HITS_DB *db, int i, char **entry, int ascii)
{ HITS_READ *reads;
FILE *quiva;
int rlen;
if (db != Active_DB)
{ if (db->tracks == NULL || strcmp(db->tracks->name,".@qvs") != 0)
{ EPRINTF(EPLACE,"%s: QV's are not loaded (Load_QVentry)\n",Prog_Name);
EXIT(1);
}
Active_QV = (HITS_QV *) db->tracks;
Active_DB = db;
}
if (i >= db->nreads)
{ EPRINTF(EPLACE,"%s: Index out of bounds (Load_QVentry)\n",Prog_Name);
EXIT(1);
}
reads = db->reads;
quiva = Active_QV->quiva;
rlen = reads[i].rlen;
fseeko(quiva,reads[i].coff,SEEK_SET);
if (Uncompress_Next_QVentry(quiva,entry,Active_QV->coding+Active_QV->table[i],rlen))
EXIT(1);
if (ascii != 1)
{ char *deltag = entry[1];
if (ascii != 2)
{ char x = deltag[rlen];
deltag[rlen] = '\0';
Number_Read(deltag);
deltag[rlen] = x;
}
else
{ int j;
int u = 'A'-'a';
for (j = 0; j < rlen; j++)
deltag[j] = (char) (deltag[j]+u);
}
}
return (0);
}
/*******************************************************************************************
*
* BLOCK LOAD OF ALL READS (PRIMARILY FOR DALIGNER)
*
********************************************************************************************/
// Allocate a block big enough for all the uncompressed sequences, read them into it,
// reset the 'off' in each read record to be its in-memory offset, and set the
// bases pointer to point at the block after closing the bases file. If ascii is
// non-zero then the reads are converted to ACGT ascii, otherwise the reads are left
// as numeric strings over 0(A), 1(C), 2(G), and 3(T).
int Read_All_Sequences(HITS_DB *db, int ascii)
{ FILE *bases;
int nreads = db->nreads;
HITS_READ *reads = db->reads;
void (*translate)(char *s);
char *seq;
int64 o, off;
int i, len, clen;
bases = Fopen(Catenate(db->path,"","",".bps"),"r");
if (bases == NULL)
EXIT(1);
seq = (char *) Malloc(db->totlen+nreads+4,"Allocating All Sequence Reads");
if (seq == NULL)
{ fclose(bases);
EXIT(1);
}
*seq++ = 4;
if (ascii == 1)
translate = Lower_Read;
else
translate = Upper_Read;
o = 0;
for (i = 0; i < nreads; i++)
{ len = reads[i].rlen;
off = reads[i].boff;
if (ftello(bases) != off)
fseeko(bases,off,SEEK_SET);
clen = COMPRESSED_LEN(len);
if (clen > 0)
{ if (fread(seq+o,clen,1,bases) != 1)
{ EPRINTF(EPLACE,"%s: Read of .bps file failed (Read_All_Sequences)\n",Prog_Name);
free(seq);
fclose(bases);
EXIT(1);
}
}
Uncompress_Read(len,seq+o);
if (ascii)
translate(seq+o);
reads[i].boff = o;
o += (len+1);
}
reads[nreads].boff = o;
fclose(bases);
db->bases = (void *) seq;
db->loaded = 1;
return (0);
}
int List_DB_Files(char *path, void actor(char *path, char *extension))
{ int status, plen, rlen, dlen;
char *root, *pwd, *name;
int isdam;
DIR *dirp;
struct dirent *dp;
status = 0;
pwd = PathTo(path);
plen = strlen(path);
if (strcmp(path+(plen-4),".dam") == 0)
root = Root(path,".dam");
else
root = Root(path,".db");
rlen = strlen(root);
if (root == NULL || pwd == NULL)
{ free(pwd);
free(root);
EXIT(1);
}
if ((dirp = opendir(pwd)) == NULL)
{ EPRINTF(EPLACE,"%s: Cannot open directory %s (List_DB_Files)\n",Prog_Name,pwd);
status = -1;
goto error;
}
isdam = 0;
while ((dp = readdir(dirp)) != NULL) // Get case dependent root name (if necessary)
{ name = dp->d_name;
if (strcmp(name,Catenate("","",root,".db")) == 0)
break;
if (strcmp(name,Catenate("","",root,".dam")) == 0)
{ isdam = 1;
break;
}
if (strcasecmp(name,Catenate("","",root,".db")) == 0)
{ strncpy(root,name,rlen);
break;
}
if (strcasecmp(name,Catenate("","",root,".dam")) == 0)
{ strncpy(root,name,rlen);
isdam = 1;
break;
}
}
if (dp == NULL)
{ EPRINTF(EPLACE,"%s: Cannot find %s (List_DB_Files)\n",Prog_Name,pwd);
status = -1;
closedir(dirp);
goto error;
}
if (isdam)
actor(Catenate(pwd,"/",root,".dam"),"dam");
else
actor(Catenate(pwd,"/",root,".db"),"db");
rewinddir(dirp); // Report each auxiliary file
while ((dp = readdir(dirp)) != NULL)
{ name = dp->d_name;
dlen = strlen(name);
#ifdef HIDE_FILES
if (name[0] != '.')
continue;
dlen -= 1;
name += 1;
#endif
if (dlen < rlen+1)
continue;
if (name[rlen] != '.')
continue;
if (strncmp(name,root,rlen) != 0)
continue;
actor(Catenate(pwd,PATHSEP,name,""),name+(rlen+1));
}
closedir(dirp);
error:
free(pwd);
free(root);
return (status);
}
void Print_Read(char *s, int width)
{ int i;
if (s[0] < 4)
{ for (i = 0; s[i] != 4; i++)
{ if (i%width == 0 && i != 0)
printf("\n");
printf("%d",s[i]);
}
printf("\n");
}
else
{ for (i = 0; s[i] != '\0'; i++)
{ if (i%width == 0 && i != 0)
printf("\n");
printf("%c",s[i]);
}
printf("\n");
}
}
|