-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch.py
61 lines (41 loc) · 1.94 KB
/
batch.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
import logging
from NIPTool.build.document import build_sample, build_batch
from NIPTool.models.validation import requiered_fields
from NIPTool.exeptions import MissingResultsError
from pathlib import Path
import json
LOG = logging.getLogger(__name__)
def check_requiered_fields(document):
"""Check if document keys contain required fields"""
if not set(requiered_fields).issubset(set(document.keys())):
LOG.info(f"Could not add document {document}. Requiered fields missing.")
return False
return True
def load_batch(adapter, batch_data: dict, request_data: dict) -> None:
"""Function to load data from fluffy result file."""
mongo_batch = build_batch(batch_data, request_data)
adapter.add_or_update_document(mongo_batch, adapter.batch_collection)
def load_samples(adapter, batch_data: list, project_name: str) -> None:
"""Function to load data from fluffy result file."""
for sample in batch_data:
if not check_requiered_fields(sample):
continue
mongo_sample = build_sample(sample)
mongo_sample["SampleProject"] = project_name
adapter.add_or_update_document(mongo_sample, adapter.sample_collection)
def load_concentrations(adapter, concentrations_file: str) -> None:
"""Function to load concentrations to samples in the database."""
file = Path(concentrations_file)
if not file.exists():
raise MissingResultsError("Concentrations file missing.")
with open(file) as data_file:
concentrations = json.load(data_file)
for sample, concentration in concentrations.items():
mongo_sample = adapter.sample(sample)
if not mongo_sample:
LOG.warning(
f"Trying to add concentration to sample {sample} but it doesnt exist in the database."
)
return
mongo_sample["concentration"] = concentration
adapter.add_or_update_document(mongo_sample, adapter.sample_collection)