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 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
|
##***********************************************************************
##
## Methods for EnsDb classes
##
##***********************************************************************
setMethod("show", "EnsDb", function(object) {
if (is.null(object@ensdb)) {
cat("Dash it! Got an empty thing!\n")
} else {
info <- dbGetQuery(object@ensdb, "select * from metadata")
cat("EnsDb for Ensembl:\n")
if (inherits(object@ensdb, "SQLiteConnection"))
cat(paste0("|Backend: SQLite\n"))
if (inherits(object@ensdb, "MySQLConnection"))
cat(paste0("|Backend: MySQL\n"))
for (i in 1:nrow(info)) {
cat(paste0("|", info[ i, "name" ], ": ",
info[ i, "value" ], "\n"))
}
## gene and transcript info.
cat(paste0("| No. of genes: ",
dbGetQuery(object@ensdb,
"select count(distinct gene_id) from gene")[1, 1],
".\n"))
cat(paste0("| No. of transcripts: ",
dbGetQuery(object@ensdb,
"select count(distinct tx_id) from tx")[1, 1],
".\n"))
if (hasProteinData(object))
cat("|Protein data available.\n")
flts <- .activeFilter(object)
if (is(flts, "AnnotationFilter") | is(flts, "AnnotationFilterList")) {
cat("|Active filter(s):\n")
show(flts)
}
}
})
############################################################
## organism
setMethod("organism", "EnsDb", function(object){
Species <- .getMetaDataValue(object@ensdb, "Organism")
## reformat the e.g. homo_sapiens string into Homo sapiens
#
Species <- gsub(Species, pattern="_", replacement=" ", fixed=TRUE)
Species <- .organismName(Species)
return(Species)
})
############################################################
## metadata
setMethod("metadata", "EnsDb", function(x, ...){
Res <- dbGetQuery(dbconn(x), "select * from metadata")
return(Res)
})
############################################################
## Validation
##
validateEnsDb <- function(object){
## check if the database contains all required tables...
if(!is.null(object@ensdb)){
msg <- validMsg(NULL, NULL)
OK <- dbHasRequiredTables(object@ensdb)
if (is.character(OK))
msg <- validMsg(msg, OK)
OK <- dbHasValidTables(object@ensdb)
if (is.character(OK))
msg <- validMsg(msg, OK)
if (hasProteinData(object)) {
OK <- dbHasRequiredTables(
object@ensdb,
tables = .ensdb_protein_tables(dbSchemaVersion(dbconn(object))))
if (is.character(OK))
msg <- validMsg(msg, OK)
OK <- dbHasValidTables(
object@ensdb,
tables = .ensdb_protein_tables(dbSchemaVersion(dbconn(object))))
if (is.character(OK))
msg <- validMsg(msg, OK)
cdsTx <- dbGetQuery(dbconn(object),
"select tx_id, tx_cds_seq_start from tx");
if (is.character(cdsTx$tx_cds_seq_start)) {
suppressWarnings(
cdsTx[, "tx_cds_seq_start"] <- as.numeric(cdsTx$tx_cds_seq_start)
)
}
cdsTx <- cdsTx[!is.na(cdsTx$tx_cds_seq_start), "tx_id"]
protTx <- dbGetQuery(dbconn(object),
"select distinct tx_id from protein")$tx_id
if (!all(cdsTx %in% protTx))
msg <- validMsg(msg, paste0("Not all transcripts with a CDS ",
"are assigned to a protein ID!"))
if (!all(protTx %in% cdsTx))
msg <- validMsg(msg, paste0("Not all proteins are assigned to ",
"a transcript with a CDS!"))
}
if (is.null(msg)) TRUE
else msg
}
return(TRUE)
}
setValidity("EnsDb", validateEnsDb)
setMethod("initialize", "EnsDb", function(.Object,...){
OK <- validateEnsDb(.Object)
if(class(OK)=="character"){
stop(OK)
}
callNextMethod(.Object, ...)
})
############################################################
## dbconn
setMethod("dbconn", "EnsDb", function(x){
return(x@ensdb)
})
############################################################
## ensemblVersion
##
## returns the ensembl version of the package.
setMethod("ensemblVersion", "EnsDb", function(x){
eVersion <- getMetadataValue(x, "ensembl_version")
return(eVersion)
})
############################################################
## getMetadataValue
##
## returns the metadata value for the specified name/key
setMethod("getMetadataValue", "EnsDb", function(x, name){
if(missing(name))
stop("Argument name has to be specified!")
return(metadata(x)[metadata(x)$name==name, "value"])
})
############################################################
## seqinfo
setMethod("seqinfo", "EnsDb", function(x){
Chrs <- dbGetQuery(dbconn(x), "select * from chromosome")
Chr.build <- .getMetaDataValue(dbconn(x), "genome_build")
Chrs$seq_name <- formatSeqnamesFromQuery(x, Chrs$seq_name)
SI <- Seqinfo(seqnames=Chrs$seq_name,
seqlengths=Chrs$seq_length,
isCircular=Chrs$is_circular==1, genome=Chr.build)
return(SI)
})
############################################################
## seqlevels
setMethod("seqlevels", "EnsDb", function(x){
Chrs <- dbGetQuery(dbconn(x), "select distinct seq_name from chromosome")
Chrs <- formatSeqnamesFromQuery(x, Chrs$seq_name)
return(Chrs)
})
############################################################
## getGenomeFaFile
##
## queries the dna.toplevel.fa file from AnnotationHub matching the current
## Ensembl version
## Update: if we can't find a FaFile matching the Ensembl version we suggest
## ones that might match.
setMethod("getGenomeFaFile", "EnsDb", function(x, pattern = "dna.toplevel.fa") {
if (!requireNamespace("AnnotationHub", quietly = TRUE)) {
stop("The 'AnnotationHub' package is needed for this function to ",
"work. Please install it.", call. = FALSE)
}
ah <- AnnotationHub::AnnotationHub()
## Reduce the AnnotationHub to species, provider and genome version.
ah <- .reduceAH(ah, organism = organism(x), dataprovider = "Ensembl",
genome = unique(genome(x)))
if(length(ah) == 0)
stop("Can not find any ressources in AnnotationHub for organism: ",
organism(x), ", data provider: Ensembl and genome version: ",
unique(genome(x)), "!")
## Reduce to all Fasta files with toplevel or primary_assembly.
ah <- ah[ah$rdataclass == "FaFile", ]
if(length(ah) == 0)
stop("No FaFiles available in AnnotationHub for organism: ",
organism(x), ", data provider: Ensembl and genome version: ",
unique(genome(x)), "! You might also try to use the",
" 'getGenomeTwoBitFile' method instead.")
## Reduce to dna.toplevel or dna.primary_assembly.
idx <- c(grep(ah$title, pattern = "dna.toplevel"),
grep(ah$title, pattern = "dna.primary_assembly"))
if(length(idx) == 0)
stop("No genome assembly fasta file available for organism: ",
organism(x), ", data provider: Ensembl and genome version: ",
unique(genome(x)), "!")
ah <- ah[idx, ]
## Get the Ensembl version from the source url.
ensVers <- .ensVersionFromSourceUrl(ah$sourceurl)
if(any(ensVers == ensemblVersion(x))){
## Got it.
itIs <- which(ensVers == ensemblVersion(x))
}else{
## Get the "closest" one.
diffs <- abs(ensVers - as.numeric(ensemblVersion(x)))
itIs <- which(diffs == min(diffs))[1]
message("Returning the Fasta file for Ensembl version ", ensVers[itIs],
" since no file for Ensembl version ", ensemblVersion(x),
" is available.")
}
## Getting the ressource.
Dna <- ah[[names(ah)[itIs]]]
## generate an index if none is available
if(is.na(index(Dna))){
indexFa(Dna)
Dna <- FaFile(path(Dna))
}
return(Dna)
})
## Just restricting the Annotation Hub to entries matching the species and the
## genome; not yet the Ensembl version.
.reduceAH <- function(ah, organism = NULL, dataprovider = "Ensembl",
genome = NULL){
if(!is.null(dataprovider))
ah <- ah[ah$dataprovider == dataprovider, ]
if(!is.null(organism))
ah <- ah[ah$species == organism, ]
if(!is.null(genome))
ah <- ah[ah$genome == genome, ]
return(ah)
}
.ensVersionFromSourceUrl <- function(url){
url <- strsplit(url, split="/", fixed=TRUE)
ensVers <- unlist(lapply(url, function(z){
idx <- grep(z, pattern="^release")
if(length(idx) == 0)
return(-1)
return(as.numeric(unlist(strsplit(z[idx], split="-"))[2]))
}))
return(ensVers)
}
############################################################
## getGenomeTwoBitFile
##
## Search and retrieve a genomic DNA resource through a TwoBitFile
## from AnnotationHub.
setMethod("getGenomeTwoBitFile", "EnsDb", function(x){
ah <- AnnotationHub::AnnotationHub()
## Reduce the AnnotationHub to species, provider and genome version.
ah <- .reduceAH(ah, organism = organism(x), dataprovider = "Ensembl",
genome = unique(genome(x)))
if(length(ah) == 0)
stop("Can not find any ressources in AnnotationHub for organism: ",
organism(x), ", data provider: Ensembl and genome version: ",
unique(genome(x)), "!")
## Reduce to all Fasta files with toplevel or primary_assembly.
ah <- ah[ah$rdataclass == "TwoBitFile", ]
if(length(ah) == 0)
stop("No TwoBitFile available in AnnotationHub for organism: ",
organism(x), ", data provider: Ensembl and genome version: ",
unique(genome(x)), "!")
## Reduce to dna.toplevel or dna.primary_assembly.
idx <- c(grep(ah$title, pattern = "dna.toplevel"),
grep(ah$title, pattern = "dna.primary_assembly"))
if(length(idx) == 0)
stop("No genome assembly fasta file available for organism: ",
organism(x), ", data provider: Ensembl and genome version: ",
unique(genome(x)), "!")
ah <- ah[idx, ]
## Get the Ensembl version from the source url.
ensVers <- .ensVersionFromSourceUrl(ah$sourceurl)
if(any(ensVers == ensemblVersion(x))) {
## Got it.
itIs <- which(ensVers == ensemblVersion(x))
} else {
## Get the "closest" one.
diffs <- abs(ensVers - as.numeric(ensemblVersion(x)))
itIs <- which(diffs == min(diffs))[1]
message("Returning the TwoBit file for Ensembl version ", ensVers[itIs],
" since no file for Ensembl version ", ensemblVersion(x),
" is available.")
}
## Getting the ressource.
Dna <- ah[[names(ah)[itIs]]]
return(Dna)
})
############################################################
## listTables
setMethod("listTables", "EnsDb", function(x, ...){
if(length(x@tables)==0){
tables <- dbListTables(dbconn(x))
## read the columns for these tables.
Tables <- vector(length=length(tables), "list")
for(i in 1:length(Tables)){
Tables[[ i ]] <- colnames(dbGetQuery(dbconn(x),
paste0("select * from ",
tables[ i ],
" limit 1")))
}
names(Tables) <- tables
x@tables <- Tables
}
Tab <- x@tables
Tab <- Tab[tablesByDegree(x, tab=names(Tab))]
## Manually add tx_name as a "virtual" column; getWhat will insert the tx_id into that.
Tab$tx <- unique(c(Tab$tx, "tx_name"))
## Manually add the symbol as a "virtual" column.
Tab$gene <- unique(c(Tab$gene, "symbol"))
return(Tab)
})
############################################################
## listColumns
setMethod("listColumns", "EnsDb", function(x,
table,
skip.keys=TRUE, ...){
if(length(x@tables)==0){
tables <- dbListTables(dbconn(x))
## read the columns for these tables.
Tables <- vector(length=length(tables), "list")
for(i in 1:length(Tables)){
Tables[[ i ]] <- colnames(dbGetQuery(dbconn(x),
paste0("select * from ",
tables[ i ],
" limit 1")))
}
names(Tables) <- tables
x@tables <- Tables
}
Tab <- x@tables
## Manually add tx_name as a "virtual" column; getWhat will insert
## the tx_id into that.
Tab$tx <- unique(c(Tab$tx, "tx_name"))
## Manually add the symbol as a "virtual" column.
Tab$gene <- unique(c(Tab$gene, "symbol"))
if(!missing(table)){
columns <- unlist(Tab[names(Tab) %in% table], use.names = FALSE)
}else{
columns <- unlist(Tab, use.names=FALSE)
}
if(skip.keys){
## remove everything that has a _pk or _fk...
idx <- grep(columns, pattern="_fk$")
if(length(idx) > 0)
columns <- columns[ -idx ]
idx <- grep(columns, pattern="_pk$")
if(length(idx) > 0)
columns <- columns[ -idx ]
}
return(unique(columns))
})
############################################################
## listGenebiotypes
setMethod("listGenebiotypes", "EnsDb", function(x, ...){
return(dbGetQuery(dbconn(x), "select distinct gene_biotype from gene")[,1])
})
############################################################
## listTxbiotypes
setMethod("listTxbiotypes", "EnsDb", function(x, ...){
return(dbGetQuery(dbconn(x), "select distinct tx_biotype from tx")[,1])
})
############################################################
## cleanColumns
##
## checks columns and removes all that are not present in database tables
## the method checks internally whether the columns are in the full form,
## i.e. gene.gene_id (<table name>.<column name>)
setMethod("cleanColumns", "EnsDb", function(x, columns, ...){
if(missing(columns))
stop("No columns submitted!")
## vote of the majority
full.name <- length(grep(columns, pattern=".", fixed=TRUE)) >
floor(length(columns) / 2)
if (full.name) {
suppressWarnings(
full.columns <- unlist(prefixColumns(x,
unlist(listTables(x)),
clean = FALSE),
use.names=TRUE)
)
bm <- columns %in% full.columns
removed <- columns[ !bm ]
} else {
dbtabs <- names(listTables(x))
dbtabs <- dbtabs[dbtabs != "metadata"]
bm <- columns %in% unlist(listTables(x)[dbtabs])
removed <- columns[!bm]
}
if(length(removed) > 0){
if (length(removed) == 1)
warning("Column ", paste(sQuote(removed), collapse=", "),
" is not present in the database and has been removed")
else
warning("Columns ", paste(sQuote(removed), collapse=", "),
" are not present in the database and have been removed")
}
return(columns[bm])
})
############################################################
## tablesForColumns
##
## returns the tables for the specified columns.
setMethod("tablesForColumns", "EnsDb", function(x, columns, ...){
if(missing(columns))
stop("No columns submitted!")
bm <- unlist(lapply(listTables(x), function(z){
return(any(z %in% columns))
}))
if(!any(bm))
return(NULL)
Tables <- names(bm)[ bm ]
Tables <- Tables[ !(Tables %in% c("metadata")) ]
return(Tables)
})
############################################################
## tablesByDegree
##
## returns the table names ordered by degree, i.e. edges to other tables
setMethod("tablesByDegree", "EnsDb", function(x,
tab=names(listTables(x)),
...){
Table.order <- c(gene = 1, tx = 2, tx2exon = 3, exon = 4, chromosome = 5,
protein = 6, uniprot = 7, protein_domain = 8,
entrezgene = 9,
metadata = 99)
Tab <- tab[ order(Table.order[ tab ]) ]
return(Tab)
})
############################################################
## hasProteinData
##
## Simply check if the database has required tables protein, uniprot
## and protein_domain.
#' @title Determine whether protein data is available in the database
#'
#' @aliases hasProteinData
#'
#' @description Determines whether the \code{\linkS4class{EnsDb}}
#' provides protein annotation data.
#'
#' @param x The \code{\linkS4class{EnsDb}} object.
#'
#' @return A logical of length one, \code{TRUE} if protein annotations are
#' available and \code{FALSE} otherwise.
#'
#' @author Johannes Rainer
#'
#' @seealso \code{\link{listTables}}
#'
#' @examples
#' library(EnsDb.Hsapiens.v86)
#' ## Does this database/package have protein annotations?
#' hasProteinData(EnsDb.Hsapiens.v86)
setMethod("hasProteinData", "EnsDb", function(x) {
tabs <- listTables(x)
return(all(c("protein", "uniprot", "protein_domain") %in%
names(tabs)))
})
############################################################
## genes
##
## get genes from the database.
setMethod("genes", "EnsDb", function(x,
columns = c(listColumns(x, "gene"),
"entrezid"),
filter = AnnotationFilterList(),
order.by = "",
order.type = "asc",
return.type = "GRanges"){
return.type <- match.arg(return.type, c("data.frame", "GRanges", "DataFrame"))
columns <- cleanColumns(x, unique(c(columns, "gene_id")))
## if return.type is GRanges we require columns: seq_name, gene_seq_start
## and gene_seq_end and seq_strand
if(return.type=="GRanges"){
columns <- unique(c(columns, c("gene_seq_start", "gene_seq_end",
"seq_name", "seq_strand")))
}
filter <- .processFilterParam(filter, x)
filter <- setFeatureInGRangesFilter(filter, "gene")
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, x)
retColumns <- columns
## If we don't have an order.by define one.
if(all(order.by == "")){
order.by <- NULL
if (any(columns == "seq_name"))
order.by <- c(order.by, "seq_name")
if( any(columns == "gene_seq_start"))
order.by <- c(order.by, "gene_seq_start")
if(is.null(order.by))
order.by <- ""
}
Res <- getWhat(x, columns=columns, filter=filter,
order.by=order.by, order.type=order.type,
startWith = "gene", join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "gene_id")
if (return.type=="data.frame" | return.type=="DataFrame") {
notThere <- !(retColumns %in% colnames(Res))
if(any(notThere))
warning("Columns ",
paste0("'", retColumns[notThere], "'", collapse=", "),
" not found in the database!")
retColumns <- retColumns[!notThere]
Res <- Res[, retColumns, drop = FALSE]
if(return.type=="DataFrame")
Res <- DataFrame(Res)
return(Res)
}
if (return.type=="GRanges") {
metacols <- columns[ !(columns %in% c("seq_name",
"seq_strand",
"gene_seq_start",
"gene_seq_end")) ]
suppressWarnings(
SI <- seqinfo(x)
)
SI <- SI[as.character(unique(Res$seq_name))]
GR <- GRanges(seqnames=Rle(Res$seq_name),
ranges=IRanges(start=Res$gene_seq_start, end=Res$gene_seq_end),
strand=Rle(Res$seq_strand),
seqinfo=SI[as.character(unique(Res$seq_name))],
Res[ , metacols, drop=FALSE ]
)
names(GR) <- Res$gene_id
return(GR)
}
})
############################################################
## transcripts:
##
## get transcripts from the database.
setMethod("transcripts", "EnsDb", function(x, columns = listColumns(x, "tx"),
filter = AnnotationFilterList(),
order.by = "", order.type = "asc",
return.type = "GRanges"){
return.type <- match.arg(return.type, c("data.frame", "GRanges", "DataFrame"))
columns <- cleanColumns(x, unique(c(columns, "tx_id")))
## if return.type is GRanges we require columns: seq_name, gene_seq_start
## and gene_seq_end and seq_strand
if(return.type=="GRanges"){
columns <- unique(c(columns, c("tx_seq_start",
"tx_seq_end",
"seq_name",
"seq_strand")))
}
filter <- .processFilterParam(filter, x)
filter <- setFeatureInGRangesFilter(filter, "tx")
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, x)
retColumns <- columns
## If we don't have an order.by define one.
if(all(order.by == "")){
order.by <- NULL
if(any(columns == "seq_name"))
order.by <- c(order.by, "seq_name")
if(any(columns == "tx_seq_start"))
order.by <- c(order.by, "tx_seq_start")
if(is.null(order.by))
order.by <- ""
}
Res <- getWhat(x, columns=columns, filter = filter,
order.by=order.by, order.type=order.type,
startWith = "tx", join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "tx_id")
if(return.type=="data.frame" | return.type=="DataFrame"){
notThere <- !(retColumns %in% colnames(Res))
if(any(notThere))
warning("Columns ", paste0("'", retColumns[notThere], "'",
collapse=", "),
" not found in the database!")
retColumns <- retColumns[!notThere]
Res <- Res[, retColumns, drop = FALSE]
if(return.type=="DataFrame")
Res <- DataFrame(Res)
return(Res)
}
if(return.type=="GRanges"){
notThere <- !(columns %in% colnames(Res))
if(any(notThere))
warning(paste0("Columns ", paste(columns[notThere], collapse=", "),
" not present in the result data.frame!"))
columns <- columns[!notThere]
metacols <- columns[ !(columns %in% c("seq_name",
"seq_strand",
"tx_seq_start",
"tx_seq_end")) ]
suppressWarnings(
SI <- seqinfo(x)
)
SI <- SI[as.character(unique(Res$seq_name))]
GR <- GRanges(seqnames=Rle(Res$seq_name),
ranges=IRanges(start=Res$tx_seq_start, end=Res$tx_seq_end),
strand=Rle(Res$seq_strand),
seqinfo=SI[as.character(unique(Res$seq_name))],
Res[ , metacols, drop=FALSE ]
)
names(GR) <- Res$tx_id
return(GR)
}
})
############################################################
## promoters:
##
setMethod("promoters", "EnsDb",
function(x, upstream=2000, downstream=200, ...)
{
gr <- transcripts(x, ...)
trim(suppressWarnings(promoters(gr,
upstream=upstream,
downstream=downstream)))
}
)
############################################################
## exons
##
## get exons from the database.
setMethod("exons", "EnsDb", function(x, columns = listColumns(x, "exon"),
filter = AnnotationFilterList(),
order.by = "", order.type = "asc",
return.type = "GRanges"){
return.type <- match.arg(return.type, c("data.frame", "GRanges", "DataFrame"))
if(!any(columns %in% c(listColumns(x, "exon"), "exon_idx"))){
## have to have at least one column from the gene table...
columns <- c(columns, "exon_id")
}
columns <- cleanColumns(x, unique(c(columns, "exon_id")))
## if return.type is GRanges we require columns: seq_name, gene_seq_start
## and gene_seq_end and seq_strand
if(return.type=="GRanges"){
columns <- unique(c(columns, c("exon_seq_start",
"exon_seq_end",
"seq_name",
"seq_strand")))
}
filter <- .processFilterParam(filter, x)
filter <- setFeatureInGRangesFilter(filter, "exon")
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, x)
retColumns <- columns
## If we don't have an order.by define one.
if (order.by == "") {
order.by <- NULL
if (any(columns == "seq_name"))
order.by <- c(order.by, "seq_name")
if (any(columns == "exon_seq_start"))
order.by <- c(order.by, "exon_seq_start")
if(is.null(order.by))
order.by <- ""
}
Res <- getWhat(x, columns=columns, filter=filter,
order.by=order.by, order.type=order.type,
startWith = "exon", join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "exon_id")
if(return.type=="data.frame" | return.type=="DataFrame"){
notThere <- !(retColumns %in% colnames(Res))
if(any(notThere))
warning("Columns ", paste0("'", retColumns[notThere], "'",
collapse=", "),
" not found in the database!")
retColumns <- retColumns[!notThere]
Res <- Res[, retColumns, drop = FALSE]
if(return.type=="DataFrame")
Res <- DataFrame(Res)
return(Res)
}
if(return.type=="GRanges"){
notThere <- !(columns %in% colnames(Res))
if(any(notThere))
warning(paste0("Columns ", paste(columns[notThere], collapse=", "),
" not present in the result data.frame!"))
columns <- columns[!notThere]
metacols <- columns[ !(columns %in% c("seq_name",
"seq_strand",
"exon_seq_start",
"exon_seq_end")) ]
suppressWarnings(
SI <- seqinfo(x)
)
SI <- SI[as.character(unique(Res$seq_name))]
GR <- GRanges(seqnames=Rle(Res$seq_name),
ranges=IRanges(start=Res$exon_seq_start, end=Res$exon_seq_end),
strand=Rle(Res$seq_strand),
seqinfo=SI[as.character(unique(Res$seq_name))],
Res[ , metacols, drop=FALSE ]
)
names(GR) <- Res$exon_id
return(GR)
}
})
############################################################
## exonsBy
##
## should return a GRangesList
setMethod("exonsBy", "EnsDb", function(x, by = c("tx", "gene"),
columns = listColumns(x, "exon"),
filter = AnnotationFilterList(),
use.names = FALSE) {
by <- match.arg(by, c("tx", "gene"))
bySuff <- "_id"
if (use.names) {
if (by == "tx") {
use.names <- FALSE
warning("Argument use.names ignored as no transcript names are available.")
} else {
columns <- unique(c(columns, "gene_name"))
bySuff <- "_name"
}
}
filter <- .processFilterParam(filter, x)
## We're applying eventual GRangesFilter to either gene or tx.
filter <- setFeatureInGRangesFilter(filter, by)
## Eventually add columns for the filters:
columns <- cleanColumns(x, unique(c(columns, "exon_id")))
columns <- addFilterColumns(columns, filter, x)
## Quick fix; rename any exon_rank to exon_idx.
columns[columns == "exon_rank"] <- "exon_idx"
## The minimum columns we need, in addition to "columns"
min.columns <- c(paste0(by, "_id"), "seq_name","exon_seq_start",
"exon_seq_end", "exon_id", "seq_strand")
by.id.full <- unlist(prefixColumns(x, columns = paste0(by, "_id"),
clean = FALSE),
use.names = FALSE)
if (by == "gene") {
## tx columns have to be removed, since the same exon can be part of
## more than one tx
txcolumns <- c(listColumns(x, "tx"), "exon_idx")
txcolumns <- txcolumns[txcolumns != "gene_id"]
torem <- columns %in% txcolumns
if (any(torem))
warning("Columns ",
paste(columns[ torem ], collapse = ","),
" have been removed as they are not allowed if exons",
" are fetched by gene.")
columns <- columns[!torem]
} else {
min.columns <- unique(c(min.columns, "exon_idx"))
columns <- c(columns, "exon_idx")
}
## define the minimal columns that we need...
ret_cols <- unique(columns) ## before adding the "min.columns"
columns <- unique(c(columns, min.columns))
## get the seqinfo:
suppressWarnings(
SI <- seqinfo(x)
)
## Resolve ordering problems.
orderR <- orderResultsInR(x)
if (orderR) {
order.by <- ""
} else {
if (by == "gene") {
order.by <- paste0("gene.gene_id, ",
"case when seq_strand = 1 then exon_seq_start",
" when seq_strand = -1 then (exon_seq_end * -1)",
" end")
} else {
## Funny thing is the query takes longer if I use tx2exon.tx_id!
order.by <- "tx.gene_id, tx.tx_id, tx2exon.exon_idx"
}
}
Res <- getWhat(x, columns = columns, filter = filter,
order.by = order.by, skip.order.check = TRUE,
startWith = by, join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "exon_id")
## Now, order in R, if not already done in SQL.
if (orderR) {
if (by == "gene") {
startend <- (Res$seq_strand == 1) * Res$exon_seq_start +
(Res$seq_strand == -1) * (Res$exon_seq_end * -1)
Res <- Res[order(Res$gene_id, startend,
method = "radix"), ]
} else {
Res <- Res[order(Res$tx_id, Res$exon_idx,
method = "radix"), ]
}
}
SI <- SI[as.character(unique(Res$seq_name))]
## replace exon_idx with exon_rank
colnames(Res)[colnames(Res) == "exon_idx"] <- "exon_rank"
columns[columns == "exon_idx"] <- "exon_rank"
ret_cols[ret_cols == "exon_idx"] <- "exon_rank"
notThere <- !(ret_cols %in% colnames(Res))
if (any(notThere))
warning("Columns ", paste0("'", ret_cols[notThere], "'",
collapse = ", "),
" not found in the database!")
ret_cols <- ret_cols[!notThere]
columns.metadata <- ret_cols[!(ret_cols %in% c("seq_name", "seq_strand",
"exon_seq_start",
"exon_seq_end"))]
columns.metadata <- match(columns.metadata, colnames(Res))
GR <- GRanges(seqnames = Rle(Res$seq_name),
strand = Rle(Res$seq_strand),
ranges = IRanges(start = Res$exon_seq_start,
end = Res$exon_seq_end),
seqinfo = SI,
Res[, columns.metadata, drop=FALSE]
)
split(GR, Res[, paste0(by, bySuff)])
})
############################################################
## transcriptsBy
##
setMethod("transcriptsBy", "EnsDb", function(x, by = c("gene", "exon"),
columns = listColumns(x, "tx"),
filter = AnnotationFilterList()) {
if (any(by == "cds"))
stop("fetching transcripts by cds is not (yet) implemented.")
by <- match.arg(by, c("gene", "exon"))
byId <- paste0(by, "_id")
min.columns <- c(paste0(by, "_id"), "seq_name", "tx_seq_start",
"tx_seq_end", "tx_id", "seq_strand")
## can not have exon columns!
ex_cols <- c(listColumns(x, "exon"), "exon_idx")
ex_cols <- ex_cols[ex_cols != "tx_id"]
torem <- columns %in% ex_cols
if (any(torem))
warning("Columns ",
paste(columns[ torem ], collapse=","),
" have been removed as they are not allowed if",
" transcripts are fetched.")
columns <- columns[!torem]
## Process filters
filter <- .processFilterParam(filter, x)
## GRanges filter should be based on either gene or exon coors.
filter <- setFeatureInGRangesFilter(filter, by)
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, x)
ret_cols <- unique(columns)
## define the minimal columns that we need...
columns <- cleanColumns(x, unique(c(columns, min.columns)))
## get the seqinfo:
suppressWarnings(
SI <- seqinfo(x)
)
byIdFull <- unlist(prefixColumns(x, columns = byId, clean = FALSE),
use.names = FALSE)
orderR <- orderResultsInR(x)
if (orderR) {
order.by <- ""
} else {
order.by <- paste0(byIdFull ,
", case when seq_strand = 1 then tx_seq_start",
" when seq_strand = -1 then (tx_seq_end * -1) end")
}
Res <- getWhat(x, columns=columns, filter=filter,
order.by=order.by, skip.order.check=TRUE,
startWith = by, join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "tx_id")
if (orderR) {
startEnd <- (Res$seq_strand == 1) * Res$tx_seq_start +
(Res$seq_strand == -1) * (Res$tx_seq_end * -1)
Res <- Res[order(Res[, byId], startEnd, method = "radix"), ]
}
SI <- SI[as.character(unique(Res$seq_name))]
## Replace exon_idx with exon_rank
colnames(Res) <- gsub(colnames(Res), pattern = "exon_idx",
replacement = "exon_rank", fixed = TRUE)
ret_cols[ret_cols == "exon_idx"] <- "exon_rank"
notThere <- !(ret_cols %in% colnames(Res))
if(any(notThere))
warning("Columns ", paste0("'", ret_cols[notThere], "'", collapse=", "),
" not found in the database!")
ret_cols <- ret_cols[!notThere]
columns.metadata <- ret_cols[!(ret_cols %in% c("seq_name", "seq_strand",
"tx_seq_start",
"tx_seq_end"))]
columns.metadata <- match(columns.metadata, colnames(Res))
GR <- GRanges(seqnames=Rle(Res$seq_name),
strand=Rle(Res$seq_strand),
ranges=IRanges(start=Res$tx_seq_start, end=Res$tx_seq_end),
seqinfo=SI,
Res[ , columns.metadata, drop=FALSE ]
)
return(split(GR, Res[ , byId]))
})
############################################################
## lengthOf
## for GRangesList...
setMethod("lengthOf", "GRangesList", function(x, ...){
return(sum(width(reduce(x))))
## return(unlist(lapply(width(reduce(x)), sum)))
})
## return the length of genes or transcripts
setMethod("lengthOf", "EnsDb", function(x, of="gene",
filter=AnnotationFilterList()){
of <- match.arg(of, c("gene", "tx"))
## get the exons by gene or transcript from the database...
suppressWarnings(
GRL <- exonsBy(x, by=of, filter=filter)
)
return(lengthOf(GRL))
})
####============================================================
## transcriptLengths
##
## For TxDb: calls just the function (not method!) from the GenomicFeatures
## package.
## For EnsDb: calls the .transcriptLengths function.
.transcriptLengths <- function(x, with.cds_len = FALSE, with.utr5_len = FALSE,
with.utr3_len = FALSE,
filter = AnnotationFilterList()) {
filter <- .processFilterParam(filter, x)
allTxs <- transcripts(x, filter = filter)
if (length(allTxs) == 0)
return(data.frame(tx_id = character(), gene_id = character(),
nexon = integer(), tx_len = integer()))
exns <- exonsBy(x, filter = TxIdFilter(allTxs$tx_id))
## Match ordering
exns <- exns[match(allTxs$tx_id, names(exns))]
## Calculate length of transcripts.
txLengths <- sum(width(exns))
## Calculate no. of exons.
## build result data frame:
Res <- data.frame(tx_id = allTxs$tx_id, gene_id = allTxs$gene_id,
nexon = lengths(exns), tx_len = txLengths,
stringsAsFactors = FALSE)
if(!any(c(with.cds_len, with.utr5_len, with.utr3_len))) {
## Return what we've got thus far.
return(Res)
}
if (with.cds_len)
Res$cds_len <- NA
if (with.utr5_len)
Res$utr5_len <- NA
if (with.utr3_len)
Res$utr3_len <- NA
## Otherwise do the remaining stuff...
txs <- allTxs[!is.na(allTxs$tx_cds_seq_start)]
if (length(txs) > 0) {
cExns <- exns[txs$tx_id]
cReg <- GRanges(seqnames = seqnames(txs),
ranges = IRanges(start = txs$tx_cds_seq_start,
end = txs$tx_cds_seq_end),
strand = strand(txs),
tx_id = txs$tx_id)
cReg <- split(cReg, f = cReg$tx_id)
## Match order.
cReg <- cReg[match(txs$tx_id, names(cReg))]
cdsExns <- intersect(cReg, cExns)
## cExns: all exons of coding transcripts (includes untranslated
## and translated region)
## cReg: just the start-end position of the coding region of the tx.
## cdsExns: the coding part of all exons of the tx.
if (with.cds_len) {
## Calculate CDS length
cdsLengths <- sum(width(cdsExns))
Res[names(cdsLengths), "cds_len"] <- cdsLengths
}
if (with.utr3_len | with.utr5_len) {
## ! UTR is the difference between the exons and the cds-exons
## Note: order of parameters is important!
utrReg <- setdiff(cExns, cdsExns)
leftOfCds <- utrReg[end(utrReg) < start(cReg)]
rightOfCds <- utrReg[start(utrReg) > end(cReg)]
## Calculate lengths.
leftOfLengths <- sum(width(leftOfCds))
rightOfLengths <- sum(width(rightOfCds))
minusTx <- which(as.character(strand(txs)) == "-" )
if (with.utr3_len) {
## Ordering of txs and all other stuff matches.
tmp <- rightOfLengths
tmp[minusTx] <- leftOfLengths[minusTx]
Res[names(tmp), "utr3_len"] <- tmp
}
if (with.utr5_len) {
tmp <- leftOfLengths
tmp[minusTx] <- rightOfLengths[minusTx]
Res[names(tmp), "utr5_len"] <- tmp
}
}
}
Res
}
############################################################
## cdsBy
##
## Return coding region ranges by tx or by gene.
setMethod("cdsBy", "EnsDb", function(x, by = c("tx", "gene"),
columns = NULL,
filter = AnnotationFilterList(),
use.names = FALSE){
by <- match.arg(by, c("tx", "gene"))
filter <- .processFilterParam(filter, x)
filter <- setFeatureInGRangesFilter(filter, by)
columns <- cleanColumns(x, columns)
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, x)
## Add a filter ensuring that only coding transcripts are queried.
filter <- AnnotationFilterList(OnlyCodingTxFilter() ,filter)
bySuff <- "_id"
if (by == "tx") {
## adding exon_id, exon_idx to the columns.
columns <- unique(c(columns, "exon_id", "exon_idx"))
if (use.names)
warning("Not considering use.names as no transcript names are",
" available.")
} else {
columns <- unique(c("gene_id", columns))
if( use.names) {
bySuff <- "_name"
columns <- c(columns, "gene_name")
}
}
byId <- paste0(by, bySuff)
## Query the data
fetchCols <- unique(c(byId, columns, "tx_cds_seq_start", "tx_cds_seq_end",
"seq_name", "seq_strand", "exon_idx", "exon_id",
"exon_seq_start", "exon_seq_end"))
## Ordering of the results:
## Force ordering in R by default here to fix issue #11
##orderR <- orderResultsInR(x)
orderR <- TRUE
if (orderR) {
order.by <- ""
} else {
if (by == "tx") {
## Here we want to sort the exons by exon_idx
order.by <- "tx.tx_id, tx2exon.exon_idx"
} else {
## Here we want to sort the transcripts by tx start.
order.by <- paste0("gene.gene_id, case when seq_strand = 1 then",
" tx_cds_seq_start when seq_strand = -1 then",
"(tx_cds_seq_end * -1) end")
}
}
Res <- getWhat(x, columns = fetchCols,
filter = filter,
order.by = order.by,
skip.order.check = TRUE,
startWith = by, join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "exon_id")
## Remove rows with NA in tx_cds_seq_start; that's the case for "old"
## databases.
nas <- is.na(Res$tx_cds_seq_start)
if (any(nas))
Res <- Res[!nas, ]
## Remove exons that are not within the cds.
Res <- Res[Res$exon_seq_end >= Res$tx_cds_seq_start &
Res$exon_seq_start <= Res$tx_cds_seq_end,
, drop = FALSE]
if (orderR) {
## And finally ordering them.
if (by == "tx") {
Res <- Res[order(Res$tx_id, Res$exon_idx, method = "radix"), ]
} else {
startend <- (Res$seq_strand == 1) * Res$tx_cds_seq_start +
(Res$seq_strand == -1) * (Res$tx_cds_seq_end * -1)
Res <- Res[order(Res$gene_id, startend, method = "radix"), ]
}
}
if(nrow(Res)==0){
warning("No cds found!")
return(NULL)
}
cdsStarts <- pmax.int(Res$exon_seq_start, Res$tx_cds_seq_start)
cdsEnds <- pmin.int(Res$exon_seq_end, Res$tx_cds_seq_end)
## get the seqinfo:
suppressWarnings(
SI <- seqinfo(x)
)
SI <- SI[as.character(unique(Res$seq_name))]
## Rename columns exon_idx to exon_rank, if present
if(any(colnames(Res) == "exon_idx")){
colnames(Res)[colnames(Res) == "exon_idx"] <- "exon_rank"
columns[columns == "exon_idx"] <- "exon_rank"
}
## Building the result.
if(length(columns) > 0){
notThere <- !(columns %in% colnames(Res))
if(any(notThere))
warning(paste0("Columns ", paste(columns[notThere], collapse=", "),
" not present in the result data.frame!"))
columns <- columns[!notThere]
GR <- GRanges(seqnames=Rle(Res$seq_name),
strand=Rle(Res$seq_strand),
ranges=IRanges(start=cdsStarts, end=cdsEnds),
seqinfo=SI,
Res[, columns, drop=FALSE])
}else{
GR <- GRanges(seqnames=Rle(Res$seq_name),
strand=Rle(Res$seq_strand),
ranges=IRanges(start=cdsStarts, end=cdsEnds),
seqinfo=SI)
}
GR <- split(GR, Res[, paste0(by, bySuff)])
## For "by gene" we reduce the redundant ranges;
## that way we loose however all additional columns!
if(by == "gene")
GR <- reduce(GR)
return(GR)
})
############################################################
## getUTRsByTranscript
##
getUTRsByTranscript <- function(x, what, columns = NULL,
filter = AnnotationFilterList()) {
filter <- .processFilterParam(filter, x)
columns <- cleanColumns(x, columns)
filter <- setFeatureInGRangesFilter(filter, "tx")
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, x)
columns <- unique(c(columns, "exon_id", "exon_idx"))
## Add the filter for coding tx only.
filter <- AnnotationFilterList(OnlyCodingTxFilter(), filter)
## what do we need: tx_cds_seq_start, tx_cds_seq_end and exon_idx
fetchCols <- unique(c("tx_id", columns, "tx_cds_seq_start",
"tx_cds_seq_end", "seq_name", "seq_strand",
"exon_seq_start", "exon_seq_end"))
order.by <- "tx.tx_id"
## get the seqinfo:
suppressWarnings(
SI <- seqinfo(x)
)
## Note: doing that with a single query and some coordinate juggling
## is faster than calling exonsBy and GRangesList setdiff etc.
Res <- getWhat(x, columns=fetchCols,
filter=filter,
order.by=order.by,
skip.order.check=TRUE,
startWith = "tx", join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(x)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "exon_id")
nas <- is.na(Res$tx_cds_seq_start)
if (any(nas))
Res <- Res[!nas, ]
## Remove exons that are within the cds.
Res <- Res[Res$exon_seq_start < Res$tx_cds_seq_start |
Res$exon_seq_end > Res$tx_cds_seq_end, , drop=FALSE]
if (nrow(Res) == 0) {
warning(paste0("No ", what, "UTR found!"))
return(NULL)
}
## Rename columns exon_idx to exon_rank, if present
if (any(colnames(Res) == "exon_idx")) {
colnames(Res) <- sub(colnames(Res), pattern = "exon_idx",
replacement = "exon_rank", fixed = TRUE)
columns[columns == "exon_idx"] <- "exon_rank"
}
if (what == "five") {
## All those on the forward strand for which the exon start is smaller
## than the cds start and those on the reverse strand with an exon end
## larger than the cds end.
Res <- Res[(Res$seq_strand > 0 & Res$exon_seq_start < Res$tx_cds_seq_start)
| (Res$seq_strand < 0 & Res$exon_seq_end > Res$tx_cds_seq_end),
, drop=FALSE]
} else {
## Other way round.
Res <- Res[(Res$seq_strand > 0 & Res$exon_seq_end > Res$tx_cds_seq_end) |
(Res$seq_strand < 0 & Res$exon_seq_start < Res$tx_cds_seq_start),
, drop=FALSE]
}
if (nrow(Res) == 0) {
warning(paste0("No ", what, "UTR found!"))
return(NULL)
}
## Increase the cds end by 1 and decrease the start by 1, thus,
## avoiding that the UTR overlaps the cds
Res$tx_cds_seq_end <- Res$tx_cds_seq_end + 1L
Res$tx_cds_seq_start <- Res$tx_cds_seq_start - 1L
utrStarts <- rep(0, nrow(Res))
utrEnds <- utrStarts
## Distinguish between stuff which is left of and right of the CDS:
## Left of the CDS: can be either 5' for + strand or 3' for - strand.
bm <- which(Res$exon_seq_start <= Res$tx_cds_seq_start)
if (length(bm) > 0) {
if (what == "five") {
## 5' and left of CDS means we're having 5' CDSs
bm <- bm[Res$seq_strand[bm] > 0]
if(length(bm) > 0){
utrStarts[bm] <- Res$exon_seq_start[bm]
utrEnds[bm] <- pmin.int(Res$exon_seq_end[bm],
Res$tx_cds_seq_start[bm])
}
} else {
bm <- bm[Res$seq_strand[bm] < 0]
if (length(bm) > 0) {
utrStarts[bm] <- Res$exon_seq_start[bm]
utrEnds[bm] <- pmin.int(Res$exon_seq_end[bm],
Res$tx_cds_seq_start[bm])
}
}
}
## Right of the CDS: can be either 5' for - strand of 3' for + strand.
bm <- which(Res$exon_seq_end >= Res$tx_cds_seq_end)
if (length(bm) > 0) {
if (what == "five") {
## Right of CDS is 5' for - strand.
bm <- bm[Res$seq_strand[bm] < 0]
if (length(bm) > 0) {
utrStarts[bm] <- pmax.int(Res$exon_seq_start[bm],
Res$tx_cds_seq_end[bm])
utrEnds[bm] <- Res$exon_seq_end[bm]
}
} else {
## Right of CDS is 3' for + strand
bm <- bm[Res$seq_strand[bm] > 0]
if (length(bm) > 0) {
utrStarts[bm] <- pmax.int(Res$exon_seq_start[bm],
Res$tx_cds_seq_end[bm])
utrEnds[bm] <- Res$exon_seq_end[bm]
}
}
}
notThere <- !(columns %in% colnames(Res))
if (any(notThere))
warning(paste0("Columns ", paste(columns[notThere], collapse=", "),
" not present in the result data.frame!"))
columns <- columns[!notThere]
SI <- SI[as.character(unique(Res$seq_name))]
GR <- GRanges(seqnames = Rle(Res$seq_name),
strand = Rle(Res$seq_strand),
ranges = IRanges(start=utrStarts, end=utrEnds),
seqinfo = SI,
Res[, columns, drop = FALSE])
GR <- split(GR, Res[, "tx_id"])
return(GR)
}
############################################################
## threeUTRsByTranscript
##
setMethod("threeUTRsByTranscript", "EnsDb",
function(x, columns = NULL, filter = AnnotationFilterList()) {
filter <- .processFilterParam(filter, x)
getUTRsByTranscript(x = x, what = "three", columns = columns,
filter = filter)
})
############################################################
## fiveUTRsByTranscript
##
setMethod("fiveUTRsByTranscript", "EnsDb",
function(x, columns = NULL, filter = AnnotationFilterList()) {
filter <- .processFilterParam(filter, x)
getUTRsByTranscript(x = x, what = "five", columns = columns,
filter = filter)
})
############################################################
## toSAF... function to transform a GRangesList into a data.frame
## corresponding to the SAF format.
## assuming the names of the GRangesList to be the GeneID and the
## element (GRanges) the start/end coordinates
## of an exon, transcript or the gene itself.
.toSaf <- function(x){
DF <- as.data.frame(x)
colnames(DF)[ colnames(DF)=="group_name" ] <- "GeneID"
colnames(DF)[ colnames(DF)=="seqnames" ] <- "Chr"
colnames(DF)[ colnames(DF)=="start" ] <- "Start"
colnames(DF)[ colnames(DF)=="end" ] <- "End"
colnames(DF)[ colnames(DF)=="strand" ] <- "Strand"
return(DF[ , c("GeneID", "Chr", "Start", "End", "Strand")])
}
## for GRangesList...
setMethod("toSAF", "GRangesList", function(x, ...){
return(.toSaf(x))
})
############################################################
## buildQuery
setMethod("buildQuery", "EnsDb",
function(x, columns=c("gene_id", "gene_biotype", "gene_name"),
filter = AnnotationFilterList(), order.by="",
order.type="asc",
skip.order.check=FALSE){
return(.buildQuery(x=x,
columns=columns,
filter=filter,
order.by=order.by,
order.type=order.type,
skip.order.check=skip.order.check))
})
############################################################
## getWhat
##
## Method that wraps the internal .getWhat function to retrieve data from the
## database. In addition, if present, we're renaming chromosome names depending
## on the ucscChromosomeNames option.
## Additional parameters:
## o startWith: the name of the database table from which the join should start
## or NULL for the default behaviour (i.e. genes-> tx etc).
## o join: the type of join that should be used; one of "join",
## "left outer join" or "suggested".
setMethod("getWhat", "EnsDb",
function(x, columns = c("gene_id", "gene_biotype", "gene_name"),
filter = AnnotationFilterList(), order.by = "",
order.type = "asc", group.by = NULL,
skip.order.check = FALSE, startWith = NULL,
join = "suggested") {
Res <- .getWhat(x = x,
columns = columns,
filter = filter,
order.by = order.by,
order.type = order.type,
group.by = group.by,
skip.order.check = skip.order.check,
startWith = startWith,
join = join)
## Eventually renaming seqnames according to the specified style.
if(any(colnames(Res) == "seq_name"))
Res$seq_name <- formatSeqnamesFromQuery(x, Res$seq_name)
return(Res)
})
############################################################
## disjointExons
##
## that's similar to the code from the GenomicFeatures package.
setMethod("disjointExons", "EnsDb",
function(x, aggregateGenes = FALSE, includeTranscripts = TRUE,
filter = AnnotationFilterList(), ...){
filter <- .processFilterParam(filter, x)
exonsByGene <- exonsBy(x, by = "gene", filter = filter)
exonicParts <- disjoin(unlist(exonsByGene, use.names = FALSE))
if (aggregateGenes) {
foGG <- findOverlaps(exonsByGene, exonsByGene)
aggregateNames <- GenomicFeatures:::.listNames(names(exonsByGene),
as.list(foGG))
foEG <- findOverlaps(exonicParts, exonsByGene, select="first")
gene_id <- aggregateNames[foEG]
pasteNames <- GenomicFeatures:::.pasteNames(names(exonsByGene),
as.list(foGG))[foEG]
orderByGeneName <- order(pasteNames)
exonic_rle <- runLength(Rle(pasteNames[orderByGeneName]))
} else {
## drop exonic parts that overlap > 1 gene
foEG <- findOverlaps(exonicParts, exonsByGene)
idxList <- as.list(foEG)
if (any(keep <- countQueryHits(foEG) == 1)) {
idxList <- idxList[keep]
exonicParts <- exonicParts[keep]
}
gene_id <- GenomicFeatures:::.listNames(names(exonsByGene),
idxList)
orderByGeneName <- order(unlist(gene_id, use.names=FALSE))
exonic_rle <- runLength(Rle(unlist(gene_id[orderByGeneName],
use.names=FALSE)))
}
values <- DataFrame(gene_id)
if (includeTranscripts) {
exonsByTx <- exonsBy(x, by="tx", filter=filter)
foET <- findOverlaps(exonicParts, exonsByTx)
values$tx_name <- GenomicFeatures:::.listNames(names(exonsByTx),
as.list(foET))
}
mcols(exonicParts) <- values
exonicParts <- exonicParts[orderByGeneName]
exonic_part <- unlist(lapply(exonic_rle, seq_len), use.names=FALSE)
exonicParts$exonic_part <- exonic_part
return(exonicParts)
}
)
############################################################
## getGeneRegionTrackForGviz
## Fetch data to add as a GeneTrack.
## filter ... Used to filter the result.
## chromosome, start, end ... Either all or none has to be specified. If
## specified, the function first retrieves all
## transcripts that have an exon in the specified
## range and adds them as a TranscriptidFilter to
## the filters. The query to fetch the "real" data
## is performed afterwards.
## featureIs ... Wheter gene_biotype or tx_biotype should be
## mapped to the column feature.
setMethod(
"getGeneRegionTrackForGviz",
"EnsDb",
function(x, filter = AnnotationFilterList(), chromosome = NULL,
start = NULL, end = NULL, featureIs = "gene_biotype")
{
featureIs <- match.arg(featureIs, c("gene_biotype", "tx_biotype"))
filter <- .processFilterParam(filter, x)
if(missing(chromosome))
chromosome <- NULL
if(missing(start))
start <- NULL
if(missing(end))
end <- NULL
## If only chromosome is specified, create a SeqNameFilter and
## add it to the filter
if(is.null(start) & is.null(end) & !is.null(chromosome)){
filter <- AnnotationFilterList(filter, SeqNameFilter(chromosome))
chromosome <- NULL
}
if(any(c(!is.null(chromosome), !is.null(start), !is.null(end)))){
## Require however that all are defined!!!
if(all(c(!is.null(chromosome), !is.null(start), !is.null(end)))){
## Fix eventually provided UCSC chromosome names:
chromosome <- ucscToEns(chromosome)
## Define a GRangesFilter to include all features that overlap
## that region.
grg <- GRangesFilter(GRanges(seqnames = chromosome,
ranges = IRanges(start, end)),
feature = "tx", type = "any")
tids <- transcripts(x, filter = grg, columns = "tx_id")$tx_id
filter <- AnnotationFilterList(filter, TxIdFilter(tids))
}else{
stop("Either all or none of arguments 'chromosome', 'start' and",
" 'end' have to be specified!")
}
}
## Return a data.frame with columns: chromosome, start, end, width,
## strand, feature,
## gene, exon, transcript and symbol.
## 1) Query the data as we usually would.
## 2) Perform an additional query to get cds and utr, remove all entries
## from the first result for the same transcripts and rbind the
## data.frames.
needCols <- c("seq_name", "exon_seq_start", "exon_seq_end", "seq_strand",
featureIs, "gene_id", "exon_id",
"exon_idx", "tx_id", "gene_name")
## That's the names to which we map the original columns from the EnsDb.
names(needCols) <- c("chromosome", "start", "end", "strand",
"feature", "gene", "exon", "exon_rank", "transcript",
"symbol")
txs <- transcripts(x, filter = filter,
columns = needCols, return.type = "data.frame")
## Rename columns
idx <- match(needCols, colnames(txs))
notThere <- is.na(idx)
idx <- idx[!notThere]
colnames(txs)[idx] <- names(needCols)[!notThere]
txs <- txs[, !(colnames(txs) %in% c("tx_seq_start", "tx_seq_end",
"seq_name"))]
## now processing the 5utr
fUtr <- fiveUTRsByTranscript(x, filter = filter, columns = needCols)
if(length(fUtr) > 0){
fUtr <- as(unlist(fUtr, use.names = FALSE), "data.frame")
fUtr <- fUtr[, !(colnames(fUtr) %in% c("width", "seq_name",
"exon_seq_start",
"exon_seq_end", "strand"))]
colnames(fUtr)[1] <- "chromosome"
idx <- match(needCols, colnames(fUtr))
notThere <- is.na(idx)
idx <- idx[!notThere]
colnames(fUtr)[idx] <- names(needCols)[!notThere]
## Force being in the correct ordering:
fUtr <- fUtr[, names(needCols)]
fUtr$feature <- "utr5"
## Remove transcripts from the txs data.frame
txs <- txs[!(txs$transcript %in% fUtr$transcript), , drop=FALSE]
}
tUtr <- threeUTRsByTranscript(x, filter = filter, columns=needCols)
if(length(tUtr) > 0){
tUtr <- as(unlist(tUtr, use.names = FALSE), "data.frame")
tUtr <- tUtr[, !(colnames(tUtr) %in% c("width", "seq_name",
"exon_seq_start",
"exon_seq_end", "strand"))]
colnames(tUtr)[1] <- "chromosome"
idx <- match(needCols, colnames(tUtr))
notThere <- is.na(idx)
idx <- idx[!notThere]
colnames(tUtr)[idx] <- names(needCols)[!notThere]
## Force being in the correct ordering:
tUtr <- tUtr[, names(needCols)]
tUtr$feature <- "utr3"
## Remove transcripts from the txs data.frame
if(nrow(txs) > 0){
txs <- txs[!(txs$transcript %in% tUtr$transcript), , drop=FALSE]
}
}
cds <- cdsBy(x, filter = filter, columns = needCols)
if(length(cds) > 0){
cds <- as(unlist(cds, use.names=FALSE), "data.frame")
cds <- cds[, !(colnames(cds) %in% c("width", "seq_name",
"exon_seq_start",
"exon_seq_end", "strand"))]
colnames(cds)[1] <- "chromosome"
idx <- match(needCols, colnames(cds))
notThere <- is.na(idx)
idx <- idx[!notThere]
colnames(cds)[idx] <- names(needCols)[!notThere]
## Force being in the correct ordering:
cds <- cds[, names(needCols)]
## Remove transcripts from the txs data.frame
if(nrow(txs) > 0){
txs <- txs[!(txs$transcript %in% cds$transcript), , drop=FALSE]
}
}
## If there are any txs, they are noncoding...
if (nrow(txs))
txs$feature <- "utr"
if(length(fUtr) > 0){
txs <- rbind(txs, fUtr)
}
if(length(tUtr) > 0){
txs <- rbind(txs, tUtr)
}
if(length(cds) > 0){
txs <- rbind(txs, cds)
}
## Convert into GRanges.
suppressWarnings(
SI <- seqinfo(x)
)
SI <- SI[as.character(unique(txs$chromosome))]
GR <- GRanges(seqnames=Rle(txs$chromosome),
strand=Rle(txs$strand),
ranges=IRanges(start=txs$start, end=txs$end),
seqinfo=SI,
txs[, c("feature", "gene", "exon", "exon_rank",
"transcript", "symbol"), drop=FALSE])
return(GR)
})
####============================================================
## properties
##
## Get access to the "hidden" .properties slot and return it.
## This ensures that we're not generating an error for objects that
## do not have yet that slot.
####------------------------------------------------------------
setMethod("properties", "EnsDb", function(x, ...){
if(.hasSlot(x, ".properties")){
return(x@.properties)
}else{
warning("The present EnsDb instance has no .properties slot! ",
"Please use 'updateEnsDb' to update the object!")
return(list())
}
})
####============================================================
## getProperty
##
## Return the value for the property with the specified name or
## NA if not present.
####------------------------------------------------------------
setMethod("getProperty", "EnsDb", function(x, name, default = NA){
props <- properties(x)
if(any(names(props) == name)){
return(props[[name]])
}else{
return(default)
}
})
####============================================================
## setProperty
##
## Sets a property in the object. The value has to be a named vector.
####------------------------------------------------------------
setMethod("setProperty", "EnsDb", function(x, ...){
dotL <- list(...)
if(length(dotL) == 0){
stop("No property specified! The property has to be submitted ",
"in the format name=value!")
return(x)
}
if(length(dotL) > 1){
warning("'setProperty' does only support setting of a single property!",
" Using the first submitted one.")
dotL <- dotL[1]
}
if(is.null(names(dotL)) | names(dotL) == "")
stop("A name is required! Use name=value!")
if(.hasSlot(x, ".properties")){
x@.properties[names(dotL)] <- dotL[[1]]
}else{
warning("The present EnsDb instance has no .properties slot! ",
"Please use 'updateEnsDb' to update the object!")
}
return(x)
})
#' remove the property with the specified name.
#' @noRd
dropProperty <- function(x, name) {
if (missing(name))
return(x)
prps <- x@.properties
if (any(names(prps) == name))
prps <- prps[names(prps) != name]
x@.properties <- prps
x
}
####============================================================
## updateEnsDb
##
## Update any "old" EnsDb instance to the most recent implementation.
####------------------------------------------------------------
setMethod("updateEnsDb", "EnsDb", function(x, ...){
newE <- new("EnsDb", ensdb=x@ensdb, tables=x@tables)
if(.hasSlot(x, ".properties"))
newE@.properties <- x@.properties
return(newE)
})
####============================================================
## transcriptsByOverlaps
##
## Just "re-implementing" the transcriptsByOverlaps methods from the
## GenomicFeature package, finetuning and adapting it for EnsDbs
####------------------------------------------------------------
setMethod("transcriptsByOverlaps", "EnsDb",
function(x, ranges, maxgap = -1L, minoverlap = 0L,
type = c("any", "start", "end"),
columns = listColumns(x, "tx"),
filter = AnnotationFilterList()) {
if (missing(ranges))
stop("Parameter 'ranges' is missing!")
filter <- .processFilterParam(filter, x)
SLs <- unique(as.character(seqnames(ranges)))
filter <- AnnotationFilterList(filter, SeqNameFilter(SLs))
columns <- cleanColumns(x, columns)
subsetByOverlaps(transcripts(x, columns = columns, filter = filter),
ranges, maxgap = maxgap, minoverlap = minoverlap,
type = match.arg(type))
})
####============================================================
## exonsByOverlaps
##
####------------------------------------------------------------
setMethod("exonsByOverlaps", "EnsDb",
function(x, ranges, maxgap = -1L, minoverlap = 0L,
type = c("any", "start", "end"),
columns = listColumns(x, "exon"),
filter = AnnotationFilterList()) {
if(missing(ranges))
stop("Parameter 'ranges' is missing!")
filter <- .processFilterParam(filter, x)
SLs <- unique(as.character(seqnames(ranges)))
filter <- AnnotationFilterList(filter, SeqNameFilter(SLs))
columns <- cleanColumns(x, columns)
subsetByOverlaps(exons(x, columns = columns, filter = filter),
ranges, maxgap = maxgap, minoverlap = minoverlap,
type = match.arg(type))
})
############################################################
## returnFilterColumns
##
## Method to set the option whether or not the filter columns should be
## returned too.
setMethod("returnFilterColumns", "EnsDb", function(x) {
return(getProperty(x, "returnFilterColumns"))
})
setReplaceMethod("returnFilterColumns", "EnsDb", function(x, value) {
if(!is.logical(value))
stop("'value' has to be a logical!")
if(length(value) > 1)
stop("'value' has to be a logical of length 1!")
x <- setProperty(x, returnFilterColumns=value)
return(x)
})
############################################################
## orderResultsInR
##
## Whether the results should be ordered in R instead of in the
## SQL call
setMethod("orderResultsInR", "EnsDb", function(x) {
return(getProperty(x, "orderResultsInR", default = FALSE))
})
setReplaceMethod("orderResultsInR", "EnsDb", function(x, value) {
if(!is.logical(value))
stop("'value' has to be a logical!")
if(length(value) > 1)
stop("'value' has to be a logical of length 1!")
x <- setProperty(x, orderResultsInR = value)
return(x)
})
############################################################
## useMySQL
##
## Switch from RSQlite backend to a MariaDB/MySQL backend.
#' @title Use a MariaDB/MySQL backend
#'
#' @aliases useMySQL
#'
#' @description Change the SQL backend from \emph{SQLite} to \emph{MySQL}.
#' When first called on an \code{\linkS4class{EnsDb}} object, the function
#' tries to create and save all of the data into a MySQL database. All
#' subsequent calls will connect to the already existing MySQL database.
#'
#' @details This functionality requires that the \code{RMariaDB} package is
#' installed and that the user has (write) access to a running MySQL server.
#' If the corresponding database does already exist users without write
#' access can use this functionality.
#'
#' @note At present the function does not evaluate whether the versions
#' between the SQLite and MariaDB/MySQL database differ.
#'
#' @param x The \code{\linkS4class{EnsDb}} object.
#'
#' @param host Character vector specifying the host on which the MariaDB/MySQL
#' server runs.
#'
#' @param port The port on which the MariaDB/MySQL server can be accessed.
#'
#' @param user The user name for the MariaDB/MySQL server.
#'
#' @param pass The password for the MariaDB/MySQL server.
#'
#' @return A \code{\linkS4class{EnsDb}} object providing access to the
#' data stored in the MySQL backend.
#'
#' @author Johannes Rainer
#'
#' @examples
#' ## Load the EnsDb database (SQLite backend).
#' library(EnsDb.Hsapiens.v86)
#' edb <- EnsDb.Hsapiens.v86
#' ## Now change the backend to MySQL; my_user and my_pass should
#' ## be the user name and password to access the MySQL server.
#' \dontrun{
#' edb_mysql <- useMySQL(edb, host = "localhost", user = my_user, pass = my_pass)
#' }
setMethod("useMySQL", "EnsDb", function(x, host = "localhost",
port = 3306, user, pass) {
if (missing(user))
stop("'user' has to be specified.")
if (missing(pass))
stop("'pass' has to be specified.")
## Check if RMariaDB package is available.
if(requireNamespace("RMariaDB", quietly = TRUE)) {
## Check if we can connect to MySQL.
driva <- dbDriver("MariaDB")
con <- dbConnect(driva, host = host, user = user, pass = pass,
port = port)
## Check if database is available.
dbs <- dbGetQuery(con, "show databases;")
## sqliteName should be in the format EnsDb.Hsapiens.v75!
sqliteName <- .makePackageName(dbconn(x))
## sqliteName <- sub(basename(dbfile(dbconn(x))),
## pattern = ".sqlite", replacement = "",
## fixed = TRUE)
mysqlName <- SQLiteName2MySQL(sqliteName)
if (nrow(dbs) == 0 | !any(dbs$Database == mysqlName)) {
message("Database not available, trying to create it...",
appendLF = FALSE)
dbExecute(con, paste0("create database ", mysqlName))
message("OK")
}
dbDisconnect(con)
## Connect to the database and check if we've got all tables.
con <- dbConnect(driva, host = host, user = user, pass = pass,
dbname = mysqlName)
## If we've got no tables we try to feed the SQLite database
if (length(dbListTables(con)) == 0)
feedEnsDb2MySQL(x, mysql = con)
## Check if we've got all required tables.
OK <- dbHasRequiredTables(con)
if (is.character(OK))
stop(OK)
OK <- dbHasValidTables(con)
if (is.character(OK))
stop(OK)
## Check if the versions/creation date differ.
metadata_pkg <- metadata(x)
## Now store the connection into the @ensdb slot
## dbDisconnect(x@ensdb)
## x@ensdb <- NULL
x@ensdb <- con
metadata_db <- metadata(x)
cre_pkg <- metadata_pkg[metadata_pkg$name == "Creation time", "value"]
cre_db <- metadata_db[metadata_db$name == "Creation time", "value"]
if (cre_pkg != cre_db) {
message("Creation date between the package and the information in",
" the database differ:\n o package: ", cre_pkg,
"\n o database: ", cre_db, ".\nYou might consider to delete",
" the database and re-install it calling this function.")
}
return(x)
} else {
stop("Package 'RMariaDB' not available.")
}
})
############################################################
## proteins
##
## If return type is GRanges, make a seqlevel and seqinfo for each protein, i.e.
## put each protein on its own sequence.
#' @title Protein related functionality
#'
#' @aliases proteins
#'
#' @description This help page provides information about most of the
#' functionality related to protein annotations in \code{ensembldb}.
#'
#' The \code{proteins} method retrieves protein related annotations from
#' an \code{\linkS4class{EnsDb}} database.
#'
#' @details The \code{proteins} method performs the query starting from the
#' \code{protein} tables and can hence return all annotations from the
#' database that are related to proteins and transcripts encoding these
#' proteins from the database. Since \code{proteins} does thus only query
#' annotations for protein coding transcripts, the \code{\link{genes}} or
#' \code{\link{transcripts}} methods have to be used to retrieve annotations
#' for non-coding transcripts.
#'
#' @param object The \code{\linkS4class{EnsDb}} object.
#'
#' @param columns For \code{proteins}: character vector defining the columns to
#' be extracted from the database. Can be any column(s) listed by the
#' \code{\link{listColumns}} method.
#'
#' @param filter For \code{proteins}: A filter object extending
#' \code{AnnotationFilter} or a list of such objects to select
#' specific entries from the database. See \code{\link{Filter-classes}} for
#' a documentation of available filters and use
#' \code{\link{supportedFilters}} to get the full list of supported filters.
#'
#' @param order.by For \code{proteins}: a character vector specifying the
#' column(s) by which the result should be ordered.
#'
#' @param order.type For \code{proteins}: if the results should be ordered
#' ascending (\code{order.type = "asc"}) or descending
#' (\code{order.type = "desc"})
#'
#' @param return.type For \code{proteins}: character of lenght one specifying
#' the type of the returned object. Can be either \code{"DataFrame"},
#' \code{"data.frame"} or \code{"AAStringSet"}.
#'
#' @return The \code{proteins} method returns protein related annotations from
#' an \code{\linkS4class{EnsDb}} object with its \code{return.type} argument
#' allowing to define the type of the returned object. Note that if
#' \code{return.type = "AAStringSet"} additional annotation columns are
#' stored in a \code{DataFrame} that can be accessed with the \code{mcols}
#' method on the returned object.
#'
#' @rdname ProteinFunctionality
#'
#' @author Johannes Rainer
#'
#' @examples
#' library(ensembldb)
#' library(EnsDb.Hsapiens.v86)
#' edb <- EnsDb.Hsapiens.v86
#' ## Get all proteins from tha database for the gene ZBTB16, if protein
#' ## annotations are available
#' if (hasProteinData(edb))
#' proteins(edb, filter = GeneNameFilter("ZBTB16"))
setMethod("proteins", "EnsDb", function(object,
columns = listColumns(object, "protein"),
filter = AnnotationFilterList(),
order.by = "",
order.type = "asc",
return.type = "DataFrame") {
if (!hasProteinData(object))
stop("The used EnsDb does not provide protein annotations!",
" Thus, 'proteins' can not be used.")
return.type <- match.arg(return.type, c("DataFrame", "AAStringSet",
"data.frame"))
columns <- cleanColumns(object, unique(c(columns, "protein_id")))
filter <- .processFilterParam(filter, object)
filter <- setFeatureInGRangesFilter(filter, "tx")
## Eventually add columns for the filters:
columns <- addFilterColumns(columns, filter, object)
## Check that we don't have any exon columns here.
ex_cols <- unique(listColumns(object, c("exon", "tx2exon")))
ex_cols <- ex_cols[ex_cols != "tx_id"]
if (any(columns %in% ex_cols)) {
warning("Exon specific columns are not allowed for proteins. Columns ",
paste0("'", columns[columns %in% ex_cols], "'", collapse = ", "),
" have been removed.")
columns <- columns[!(columns %in% ex_cols)]
}
retColumns <- columns
## Process order.by:
## If not specified we might want to order them by seq_name or tx_seq_start
## if present in parameter columns
if (all(order.by == "")) {
order.by <- NULL
if (any(columns == "seq_name"))
order.by <- "seq_name"
seq_col_idx <- grep(columns, pattern = "_seq_")
if (length(seq_col_idx) > 0)
order.by <- c(order.by, columns[seq_col_idx[1]])
if (is.null(order.by))
order.by <- ""
}
## If we're going to return a GRanges we need to know the length of the
## peptide sequence.
if (return.type == "AAStringSet") {
columns <- unique(c(columns, "protein_sequence"))
}
## protein_id is *always* required
columns <- unique(c(columns), "protein_id")
## Get the data
Res <- getWhat(object, columns = columns, filter = filter,
order.by = order.by, order.type = order.type,
startWith = "protein", join = "suggested")
## issue #48: collapse entrezid column if dbschema 2.0 is used.
if (as.numeric(dbSchemaVersion(object)) > 1 & any(columns == "entrezid"))
Res <- .collapseEntrezidInTable(Res, by = "protein_id")
## Now process the result.
cols_not_found <- !(retColumns %in% colnames(Res))
retColumns <- retColumns[!cols_not_found]
if (any(cols_not_found))
warning("Columns ", paste0("'", retColumns[cols_not_found], "'",
collapse = ", "),
" not found in the database!")
if (return.type == "AAStringSet") {
aass <- AAStringSet(Res$protein_sequence)
names(aass) <- Res$protein_id
## Add the mcols:
retColumns <- retColumns[retColumns != "protein_sequence"]
if (length(retColumns) > 0)
mcols(aass) <- DataFrame(Res[, retColumns, drop = FALSE])
return(aass)
} else {
Res <- Res[, retColumns, drop = FALSE]
if (return.type == "DataFrame")
Res <- DataFrame(Res)
return(Res)
}
return(NULL)
})
############################################################
## listUniprotDbs
#' @aliases listUniprotDbs
#'
#' @description The \code{listUniprotDbs} method lists all Uniprot database
#' names in the \code{EnsDb}.
#'
#' @examples
#'
#' ## List the names of all Uniprot databases from which Uniprot IDs are
#' ## available in the EnsDb
#' if (hasProteinData(edb))
#' listUniprotDbs(edb)
#'
#' @rdname ProteinFunctionality
setMethod("listUniprotDbs", "EnsDb", function(object) {
if (!hasProteinData(object))
stop("The provided EnsDb database does not provide protein annotations!")
res <- dbGetQuery(dbconn(object), "select distinct uniprot_db from uniprot")
return(res$uniprot_db)
})
############################################################
## listUniprotMappingTypes
#' @aliases listUniprotMappingTypes
#'
#' @description The \code{listUniprotMappingTypes} method lists all methods
#' that were used for the mapping of Uniprot IDs to Ensembl protein IDs.
#'
#' @examples
#'
#' ## List the type of all methods that were used to map Uniprot IDs to Ensembl
#' ## protein IDs
#' if (hasProteinData(edb))
#' listUniprotMappingTypes(edb)
#'
#' @rdname ProteinFunctionality
setMethod("listUniprotMappingTypes", "EnsDb", function(object) {
if (!hasProteinData(object))
stop("The provided EnsDb database does not provide protein annotations!")
res <- dbGetQuery(dbconn(object),
"select distinct uniprot_mapping_type from uniprot")
return(res$uniprot_mapping_type)
})
#' @description `supportedFilters` returns a `data.frame` with the
#' names of all filters and the corresponding field supported by the
#' `EnsDb` object.
#'
#' @param object For `supportedFilters`: an `EnsDb` object.
#'
#' @param ... For `supportedFilters`: currently not used.
#'
#' @return For `supportedFilters`: a `data.frame` with the names and
#' the corresponding field of the supported filter classes.
#'
#' @md
#'
#' @rdname Filter-classes
setMethod("supportedFilters", "EnsDb", function(object, ...) {
.supportedFilters(object)
})
#' @title Globally add filters to an EnsDb database
#'
#' @aliases addFilter addFilter,EnsDb-method
#'
#' @description These methods allow to set, delete or show globally defined
#' filters on an \code{\linkS4class{EnsDb}} object.
#'
#' \code{addFilter}: adds an annotation filter to the \code{EnsDb} object.
#'
#' @details Adding a filter to an \code{EnsDb} object causes this filter to be
#' permanently active. The filter will be used for all queries to the
#' database and is added to all additional filters passed to the methods
#' such as \code{\link{genes}}.
#'
#' @param x The \code{\linkS4class{EnsDb}} object to which the filter should be
#' added.
#'
#' @param filter The filter as an
#' \code{\link[AnnotationFilter]{AnnotationFilter}},
#' \code{\link[AnnotationFilter]{AnnotationFilterList}} or filter
#' expression. See
#'
#' @return \code{addFilter} and \code{filter} return an \code{EnsDb} object
#' with the specified filter added.
#'
#' \code{activeFilter} returns an
#' \code{\link[AnnotationFilter]{AnnotationFilterList}} object being the
#' active global filter or \code{NA} if no filter was added.
#'
#' \code{dropFilter} returns an \code{EnsDb} object with all eventually
#' present global filters removed.
#'
#' @author Johannes Rainer
#'
#' @rdname global-filters
#'
#' @seealso \code{\link{Filter-classes}} for a list of all supported filters.
#'
#' @examples
#' library(EnsDb.Hsapiens.v86)
#' edb <- EnsDb.Hsapiens.v86
#'
#' ## Add a global SeqNameFilter to the database such that all subsequent
#' ## queries will be applied on the filtered database.
#' edb_y <- addFilter(edb, SeqNameFilter("Y"))
#'
#' ## Note: using the filter function is equivalent to a call to addFilter.
#'
#' ## Each call returns now only features encoded on chromosome Y
#' gns <- genes(edb_y)
#'
#' seqlevels(gns)
#'
#' ## Get all lincRNA gene transcripts on chromosome Y
#' transcripts(edb_y, filter = ~ gene_biotype == "lincRNA")
#'
#' ## Get the currently active global filter:
#' activeFilter(edb_y)
#'
#' ## Delete this filter again.
#' edb_y <- dropFilter(edb_y)
#'
#' activeFilter(edb_y)
setMethod("addFilter", "EnsDb", function(x, filter = AnnotationFilterList()) {
.addFilter(x, filter)
})
#' @aliases dropFilter dropFilter,EnsDb-method
#'
#' @description \code{dropFilter} deletes all globally set filters from the
#' \code{EnsDb} object.
#'
#' @rdname global-filters
setMethod("dropFilter", "EnsDb", function(x) {
.dropFilter(x)
})
#' @aliases activeFilter activeFilter,EnsDb-method
#'
#' @description \code{activeFilter} returns the globally set filter from an
#' \code{EnsDb} object.
#'
#' @rdname global-filters
setMethod("activeFilter", "EnsDb", function(x) {
.activeFilter(x)
})
setMethod("intronsByTranscript", "EnsDb", function(x, ..., use.names = FALSE) {
txs <- transcripts(x, ...)
exns <- exonsBy(x, by = "tx", ...)
txs <- txs[match(names(exns), names(txs))]
psetdiff(txs, exns)
})
|