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
|
"""\
Main class and helper functions.
"""
import warnings
import collections.abc as cabc
from collections import OrderedDict
from copy import copy, deepcopy
from enum import Enum
from functools import partial, singledispatch
from pathlib import Path
from os import PathLike
from typing import Any, Union, Optional # Meta
from typing import Iterable, Sequence, Mapping, MutableMapping # Generic ABCs
from typing import Tuple, List # Generic
import h5py
from natsort import natsorted
import numpy as np
from numpy import ma
import pandas as pd
from pandas.api.types import is_string_dtype, is_categorical_dtype
from scipy import sparse
from scipy.sparse import issparse
from .raw import Raw
from .index import _normalize_indices, _subset, Index, Index1D, get_vector
from .file_backing import AnnDataFileManager
from .access import ElementRef
from .aligned_mapping import (
AxisArrays,
AxisArraysView,
PairwiseArrays,
PairwiseArraysView,
Layers,
LayersView,
)
from .views import (
ArrayView,
DictView,
DataFrameView,
as_view,
_resolve_idxs,
)
from .sparse_dataset import SparseDataset
from .. import utils
from ..utils import convert_to_dict, ensure_df_homogeneous
from ..logging import anndata_logger as logger
from ..compat import (
ZarrArray,
ZappyArray,
DaskArray,
Literal,
_slice_uns_sparse_matrices,
_move_adj_mtx,
_overloaded_uns,
OverloadedDict,
)
class StorageType(Enum):
Array = np.ndarray
Masked = ma.MaskedArray
Sparse = sparse.spmatrix
ZarrArray = ZarrArray
ZappyArray = ZappyArray
DaskArray = DaskArray
@classmethod
def classes(cls):
return tuple(c.value for c in cls.__members__.values())
# for backwards compat
def _find_corresponding_multicol_key(key, keys_multicol):
"""Find the corresponding multicolumn key."""
for mk in keys_multicol:
if key.startswith(mk) and "of" in key:
return mk
return None
# for backwards compat
def _gen_keys_from_multicol_key(key_multicol, n_keys):
"""Generates single-column keys from multicolumn key."""
keys = [f"{key_multicol}{i + 1:03}of{n_keys:03}" for i in range(n_keys)]
return keys
def _check_2d_shape(X):
"""\
Check shape of array or sparse matrix.
Assure that X is always 2D: Unlike numpy we always deal with 2D arrays.
"""
if X.dtype.names is None and len(X.shape) != 2:
raise ValueError(
f"X needs to be 2-dimensional, not {len(X.shape)}-dimensional."
)
@singledispatch
def _gen_dataframe(anno, length, index_names):
if anno is None or len(anno) == 0:
return pd.DataFrame(index=pd.RangeIndex(0, length, name=None).astype(str))
for index_name in index_names:
if index_name in anno:
return pd.DataFrame(
anno,
index=anno[index_name],
columns=[k for k in anno.keys() if k != index_name],
)
return pd.DataFrame(anno, index=pd.RangeIndex(0, length, name=None).astype(str))
@_gen_dataframe.register(pd.DataFrame)
def _(anno, length, index_names):
anno = anno.copy()
if not is_string_dtype(anno.index):
warnings.warn("Transforming to str index.", ImplicitModificationWarning)
anno.index = anno.index.astype(str)
return anno
@_gen_dataframe.register(pd.Series)
@_gen_dataframe.register(pd.Index)
def _(anno, length, index_names):
raise ValueError(f"Cannot convert {type(anno)} to DataFrame")
class ImplicitModificationWarning(UserWarning):
"""\
Raised whenever initializing an object or assigning a property changes
the type of a part of a parameter or the value being assigned.
Examples
========
>>> import pandas as pd
>>> adata = AnnData(obs=pd.DataFrame(index=[0, 1, 2])) # doctest: +SKIP
ImplicitModificationWarning: Transforming to str index.
"""
pass
class AnnData(metaclass=utils.DeprecationMixinMeta):
"""\
An annotated data matrix.
:class:`~anndata.AnnData` stores a data matrix :attr:`X` together with annotations
of observations :attr:`obs` (:attr:`obsm`, :attr:`obsp`),
variables :attr:`var` (:attr:`varm`, :attr:`varp`),
and unstructured annotations :attr:`uns`.
.. figure:: https://falexwolf.de/img/scanpy/anndata.svg
:width: 350px
An :class:`~anndata.AnnData` object `adata` can be sliced like a
:class:`~pandas.DataFrame`,
for instance `adata_subset = adata[:, list_of_variable_names]`.
:class:`~anndata.AnnData`’s basic structure is similar to R’s ExpressionSet
[Huber15]_. If setting an `.h5ad`-formatted HDF5 backing file `.filename`,
data remains on the disk but is automatically loaded into memory if needed.
See this `blog post`_ for more details.
.. _blog post: http://falexwolf.de/blog/171223_AnnData_indexing_views_HDF5-backing/
Parameters
----------
X
A #observations × #variables data matrix. A view of the data is used if the
data type matches, otherwise, a copy is made.
obs
Key-indexed one-dimensional observations annotation of length #observations.
var
Key-indexed one-dimensional variables annotation of length #variables.
uns
Key-indexed unstructured annotation.
obsm
Key-indexed multi-dimensional observations annotation of length #observations.
If passing a :class:`~numpy.ndarray`, it needs to have a structured datatype.
varm
Key-indexed multi-dimensional variables annotation of length #variables.
If passing a :class:`~numpy.ndarray`, it needs to have a structured datatype.
layers
Key-indexed multi-dimensional arrays aligned to dimensions of `X`.
dtype
Data type used for storage.
shape
Shape tuple (#observations, #variables). Can only be provided if `X` is `None`.
filename
Name of backing file. See :class:`h5py.File`.
filemode
Open mode of backing file. See :class:`h5py.File`.
See Also
--------
read_h5ad
read_csv
read_excel
read_hdf
read_loom
read_zarr
read_mtx
read_text
read_umi_tools
Notes
-----
:class:`~anndata.AnnData` stores observations (samples) of variables/features
in the rows of a matrix.
This is the convention of the modern classics of statistics [Hastie09]_
and machine learning [Murphy12]_,
the convention of dataframes both in R and Python and the established statistics
and machine learning packages in Python (statsmodels_, scikit-learn_).
Single dimensional annotations of the observation and variables are stored
in the :attr:`obs` and :attr:`var` attributes as :class:`~pandas.DataFrame`\\ s.
This is intended for metrics calculated over their axes.
Multi-dimensional annotations are stored in :attr:`obsm` and :attr:`varm`,
which are aligned to the objects observation and variable dimensions respectively.
Square matrices representing graphs are stored in :attr:`obsp` and :attr:`varp`,
with both of their own dimensions aligned to their associated axis.
Additional measurements across both observations and variables are stored in
:attr:`layers`.
Indexing into an AnnData object can be performed by relative position
with numeric indices (like pandas’ :meth:`~pandas.DataFrame.iloc`),
or by labels (like :meth:`~pandas.DataFrame.loc`).
To avoid ambiguity with numeric indexing into observations or variables,
indexes of the AnnData object are converted to strings by the constructor.
Subsetting an AnnData object by indexing into it will also subset its elements
according to the dimensions they were aligned to.
This means an operation like `adata[list_of_obs, :]` will also subset :attr:`obs`,
:attr:`obsm`, and :attr:`layers`.
Subsetting an AnnData object returns a view into the original object,
meaning very little additional memory is used upon subsetting.
This is achieved lazily, meaning that the constituent arrays are subset on access.
Copying a view causes an equivalent “real” AnnData object to be generated.
Attempting to modify a view (at any attribute except X) is handled
in a copy-on-modify manner, meaning the object is initialized in place.
Here’s an example::
batch1 = adata[adata.obs["batch"] == "batch1", :]
batch1.obs["value"] = 0 # This makes batch1 a “real” AnnData object
At the end of this snippet: `adata` was not modified,
and `batch1` is its own AnnData object with its own data.
Similar to Bioconductor’s `ExpressionSet` and :mod:`scipy.sparse` matrices,
subsetting an AnnData object retains the dimensionality of its constituent arrays.
Therefore, unlike with the classes exposed by :mod:`pandas`, :mod:`numpy`,
and `xarray`, there is no concept of a one dimensional AnnData object.
AnnDatas always have two inherent dimensions, :attr:`obs` and :attr:`var`.
Additionally, maintaining the dimensionality of the AnnData object allows for
consistent handling of :mod:`scipy.sparse` matrices and :mod:`numpy` arrays.
.. _statsmodels: http://www.statsmodels.org/stable/index.html
.. _scikit-learn: http://scikit-learn.org/
"""
_BACKED_ATTRS = ["X", "raw.X"]
# backwards compat
_H5_ALIASES = dict(
X={"X", "_X", "data", "_data"},
obs={"obs", "_obs", "smp", "_smp"},
var={"var", "_var"},
uns={"uns"},
obsm={"obsm", "_obsm", "smpm", "_smpm"},
varm={"varm", "_varm"},
layers={"layers", "_layers"},
)
_H5_ALIASES_NAMES = dict(
obs={"obs_names", "smp_names", "row_names", "index"},
var={"var_names", "col_names", "index"},
)
def __init__(
self,
X: Optional[Union[np.ndarray, sparse.spmatrix, pd.DataFrame]] = None,
obs: Optional[Union[pd.DataFrame, Mapping[str, Iterable[Any]]]] = None,
var: Optional[Union[pd.DataFrame, Mapping[str, Iterable[Any]]]] = None,
uns: Optional[Mapping[str, Any]] = None,
obsm: Optional[Union[np.ndarray, Mapping[str, Sequence[Any]]]] = None,
varm: Optional[Union[np.ndarray, Mapping[str, Sequence[Any]]]] = None,
layers: Optional[Mapping[str, Union[np.ndarray, sparse.spmatrix]]] = None,
raw: Optional[Mapping[str, Any]] = None,
dtype: Union[np.dtype, str] = "float32",
shape: Optional[Tuple[int, int]] = None,
filename: Optional[PathLike] = None,
filemode: Optional[Literal["r", "r+"]] = None,
asview: bool = False,
*,
obsp: Optional[Union[np.ndarray, Mapping[str, Sequence[Any]]]] = None,
varp: Optional[Union[np.ndarray, Mapping[str, Sequence[Any]]]] = None,
oidx: Index1D = None,
vidx: Index1D = None,
):
if asview:
if not isinstance(X, AnnData):
raise ValueError("`X` has to be an AnnData object.")
self._init_as_view(X, oidx, vidx)
else:
self._init_as_actual(
X=X,
obs=obs,
var=var,
uns=uns,
obsm=obsm,
varm=varm,
raw=raw,
layers=layers,
dtype=dtype,
shape=shape,
obsp=obsp,
varp=varp,
filename=filename,
filemode=filemode,
)
def _init_as_view(self, adata_ref: "AnnData", oidx: Index, vidx: Index):
if adata_ref.isbacked and adata_ref.is_view:
raise ValueError(
"Currently, you cannot index repeatedly into a backed AnnData, "
"that is, you cannot make a view of a view."
)
self._is_view = True
if isinstance(oidx, (int, np.integer)):
oidx = slice(oidx, oidx + 1, 1)
if isinstance(vidx, (int, np.integer)):
vidx = slice(vidx, vidx + 1, 1)
if adata_ref.is_view:
prev_oidx, prev_vidx = adata_ref._oidx, adata_ref._vidx
adata_ref = adata_ref._adata_ref
oidx, vidx = _resolve_idxs((prev_oidx, prev_vidx), (oidx, vidx), adata_ref)
self._adata_ref = adata_ref
self._oidx = oidx
self._vidx = vidx
# the file is the same as of the reference object
self.file = adata_ref.file
# views on attributes of adata_ref
obs_sub = adata_ref.obs.iloc[oidx]
var_sub = adata_ref.var.iloc[vidx]
self._obsm = adata_ref.obsm._view(self, (oidx,))
self._varm = adata_ref.varm._view(self, (vidx,))
self._layers = adata_ref.layers._view(self, (oidx, vidx))
self._obsp = adata_ref.obsp._view(self, oidx)
self._varp = adata_ref.varp._view(self, vidx)
# Speical case for old neighbors, backwards compat. Remove in anndata 0.8.
uns_new = _slice_uns_sparse_matrices(
copy(adata_ref._uns), self._oidx, adata_ref.n_obs
)
# fix categories
self._remove_unused_categories(adata_ref.obs, obs_sub, uns_new)
self._remove_unused_categories(adata_ref.var, var_sub, uns_new)
# set attributes
self._obs = DataFrameView(obs_sub, view_args=(self, "obs"))
self._var = DataFrameView(var_sub, view_args=(self, "var"))
self._uns = DictView(uns_new, view_args=(self, "uns"))
self._n_obs = len(self.obs)
self._n_vars = len(self.var)
# set data
if self.isbacked:
self._X = None
# set raw, easy, as it’s immutable anyways...
if adata_ref._raw is not None:
# slicing along variables axis is ignored
self._raw = adata_ref.raw[oidx]
self._raw._adata = self
else:
self._raw = None
def _init_as_actual(
self,
X=None,
obs=None,
var=None,
uns=None,
obsm=None,
varm=None,
varp=None,
obsp=None,
raw=None,
layers=None,
dtype="float32",
shape=None,
filename=None,
filemode=None,
):
# view attributes
self._is_view = False
self._adata_ref = None
self._oidx = None
self._vidx = None
# ----------------------------------------------------------------------
# various ways of initializing the data
# ----------------------------------------------------------------------
# If X is a data frame, we store its indices for verification
x_indices = []
# init from file
if filename is not None:
self.file = AnnDataFileManager(self, filename, filemode)
else:
self.file = AnnDataFileManager(self, None)
# init from AnnData
if isinstance(X, AnnData):
if any((obs, var, uns, obsm, varm, obsp, varp)):
raise ValueError(
"If `X` is a dict no further arguments must be provided."
)
X, obs, var, uns, obsm, varm, obsp, varp, layers, raw = (
X._X,
X.obs,
X.var,
X.uns,
X.obsm,
X.varm,
X.obsp,
X.varp,
X.layers,
X.raw,
)
# init from DataFrame
elif isinstance(X, pd.DataFrame):
# to verify index matching, we wait until obs and var are DataFrames
if obs is None:
obs = pd.DataFrame(index=X.index)
elif not isinstance(X.index, pd.RangeIndex):
x_indices.append(("obs", "index", X.index))
if var is None:
var = pd.DataFrame(index=X.columns)
elif not isinstance(X.columns, pd.RangeIndex):
x_indices.append(("var", "columns", X.columns))
X = ensure_df_homogeneous(X, "X")
# ----------------------------------------------------------------------
# actually process the data
# ----------------------------------------------------------------------
# check data type of X
if X is not None:
for s_type in StorageType:
if isinstance(X, s_type.value):
break
else:
class_names = ", ".join(c.__name__ for c in StorageType.classes())
raise ValueError(
f"`X` needs to be of one of {class_names}, not {type(X)}."
)
if shape is not None:
raise ValueError("`shape` needs to be `None` if `X` is not `None`.")
_check_2d_shape(X)
# if type doesn’t match, a copy is made, otherwise, use a view
if issparse(X) or isinstance(X, ma.MaskedArray):
# TODO: maybe use view on data attribute of sparse matrix
# as in readwrite.read_10x_h5
if X.dtype != np.dtype(dtype):
X = X.astype(dtype)
elif isinstance(X, ZarrArray):
X = X.astype(dtype)
else: # is np.ndarray or a subclass, convert to true np.ndarray
X = np.array(X, dtype, copy=False)
# data matrix and shape
self._X = X
self._n_obs, self._n_vars = self._X.shape
else:
self._X = None
self._n_obs = len([] if obs is None else obs)
self._n_vars = len([] if var is None else var)
# check consistency with shape
if shape is not None:
if self._n_obs == 0:
self._n_obs = shape[0]
else:
if self._n_obs != shape[0]:
raise ValueError("`shape` is inconsistent with `obs`")
if self._n_vars == 0:
self._n_vars = shape[1]
else:
if self._n_vars != shape[1]:
raise ValueError("`shape` is inconsistent with `var`")
# annotations
self._obs = _gen_dataframe(obs, self._n_obs, ["obs_names", "row_names"])
self._var = _gen_dataframe(var, self._n_vars, ["var_names", "col_names"])
# now we can verify if indices match!
for attr_name, x_name, idx in x_indices:
attr = getattr(self, attr_name)
if isinstance(attr.index, pd.RangeIndex):
attr.index = idx
elif not idx.equals(attr.index):
raise ValueError(f"Index of {attr_name} must match {x_name} of X.")
# unstructured annotations
self.uns = uns or OrderedDict()
# TODO: Think about consequences of making obsm a group in hdf
self._obsm = AxisArrays(self, 0, vals=convert_to_dict(obsm))
self._varm = AxisArrays(self, 1, vals=convert_to_dict(varm))
self._obsp = PairwiseArrays(self, 0, vals=convert_to_dict(obsp))
self._varp = PairwiseArrays(self, 1, vals=convert_to_dict(varp))
# Backwards compat for connectivities matrices in uns["neighbors"]
_move_adj_mtx({"uns": self._uns, "obsp": self._obsp})
self._check_dimensions()
self._check_uniqueness()
if self.filename:
assert not isinstance(
raw, Raw
), "got raw from other adata but also filename?"
if {"raw", "raw.X"} & set(self.file):
raw = dict(X=None, **raw)
if not raw:
self._raw = None
elif isinstance(raw, cabc.Mapping):
self._raw = Raw(self, **raw)
else: # is a Raw from another AnnData
self._raw = Raw(self, raw._X, raw.var, raw.varm)
# clean up old formats
self._clean_up_old_format(uns)
# layers
self._layers = Layers(self, layers)
def __sizeof__(self) -> int:
size = 0
for attr in ["_X", "_obs", "_var", "_uns", "_obsm", "_varm"]:
s = getattr(self, attr).__sizeof__()
size += s
return size
def _gen_repr(self, n_obs, n_vars) -> str:
if self.isbacked:
backed_at = f" backed at {str(self.filename)!r}"
else:
backed_at = ""
descr = f"AnnData object with n_obs × n_vars = {n_obs} × {n_vars}{backed_at}"
for attr in [
"obs",
"var",
"uns",
"obsm",
"varm",
"layers",
"obsp",
"varp",
]:
keys = getattr(self, attr).keys()
if len(keys) > 0:
descr += f"\n {attr}: {str(list(keys))[1:-1]}"
return descr
def __repr__(self) -> str:
if self.is_view:
return "View of " + self._gen_repr(self.n_obs, self.n_vars)
else:
return self._gen_repr(self.n_obs, self.n_vars)
def __eq__(self, other):
"""Equality testing"""
raise NotImplementedError(
"Equality comparisons are not supported for AnnData objects, "
"instead compare the desired attributes."
)
@property
def shape(self) -> Tuple[int, int]:
"""Shape of data matrix (:attr:`n_obs`, :attr:`n_vars`)."""
return self.n_obs, self.n_vars
@property
def X(self) -> Optional[Union[np.ndarray, sparse.spmatrix, ArrayView]]:
"""Data matrix of shape :attr:`n_obs` × :attr:`n_vars`."""
if self.isbacked:
if not self.file.is_open:
self.file.open()
X = self.file["X"]
if isinstance(X, h5py.Group):
X = SparseDataset(X)
# TODO: This should get replaced/ handled elsewhere
# This is so that we can index into a backed dense dataset with
# indices that aren’t strictly increasing
if self.is_view and isinstance(X, h5py.Dataset):
ordered = [self._oidx, self._vidx] # this will be mutated
rev_order = [slice(None), slice(None)]
for axis, axis_idx in enumerate(ordered.copy()):
if isinstance(axis_idx, np.ndarray) and axis_idx.dtype.type != bool:
order = np.argsort(axis_idx)
ordered[axis] = axis_idx[order]
rev_order[axis] = np.argsort(order)
# from hdf5, then to real order
X = X[tuple(ordered)][tuple(rev_order)]
elif self.is_view:
X = X[self._oidx, self._vidx]
elif self.is_view:
X = as_view(
_subset(self._adata_ref.X, (self._oidx, self._vidx)),
ElementRef(self, "X"),
)
else:
X = self._X
return X
# if self.n_obs == 1 and self.n_vars == 1:
# return X[0, 0]
# elif self.n_obs == 1 or self.n_vars == 1:
# if issparse(X): X = X.toarray()
# return X.flatten()
# else:
# return X
@X.setter
def X(self, value: Optional[Union[np.ndarray, sparse.spmatrix]]):
if not isinstance(value, StorageType.classes()) and not np.isscalar(value):
if hasattr(value, "to_numpy") and hasattr(value, "dtypes"):
value = ensure_df_homogeneous(value, "X")
else: # TODO: asarray? asanyarray?
value = np.array(value)
if value is None:
if self.is_view:
raise ValueError(
"Copy the view before setting the data matrix to `None`."
)
if self.isbacked:
raise ValueError("Not implemented.")
self._X = None
return
# If indices are both arrays, we need to modify them
# so we don’t set values like coordinates
# This can occur if there are succesive views
if (
self.is_view
and isinstance(self._oidx, np.ndarray)
and isinstance(self._vidx, np.ndarray)
):
oidx, vidx = np.ix_(self._oidx, self._vidx)
else:
oidx, vidx = self._oidx, self._vidx
if (
np.isscalar(value)
or (self.n_vars == 1 and self.n_obs == len(value))
or (self.n_obs == 1 and self.n_vars == len(value))
or self.shape == value.shape
):
if not np.isscalar(value) and self.shape != value.shape:
# For assigning vector of values to 2d array or matrix
# Not neccesary for row of 2d array
value = value.reshape(self.shape)
if self.isbacked:
if self.is_view:
X = self.file["X"]
if isinstance(X, h5py.Group):
X = SparseDataset(X)
X[oidx, vidx] = value
else:
self._set_backed("X", value)
else:
if self.is_view:
if sparse.issparse(self._adata_ref._X) and isinstance(
value, np.ndarray
):
value = sparse.coo_matrix(value)
self._adata_ref._X[oidx, vidx] = value
else:
self._X = value
else:
raise ValueError(
f"Data matrix has wrong shape {value.shape}, "
f"need to be {self.shape}."
)
@property
def layers(self) -> Union[Layers, LayersView]:
"""\
Dictionary-like object with values of the same dimensions as :attr:`X`.
Layers in AnnData are inspired by loompy’s :ref:`loomlayers`.
Return the layer named `"unspliced"`::
adata.layers["unspliced"]
Create or replace the `"spliced"` layer::
adata.layers["spliced"] = ...
Assign the 10th column of layer `"spliced"` to the variable a::
a = adata.layers["spliced"][:, 10]
Delete the `"spliced"` layer::
del adata.layers["spliced"]
Return layers’ names::
adata.layers.keys()
"""
return self._layers
@layers.setter
def layers(self, value):
layers = Layers(self, vals=convert_to_dict(value))
if self.is_view:
self._init_as_actual(self.copy())
self._layers = layers
@layers.deleter
def layers(self):
self.layers = dict()
@property
def raw(self) -> Raw:
"""\
Store raw version of :attr:`X` and :attr:`var` as `.raw.X` and `.raw.var`.
The :attr:`raw` attribute is initialized with the current content
of an object by setting::
adata.raw = adata
Its content can be deleted::
adata.raw = None
# or
del adata.raw
Upon slicing an AnnData object along the obs (row) axis, :attr:`raw`
is also sliced. Slicing an AnnData object along the vars (columns) axis
leaves :attr:`raw` unaffected. Note that you can call::
adata.raw[:, 'orig_variable_name'].X
to retrieve the data associated with a variable that might have been
filtered out or "compressed away" in :attr:`X`.
"""
return self._raw
@raw.setter
def raw(self, value: "AnnData"):
if value is None:
del self.raw
elif not isinstance(value, AnnData):
raise ValueError("Can only init raw attribute with an AnnData object.")
else:
if self.is_view:
self._init_as_actual(self.copy())
self._raw = Raw(value)
@raw.deleter
def raw(self):
if self.is_view:
self._init_as_actual(self.copy())
self._raw = None
@property
def n_obs(self) -> int:
"""Number of observations."""
return self._n_obs
@property
def n_vars(self) -> int:
"""Number of variables/features."""
return self._n_vars
def _set_dim_df(self, value: pd.DataFrame, attr: str):
if not isinstance(value, pd.DataFrame):
raise ValueError(f"Can only assign pd.DataFrame to {attr}.")
value_idx = self._prep_dim_index(value.index, attr)
if self.is_view:
self._init_as_actual(self.copy())
setattr(self, f"_{attr}", value)
self._set_dim_index(value_idx, attr)
def _prep_dim_index(self, value, attr: str) -> pd.Index:
"""Prepares index to be uses as obs_names or var_names for AnnData object.AssertionError
If a pd.Index is passed, this will use a reference, otherwise a new index object is created.
"""
if self.shape[attr == "var"] != len(value):
raise ValueError(
f"Length of passed value for {attr}_names is {len(value)}, but this AnnData has shape: {self.shape}"
)
if isinstance(value, pd.Index) and not isinstance(
value.name, (str, type(None))
):
raise ValueError(
f"AnnData expects .{attr}.index.name to be a string or None, "
f"but you passed a name of type {type(value.name).__name__!r}"
)
else:
value = pd.Index(value)
if not isinstance(value.name, (str, type(None))):
value.name = None
if not isinstance(value, pd.RangeIndex) and not isinstance(
value[0], (str, bytes)
):
logger.warning(
f"AnnData expects .{attr}.index to contain strings, "
f"but your first indices are: {value[:2]}, …"
)
return value
def _set_dim_index(self, value: pd.Index, attr: str):
# Assumes _prep_dim_index has been run
if self.is_view:
self._init_as_actual(self.copy())
getattr(self, attr).index = value
for v in getattr(self, f"{attr}m").values():
if isinstance(v, pd.DataFrame):
v.index = value
@property
def obs(self) -> pd.DataFrame:
"""One-dimensional annotation of observations (`pd.DataFrame`)."""
return self._obs
@obs.setter
def obs(self, value: pd.DataFrame):
self._set_dim_df(value, "obs")
@obs.deleter
def obs(self):
self.obs = pd.DataFrame(index=self.obs_names)
@property
def obs_names(self) -> pd.Index:
"""Names of observations (alias for `.obs.index`)."""
return self.obs.index
@obs_names.setter
def obs_names(self, names: Sequence[str]):
names = self._prep_dim_index(names, "obs")
self._set_dim_index(names, "obs")
@property
def var(self) -> pd.DataFrame:
"""One-dimensional annotation of variables/ features (`pd.DataFrame`)."""
return self._var
@var.setter
def var(self, value: pd.DataFrame):
self._set_dim_df(value, "var")
@var.deleter
def var(self):
self.var = pd.DataFrame(index=self.var_names)
@property
def var_names(self) -> pd.Index:
"""Names of variables (alias for `.var.index`)."""
return self.var.index
@var_names.setter
def var_names(self, names: Sequence[str]):
names = self._prep_dim_index(names, "var")
self._set_dim_index(names, "var")
@property
def uns(self) -> MutableMapping:
"""Unstructured annotation (ordered dictionary)."""
uns = _overloaded_uns(self)
if self.is_view:
uns = DictView(uns, view_args=(self, "uns"))
return uns
@uns.setter
def uns(self, value: MutableMapping):
if not isinstance(value, MutableMapping):
raise ValueError(
"Only mutable mapping types (e.g. dict) are allowed for `.uns`."
)
if isinstance(value, (OverloadedDict, DictView)):
value = value.copy()
if self.is_view:
self._init_as_actual(self.copy())
self._uns = value
@uns.deleter
def uns(self):
self.uns = OrderedDict()
@property
def obsm(self) -> Union[AxisArrays, AxisArraysView]:
"""\
Multi-dimensional annotation of observations
(mutable structured :class:`~numpy.ndarray`).
Stores for each key a two or higher-dimensional :class:`~numpy.ndarray`
of length `n_obs`.
Is sliced with `data` and `obs` but behaves otherwise like a :term:`mapping`.
"""
return self._obsm
@obsm.setter
def obsm(self, value):
obsm = AxisArrays(self, 0, vals=convert_to_dict(value))
if self.is_view:
self._init_as_actual(self.copy())
self._obsm = obsm
@obsm.deleter
def obsm(self):
self.obsm = dict()
@property
def varm(self) -> Union[AxisArrays, AxisArraysView]:
"""\
Multi-dimensional annotation of variables/features
(mutable structured :class:`~numpy.ndarray`).
Stores for each key a two or higher-dimensional :class:`~numpy.ndarray`
of length `n_vars`.
Is sliced with `data` and `var` but behaves otherwise like a :term:`mapping`.
"""
return self._varm
@varm.setter
def varm(self, value):
varm = AxisArrays(self, 1, vals=convert_to_dict(value))
if self.is_view:
self._init_as_actual(self.copy())
self._varm = varm
@varm.deleter
def varm(self):
self.varm = dict()
@property
def obsp(self) -> Union[PairwiseArrays, PairwiseArraysView]:
"""\
Pairwise annotation of observations,
a mutable mapping with array-like values.
Stores for each key a two or higher-dimensional :class:`~numpy.ndarray`
whose first two dimensions are of length `n_obs`.
Is sliced with `data` and `obs` but behaves otherwise like a :term:`mapping`.
"""
return self._obsp
@obsp.setter
def obsp(self, value):
obsp = PairwiseArrays(self, 0, vals=convert_to_dict(value))
if self.is_view:
self._init_as_actual(self.copy())
self._obsp = obsp
@obsp.deleter
def obsp(self):
self.obsp = dict()
@property
def varp(self) -> Union[PairwiseArrays, PairwiseArraysView]:
"""\
Pairwise annotation of observations,
a mutable mapping with array-like values.
Stores for each key a two or higher-dimensional :class:`~numpy.ndarray`
whose first two dimensions are of length `n_var`.
Is sliced with `data` and `var` but behaves otherwise like a :term:`mapping`.
"""
return self._varp
@varp.setter
def varp(self, value):
varp = PairwiseArrays(self, 1, vals=convert_to_dict(value))
if self.is_view:
self._init_as_actual(self.copy())
self._varp = varp
@varp.deleter
def varp(self):
self.varp = dict()
def obs_keys(self) -> List[str]:
"""List keys of observation annotation :attr:`obs`."""
return self._obs.keys().tolist()
def var_keys(self) -> List[str]:
"""List keys of variable annotation :attr:`var`."""
return self._var.keys().tolist()
def obsm_keys(self) -> List[str]:
"""List keys of observation annotation :attr:`obsm`."""
return list(self._obsm.keys())
def varm_keys(self) -> List[str]:
"""List keys of variable annotation :attr:`varm`."""
return list(self._varm.keys())
def uns_keys(self) -> List[str]:
"""List keys of unstructured annotation."""
return sorted(list(self._uns.keys()))
@property
def isbacked(self) -> bool:
"""`True` if object is backed on disk, `False` otherwise."""
return self.filename is not None
@property
def is_view(self) -> bool:
"""`True` if object is view of another AnnData object, `False` otherwise."""
return self._is_view
@property
def filename(self) -> Optional[Path]:
"""\
Change to backing mode by setting the filename of a `.h5ad` file.
- Setting the filename writes the stored data to disk.
- Setting the filename when the filename was previously another name
moves the backing file from the previous file to the new file.
If you want to copy the previous file, use `copy(filename='new_filename')`.
"""
return self.file.filename
@filename.setter
def filename(self, filename: Optional[PathLike]):
# convert early for later comparison
filename = None if filename is None else Path(filename)
# change from backing-mode back to full loading into memory
if filename is None:
if self.filename is not None:
self.file._to_memory_mode()
else:
# both filename and self.filename are None
# do nothing
return
else:
if self.filename is not None:
if self.filename != filename:
# write the content of self to the old file
# and close the file
self.write()
self.filename.rename(filename)
else:
# do nothing
return
else:
# change from memory to backing-mode
# write the content of self to disk
self.write(filename, force_dense=True)
# open new file for accessing
self.file.open(filename, "r+")
# as the data is stored on disk, we can safely set self._X to None
self._X = None
def _set_backed(self, attr, value):
from .._io.utils import write_attribute
write_attribute(self.file._file, attr, value)
def _normalize_indices(self, index: Optional[Index]) -> Tuple[slice, slice]:
return _normalize_indices(index, self.obs_names, self.var_names)
# TODO: this is not quite complete...
def __delitem__(self, index: Index):
obs, var = self._normalize_indices(index)
# TODO: does this really work?
if not self.isbacked:
del self._X[obs, var]
else:
X = self.file["X"]
del X[obs, var]
self._set_backed("X", X)
if var == slice(None):
del self._obs.iloc[obs, :]
if obs == slice(None):
del self._var.iloc[var, :]
def __getitem__(self, index: Index) -> "AnnData":
"""Returns a sliced view of the object."""
oidx, vidx = self._normalize_indices(index)
return AnnData(self, oidx=oidx, vidx=vidx, asview=True)
def _remove_unused_categories(self, df_full, df_sub, uns):
for k in df_full:
if not is_categorical_dtype(df_full[k]):
continue
all_categories = df_full[k].cat.categories
df_sub[k].cat.remove_unused_categories(inplace=True)
# also correct the colors...
color_key = f"{k}_colors"
if color_key not in uns:
continue
color_vec = uns[color_key]
if np.array(color_vec).ndim == 0:
# Make 0D arrays into 1D ones
uns[color_key] = np.array(color_vec)[(None,)]
elif len(color_vec) != len(all_categories):
# Reset colors
del uns[color_key]
else:
idx = np.where(np.in1d(all_categories, df_sub[k].cat.categories))[0]
uns[color_key] = np.array(color_vec)[(idx,)]
def rename_categories(self, key: str, categories: Sequence[Any]):
"""\
Rename categories of annotation `key` in :attr:`obs`, :attr:`var`,
and :attr:`uns`.
Only supports passing a list/array-like `categories` argument.
Besides calling `self.obs[key].cat.categories = categories` –
similar for :attr:`var` - this also renames categories in unstructured
annotation that uses the categorical annotation `key`.
Parameters
----------
key
Key for observations or variables annotation.
categories
New categories, the same number as the old categories.
"""
if isinstance(categories, Mapping):
raise ValueError("Only list-like `categories` is supported.")
if key in self.obs:
old_categories = self.obs[key].cat.categories.tolist()
self.obs[key].cat.rename_categories(categories, inplace=True)
elif key in self.var:
old_categories = self.var[key].cat.categories.tolist()
self.var[key].cat.rename_categories(categories, inplace=True)
else:
raise ValueError(f"{key} is neither in `.obs` nor in `.var`.")
# this is not a good solution
# but depends on the scanpy conventions for storing the categorical key
# as `groupby` in the `params` slot
for k1, v1 in self.uns.items():
if not (
isinstance(v1, Mapping)
and "params" in v1
and "groupby" in v1["params"]
and v1["params"]["groupby"] == key
):
continue
for k2, v2 in v1.items():
# picks out the recarrays that are named according to the old
# categories
if isinstance(v2, np.ndarray) and v2.dtype.names is not None:
if list(v2.dtype.names) == old_categories:
self.uns[k1][k2].dtype.names = categories
else:
logger.warning(
f"Omitting {k1}/{k2} as old categories do not match."
)
def strings_to_categoricals(self, df: Optional[pd.DataFrame] = None):
"""\
Transform string annotations to categoricals.
Only affects string annotations that lead to less categories than the
total number of observations.
Params
------
df
If `df` is `None`, modifies both :attr:`obs` and :attr:`var`,
otherwise modifies `df` inplace.
Notes
-----
Turns the view of an :class:`~anndata.AnnData` into an actual
:class:`~anndata.AnnData`.
"""
dont_modify = False # only necessary for backed views
if df is None:
dfs = [self.obs, self.var]
if self.is_view and self.isbacked:
dont_modify = True
else:
dfs = [df]
for df in dfs:
string_cols = [
key
for key in df.columns
if is_string_dtype(df[key]) and not is_categorical_dtype(df[key])
]
for key in string_cols:
# make sure we only have strings
# (could be that there are np.nans (float), -666, "-666", for instance)
c = df[key].astype("U")
# make a categorical
c = pd.Categorical(c, categories=natsorted(np.unique(c)))
if len(c.categories) >= len(c):
continue
if dont_modify:
raise RuntimeError(
"Please call `.strings_to_categoricals()` on full "
"AnnData, not on this view. You might encounter this"
"error message while copying or writing to disk."
)
if self.is_view:
warnings.warn(
"Initializing view as actual.", ImplicitModificationWarning
)
# If `self` is a view, it will be actualized in the next line,
# therefore the previous warning
df[key] = c
logger.info(f"... storing {key!r} as categorical")
_sanitize = strings_to_categoricals # backwards compat
def _inplace_subset_var(self, index: Index1D):
"""\
Inplace subsetting along variables dimension.
Same as `adata = adata[:, index]`, but inplace.
"""
adata_subset = self[:, index].copy()
self._init_as_actual(adata_subset, dtype=self._X.dtype)
def _inplace_subset_obs(self, index: Index1D):
"""\
Inplace subsetting along variables dimension.
Same as `adata = adata[index, :]`, but inplace.
"""
adata_subset = self[index].copy()
self._init_as_actual(adata_subset, dtype=self.X.dtype)
# TODO: Update, possibly remove
def __setitem__(
self, index: Index, val: Union[int, float, np.ndarray, sparse.spmatrix]
):
if self.is_view:
raise ValueError("Object is view and cannot be accessed with `[]`.")
obs, var = self._normalize_indices(index)
if not self.isbacked:
self._X[obs, var] = val
else:
X = self.file["X"]
X[obs, var] = val
self._set_backed("X", X)
def __len__(self) -> int:
return self.shape[0]
def transpose(self) -> "AnnData":
"""\
Transpose whole object.
Data matrix is transposed, observations and variables are interchanged.
Ignores `.raw`.
"""
if not self.isbacked:
X = self.X
else:
X = self.file["X"]
if self.is_view:
raise ValueError(
"You’re trying to transpose a view of an `AnnData`, "
"which is currently not implemented. Call `.copy()` before transposing."
)
def t_csr(m: sparse.spmatrix) -> sparse.csr_matrix:
return m.T.tocsr() if sparse.isspmatrix_csr(m) else m.T
return AnnData(
t_csr(X),
obs=self.var,
var=self.obs,
# we're taking a private attributes here to be able to modify uns of the original object
uns=self._uns,
obsm=self.varm.flipped(),
varm=self.obsm.flipped(),
obsp=self.varp.copy(),
varp=self.obsp.copy(),
filename=self.filename,
layers={k: t_csr(v) for k, v in self.layers.items()},
dtype=self.X.dtype.name,
)
T = property(transpose)
def to_df(self, layer=None) -> pd.DataFrame:
"""\
Generate shallow :class:`~pandas.DataFrame`.
The data matrix :attr:`X` is returned as
:class:`~pandas.DataFrame`, where :attr:`obs_names` initializes the
index, and :attr:`var_names` the columns.
* No annotations are maintained in the returned object.
* The data matrix is densified in case it is sparse.
Params
------
layer : str
Key for `.layers`.
"""
if layer is not None:
X = self.layers[layer]
else:
X = self.X
if issparse(X):
X = X.toarray()
return pd.DataFrame(X, index=self.obs_names, columns=self.var_names)
def _get_X(self, use_raw=False, layer=None):
"""\
Convenience method for getting expression values
with common arguments and error handling.
"""
is_layer = layer is not None
if use_raw and is_layer:
raise ValueError(
"Cannot use expression from both layer and raw. You provided:"
f"`use_raw={use_raw}` and `layer={layer}`"
)
if is_layer:
return self.layers[layer]
elif use_raw:
if self.raw is None:
raise ValueError("This AnnData doesn’t have a value in `.raw`.")
return self.raw.X
else:
return self.X
def obs_vector(self, k: str, *, layer: Optional[str] = None) -> np.ndarray:
"""\
Convenience function for returning a 1 dimensional ndarray of values
from :attr:`X`, :attr:`layers`\\ `[k]`, or :attr:`obs`.
Made for convenience, not performance.
Intentionally permissive about arguments, for easy iterative use.
Params
------
k
Key to use. Should be in :attr:`var_names` or :attr:`obs`\\ `.columns`.
layer
What layer values should be returned from. If `None`, :attr:`X` is used.
Returns
-------
A one dimensional nd array, with values for each obs in the same order
as :attr:`obs_names`.
"""
if layer == "X":
if "X" in self.layers:
pass
else:
warnings.warn(
"In a future version of AnnData, access to `.X` by passing"
" `layer='X'` will be removed. Instead pass `layer=None`.",
FutureWarning,
)
layer = None
return get_vector(self, k, "obs", "var", layer=layer)
def var_vector(self, k, *, layer: Optional[str] = None) -> np.ndarray:
"""\
Convenience function for returning a 1 dimensional ndarray of values
from :attr:`X`, :attr:`layers`\\ `[k]`, or :attr:`obs`.
Made for convenience, not performance. Intentionally permissive about
arguments, for easy iterative use.
Params
------
k
Key to use. Should be in :attr:`obs_names` or :attr:`var`\\ `.columns`.
layer
What layer values should be returned from. If `None`, :attr:`X` is used.
Returns
-------
A one dimensional nd array, with values for each var in the same order
as :attr:`var_names`.
"""
if layer == "X":
if "X" in self.layers:
pass
else:
warnings.warn(
"In a future version of AnnData, access to `.X` by passing "
"`layer='X'` will be removed. Instead pass `layer=None`.",
FutureWarning,
)
layer = None
return get_vector(self, k, "var", "obs", layer=layer)
@utils.deprecated("obs_vector")
def _get_obs_array(self, k, use_raw=False, layer=None):
"""\
Get an array from the layer (default layer='X') along the :attr:`obs`
dimension by first looking up `obs.keys` and then :attr:`obs_names`.
"""
if not use_raw or k in self.obs.columns:
return self.obs_vector(k=k, layer=layer)
else:
return self.raw.obs_vector(k)
@utils.deprecated("var_vector")
def _get_var_array(self, k, use_raw=False, layer=None):
"""\
Get an array from the layer (default layer='X') along the :attr:`var`
dimension by first looking up `var.keys` and then :attr:`var_names`.
"""
if not use_raw or k in self.var.columns:
return self.var_vector(k=k, layer=layer)
else:
return self.raw.var_vector(k)
def copy(self, filename: Optional[PathLike] = None) -> "AnnData":
"""Full copy, optionally on disk."""
if not self.isbacked:
if self.is_view:
# TODO: How do I unambiguously check if this is a copy?
# Subsetting this way means we don’t have to have a view type
# defined for the matrix, which is needed for some of the
# current distributed backend.
X = _subset(self._adata_ref.X, (self._oidx, self._vidx)).copy()
else:
X = self.X.copy()
# TODO: Figure out what case this is:
if X is not None:
dtype = X.dtype
if X.shape != self.shape:
X = X.reshape(self.shape)
else:
dtype = "float32"
return AnnData(
X=X,
obs=self.obs.copy(),
var=self.var.copy(),
# deepcopy on DictView does not work and is unnecessary
# as uns was copied already before
uns=self._uns.copy()
if isinstance(self.uns, DictView)
else deepcopy(self._uns),
obsm=self.obsm.copy(),
varm=self.varm.copy(),
obsp=self.obsp.copy(),
varp=self.varp.copy(),
raw=self.raw.copy() if self.raw is not None else None,
layers=self.layers.copy(),
dtype=dtype,
)
else:
from .._io import read_h5ad
if filename is None:
raise ValueError(
"To copy an AnnData object in backed mode, "
"pass a filename: `.copy(filename='myfilename.h5ad')`."
)
mode = self.file._filemode
self.write(filename)
return read_h5ad(filename, backed=mode)
def concatenate(
self,
*adatas: "AnnData",
join: str = "inner",
batch_key: str = "batch",
batch_categories: Sequence[Any] = None,
uns_merge: Optional[str] = None,
index_unique: Optional[str] = "-",
fill_value=None,
) -> "AnnData":
"""\
Concatenate along the observations axis.
The :attr:`uns`, :attr:`varm` and :attr:`obsm` attributes are ignored.
Currently, this works only in `'memory'` mode.
Parameters
----------
adatas
AnnData matrices to concatenate with. Each matrix is referred to as
a “batch”.
join
Use intersection (`'inner'`) or union (`'outer'`) of variables.
batch_key
Add the batch annotation to :attr:`obs` using this key.
batch_categories
Use these as categories for the batch annotation. By default, use increasing numbers.
uns_merge
Strategy to use for merging entries of uns. These strategies are applied recusivley.
Currently implemented strategies include:
* `None`: The default. The concatenated object will just have an empty dict for `uns`.
* `"same"`: Only entries which have the same value in all AnnData objects are kept.
* `"unique"`: Only entries which have one unique value in all AnnData objects are kept.
* `"first"`: The first non-missing value is used.
* `"only"`: A value is included if only one of the AnnData objects has a value at this
path.
index_unique
Make the index unique by joining the existing index names with the
batch category, using `index_unique='-'`, for instance. Provide
`None` to keep existing indices.
fill_value
Scalar value to fill newly missing values in arrays with. Note: only applies to arrays
and sparse matrices (not dataframes) and will only be used if `join="outer"`.
.. note::
If not provided, the default value is `0` for sparse matrices and `np.nan`
for numpy arrays. See the examples below for more information.
Returns
-------
:class:`~anndata.AnnData`
The concatenated :class:`~anndata.AnnData`, where `adata.obs[batch_key]`
stores a categorical variable labeling the batch.
Notes
-----
.. warning::
If you use `join='outer'` this fills 0s for sparse data when
variables are absent in a batch. Use this with care. Dense data is
filled with `NaN`. See the examples.
Examples
--------
Joining on intersection of variables.
>>> adata1 = AnnData(
... np.array([[1, 2, 3], [4, 5, 6]]),
... dict(obs_names=['s1', 's2'], anno1=['c1', 'c2']),
... dict(var_names=['a', 'b', 'c'], annoA=[0, 1, 2]),
... )
>>> adata2 = AnnData(
... np.array([[1, 2, 3], [4, 5, 6]]),
... dict(obs_names=['s3', 's4'], anno1=['c3', 'c4']),
... dict(var_names=['d', 'c', 'b'], annoA=[0, 1, 2]),
... )
>>> adata3 = AnnData(
... np.array([[1, 2, 3], [4, 5, 6]]),
... dict(obs_names=['s1', 's2'], anno2=['d3', 'd4']),
... dict(var_names=['d', 'c', 'b'], annoA=[0, 2, 3], annoB=[0, 1, 2]),
... )
>>> adata = adata1.concatenate(adata2, adata3)
>>> adata
AnnData object with n_obs × n_vars = 6 × 2
obs: 'anno1', 'anno2', 'batch'
var: 'annoA-0', 'annoA-1', 'annoA-2', 'annoB-2'
>>> adata.X
array([[2., 3.],
[5., 6.],
[3., 2.],
[6., 5.],
[3., 2.],
[6., 5.]], dtype=float32)
>>> adata.obs
anno1 anno2 batch
s1-0 c1 NaN 0
s2-0 c2 NaN 0
s3-1 c3 NaN 1
s4-1 c4 NaN 1
s1-2 NaN d3 2
s2-2 NaN d4 2
>>> adata.var.T
b c
annoA-0 1 2
annoA-1 2 1
annoA-2 3 2
annoB-2 2 1
Joining on the union of variables.
>>> outer = adata1.concatenate(adata2, adata3, join='outer')
>>> outer
AnnData object with n_obs × n_vars = 6 × 4
obs: 'anno1', 'anno2', 'batch'
var: 'annoA-0', 'annoA-1', 'annoA-2', 'annoB-2'
>>> outer.var.T
a b c d
annoA-0 0.0 1.0 2.0 NaN
annoA-1 NaN 2.0 1.0 0.0
annoA-2 NaN 3.0 2.0 0.0
annoB-2 NaN 2.0 1.0 0.0
>>> outer.var_names
Index(['a', 'b', 'c', 'd'], dtype='object')
>>> outer.X
array([[ 1., 2., 3., nan],
[ 4., 5., 6., nan],
[nan, 3., 2., 1.],
[nan, 6., 5., 4.],
[nan, 3., 2., 1.],
[nan, 6., 5., 4.]], dtype=float32)
>>> outer.X.sum(axis=0)
array([nan, 25., 23., nan], dtype=float32)
>>> import pandas as pd
>>> Xdf = pd.DataFrame(outer.X, columns=outer.var_names)
>>> Xdf
a b c d
0 1.0 2.0 3.0 NaN
1 4.0 5.0 6.0 NaN
2 NaN 3.0 2.0 1.0
3 NaN 6.0 5.0 4.0
4 NaN 3.0 2.0 1.0
5 NaN 6.0 5.0 4.0
>>> Xdf.sum()
a 5.0
b 25.0
c 23.0
d 10.0
dtype: float32
One way to deal with missing values is to use masked arrays:
>>> from numpy import ma
>>> outer.X = ma.masked_invalid(outer.X)
>>> outer.X
masked_array(
data=[[1.0, 2.0, 3.0, --],
[4.0, 5.0, 6.0, --],
[--, 3.0, 2.0, 1.0],
[--, 6.0, 5.0, 4.0],
[--, 3.0, 2.0, 1.0],
[--, 6.0, 5.0, 4.0]],
mask=[[False, False, False, True],
[False, False, False, True],
[ True, False, False, False],
[ True, False, False, False],
[ True, False, False, False],
[ True, False, False, False]],
fill_value=1e+20,
dtype=float32)
>>> outer.X.sum(axis=0).data
array([ 5., 25., 23., 10.], dtype=float32)
The masked array is not saved but has to be reinstantiated after saving.
>>> outer.write('./test.h5ad')
>>> from anndata import read_h5ad
>>> outer = read_h5ad('./test.h5ad')
>>> outer.X
array([[ 1., 2., 3., nan],
[ 4., 5., 6., nan],
[nan, 3., 2., 1.],
[nan, 6., 5., 4.],
[nan, 3., 2., 1.],
[nan, 6., 5., 4.]], dtype=float32)
For sparse data, everything behaves similarly,
except that for `join='outer'`, zeros are added.
>>> from scipy.sparse import csr_matrix
>>> adata1 = AnnData(
... csr_matrix([[0, 2, 3], [0, 5, 6]]),
... dict(obs_names=['s1', 's2'], anno1=['c1', 'c2']),
... dict(var_names=['a', 'b', 'c']),
... )
>>> adata2 = AnnData(
... csr_matrix([[0, 2, 3], [0, 5, 6]]),
... dict(obs_names=['s3', 's4'], anno1=['c3', 'c4']),
... dict(var_names=['d', 'c', 'b']),
... )
>>> adata3 = AnnData(
... csr_matrix([[1, 2, 0], [0, 5, 6]]),
... dict(obs_names=['s5', 's6'], anno2=['d3', 'd4']),
... dict(var_names=['d', 'c', 'b']),
... )
>>> adata = adata1.concatenate(adata2, adata3, join='outer')
>>> adata.var_names
Index(['a', 'b', 'c', 'd'], dtype='object')
>>> adata.X.toarray()
array([[0., 2., 3., 0.],
[0., 5., 6., 0.],
[0., 3., 2., 0.],
[0., 6., 5., 0.],
[0., 0., 2., 1.],
[0., 6., 5., 0.]], dtype=float32)
"""
from .merge import concat, merge_outer, merge_dataframes, merge_same
if self.isbacked:
raise ValueError("Currently, concatenate does only work in memory mode.")
if len(adatas) == 0:
return self.copy()
elif len(adatas) == 1 and not isinstance(adatas[0], AnnData):
adatas = adatas[0] # backwards compatibility
all_adatas = (self,) + tuple(adatas)
out = concat(
all_adatas,
axis=0,
join=join,
label=batch_key,
keys=batch_categories,
uns_merge=uns_merge,
fill_value=fill_value,
index_unique=index_unique,
pairwise=False,
)
### Backwards compat (some of this could be more efficient)
# obs used to always be an outer join
out.obs = concat(
[AnnData(sparse.csr_matrix(a.shape), obs=a.obs) for a in all_adatas],
axis=0,
join="outer",
label=batch_key,
keys=batch_categories,
index_unique=index_unique,
).obs
# Removing varm
del out.varm
# Implementing old-style merging of var
if batch_categories is None:
batch_categories = np.arange(len(all_adatas)).astype(str)
pat = rf"-({'|'.join(batch_categories)})$"
out.var = merge_dataframes(
[a.var for a in all_adatas],
out.var_names,
partial(merge_outer, batch_keys=batch_categories, merge=merge_same),
)
out.var = out.var.iloc[
:,
(
out.var.columns.str.extract(pat, expand=False)
.fillna("")
.argsort(kind="stable")
),
]
return out
def var_names_make_unique(self, join: str = "-"):
# Important to go through the setter so obsm dataframes are updated too
self.var_names = utils.make_index_unique(self.var.index, join)
var_names_make_unique.__doc__ = utils.make_index_unique.__doc__
def obs_names_make_unique(self, join: str = "-"):
# Important to go through the setter so obsm dataframes are updated too
self.obs_names = utils.make_index_unique(self.obs.index, join)
obs_names_make_unique.__doc__ = utils.make_index_unique.__doc__
def _check_uniqueness(self):
if not self.obs.index.is_unique:
utils.warn_names_duplicates("obs")
if not self.var.index.is_unique:
utils.warn_names_duplicates("var")
def __contains__(self, key: Any):
raise AttributeError(
"AnnData has no attribute __contains__, don’t check `in adata`."
)
def _check_dimensions(self, key=None):
if key is None:
key = {"obs", "var", "obsm", "varm"}
else:
key = {key}
if "obs" in key and len(self._obs) != self._n_obs:
raise ValueError(
"Observations annot. `obs` must have number of rows of `X`"
f" ({self._n_obs}), but has {self._obs.shape[0]} rows."
)
if "var" in key and len(self._var) != self._n_vars:
raise ValueError(
"Variables annot. `var` must have number of columns of `X`"
f" ({self._n_vars}), but has {self._var.shape[0]} rows."
)
if "obsm" in key:
obsm = self._obsm
if (
not all([o.shape[0] == self._n_obs for o in obsm.values()])
and len(obsm.dim_names) != self._n_obs
):
raise ValueError(
"Observations annot. `obsm` must have number of rows of `X`"
f" ({self._n_obs}), but has {len(obsm)} rows."
)
if "varm" in key:
varm = self._varm
if (
not all([v.shape[0] == self._n_vars for v in varm.values()])
and len(varm.dim_names) != self._n_vars
):
raise ValueError(
"Variables annot. `varm` must have number of columns of `X`"
f" ({self._n_vars}), but has {len(varm)} rows."
)
def write_h5ad(
self,
filename: Optional[PathLike] = None,
compression: Optional[Literal["gzip", "lzf"]] = None,
compression_opts: Union[int, Any] = None,
force_dense: Optional[bool] = None,
as_dense: Sequence[str] = (),
):
"""\
Write `.h5ad`-formatted hdf5 file.
.. note::
Setting compression to `'gzip'` can save disk space
but will slow down writing and subsequent reading.
Prior to v0.6.16, this was the default for parameter `compression`.
Generally, if you have sparse data that are stored as a dense matrix,
you can dramatically improve performance and reduce disk space
by converting to a :class:`~scipy.sparse.csr_matrix`::
from scipy.sparse import csr_matrix
adata.X = csr_matrix(adata.X)
Parameters
----------
filename
Filename of data file. Defaults to backing file.
compression
See the h5py :ref:`dataset_compression`.
compression_opts
See the h5py :ref:`dataset_compression`.
as_dense
Sparse arrays in AnnData object to write as dense. Currently only
supports `X` and `raw/X`.
force_dense
Write sparse data as a dense matrix.
Defaults to `True` if object is backed, otherwise to `False`.
"""
from .._io.write import _write_h5ad
if filename is None and not self.isbacked:
raise ValueError("Provide a filename!")
if filename is None:
filename = self.filename
_write_h5ad(
Path(filename),
self,
compression=compression,
compression_opts=compression_opts,
force_dense=force_dense,
as_dense=as_dense,
)
if self.isbacked:
self.file.close()
write = write_h5ad # a shortcut and backwards compat
def write_csvs(self, dirname: PathLike, skip_data: bool = True, sep: str = ","):
"""\
Write annotation to `.csv` files.
It is not possible to recover the full :class:`~anndata.AnnData` from
these files. Use :meth:`write` for this.
Parameters
----------
dirname
Name of directory to which to export.
skip_data
Skip the data matrix :attr:`X`.
sep
Separator for the data.
"""
from .._io.write import write_csvs
write_csvs(dirname, self, skip_data=skip_data, sep=sep)
def write_loom(self, filename: PathLike, write_obsm_varm: bool = False):
"""\
Write `.loom`-formatted hdf5 file.
Parameters
----------
filename
The filename.
"""
from .._io.write import write_loom
write_loom(filename, self, write_obsm_varm=write_obsm_varm)
def write_zarr(
self,
store: Union[MutableMapping, PathLike],
chunks: Union[bool, int, Tuple[int, ...], None] = None,
):
"""\
Write a hierarchical Zarr array store.
Parameters
----------
store
The filename, a :class:`~typing.MutableMapping`, or a Zarr storage class.
chunks
Chunk shape.
"""
from .._io.write import write_zarr
write_zarr(store, self, chunks=chunks)
def chunked_X(self, chunk_size: Optional[int] = None):
"""\
Return an iterator over the rows of the data matrix :attr:`X`.
Parameters
----------
chunk_size
Row size of a single chunk.
"""
if chunk_size is None:
# Should be some adaptive code
chunk_size = 6000
start = 0
n = self.n_obs
for _ in range(int(n // chunk_size)):
end = start + chunk_size
yield (self.X[start:end], start, end)
start = end
if start < n:
yield (self.X[start:n], start, n)
def chunk_X(
self,
select: Union[int, Sequence[int], np.ndarray] = 1000,
replace: bool = True,
):
"""\
Return a chunk of the data matrix :attr:`X` with random or specified indices.
Parameters
----------
select
Depending on the type:
:class:`int`
A random chunk with `select` rows will be returned.
:term:`sequence` (e.g. a list, tuple or numpy array) of :class:`int`
A chunk with these indices will be returned.
replace
If `select` is an integer then `True` means random sampling of
indices with replacement, `False` without replacement.
"""
if isinstance(select, int):
select = select if select < self.n_obs else self.n_obs
choice = np.random.choice(self.n_obs, select, replace)
elif isinstance(select, (np.ndarray, cabc.Sequence)):
choice = np.asarray(select)
else:
raise ValueError("select should be int or array")
reverse = None
if self.isbacked:
# h5py can only slice with a sorted list of unique index values
# so random batch with indices [2, 2, 5, 3, 8, 10, 8] will fail
# this fixes the problem
indices, reverse = np.unique(choice, return_inverse=True)
selection = self.X[indices.tolist()]
else:
selection = self.X[choice]
selection = selection.toarray() if issparse(selection) else selection
return selection if reverse is None else selection[reverse]
# --------------------------------------------------------------------------
# all of the following is for backwards compat
# --------------------------------------------------------------------------
@property
@utils.deprecated("is_view")
def isview(self):
return self.is_view
def _clean_up_old_format(self, uns):
# multicolumn keys
# all of the rest is only for backwards compat
for bases in [["obs", "smp"], ["var"]]:
axis = bases[0]
for k in [f"{p}{base}_keys_multicol" for p in ["", "_"] for base in bases]:
if uns and k in uns:
keys = list(uns[k])
del uns[k]
break
else:
keys = []
# now, for compat, fill the old multicolumn entries into obsm and varm
# and remove them from obs and var
m_attr = getattr(self, f"_{axis}m")
for key in keys:
m_attr[key] = self._get_and_delete_multicol_field(axis, key)
def _get_and_delete_multicol_field(self, a, key_multicol):
keys = []
for k in getattr(self, a).columns:
if k.startswith(key_multicol):
keys.append(k)
values = getattr(self, a)[keys].values
getattr(self, a).drop(keys, axis=1, inplace=True)
return values
|