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 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
|
/*
Copyright 2007, 2008 Daniel Zerbino (zerbino@ebi.ac.uk)
This file is part of Velvet.
Velvet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Velvet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Velvet; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#include "globals.h"
#include "graph.h"
#include "concatenatedGraph.h"
#include "recycleBin.h"
#include "locallyCorrectedGraph.h"
#include "passageMarker.h"
#include "readSet.h"
#include "utility.h"
#include "scaffold.h"
#define BLOCK_SIZE 100000
#define LN2 1.4
static int PEBBLE_ROUND_NUM = 0;
typedef struct readOccurence_st ReadOccurence;
static double paired_exp_fraction = 0.1;
struct connection_st {
Node *destination;
Connection *right;
Connection *left;
Connection *twin;
float distance;
float variance;
IDnum direct_count;
IDnum paired_count;
unsigned char clean;
} ATTRIBUTE_PACKED;
struct readOccurence_st {
IDnum position;
IDnum offset;
IDnum nodeID;
} ATTRIBUTE_PACKED;
// Global params
static IDnum UNRELIABLE_CONNECTION_CUTOFF = 5;
// Global pointers
static Graph *graph;
static Connection **scaffold = NULL;
static RecycleBin *connectionMemory = NULL;
static boolean estimated[CATEGORIES + 1];
#ifdef _OPENMP
#define READS_PER_LOCK 32
/* Array of reads locks */
static omp_lock_t *readsLocks = NULL;
/* Array of per-node locks */
static omp_lock_t *nodeLocks = NULL;
static void
createReadsLocks()
{
Coordinate nbLocks;
Coordinate lockIndex;
if (readsLocks)
free (readsLocks);
nbLocks = 1 + sequenceCount(graph) / READS_PER_LOCK;
readsLocks = mallocOrExit(nbLocks, omp_lock_t);
#pragma omp parallel for
for (lockIndex = 0; lockIndex < nbLocks; lockIndex++)
omp_init_lock(readsLocks + lockIndex);
}
static inline void lockRead(IDnum readID)
{
omp_set_lock (readsLocks + readID / READS_PER_LOCK);
}
static inline void unLockRead(IDnum readID)
{
omp_unset_lock (readsLocks + readID / READS_PER_LOCK);
}
static void
createNodeLocks(Graph *graph)
{
IDnum nbNodes;
IDnum nodeIndex;
nbNodes = nodeCount(graph) + 1;
if (nodeLocks)
free (nodeLocks);
nodeLocks = mallocOrExit(nbNodes, omp_lock_t);
#pragma omp parallel for
for (nodeIndex = 0; nodeIndex < nbNodes; nodeIndex++)
omp_init_lock(nodeLocks + nodeIndex);
}
/* Tries to avoid deadlocking */
static inline void lockTwoNodes(IDnum nodeID, IDnum node2ID)
{
if (nodeID < 0)
nodeID = -nodeID;
if (node2ID < 0)
node2ID = -node2ID;
/* Lock lowest ID first to avoid deadlocks */
if (nodeID < node2ID)
{
omp_set_lock (nodeLocks + nodeID);
omp_set_lock (nodeLocks + node2ID);
}
else
{
omp_set_lock (nodeLocks + node2ID);
omp_set_lock (nodeLocks + nodeID);
}
}
static inline void unLockTwoNodes(IDnum nodeID, IDnum node2ID)
{
if (nodeID < 0)
nodeID = -nodeID;
if (node2ID < 0)
node2ID = -node2ID;
omp_unset_lock (nodeLocks + nodeID);
omp_unset_lock (nodeLocks + node2ID);
}
#endif
static Connection *allocateConnection()
{
Connection *connect;
#ifdef _OPENMP
#pragma omp critical
{
#endif
if (connectionMemory == NULL)
connectionMemory =
newRecycleBin(sizeof(Connection), BLOCK_SIZE);
connect = allocatePointer(connectionMemory);
#ifdef _OPENMP
}
#endif
connect->destination = NULL;
connect->clean = false;
return connect;
}
static void deallocateConnection(Connection * connect)
{
deallocatePointer(connectionMemory, connect);
}
Node * getConnectionDestination(Connection * connect) {
return connect->destination;
}
Connection * getNextConnection(Connection * connect) {
return connect->right;
}
Connection * getTwinConnection(Connection * connect) {
return connect->twin;
}
Coordinate getConnectionDistance(Connection * connect) {
return (Coordinate) connect->distance;
}
double getConnectionVariance(Connection * connect) {
return connect->variance;
}
IDnum getConnectionDirectCount(Connection * connect) {
return connect->direct_count;
}
IDnum getConnectionPairedCount(Connection * connect) {
return connect->paired_count;
}
Connection * getConnection(Node * node) {
return scaffold[getNodeID(node) + nodeCount(graph)];
}
void incrementConnectionDistance(Connection * connect, Coordinate increment) {
connect->distance += increment;
}
static double norm(double X)
{
return 0.4 * exp(-X * X / 2);
}
static double normInt(double X, double Y)
{
return (erf(0.7 * Y) - erf(0.7 * X)) / 2;
}
static IDnum expectedNumberOfConnections(IDnum IDA, Connection * connect,
IDnum ** counts, Category cat)
{
Node *A = getNodeInGraph(graph, IDA);
Node *B = connect->destination;
double left, middle, right;
Coordinate longLength, shortLength, D;
IDnum longCount;
double M, N, O, P;
Coordinate mu = getInsertLength(graph, cat);
double sigma = sqrt(getInsertLength_var(graph, cat));
double result;
if (mu <= 0)
return 0;
if (getNodeLength(A) < getNodeLength(B)) {
longLength = getNodeLength(B);
shortLength = getNodeLength(A);
longCount = counts[cat][getNodeID(B) + nodeCount(graph)];
} else {
longLength = getNodeLength(A);
shortLength = getNodeLength(B);
longCount = counts[cat][IDA + nodeCount(graph)];
}
D = getConnectionDistance(connect) - (longLength + shortLength) / 2;
M = (D - mu) / sigma;
N = (D + shortLength - mu) / sigma;
O = (D + longLength - mu) / sigma;
P = (D + shortLength + longLength - mu) / sigma;
left = ((norm(M) - norm(N)) - M * normInt(M, N)) * sigma;
middle = shortLength * normInt(N, O);
right = ((norm(O) - norm(P)) - P * normInt(O, P)) * (-sigma);
result = (longCount * (left + middle + right)) / longLength;
if (result > 0)
return (IDnum) result;
else
return 0;
}
void destroyConnection(Connection * connect, IDnum nodeID)
{
Connection *previous, *next;
//velvetLog("Destroying connection from %li to %li\n", nodeID, getNodeID(connect->destination));
if (connect == NULL)
return;
previous = connect->left;
next = connect->right;
if (previous != NULL)
previous->right = next;
if (next != NULL)
next->left = previous;
if (scaffold[nodeID + nodeCount(graph)] == connect)
scaffold[nodeID + nodeCount(graph)] = next;
if (connect->twin != NULL) {
connect->twin->twin = NULL;
destroyConnection(connect->twin,
getNodeID(connect->destination));
}
deallocateConnection(connect);
}
static boolean testConnection(IDnum IDA,
Connection *connect,
IDnum **counts,
boolean *shadows)
{
IDnum total = 0;
Category cat;
// Spare unique -> undetermined node connections
if (!getUniqueness(connect->destination))
return true;
// Destroy tenuous connections
if (connect->paired_count + connect->direct_count <
UNRELIABLE_CONNECTION_CUTOFF)
return false;
for (cat = 0; cat < CATEGORIES; cat++)
if (!shadows[cat] || cat <= PEBBLE_ROUND_NUM)
total += expectedNumberOfConnections(IDA, connect, counts, cat);
// Remove inconsistent connections
return connect->paired_count >= total * paired_exp_fraction;
}
static IDnum *computeReadToNodeCounts(Coordinate *totalCount)
{
IDnum nodeIndex;
IDnum maxNodeIndex = 2 * nodeCount(graph) + 1;
IDnum maxReadIndex = sequenceCount(graph) + 1;
IDnum *readNodeCounts = callocOrExit(maxReadIndex, IDnum);
unsigned char *readMarker = callocOrExit(1 + maxReadIndex / 8, unsigned char);
Coordinate total = 0;
velvetLog("Computing read to node mapping array sizes\n");
#ifdef _OPENMP
#pragma omp parallel for reduction(+:total)
#endif
for (nodeIndex = 0; nodeIndex < maxNodeIndex; nodeIndex++) {
Node *node;
ShortReadMarker *nodeArray;
IDnum nodeReadCount;
IDnum readIndex;
node = getNodeInGraph(graph, nodeIndex - nodeCount(graph));
if (node == NULL)
continue;
nodeArray = getNodeReads(node, graph);
nodeReadCount = getNodeReadCount(node, graph);
// Short reads
for (readIndex = 0; readIndex < nodeReadCount; readIndex++) {
ShortReadMarker *shortMarker;
IDnum readID;
shortMarker = getShortReadMarkerAtIndex(nodeArray,
readIndex);
readID = getShortReadMarkerID(shortMarker);
#ifdef _OPENMP
#pragma omp atomic
#endif
readNodeCounts[readID]++;
total++;
}
}
for (nodeIndex = 0; nodeIndex < maxNodeIndex; nodeIndex++) {
Node *node;
PassageMarkerI marker;
node = getNodeInGraph(graph, nodeIndex - nodeCount(graph));
if (node == NULL)
continue;
// Long reads
for (marker = getMarker(node); marker != NULL_IDX;
marker = getNextInNode(marker)) {
IDnum readIndex = getPassageMarkerSequenceID(marker);;
if (readIndex < 0)
continue;
const unsigned int idx = readIndex / 8;
const unsigned int mask = 1 << (readIndex & 7);
if (readMarker[idx] & mask)
continue;
readNodeCounts[readIndex]++;
total++;
readMarker[idx] |= mask;
}
// Clean up marker array
for (marker = getMarker(node); marker != NULL_IDX;
marker = getNextInNode(marker)) {
IDnum readIndex = getPassageMarkerSequenceID(marker);
if (readIndex > 0)
// No need to go bit-wise
readMarker[readIndex / 8] = 0;
}
}
*totalCount = total;
free(readMarker);
return readNodeCounts;
}
static ReadOccurence **allocateReadToNodeTables(IDnum * readNodeCounts,
Coordinate totalCount,
ReadOccurence **readNodesArray)
{
Coordinate offset = 0;
IDnum readIndex;
IDnum maxReadIndex = sequenceCount(graph) + 1;
ReadOccurence **readNodes = callocOrExit(maxReadIndex, ReadOccurence *);
*readNodesArray = callocOrExit(totalCount, ReadOccurence);
for (readIndex = 1; readIndex < maxReadIndex; readIndex++) {
if (readNodeCounts[readIndex] != 0) {
readNodes[readIndex] = *readNodesArray + offset;
offset += readNodeCounts[readIndex];
readNodeCounts[readIndex] = 0;
}
}
return readNodes;
}
static void computePartialReadToNodeMappingShort(IDnum nodeID,
ReadOccurence ** readNodes,
IDnum * readNodeCounts)
{
ShortReadMarker *shortMarker;
IDnum index, readIndex;
ReadOccurence *readArray, *readOccurence;
Node *node = getNodeInGraph(graph, nodeID);
ShortReadMarker *nodeArray = getNodeReads(node, graph);
IDnum nodeReadCount = getNodeReadCount(node, graph);
for (index = 0; index < nodeReadCount; index++) {
shortMarker = getShortReadMarkerAtIndex(nodeArray, index);
readIndex = getShortReadMarkerID(shortMarker);
readArray = readNodes[readIndex];
#ifdef _OPENMP
lockRead(readIndex);
#endif
readOccurence = &readArray[readNodeCounts[readIndex]];
readOccurence->nodeID = nodeID;
readOccurence->position =
getShortReadMarkerPosition(shortMarker);
readOccurence->offset =
getShortReadMarkerOffset(shortMarker);
readNodeCounts[readIndex]++;
#ifdef _OPENMP
unLockRead(readIndex);
#endif
}
}
static void computePartialReadToNodeMappingLong(IDnum nodeID,
ReadOccurence ** readNodes,
IDnum * readNodeCounts,
unsigned char *readMarker,
ReadSet * reads)
{
IDnum readIndex;
ReadOccurence *readArray, *readOccurence;
Node *node = getNodeInGraph(graph, nodeID);
PassageMarkerI marker;
for (marker = getMarker(node); marker != NULL_IDX;
marker = getNextInNode(marker)) {
readIndex = getPassageMarkerSequenceID(marker);
if (readIndex <= 0 || reads->categories[readIndex - 1] == REFERENCE)
continue;
const unsigned int idx = readIndex / 8;
const unsigned int mask = 1 << (readIndex & 7);
if (readMarker[idx] & mask) {
readArray = readNodes[readIndex];
readOccurence =
&readArray[readNodeCounts[readIndex] - 1];
readOccurence->position = -1;
readOccurence->offset = -1;
} else {
readArray = readNodes[readIndex];
readOccurence =
&readArray[readNodeCounts[readIndex]];
readOccurence->nodeID = nodeID;
readOccurence->position = getStartOffset(marker);
readOccurence->offset =
getPassageMarkerStart(marker);
readNodeCounts[readIndex]++;
readMarker[idx] |= mask;
}
}
for (marker = getMarker(node); marker != NULL_IDX;
marker = getNextInNode(marker)) {
readIndex = getPassageMarkerSequenceID(marker);
if (readIndex > 0)
// No need to go bit-wise
readMarker[readIndex / 8] = 0;
}
}
static ReadOccurence **computeReadToNodeMappings(IDnum * readNodeCounts,
ReadSet * reads,
Coordinate totalCount,
ReadOccurence **readNodesArray)
{
unsigned char *readMarker;
IDnum nodeID;
IDnum nodes = nodeCount(graph);
ReadOccurence **readNodes = allocateReadToNodeTables(readNodeCounts,
totalCount,
readNodesArray);
velvetLog("Computing read to node mappings\n");
#ifdef _OPENMP
createReadsLocks();
#pragma omp parallel for
#endif
for (nodeID = -nodes; nodeID <= nodes; nodeID++)
if (nodeID != 0 && getNodeInGraph(graph, nodeID))
computePartialReadToNodeMappingShort(nodeID, readNodes,
readNodeCounts);
#ifdef _OPENMP
free(readsLocks);
readsLocks = NULL;
#endif
readMarker = callocOrExit(1 + sequenceCount(graph) / 8, unsigned char);
for (nodeID = -nodes; nodeID <= nodes; nodeID++)
if (nodeID != 0 && getNodeInGraph(graph, nodeID))
computePartialReadToNodeMappingLong(nodeID, readNodes,
readNodeCounts,
readMarker,
reads);
free(readMarker);
return readNodes;
}
static unsigned char * countCoOccurences(IDnum * coOccurencesCount,
ReadOccurence ** readNodes,
IDnum * readNodeCounts,
IDnum * readPairs,
Category * cats)
{
IDnum readIndex, readPairIndex;
IDnum readNodeCount;
IDnum readOccurenceIndex, readPairOccurenceIndex;
ReadOccurence * readOccurence, *readPairOccurence;
unsigned char *interestingReads = callocOrExit(1 + sequenceCount(graph) / 8, unsigned char);
Category libID;
for (libID = 0; libID < CATEGORIES + 1; libID++)
coOccurencesCount[libID] = 0;
for (readIndex = 0; readIndex < sequenceCount(graph); readIndex++) {
// Eliminating dodgy, unpaired, already counted or user-specified reads
if ( readPairs[readIndex] < readIndex
|| getInsertLength(graph, cats[readIndex]) > -1)
continue;
// Check for co-occurence
// We know that for each read the read occurences are ordered by increasing node ID
// Therefore one list is followed by increasing index, whereas the other is followed
// by decreasing index
libID = cats[readIndex] / 2;
readPairIndex = readPairs[readIndex];
readOccurenceIndex = 0;
readOccurence = readNodes[readIndex + 1];
readNodeCount = readNodeCounts[readIndex + 1];
readPairOccurenceIndex = readNodeCounts[readPairIndex + 1] - 1;
readPairOccurence = &(readNodes[readPairIndex + 1][readPairOccurenceIndex]);
while (readOccurenceIndex < readNodeCount && readPairOccurenceIndex >= 0) {
if (readOccurence->nodeID == -readPairOccurence->nodeID) {
if (readOccurence->position > 0 && readPairOccurence->position > 0) {
coOccurencesCount[libID]++;
interestingReads[readIndex / 8] |= 1 << (readIndex & 7);
break;
} else {
readOccurence++;
readOccurenceIndex++;
readPairOccurence--;
readPairOccurenceIndex--;
}
} else if (readOccurence->nodeID < -readPairOccurence->nodeID) {
readOccurence++;
readOccurenceIndex++;
} else {
readPairOccurence--;
readPairOccurenceIndex--;
}
}
}
return interestingReads;
}
static void measureCoOccurences(IDnum ** coOccurences,
unsigned char * interestingReads,
ReadOccurence ** readNodes,
IDnum * readNodeCounts,
IDnum * readPairs,
Category * cats)
{
IDnum coOccurencesIndex[CATEGORIES + 1];
IDnum observationIndex;
IDnum readIndex, readPairIndex;
IDnum readNodeCount;
IDnum readOccurenceIndex, readPairOccurenceIndex;
ReadOccurence * readOccurence, *readPairOccurence;
Category libID;
for (libID = 0; libID < CATEGORIES + 1; libID++)
coOccurencesIndex[libID] = 0;
for (readIndex = 0; readIndex < sequenceCount(graph); readIndex++) {
// Eliminating dodgy, unpaired, already counted or user-specified reads
if (!(interestingReads[readIndex / 8] & (1 << (readIndex & 7))))
continue;
// Find co-occurence
// We know that for each read the read occurences are ordered by increasing node ID
libID = cats[readIndex]/2;
readPairIndex = readPairs[readIndex];
observationIndex = coOccurencesIndex[libID];
readOccurence = readNodes[readIndex + 1];
readOccurenceIndex = 0;
readNodeCount = readNodeCounts[readIndex + 1];
readPairOccurenceIndex = readNodeCounts[readPairIndex + 1] - 1;
readPairOccurence = &(readNodes[readPairIndex + 1][readPairOccurenceIndex]);
while (readOccurenceIndex < readNodeCount && readPairOccurenceIndex >= 0) {
if (readOccurence->nodeID == -readPairOccurence->nodeID) {
if (readOccurence->position > 0 && readPairOccurence->position > 0) {
coOccurences[libID][observationIndex] =
getNodeLength(getNodeInGraph(graph, readOccurence->nodeID))
+ getWordLength(graph) - 1
- (readOccurence->position - readOccurence->offset)
- (readPairOccurence->position - readPairOccurence->offset);
coOccurencesIndex[libID]++;
break;
} else {
readOccurence++;
readOccurenceIndex++;
readPairOccurence--;
readPairOccurenceIndex--;
}
} else if (readOccurence->nodeID < -readPairOccurence->nodeID) {
readOccurence++;
readOccurenceIndex++;
} else {
readPairOccurence--;
readPairOccurenceIndex--;
}
}
}
}
int compareReadOccurences(const void *A, const void * B) {
IDnum * cA = (IDnum *) A;
IDnum * cB = (IDnum *) B;
if (*cA > *cB)
return 1;
if (*cA == *cB)
return 0;
return -1;
}
static void estimateLibraryInsertLength(IDnum * coOccurences, IDnum coOccurencesCount, Category libID) {
Coordinate median, variance;
IDnum index;
int counter = 0;
qsort(coOccurences, coOccurencesCount, sizeof(IDnum), compareReadOccurences);
median = coOccurences[coOccurencesCount / 2];
// Modified variance around the median (proxy for expected value)
// interval censoring
variance = 0;
for (index = 0; index < coOccurencesCount; index++) {
if (coOccurences[index] > 0 && coOccurences[index] < 5 * median) {
variance += (coOccurences[index] - median) * (coOccurences[index] - median);
counter++;
}
}
if (counter)
variance /= counter;
else {
variance = 0;
for (index = 0; index < coOccurencesCount; index++)
variance += (coOccurences[index] - median) * (coOccurences[index] - median);
variance /= coOccurencesCount;
}
// To avoid subsequent divisions by zero
if (variance == 0)
variance = 1;
velvetLog("Paired-end library %i has length: %lli, sample standard deviation: %lli\n", libID + 1, (long long) median, (long long) sqrt(variance));
setInsertLengths(graph, libID, median, sqrt(variance));
estimated[libID] = true;
}
static void estimateLibraryInsertLengths(IDnum ** coOccurences, IDnum * coOccurencesCounts) {
Category libID;
for (libID = 0; libID < CATEGORIES + 1; libID++)
estimated[libID] = false;
for (libID = 0; libID < CATEGORIES + 1; libID++)
if (coOccurencesCounts[libID] > 0)
estimateLibraryInsertLength(coOccurences[libID], coOccurencesCounts[libID], libID);
}
static void estimateMissingInsertLengths(ReadOccurence ** readNodes, IDnum * readNodeCounts, IDnum * readPairs, Category * cats) {
IDnum * coOccurences[CATEGORIES + 1];
IDnum coOccurencesCounts[CATEGORIES + 1];
Category libID;
velvetLog("Estimating library insert lengths...\n");
unsigned char * interestingReads = countCoOccurences(coOccurencesCounts, readNodes, readNodeCounts, readPairs, cats);
for (libID = 0; libID < CATEGORIES + 1; libID++)
coOccurences[libID] = callocOrExit(coOccurencesCounts[libID], IDnum);
measureCoOccurences(coOccurences, interestingReads, readNodes, readNodeCounts, readPairs, cats);
estimateLibraryInsertLengths(coOccurences, coOccurencesCounts);
for (libID = 0; libID < CATEGORIES + 1; libID++)
free(coOccurences[libID]);
free(interestingReads);
velvetLog("Done\n");
}
static void createTwinConnection(IDnum nodeID, IDnum node2ID,
Connection * connect)
{
Connection *newConnection = allocateConnection();
IDnum nodeIndex = nodeID + nodeCount(graph);
// Fill in
newConnection->distance = connect->distance;
newConnection->variance = connect->variance;
newConnection->direct_count = connect->direct_count;
newConnection->paired_count = connect->paired_count;
newConnection->destination = getNodeInGraph(graph, node2ID);
// Batch to twin
newConnection->twin = connect;
connect->twin = newConnection;
// Insert in scaffold
newConnection->left = NULL;
newConnection->right = scaffold[nodeIndex];
if (scaffold[nodeIndex] != NULL)
scaffold[nodeIndex]->left = newConnection;
scaffold[nodeIndex] = newConnection;
}
Connection *createNewConnection(IDnum nodeID, IDnum node2ID,
IDnum direct_count,
IDnum paired_count,
Coordinate distance,
double variance)
{
Node *destination = getNodeInGraph(graph, node2ID);
IDnum nodeIndex = nodeID + nodeCount(graph);
Connection *connect = allocateConnection();
// Fill in
connect->destination = destination;
connect->direct_count = direct_count;
connect->paired_count = paired_count;
connect->distance = (double) distance;
connect->variance = variance;
// Insert in scaffold
connect->left = NULL;
connect->right = scaffold[nodeIndex];
if (scaffold[nodeIndex] != NULL)
scaffold[nodeIndex]->left = connect;
scaffold[nodeIndex] = connect;
// Event. pair up to twin
if (getUniqueness(destination))
createTwinConnection(node2ID, nodeID, connect);
else
connect->twin = NULL;
return connect;
}
void readjustConnection(Connection * connect, Coordinate distance,
double variance, IDnum direct_count,
IDnum paired_count)
{
connect->direct_count += direct_count;
connect->paired_count += paired_count;
connect->distance =
(variance * connect->distance +
distance * connect->variance) / (variance +
connect->variance);
connect->variance =
(variance *
connect->variance) / (variance + connect->variance);
if (connect->twin != NULL) {
connect->twin->distance = connect->distance;
connect->twin->variance = connect->variance;
connect->twin->direct_count = connect->direct_count;
connect->twin->paired_count = connect->paired_count;
}
}
//////////////////////////////////////
// Splay tree function for Connections
//////////////////////////////////////
/* This function can be called only if K2 has a left child */
/* Perform a rotate between a node (K2) and its left child */
/* Update heights, then return new root */
static Connection *connectionSingleRotateWithLeft(Connection * K2)
{
Connection *K1;
K1 = K2->left;
K2->left = K1->right;
K1->right = K2;
return K1; /* New root */
}
/* This function can be called only if K1 has a right child */
/* Perform a rotate between a node (K1) and its right child */
/* Update heights, then return new root */
static Connection *connectionSingleRotateWithRight(Connection * K1)
{
Connection *K2;
K2 = K1->right;
K1->right = K2->left;
K2->left = K1;
return K2; /* New root */
}
/* Top-down splay procedure, */
/* not requiring destination to be in tree */
static Connection *splayConnection(Connection * T, IDnum nodeID)
{
Connection Header;
Connection *LeftTreeMax, *RightTreeMin;
if (T == NULL)
return NULL;
Header.left = Header.right = NULL;
LeftTreeMax = RightTreeMin = &Header;
while (nodeID != getNodeID(T->destination))
{
if (nodeID < getNodeID(T->destination))
{
if (T->left == NULL)
break;
if (nodeID < getNodeID(T->left->destination))
T = connectionSingleRotateWithLeft(T);
if (T->left == NULL)
break;
/* Link right */
RightTreeMin->left = T;
RightTreeMin = T;
T = T->left;
}
else
{
if (T->right == NULL)
break;
if (nodeID > getNodeID(T->right->destination))
T = connectionSingleRotateWithRight(T);
if (T->right == NULL)
break;
/* Link left */
LeftTreeMax->right = T;
LeftTreeMax = T;
T = T->right;
}
} /* while nodeID != T->destination */
/* Reassemble */
LeftTreeMax->right = T->left;
RightTreeMin->left = T->right;
T->left = Header.right;
T->right = Header.left;
return T;
}
static Connection* findOrCreateConnection(IDnum nodeID,
IDnum node2ID)
{
Connection **T;
Connection *newConnection;
IDnum nodeIndex;
nodeIndex = nodeID + nodeCount(graph);
T = scaffold + nodeIndex;
if (*T == NULL)
{
newConnection = allocateConnection();
newConnection->left = NULL;
newConnection->right = NULL;
*T = newConnection;
}
else
{
IDnum destID;
*T = splayConnection(*T, node2ID);
destID = getNodeID((*T)->destination);
if (destID == node2ID)
newConnection = *T;
else
{
newConnection = allocateConnection();
if (node2ID < destID)
{
newConnection->left = (*T)->left;
newConnection->right = *T;
(*T)->left = NULL;
}
else if (node2ID > destID)
{
newConnection->right = (*T)->right;
newConnection->left = *T;
(*T)->right = NULL;
}
*T = newConnection;
}
}
return newConnection;
}
static Connection* findConnection(IDnum nodeID,
IDnum node2ID)
{
Connection **T;
IDnum nodeIndex;
nodeIndex = nodeID + nodeCount(graph);
T = scaffold + nodeIndex;
if (*T == NULL)
return NULL;
else
{
IDnum destID;
*T = splayConnection(*T, node2ID);
destID = getNodeID((*T)->destination);
if (destID == node2ID)
return *T;
}
return NULL;
}
RecycleBin *connectionStackMemory = NULL;
typedef struct ConnectionStack_st ConnectionStack;
struct ConnectionStack_st
{
Connection *connection;
ConnectionStack *next;
};
#ifdef _OPENMP
static void initConnectionStackMemory(void)
{
int n = omp_get_max_threads();
#pragma omp critical
{
if (connectionStackMemory == NULL)
connectionStackMemory = newRecycleBinArray(n, sizeof(ConnectionStack), BLOCK_SIZE);
}
}
#endif
static ConnectionStack *allocateConnectionStack(void)
{
#ifdef _OPENMP
#ifdef DEBUG
if (connectionStackMemory == NULL)
{
velvetLog("The memory for connection stack seems uninitialised, "
"this is probably a bug, aborting.\n");
abort();
}
#endif
return allocatePointer(getRecycleBinInArray(connectionStackMemory,
omp_get_thread_num()));
#else
if (connectionStackMemory == NULL)
connectionStackMemory =
newRecycleBin(sizeof(ConnectionStack), BLOCK_SIZE);
return allocatePointer(connectionStackMemory);
#endif
}
static void deallocateConnectionStack(ConnectionStack *stack)
{
#ifdef _OPENMP
deallocatePointer(getRecycleBinInArray(connectionStackMemory,
omp_get_thread_num()),
stack);
#else
deallocatePointer(connectionStackMemory, stack);
#endif
}
static void destroyConnectionStackMemory(void)
{
#ifdef _OPENMP
destroyRecycleBinArray(connectionStackMemory);
#else
destroyRecycleBin(connectionStackMemory);
#endif
connectionStackMemory = NULL;
}
static void pushConnectionStack(ConnectionStack **stack, Connection *connection)
{
ConnectionStack *newElement;
newElement = allocateConnectionStack();
newElement->connection = connection;
newElement->next = *stack;
*stack = newElement;
}
static Connection *popConnectionStack(ConnectionStack **stack)
{
ConnectionStack *nextElement;
Connection *connection;
if (*stack == NULL)
return NULL;
nextElement = (*stack)->next;
connection = (*stack)->connection;
deallocateConnectionStack(*stack);
*stack = nextElement;
return connection;
}
static void splayToList(Connection **connection)
{
ConnectionStack *stack = NULL;
Connection *current;
Connection *list = NULL;
if (*connection == NULL)
return;
for (current = *connection; current != NULL; current = popConnectionStack(&stack))
{
Connection *right;
Connection *left;
right = current->right;
if (right != NULL)
pushConnectionStack(&stack, right);
left = current->left;
if (left != NULL)
pushConnectionStack(&stack, left);
if (list != NULL)
list->left = current;
current->right = list;
list = current;
}
list->left = NULL;
*connection = list;
}
static void setAllConnectionsClean(void)
{
IDnum nodeID;
IDnum nodes = nodeCount(graph);
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (nodeID = 2 * nodes; nodeID >= 0; nodeID--)
{
ConnectionStack *stack = NULL;
Connection **connect;
Connection *current;
connect = scaffold + nodeID;
if (*connect == NULL)
continue;
for (current = *connect; current != NULL; current = popConnectionStack(&stack))
{
Connection *right;
Connection *left;
current->clean = true;
right = current->right;
if (right != NULL)
pushConnectionStack(&stack, right);
left = current->left;
if (left != NULL)
pushConnectionStack(&stack, left);
}
}
}
static void fillNewConnectionInTree(Connection *connect,
Node *destination,
IDnum direct_count,
IDnum paired_count,
Coordinate distance,
double variance)
{
connect->destination = destination;
connect->direct_count = direct_count;
connect->paired_count = paired_count;
connect->distance = (double)distance;
connect->variance = variance;
}
static void readjustConnectionInTree(Connection *connect,
IDnum direct_count,
IDnum paired_count,
Coordinate distance,
double variance)
{
connect->direct_count += direct_count;
connect->paired_count += paired_count;
connect->distance = (variance * connect->distance + distance * connect->variance) /
(variance + connect->variance);
connect->variance = (variance * connect->variance) / (variance + connect->variance);
if (connect->twin != NULL)
{
connect->twin->direct_count = connect->direct_count;
connect->twin->paired_count = connect->paired_count;
connect->twin->distance = connect->distance;
connect->twin->variance = connect->variance;
}
}
static void createTwinConnectionInTree(IDnum nodeID,
IDnum node2ID,
Connection *connect)
{
Connection *newConnection;
newConnection = findOrCreateConnection(nodeID, node2ID);
if (newConnection->destination == NULL)
{
fillNewConnectionInTree(newConnection,
getNodeInGraph(graph, node2ID),
connect->direct_count,
connect->paired_count,
(Coordinate)connect->distance,
connect->variance);
// Batch to twin
newConnection->twin = connect;
connect->twin = newConnection;
}
else
readjustConnectionInTree(newConnection,
connect->direct_count,
connect->paired_count,
(Coordinate)connect->distance,
connect->variance);
}
static void createConnection(IDnum nodeID,
IDnum node2ID,
IDnum direct_count,
IDnum paired_count,
Coordinate distance,
double variance)
{
Connection *connect;
if (getUniqueness(getNodeInGraph(graph, node2ID)) && node2ID < nodeID) {
return;
}
#ifdef _OPENMP
lockTwoNodes(nodeID, node2ID);
#endif
connect = findOrCreateConnection(nodeID, node2ID);
if (connect->destination == NULL)
{
Node *destination = getNodeInGraph(graph, node2ID);
fillNewConnectionInTree(connect,
destination,
direct_count,
paired_count,
distance,
variance);
if (getUniqueness(destination))
createTwinConnectionInTree(node2ID, nodeID, connect);
else
connect->twin = NULL;
}
else
readjustConnectionInTree(connect,
direct_count,
paired_count,
distance,
variance);
#ifdef _OPENMP
unLockTwoNodes(nodeID, node2ID);
#endif
}
static void projectFromSingleRead(Node * node,
ReadOccurence * readOccurence,
Coordinate position,
Coordinate offset, Coordinate length)
{
Coordinate distance = 0;
Node *target = getNodeInGraph(graph, -readOccurence->nodeID);
double variance = 1;
if (target == getTwinNode(node) || target == node)
return;
if (position < 0) {
variance += getNodeLength(node) * getNodeLength(node) / 16;
// distance += 0;
} else {
// variance += 0;
distance += position - getNodeLength(node) / 2;
}
if (readOccurence->position < 0) {
variance +=
getNodeLength(target) * getNodeLength(target) / 16;
//distance += 0;
} else {
// variance += 0;
distance +=
-readOccurence->position + getNodeLength(target) / 2;
}
if (readOccurence->offset < 0 || offset < 0) {
variance += length * length / 16;
//distance += 0;
} else {
// variance += 0;
distance += readOccurence->offset - offset;
}
// Relative ordering
if (offset > 0 && readOccurence->offset > 0) {
if (offset < readOccurence->offset) {
if (distance - getNodeLength(node)/2 - getNodeLength(target)/2 < -10)
;
else if (distance < getNodeLength(node)/2 + getNodeLength(target)/2)
createConnection(getNodeID(node), getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
else
createConnection(getNodeID(node), getNodeID(target), 1, 0,
distance, variance);
} else if (offset > readOccurence->offset) {
if (-distance - getNodeLength(node)/2 - getNodeLength(target)/2 < -10)
;
else if (-distance < getNodeLength(node)/2 + getNodeLength(target)/2)
createConnection(-getNodeID(node), -getNodeID(target), 1,
0, getNodeLength(node)/2 + getNodeLength(target)/2 , variance);
else
createConnection(-getNodeID(node), -getNodeID(target), 1,
0, -distance, variance);
}
} else if (offset > 0 && position > 0) {
if (distance - offset > -getNodeLength(node)/2 && distance - offset + length > getNodeLength(node)/2)
createConnection(getNodeID(node), getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
else if (distance - offset < -getNodeLength(node)/2 && distance - offset + length < getNodeLength(node)/2)
createConnection(-getNodeID(node), -getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
else {
createConnection(getNodeID(node), getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
createConnection(-getNodeID(node), -getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
}
} else if (readOccurence->offset > 0 && readOccurence->position > 0) {
if (-distance - readOccurence->offset > -getNodeLength(target)/2 && -distance - readOccurence->offset + length > getNodeLength(target)/2)
createConnection(-getNodeID(node), -getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
if (-distance - readOccurence->offset < -getNodeLength(target)/2 && -distance - readOccurence->offset + length < getNodeLength(target)/2)
createConnection(getNodeID(node), getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
else {
createConnection(getNodeID(node), getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
createConnection(-getNodeID(node), -getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
}
} else {
createConnection(getNodeID(node), getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
createConnection(-getNodeID(node), -getNodeID(target), 1, 0,
getNodeLength(node)/2 + getNodeLength(target)/2, variance);
}
}
static void projectFromReadPair(Node * node, ReadOccurence * readOccurence,
Coordinate position, Coordinate offset,
Coordinate insertLength,
double insertVariance,
boolean doMatePairs)
{
Coordinate distance = insertLength;
Coordinate variance = insertVariance;
Node *target = getNodeInGraph(graph, readOccurence->nodeID);
IDnum nodeID;
IDnum node2ID;
if (target == getTwinNode(node) || target == node)
return;
nodeID = getNodeID(node);
node2ID = getNodeID(target);
if (getUniqueness(target) && node2ID < nodeID)
return;
// Check if a conflicting PE (or MP from a smaller size lib) connection
// already exists
if (doMatePairs) {
Connection *reverseConnect;
#ifdef _OPENMP
lockTwoNodes(nodeID, node2ID);
#endif
reverseConnect = findConnection(-nodeID, -node2ID);
#ifdef _OPENMP
unLockTwoNodes(nodeID, node2ID);
#endif
if (reverseConnect != NULL &&
reverseConnect->clean &&
reverseConnect->paired_count +
reverseConnect->direct_count >= UNRELIABLE_CONNECTION_CUTOFF)
return;
}
if (position < 0) {
variance += getNodeLength(node) * getNodeLength(node) / 16;
// distance += 0;
} else {
// variance += 0;
distance += position - offset - getNodeLength(node) / 2;
}
if (readOccurence->position < 0) {
variance +=
getNodeLength(target) * getNodeLength(target) / 16;
//distance += 0;
} else {
// variance += 0;
distance +=
readOccurence->position - readOccurence->offset -
getNodeLength(target) / 2;
}
if (distance - getNodeLength(node)/2 - getNodeLength(target)/2 < -6 * sqrt(insertVariance))
return;
else if (distance < getNodeLength(node)/2 + getNodeLength(target)/2)
distance = getNodeLength(node)/2 + getNodeLength(target)/2;
createConnection(nodeID, node2ID, 0, 1,
distance, variance);
}
static void projectFromShortRead(Node * node,
ShortReadMarker * shortMarker,
IDnum * readPairs, Category * cats,
ReadOccurence ** readNodes,
IDnum * readNodeCounts,
ShortLength * lengths,
boolean * shadows,
boolean doMatePairs,
Category thisCat)
{
IDnum index;
IDnum readIndex = getShortReadMarkerID(shortMarker);
ReadOccurence *readArray;
IDnum readPairIndex;
Category cat;
Coordinate position = getShortReadMarkerPosition(shortMarker);
Coordinate offset = getShortReadMarkerOffset(shortMarker);
Coordinate length = lengths[getShortReadMarkerID(shortMarker) - 1];
Coordinate insertLength;
double insertVariance;
// Going through single-read information
if (!doMatePairs && readNodeCounts[readIndex] > 1) {
readArray = readNodes[readIndex];
for (index = 0; index < readNodeCounts[readIndex]; index++)
projectFromSingleRead(node, &readArray[index],
position, offset, length);
}
// Going through paired read information
if (readPairs == NULL)
return;
readPairIndex = readPairs[readIndex - 1] + 1;
if (readPairIndex == 0)
return;
cat = cats[readIndex - 1];
insertLength = getInsertLength(graph, cat);
insertVariance = getInsertLength_var(graph, cat);
cat /= 2;
if (shadows[cat] && cat > PEBBLE_ROUND_NUM)
return;
if (!shadows[cat] && !doMatePairs) {
readArray = readNodes[readPairIndex];
for (index = 0; index < readNodeCounts[readPairIndex]; index++)
projectFromReadPair(node, &readArray[index], position,
offset, insertLength, insertVariance, false);
}
else if (shadows[cat] && doMatePairs && cat == thisCat) {
readArray = readNodes[readPairIndex];
for (index = 0; index < readNodeCounts[readPairIndex]; index++)
projectFromReadPair(node, &readArray[index], position,
offset, insertLength, insertVariance, true);
}
}
static void projectFromLongRead(Node * node, PassageMarkerI marker,
IDnum * readPairs, Category * cats,
ReadOccurence ** readNodes,
IDnum * readNodeCounts,
ShortLength * lengths)
{
IDnum index;
IDnum readIndex = getPassageMarkerSequenceID(marker);
ReadOccurence *readArray;
IDnum readPairIndex;
Category cat;
Coordinate position = getStartOffset(marker);
Coordinate offset = getPassageMarkerStart(marker);
Coordinate length =
lengths[getPassageMarkerSequenceID(marker) - 1];
Coordinate insertLength;
double insertVariance;
// Going through single-read information
if (readNodeCounts[readIndex] > 1 && position > 0) {
readArray = readNodes[readIndex];
for (index = 0; index < readNodeCounts[readIndex]; index++)
projectFromSingleRead(node, &readArray[index],
position, offset, length);
}
// Going through paired read information
if (readPairs == NULL)
return;
readPairIndex = readPairs[readIndex - 1] + 1;
if (readPairIndex == 0)
return;
cat = cats[readIndex - 1];
insertLength = getInsertLength(graph, cat);
insertVariance = getInsertLength_var(graph, cat);
readArray = readNodes[readPairIndex];
for (index = 0; index < readNodeCounts[readPairIndex]; index++)
projectFromReadPair(node, &readArray[index], position,
offset, insertLength, insertVariance, false);
}
static void projectFromNode(IDnum nodeID,
ReadOccurence ** readNodes,
IDnum * readNodeCounts,
IDnum * readPairs, Category * cats,
boolean * dubious, ShortLength * lengths,
boolean * shadows,
boolean doMatePairs,
Category thisCat)
{
IDnum index;
ShortReadMarker *nodeArray, *shortMarker;
PassageMarkerI marker;
Node *node;
IDnum nodeReadCount;
node = getNodeInGraph(graph, nodeID);
if (node == NULL || !getUniqueness(node))
return;
nodeArray = getNodeReads(node, graph);
nodeReadCount = getNodeReadCount(node, graph);
for (index = 0; index < nodeReadCount; index++) {
shortMarker = getShortReadMarkerAtIndex(nodeArray, index);
if (dubious[getShortReadMarkerID(shortMarker) - 1])
continue;
projectFromShortRead(node, shortMarker, readPairs, cats,
readNodes, readNodeCounts, lengths,
shadows,
doMatePairs,
thisCat);
}
if (!doMatePairs)
for (marker = getMarker(node); marker != NULL_IDX;
marker = getNextInNode(marker)) {
if (getPassageMarkerSequenceID(marker) > 0)
projectFromLongRead(node, marker, readPairs, cats,
readNodes, readNodeCounts,
lengths);
}
}
static Connection **computeNodeToNodeMappings(ReadOccurence ** readNodes,
IDnum * readNodeCounts,
IDnum * readPairs,
Category * cats,
boolean * dubious,
boolean * shadows,
ShortLength * lengths)
{
IDnum nodeID;
IDnum nodes = nodeCount(graph);
struct timeval start, end, diff;
Category cat;
boolean hasShadow;
scaffold = callocOrExit(2 * nodes + 1, Connection *);
velvetLog("Computing direct node to node mappings\n");
gettimeofday(&start, NULL);
#ifdef _OPENMP
createNodeLocks(graph);
int threads = omp_get_max_threads();
if (threads > 32)
threads = 32;
#pragma omp parallel for num_threads(threads)
#endif
for (nodeID = -nodes; nodeID <= nodes; nodeID++)
{
if (nodeID % 10000 == 0)
velvetLog("Scaffolding node %li\n", (long) nodeID);
projectFromNode(nodeID, readNodes, readNodeCounts,
readPairs, cats, dubious, lengths, shadows, false, 0);
}
#ifdef _OPENMP
initConnectionStackMemory();
#endif
hasShadow = false;
for (cat = 0; cat < CATEGORIES; cat++)
if (shadows[cat])
{
hasShadow = true;
break;
}
if (hasShadow)
{
for (cat = 0; cat < CATEGORIES; cat++)
{
setAllConnectionsClean();
if (!shadows[cat])
continue;
velvetLog("Scaffolding MP library %i\n", cat);
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (nodeID = -nodes; nodeID <= nodes; nodeID++)
projectFromNode(nodeID, readNodes, readNodeCounts,
readPairs, cats, dubious, lengths,
shadows, true, cat);
}
}
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (nodeID = 2 * nodes; nodeID >= 0; nodeID--)
splayToList(scaffold + nodeID);
destroyConnectionStackMemory();
#ifdef _OPENMP
free(nodeLocks);
nodeLocks = NULL;
#endif
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
velvetLog(" === Nodes Scaffolded in %ld.%06ld s\n", (long) diff.tv_sec, (long) diff.tv_usec);
PEBBLE_ROUND_NUM++;
return scaffold;
}
static IDnum **countShortReads(Graph * graph, ReadSet * reads)
{
IDnum **counts = callocOrExit(CATEGORIES + 1, IDnum *);
Category cat;
IDnum nodeIndex;
IDnum nodes = nodeCount(graph);
Node *node;
ShortReadMarker *array, *marker;
IDnum readCount, readIndex, readID;
// Allocate memory where needed
for (cat = 0; cat <= CATEGORIES; cat++)
if (getInsertLength(graph, cat) > 0)
counts[cat] =
callocOrExit(2 * nodeCount(graph) + 1,
IDnum);
// Start fillin'
for (nodeIndex = 0; nodeIndex < 2 * nodes + 1; nodeIndex++) {
node = getNodeInGraph(graph, nodeIndex - nodes);
if (node == NULL || !getUniqueness(node))
continue;
array = getNodeReads(node, graph);
readCount = getNodeReadCount(node, graph);
for (readIndex = 0; readIndex < readCount; readIndex++) {
marker =
getShortReadMarkerAtIndex(array, readIndex);
readID = getShortReadMarkerID(marker);
cat = reads->categories[readID - 1];
if (cat % 2 == 1 && counts[cat / 2] != NULL)
counts[cat / 2][nodeIndex]++;
}
}
return counts;
}
static void removeUnreliableConnections(ReadSet * reads, boolean *shadows)
{
IDnum maxNodeIndex = nodeCount(graph) * 2 + 1;
IDnum index;
Connection *connect, *next;
Category cat;
IDnum **counts = countShortReads(graph, reads);
IDnum nodes = nodeCount(graph);
for (index = 0; index < maxNodeIndex; index++) {
for (connect = scaffold[index]; connect != NULL;
connect = next) {
next = connect->right;
if (!testConnection(index - nodes, connect, counts, shadows))
destroyConnection(connect, index - nodes);
}
}
// Free memory
for (cat = 0; cat <= CATEGORIES; cat++)
if (counts[cat])
free(counts[cat]);
free(counts);
}
void printConnections(ReadSet * reads, boolean * shadows)
{
IDnum maxNodeIndex = nodeCount(graph) * 2 + 1;
IDnum index;
Connection *connect, *next;
Node *node;
IDnum **counts = countShortReads(graph, reads);
IDnum nodes = nodeCount(graph);
Category cat;
puts("CONNECT IDA IDB dcount pcount dist lengthA lengthB var countA countB coordA coordB real exp distance test");
for (index = 0; index < maxNodeIndex; index++) {
node = getNodeInGraph(graph, index - nodeCount(graph));
for (connect = scaffold[index]; connect != NULL;
connect = next) {
next = getNextConnection(connect);
printf
("CONNECT %ld %ld %ld %ld %lld %lld %lld %f %ld %ld",
(long) index - nodeCount(graph),
(long) getNodeID(connect->destination),
(long) connect->direct_count,
(long) connect->paired_count,
(long long) getConnectionDistance(connect),
(long long) getNodeLength(node), (long long)
getNodeLength(connect->destination),
connect->variance,
(long) getNodeReadCount(node, graph),
(long) getNodeReadCount(connect->destination,
graph));
if (markerCount(node) == 1
&& markerCount(connect->destination) == 1)
printf(" %lld %lld %lld", (long long)
getPassageMarkerFinish(getMarker
(node)),
(long long)
getPassageMarkerFinish(getMarker
(connect->
destination)),
(long
long) (getPassageMarkerFinish
(getMarker(node)) -
getPassageMarkerFinish
(getMarker
(connect->destination))));
else
printf(" ? ? ?");
printf(" %ld",
(long) expectedNumberOfConnections(index -
nodeCount
(graph),
connect,
counts,
0));
printf(" %lld",
(long long) (getConnectionDistance(connect)
- (getNodeLength(node) +
getNodeLength
(connect->destination)) /
2));
if (testConnection(index - nodes, connect, counts, shadows))
puts(" OK");
else
puts(" NG");
}
}
for (cat = 0; cat <= CATEGORIES; cat++)
if (counts[cat])
free(counts[cat]);
free(counts);
}
void buildScaffold(Graph * argGraph,
ReadSet * reads,
boolean * dubious,
boolean * shadows)
{
IDnum *readPairs;
Category *cats;
IDnum *readNodeCounts;
ReadOccurence **readNodes;
ReadOccurence *readNodesArray = NULL;
ShortLength *lengths = getSequenceLengths(reads, getWordLength(argGraph));
Coordinate totalCount = 0;
graph = argGraph;
readPairs = reads->mateReads;
cats = reads->categories;
// Prepare primary scaffold
readNodeCounts = computeReadToNodeCounts(&totalCount);
readNodes = computeReadToNodeMappings(readNodeCounts, reads, totalCount, &readNodesArray);
estimateMissingInsertLengths(readNodes, readNodeCounts, readPairs, cats);
scaffold = computeNodeToNodeMappings(readNodes, readNodeCounts,
readPairs, cats, dubious, shadows, lengths);
removeUnreliableConnections(reads, shadows);
free(readNodesArray);
free(readNodes);
free(readNodeCounts);
free(lengths);
}
//DEBUG
void printScaffold(Graph * argGraph,
ReadSet * reads,
boolean * dubious,
boolean * shadows)
{
IDnum *readPairs;
Category *cats;
IDnum *readNodeCounts;
ReadOccurence **readNodes;
ReadOccurence *readNodesArray = NULL;
ShortLength *lengths = getSequenceLengths(reads, getWordLength(argGraph));
Coordinate totalCount = 0;
graph = argGraph;
readPairs = reads->mateReads;
cats = reads->categories;
// Prepare primary scaffold
readNodeCounts = computeReadToNodeCounts(&totalCount);
readNodes = computeReadToNodeMappings(readNodeCounts, reads, totalCount, &readNodesArray);
estimateMissingInsertLengths(readNodes, readNodeCounts, readPairs, cats);
scaffold = computeNodeToNodeMappings(readNodes, readNodeCounts,
readPairs, cats, dubious, shadows, lengths);
printConnections(reads, shadows);
free(readNodesArray);
free(readNodes);
free(readNodeCounts);
free(lengths);
cleanScaffoldMemory();
}
void setUnreliableConnectionCutoff(int val)
{
UNRELIABLE_CONNECTION_CUTOFF = (IDnum) val;
}
void cleanScaffoldMemory() {
Category libID;
for (libID = 0; libID < CATEGORIES + 1; libID++)
if (estimated[libID])
setInsertLengths(graph, libID, -1, -1);
destroyRecycleBin(connectionMemory);
free(scaffold);
connectionMemory = NULL;
}
void setPairedExpFraction(double x) {
paired_exp_fraction = x;
}
|