-
Notifications
You must be signed in to change notification settings - Fork 1
/
hdf5.Rmd
178 lines (132 loc) · 3.91 KB
/
hdf5.Rmd
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
---
title: "Working with HDF5 data in R"
date: "`r Sys.Date()`"
output:
workflowr::wflow_html:
toc: false
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(hdf5r)
```
# Introduction
From [Wikipedia](https://en.wikipedia.org/wiki/Hierarchical_Data_Format):
> Hierarchical Data Format (HDF) is a set of file formats (HDF4, HDF5) designed
to store and organize large amounts of data. Originally developed at the U.S.
National Center for Supercomputing Applications, it is supported by The HDF
Group, a non-profit corporation whose mission is to ensure continued development
of HDF5 technologies and the continued accessibility of data stored in HDF.
>
> In keeping with this goal, the HDF libraries and associated tools are
available under a liberal, BSD-like license for general use. HDF is supported by
many commercial and non-commercial software platforms and programming languages.
The freely available HDF distribution consists of the library, command-line
utilities, test suite source, Java interface, and the Java-based HDF Viewer
(HDFView).
>
> The current version, HDF5, differs significantly in design and API from the
major legacy version HDF4.
## Data
[Download ARCHS4 HDF5 data](https://maayanlab.cloud/archs4/download.html) and
load using the [hdf5r](https://github.com/hhoeflin/hdf5r) package.
```{r human}
human <- H5File$new(filename = "/data/archs4/human_gene_v2.2.h5", mode = "r")
human
```
Different groups.
```{r groups}
human[["data"]]
human[["meta"]]
```
## Getting information about objects
Use the `names()` function to get all names of objects.
```{r names}
names(human)
```
The `ls()` function provides more information.
```{r ls}
human$ls()
```
List functions.
```{r list}
hdf5r::list.attributes(human)
hdf5r::list.datasets(human)
hdf5r::list.groups(human)
hdf5r::list.objects(human)
```
## Get data
Function to get data.
```{r get_data}
wrap <- function(x){
x <- sub("^", "[['", x)
sub("$", "']]", x)
}
get_data <- function(obj, ds, ...){
s <- unlist(strsplit(x = ds, split = "/"))
txt <- paste0(obj, paste(sapply(s, wrap), collapse = "", sep = ""), ...)
eval(parse(text = txt))
}
```
Check out some of the data.
```{r check_out_data}
get_data('human', 'meta/samples/title', '[1:5]')
get_data('human', 'meta/samples/sample', '[1:5]')
get_data('human', 'meta/samples/geo_accession', '[1:5]')
get_data('human', 'meta/samples/series_id', '[1:5]')
get_data('human', 'meta/samples/characteristics_ch1', '[1:5]')
get_data('human', 'meta/samples/extract_protocol_ch1', '[1]')
get_data('human', 'meta/samples/source_name_ch1', '[1:5]')
```
## Store gene symbols
Gene symbols.
```{r meta_genes_symbol}
human[['meta']][['genes']][['symbol']]
```
Store gene symbols.
```{r my_gene_sym}
my_idx <- 1:human[['meta']][['genes']][['symbol']]$maxdims
my_gene_sym <- human[['meta']][['genes']][['symbol']][my_idx]
head(my_gene_sym)
```
## Source
Check out some sources.
```{r example_source}
unique(get_data('human', 'meta/samples/source_name_ch1', '[1:100]'))
```
Store all sources.
```{r store_source}
my_idx <- 1:human[['meta']][['samples']][['source_name_ch1']]$maxdims
my_source <- human[['meta']][['samples']][['source_name_ch1']][my_idx]
head(my_source)
```
Search.
```{r length_source}
length(my_source[grepl("immune", my_source, ignore.case = TRUE)])
length(my_source[grepl("dendritic", my_source, ignore.case = TRUE)])
```
## Expression
Genes are columns and rows are samples.
```{r data_expression}
exp_data <- human[['data']][['expression']]
exp_data
```
Sum across first ten samples.
```{r sum_ten_samples}
apply(exp_data[1:10, ], 1, sum)
```
TP53 position.
```{r tp53}
tp53_idx <- match("TP53", my_gene_sym)
tp53_idx
```
## Clean up
Close.
```{r close_all}
human$close_all()
```
## Related
* [Official ARCHS4 companion package](https://github.com/MaayanLab/archs4py).
* [ARCHS4 help page](https://maayanlab.cloud/archs4/help.html)