-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgs.py
180 lines (147 loc) · 4.96 KB
/
mgs.py
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
import json
import urllib.request
from collections import Counter
from collections.abc import Iterable
from dataclasses import dataclass
from datetime import date
from enum import Enum
from typing import NewType, Optional
from pydantic import BaseModel
from pathogen_properties import TaxID
from tree import Tree
MGS_REPO_DEFAULTS = {
"user": "naobservatory",
"repo": "mgs-pipeline",
"ref": "data-2023-07-21",
}
BioProject = NewType("BioProject", str)
Sample = NewType("Sample", str)
target_bioprojects = {
"crits_christoph": [BioProject("PRJNA661613")],
"rothman": [BioProject("PRJNA729801")],
"spurbeck": [BioProject("PRJNA924011")],
"brinch": [BioProject("PRJEB13832"), BioProject("PRJEB34633")],
}
@dataclass
class GitHubRepo:
user: str
repo: str
ref: str
def get_file(self, path: str) -> str:
file_url = (
f"https://raw.githubusercontent.com/"
f"{self.user}/{self.repo}/{self.ref}/{path}"
)
with urllib.request.urlopen(file_url) as response:
if response.status == 200:
return response.read()
else:
raise ValueError(
f"Failed to download {file_url}. "
f"Response status code: {response.status}"
)
def load_bioprojects(repo: GitHubRepo) -> dict[BioProject, list[Sample]]:
data = json.loads(repo.get_file("dashboard/metadata_bioprojects.json"))
return {
BioProject(bp): [Sample(s) for s in samples]
for bp, samples in data.items()
}
class Enrichment(Enum):
VIRAL = "viral"
PANEL = "panel"
class SampleAttributes(BaseModel):
country: str
state: Optional[str] = None
county: Optional[str] = None
location: str
fine_location: Optional[str] = None
# Fixme: Not all the dates are real dates
date: date | str
reads: int
enrichment: Optional[Enrichment] = None
method: Optional[str] = None
def load_sample_attributes(repo: GitHubRepo) -> dict[Sample, SampleAttributes]:
data = json.loads(repo.get_file("dashboard/metadata_samples.json"))
return {
Sample(s): SampleAttributes(**attribs) for s, attribs in data.items()
}
SampleCounts = dict[TaxID, dict[Sample, int]]
def load_sample_counts(repo: GitHubRepo) -> SampleCounts:
data: dict[str, dict[str, int]] = json.loads(
repo.get_file("dashboard/human_virus_sample_counts.json")
)
return {
TaxID(int(taxid)): {Sample(sample): n for sample, n in counts.items()}
for taxid, counts in data.items()
}
def load_tax_tree(repo: GitHubRepo) -> Tree[TaxID]:
data = json.loads(repo.get_file("dashboard/human_virus_tree.json"))
return Tree.tree_from_list(data).map(lambda x: TaxID(int(x)))
def make_count_tree(
taxtree: Tree[TaxID], sample_counts: SampleCounts
) -> Tree[tuple[TaxID, Counter[Sample]]]:
return taxtree.map(
lambda taxid: (
(taxid, Counter(sample_counts[taxid]))
if taxid in sample_counts
else (taxid, Counter())
),
)
def count_reads(
taxtree: Tree[TaxID] | None, sample_counts: SampleCounts
) -> Counter[Sample]:
if taxtree is None:
return Counter()
count_tree = make_count_tree(taxtree, sample_counts)
return sum(
(elem.data[1] for elem in count_tree),
start=Counter(),
)
@dataclass
class MGSData:
bioprojects: dict[BioProject, list[Sample]]
sample_attrs: dict[Sample, SampleAttributes]
read_counts: SampleCounts
tax_tree: Tree[TaxID]
@staticmethod
def from_repo(
user=MGS_REPO_DEFAULTS["user"],
repo=MGS_REPO_DEFAULTS["repo"],
ref=MGS_REPO_DEFAULTS["ref"],
):
repo = GitHubRepo(user, repo, ref)
return MGSData(
bioprojects=load_bioprojects(repo),
sample_attrs=load_sample_attributes(repo),
read_counts=load_sample_counts(repo),
tax_tree=load_tax_tree(repo),
)
def sample_attributes(
self, bioproject: BioProject, enrichment: Optional[Enrichment] = None
) -> dict[Sample, SampleAttributes]:
samples = {
s: self.sample_attrs[s] for s in self.bioprojects[bioproject]
}
if enrichment:
return {
s: attrs
for s, attrs in samples.items()
if attrs.enrichment == enrichment
}
else:
return samples
def total_reads(self, bioproject: BioProject) -> dict[Sample, int]:
return {
s: self.sample_attrs[s].reads for s in self.bioprojects[bioproject]
}
def viral_reads(
self, bioproject: BioProject, taxids: Iterable[TaxID]
) -> dict[Sample, int]:
viral_counts_by_taxid = {
taxid: count_reads(self.tax_tree[taxid], self.read_counts)
for taxid in taxids
}
return {
s: sum(viral_counts_by_taxid[taxid][s] for taxid in taxids)
for s in self.bioprojects[bioproject]
}