Witold Wydmański
commited on
Commit
·
d17ef4a
1
Parent(s):
2df789b
init
Browse files- README.md +17 -0
- index.tsv +0 -0
- metagenomic_curated.py +102 -0
- sampleMetadata.rda +0 -0
README.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1 |
---
|
2 |
license: artistic-2.0
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: artistic-2.0
|
3 |
---
|
4 |
+
|
5 |
+
# Metagenomic curated data
|
6 |
+
This is a python repack of the curated data from the [Metagenomic Data Repository](https://waldronlab.io/curatedMetagenomicData/).
|
7 |
+
|
8 |
+
Please refer to the [study list](https://experimenthub.bioconductor.org/package/curatedMetagenomicData) and [study metadata](https://waldronlab.io/curatedMetagenomicData/articles/available-studies.html) for the list of available datasets.
|
9 |
+
|
10 |
+
## Sample usage
|
11 |
+
```python
|
12 |
+
ds = datasets.load_dataset("wwydmanski/metagenomic_curated", "EH1914")
|
13 |
+
X = np.array(ds['train']['features'])
|
14 |
+
y = np.array([x['study_condition'] for x in ds['train']['metadata']])
|
15 |
+
```
|
16 |
+
|
17 |
+
## Finding a relevant dataset EHID
|
18 |
+
The easiest way to find an interesting study is via [study metadata](https://waldronlab.io/curatedMetagenomicData/articles/available-studies.html). After that, you can find corresponding EHIDs by referring on the https://experimenthub.bioconductor.org/title/{study_name} page.
|
19 |
+
|
20 |
+
Let's say that the ThomasAM_2018a study piqued your curiosity - it means that you will be able to find all relevant datasets on the [https://experimenthub.bioconductor.org/title/ThomasAM_2018a](https://experimenthub.bioconductor.org/title/ThomasAM_2018a) website.
|
index.tsv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
metagenomic_curated.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#%%
|
2 |
+
import pyreadr
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import sqlite3
|
6 |
+
import requests
|
7 |
+
import datasets
|
8 |
+
import tempfile
|
9 |
+
import rdata
|
10 |
+
import json
|
11 |
+
|
12 |
+
#%%
|
13 |
+
sqlite_url = "https://experimenthub.bioconductor.org/metadata/experimenthub.sqlite3"
|
14 |
+
DATA_URL = "https://bioconductorhubs.blob.core.windows.net/experimenthub/curatedMetagenomicData/"
|
15 |
+
|
16 |
+
CITATION = """\
|
17 |
+
Pasolli E, Schiffer L, Manghi P, Renson A, Obenchain V, Truong D, Beghini F, Malik F, Ramos M, Dowd J, Huttenhower C, Morgan M, Segata N, Waldron L (2017). Accessible, curated metagenomic data through ExperimentHub. Nat. Methods, 14 (11), 1023-1024. ISSN 1548-7091, 1548-7105, doi: 10.1038/nmeth.4468.
|
18 |
+
"""
|
19 |
+
|
20 |
+
# %%
|
21 |
+
# def get_metadata():
|
22 |
+
# with tempfile.NamedTemporaryFile(delete=False) as tmpfname:
|
23 |
+
# r = requests.get(sqlite_url, allow_redirects=True)
|
24 |
+
# open(tmpfname.name, 'wb').write(r.content)
|
25 |
+
|
26 |
+
# db = sqlite3.connect(tmpfname.name)
|
27 |
+
# cursor = db.cursor()
|
28 |
+
# cur = cursor.execute("""SELECT * FROM resources""")
|
29 |
+
|
30 |
+
# ehid = []
|
31 |
+
# descriptions = []
|
32 |
+
# for row in cur.fetchall():
|
33 |
+
# if "curatedMetagenomicData" in str(row[-1]):
|
34 |
+
# ehid.append(row[1])
|
35 |
+
# descriptions.append(row[7])
|
36 |
+
# return ehid, descriptions
|
37 |
+
|
38 |
+
def get_metadata():
|
39 |
+
ehids = []
|
40 |
+
descriptions = []
|
41 |
+
with open("index.tsv", "r") as f:
|
42 |
+
for line in f:
|
43 |
+
ehid, desc = line.split("\t")
|
44 |
+
ehids.append(ehid)
|
45 |
+
descriptions.append(desc)
|
46 |
+
return ehids, descriptions
|
47 |
+
|
48 |
+
# %%
|
49 |
+
class MetagenomicCurated(datasets.GeneratorBasedBuilder):
|
50 |
+
"""Metagenomic Curated Data"""
|
51 |
+
|
52 |
+
ehids, descriptions = get_metadata()
|
53 |
+
BUILDER_CONFIGS = [
|
54 |
+
datasets.BuilderConfig(name=ehid,
|
55 |
+
version=datasets.Version("1.0.0"),
|
56 |
+
description=d.strip())
|
57 |
+
for ehid, d in zip(ehids, descriptions)
|
58 |
+
]
|
59 |
+
|
60 |
+
def _info(self):
|
61 |
+
return datasets.DatasetInfo(
|
62 |
+
description=self.config.description,
|
63 |
+
citation=CITATION,
|
64 |
+
homepage="https://waldronlab.io/curatedMetagenomicData/index.html",
|
65 |
+
license="https://www.r-project.org/Licenses/Artistic-2.0",
|
66 |
+
)
|
67 |
+
|
68 |
+
def _split_generators(self, dl_manager):
|
69 |
+
json_url = f"https://experimenthub.bioconductor.org/ehid/{self.config.name}"
|
70 |
+
r = requests.get(json_url, allow_redirects=True)
|
71 |
+
metadata = json.loads(r.content)
|
72 |
+
url = metadata['location_prefix']+metadata['rdatapaths'][0]['rdatapath']
|
73 |
+
|
74 |
+
data_fname: str = dl_manager.download(url)
|
75 |
+
|
76 |
+
return [
|
77 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_fname}),
|
78 |
+
]
|
79 |
+
|
80 |
+
def _generate_examples(self, filepath):
|
81 |
+
parsed = rdata.parser.parse_file(filepath)
|
82 |
+
converted = rdata.conversion.convert(parsed)
|
83 |
+
expressions = list(converted.values())[0].assayData['exprs']
|
84 |
+
data_df = expressions.to_pandas().T
|
85 |
+
study_name = list(converted.keys())[0].split(".")[0]
|
86 |
+
|
87 |
+
meta = pyreadr.read_r("sampleMetadata.rda")['sampleMetadata']
|
88 |
+
metadata = meta.loc[meta['study_name'] == study_name].set_index('sample_id')
|
89 |
+
|
90 |
+
for idx, (i, row) in enumerate(data_df.iterrows()):
|
91 |
+
yield idx, {
|
92 |
+
"features": row.values,
|
93 |
+
"metadata": {i: str(j) for i, j in metadata.loc[i].to_dict().items()}
|
94 |
+
}
|
95 |
+
|
96 |
+
# %%
|
97 |
+
if __name__=="__main__":
|
98 |
+
ds = datasets.load_dataset("./metagenomic_curated.py", "EH1914")
|
99 |
+
X = np.array(ds['train']['features'])
|
100 |
+
y = np.array([x['study_condition'] for x in ds['train']['metadata']])
|
101 |
+
|
102 |
+
# %%
|
sampleMetadata.rda
ADDED
Binary file (610 kB). View file
|
|