-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_client.py
271 lines (235 loc) · 11.1 KB
/
metadata_client.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
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
__author__ = 'Paul Sarando'
import config.emblebi_ena_submit_properties
from xml.sax.saxutils import escape, unescape
import os
import json
class MetadataClient:
"""
A class for transforming DE Data Store Metadata into NCBI SRA XML template metadata.
The get_metadata method expects the DE metadata file to contain JSON in the following format:
{
"id": "642ad94c-bd2a-11e4-891f-6abdce5a08d5",
"label": "BioProject",
"metadata": [
{
"attr": "BioProject-attribute1",
"value": "BioProject-value1"
},
{
"attr": "BioProject-attribute2",
"value": "BioProject-value2"
},
...
],
"folders": [
{
"id": "6e80bd08-bd2a-11e4-891f-6abdce5a08d5",
"label": "bio_sample_1",
"metadata": [
{
"attr": "sra_sample_id",
"value": "12345.biosample"
},
{
"attr": "bio_sample-reserved-attribute",
"value": "bio_sample-reserved-attribute"
},
{
"attr": "bio_sample-attribute1",
"value": "bio_sample-value1"
},
{
"attr": "bio_sample-attribute2",
"value": "bio_sample-value2"
},
...
],
"folders": [
{
"id": "7707eb7c-bd2a-11e4-891f-6abdce5a08d5",
"label": "library1",
"metadata": [
{
"attr": "library-attribute1",
"value": "library-value1"
},
{
"attr": "library-attribute2",
"value": "library-value2"
},
...
],
"files": [
{
"id": "7b6bc394-bd31-11e4-891f-6abdce5a08d5",
"label": "fasta-file1.gz",
"md5": "064848455cab44dade17bc5a3414d8b1"
},
{
"id": "cedd728e-bd31-11e4-891f-6abdce5a08d5",
"label": "fasta-file2.fgz",
"md5": "86913eb52f5f67aaaf711f4c32c2b0c6"
},
...
]
},
...
]
},
...
]
}
The get_metadata method will return NCBI SRA XML template metadata in the following format:
{
"BioProject-attribute1": "BioProject-value1",
"BioProject-attribute2": "BioProject-value2",
"bio_samples": [
{
"name": "bio_sample_name",
"sra_sample_id": "12345.biosample",
"bio_sample-reserved-attribute": "bio_sample-reserved-attribute",
"attributes": [
{
"name": "bio_sample-attribute1",
"value": "bio_sample-value1"
},
{
"name": "bio_sample-attribute2",
"value": "bio_sample-value2"
}
]
}
],
"libraries": [
{
"name": "library_name",
"sra_sample_id": "12345.biosample",
"library-reserved-attribute": "library-reserved-attribute",
"attributes": [
{
"name": "library-attribute1",
"value": "library-value1"
},
{
"name": "library-attribute2",
"value": "library-value2"
}
],
"files": [
{
"filename": "fasta-file1.gz",
"md5": "064848455cab44dade17bc5a3414d8b1"
},
{
"filename": "fasta-file2.fgz",
"md5": "86913eb52f5f67aaaf711f4c32c2b0c6"
}
]
}
]
}
"""
def __init__(self):
#self.bio_sample_reserved_attributes = config.ncbi_sra_submit_properties.bio_sample_reserved_attributes
#self.bio_sample_dup_attributes = config.ncbi_sra_submit_properties.bio_sample_dup_attributes
#self.library_reserved_attributes = config.ncbi_sra_submit_properties.library_reserved_attributes
self.compressed_content_types = config.emblebi_ena_submit_properties.compressed_content_types
def get_metadata(self, json_file):
with open(json_file) as file:
metadata = json.load(file)
if not metadata.get('metadata'):
raise Exception("Could not load Bio Project metadata")
if not metadata.get('folders'):
raise Exception("Could not find Bio Project folder metadata")
bio_project = {"sra_object_id": metadata['id']}
project_metadata = metadata['metadata']
for attribute in project_metadata:
if not (attribute.get('attr') and attribute.get('value')):
continue
attr = attribute['attr']
value = escape(attribute['value'])
if attr in bio_project:
raise Exception("Duplicate '{0}' attribute found in Bio Project folder metadata.\nValues:\n{1}\n{2}".format(attr, bio_project[attr], value))
bio_project[attr] = value
bio_project['bio_samples'] = [self._parse_folder_metadata(folder)
for folder in metadata['folders']]
bio_project['libraries'] = [library
for bio_sample in bio_project['bio_samples']
for library in bio_sample['libraries']]
return bio_project
def _parse_folder_metadata(self, bio_sample_folder):
if not bio_sample_folder.get('path'):
raise Exception("Could not find Bio Sample folder path")
bio_sample_name = os.path.basename(bio_sample_folder['path'])
if not bio_sample_folder.get('id'):
raise Exception("Could not find Bio Sample ID: {0}".format(bio_sample_name))
if not bio_sample_folder.get('metadata'):
raise Exception("Could not load Bio Sample metadata: {0}".format(bio_sample_name))
if not bio_sample_folder.get('folders'):
raise Exception("Could not find Bio Sample folder metadata: {0}".format(bio_sample_name))
bio_sample = {"sra_sample_id": bio_sample_folder['id'],
"name": bio_sample_name,
"attributes": []}
metadata = bio_sample_folder['metadata']
for attribute in metadata:
if not (attribute.get('attr') and attribute.get('value')):
continue
attr = attribute['attr']
value = escape(attribute['value'])
if attr in self.bio_sample_reserved_attributes:
if attr in bio_sample:
raise Exception("Duplicate '{0}' attribute found in Bio Sample metadata.\nValues:\n{1}\n{2}".format(attr, bio_sample[attr], value))
bio_sample[attr] = value
else:
bio_sample['attributes'].append({'name': escape(attr), 'value': value})
if attr in self.bio_sample_dup_attributes:
# This attribute needs to be duplicated outside the attributes node as well.
bio_sample[attr] = value
bio_sample['libraries'] = [self._parse_library_metadata(bio_sample['sra_sample_id'], bio_sample['name'], library)
for library in bio_sample_folder['folders']]
return bio_sample
def _parse_library_metadata(self, sra_sample_id, bio_sample_name, library_folder):
if not library_folder.get('path'):
raise Exception("Could not find Bio Sample Library folder path")
library_name = os.path.join(bio_sample_name, os.path.basename(library_folder['path']))
if not library_folder.get('id'):
raise Exception("Could not find Bio Sample Library ID: {0}".format(library_name))
if not library_folder.get('metadata'):
raise Exception("Could not load Bio Sample Library metadata: {0}".format(library_name))
if not library_folder.get('files'):
raise Exception("Could not find Bio Sample Library file metadata: {0}".format(library_name))
library = {"library_id": library_folder['id'],
"name": library_name,
"sra_sample_id": sra_sample_id,
"attributes": []}
metadata = library_folder['metadata']
for attribute in metadata:
if not (attribute.get('attr') and attribute.get('value')):
continue
attr = attribute['attr']
value = escape(attribute['value'])
if attr in self.library_reserved_attributes:
if attr in library:
raise Exception("Duplicate '{0}' attribute found in Bio Sample Library metadata.\nValues:\n{1}\n{2}".format(attr, library[attr], value))
library[attr] = value
else:
library['attributes'].append({'name': escape(attr), 'value': value})
library['files'] = [self._parse_file_metadata(library_name, file)
for file in library_folder['files']]
return library
def _parse_file_metadata(self, library_name, file_metadata):
if not file_metadata.get('path'):
raise Exception("Could not find Bio Sample Library file path: {0}".format(library_name))
filename = os.path.basename(file_metadata['path'])
if not file_metadata.get('content-type'):
raise Exception("Could not find Bio Sample Library file content-type metadata: {0}".format(os.path.join(library_name, filename)))
if file_metadata['content-type'] not in self.compressed_content_types:
raise Exception("Bio Sample Library file does not appear to be compressed: {0}".format(os.path.join(library_name, filename)))
if not file_metadata.get('md5'):
raise Exception("Could not find Bio Sample Library file md5 metadata: {0}".format(os.path.join(library_name, filename)))
return {"filename": escape(filename),
"md5": file_metadata['md5']}
def get_bio_project_file_paths(self, bio_project_metadata, bio_project_folder):
return [os.path.join(bio_project_folder, file['name'], unescape(file['filename']))
for runs in bio_project_metadata['bundle']['runs']
for file in runs['files']]