Skip to content

Commit

Permalink
Merge 7873d28 into 6f56fd6
Browse files Browse the repository at this point in the history
  • Loading branch information
mayabrandi committed Nov 16, 2020
2 parents 6f56fd6 + 7873d28 commit a7efddc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
22 changes: 17 additions & 5 deletions NIPTool/server/templates/batch/coverage.html
Expand Up @@ -37,15 +37,27 @@
linewidth: 5,
title : 'Coverage Ratio'
},};
{% for samp, samp_data in data.items() %}
var trace1 = {
{% for samp, samp_data in scatter_data.items() %}
var scatter_plot = {
name: {{samp|tojson}},
y: {{samp_data}},
x: {{x_axis}},
y: {{samp_data.y}},
x: {{samp_data.x}},
mode: 'markers',
text: {{samp|tojson}},
type: 'scatter'};
data.push(trace1);
data.push(scatter_plot);
{% endfor %}
{% for chr, box in box_data.items() %}
var box_plot = {
name: {{chr}},
y: {{box}},
x: {{chr}},
showlegend: false,
boxpoints: false,
text: {{chr}},
marker:{color: '#ccccb3'},
type: 'box'};
data.push(box_plot);
{% endfor %}
Plotly.newPlot('cov_plot', data, layout);
</script>
Expand Down
66 changes: 45 additions & 21 deletions NIPTool/server/utils.py
Expand Up @@ -7,6 +7,8 @@
def get_sample_info(sample):
"""Sample info for sample table in batch view."""

sample_warnings = get_sample_warnings(sample)

sample_info_keys = ["Zscore_13", "Zscore_18", "Zscore_21", "CNVSegment", "FF_Formatted", "FFX", "FFY", "Zscore_X"]
for key in sample_info_keys:
val = sample.get(key)
Expand All @@ -15,25 +17,16 @@ def get_sample_info(sample):
else:
sample[key] = ""

z_score_13 = sample.get("Zscore_13")
z_score_18 = sample.get("Zscore_18")
z_score_21 = sample.get("Zscore_21")
CNVSegment = sample.get("CNVSegment")
FF = sample.get("FF_Formatted")
FFX = sample.get("FFX")
FFY = sample.get("FFY")

return {
"sample_id": sample.get("_id"),
"FF": {"value": FF, "warn": _get_ff_warning(FF),},
"CNVSegment": {"value": CNVSegment, "warn": "default",},
"FFX": {"value": FFX, "warn": "default",},
"FFY": {"value": FFY, "warn": "default",},
"Zscore_13": {"value": z_score_13, "warn": _get_tris_warning(z_score_13, FF),},
"Zscore_18": {"value": z_score_18, "warn": _get_tris_warning(z_score_18, FF),},
"Zscore_21": {"value": z_score_21, "warn": _get_tris_warning(z_score_21, FF),},
"FF": {"value": sample.get("FF_Formatted"), "warn": sample_warnings.get("FF_Formatted")},
"CNVSegment": {"value": sample.get("CNVSegment"), "warn": "default",},
"FFX": {"value": sample.get("FFX"), "warn": "default",},
"FFY": {"value": sample.get("FFY"), "warn": "default",},
"Zscore_13": {"value": sample.get("Zscore_13"), "warn": sample_warnings.get("Zscore_13")},
"Zscore_18": {"value": sample.get("Zscore_18"), "warn": sample_warnings.get("Zscore_18")},
"Zscore_21": {"value": sample.get("Zscore_21"), "warn": sample_warnings.get("Zscore_21")},
"Zscore_X": {"value": sample.get("Zscore_X")},
#'Warning': 'value': _get_warnings(sample),
"Status": _get_status(sample),
"Include": sample.get("include"),
"Comment": sample.get("comment", ""),
Expand Down Expand Up @@ -66,10 +59,21 @@ def _get_ff_warning(fetal_fraction):
else:
return "default"

def get_sample_warnings(sample):
sample_warnings = {}
fetal_fraction = sample.get('FF_Formatted')
sample_warnings["FF_Formatted"] = _get_ff_warning(fetal_fraction)
for key in ["Zscore_13", "Zscore_18", "Zscore_21"]:
z_score = sample.get(key)
sample_warnings[key] = _get_tris_warning(z_score, fetal_fraction)

return sample_warnings



def _get_tris_warning(z_score: float, fetal_fraction):
"""Get automated trisomi warning, based on preset Zscore thresholds"""
if fetal_fraction is "":
if fetal_fraction is None or z_score is None:
return "default"

if fetal_fraction <= 5:
Expand All @@ -89,15 +93,35 @@ def _get_tris_warning(z_score: float, fetal_fraction):
return warn


def get_data_for_coverage_plot(samples):
def get_scatter_data_for_coverage_plot(samples):
"""Coverage Ratio data for Coverage Plot."""

data = {}
for sample in samples:
warnings = get_sample_warnings(sample)
warnings.pop('FF_Formatted')
if set(warnings.values()) == {'default'}:
continue
sample_id = sample["_id"]
data[sample_id] = []
for i in range(1, 23):
data[sample_id].append(sample.get(f"Chr{str(i)}_Ratio", 0))
data[sample_id] = {"x": [], "y": []}
for chr in range(1, 23):
ratio = sample.get(f"Chr{str(chr)}_Ratio")
if ratio is None:
continue
data[sample_id]["y"].append(ratio)
data[sample_id]["x"].append(chr)
return data

def get_box_data_for_coverage_plot(samples):
"""Coverage Ratio data for Coverage Plot."""
data = {}
for chr in range(1, 23):
data[chr] = []
for sample in samples:
ratio = sample.get(f"Chr{str(chr)}_Ratio")
if ratio is None:
continue
data[chr].append(ratio)
return data


Expand Down
8 changes: 5 additions & 3 deletions NIPTool/server/views.py
Expand Up @@ -163,13 +163,15 @@ def fetal_fraction(batch_id):
def coverage(batch_id):
"""Batch view with coverage plot"""
batch = app.adapter.batch(batch_id)
samples = app.adapter.batch_samples(batch_id)
data = get_data_for_coverage_plot(samples)
samples = list(app.adapter.batch_samples(batch_id))
scatter_data = get_scatter_data_for_coverage_plot(samples)
box_data = get_box_data_for_coverage_plot(samples)
return render_template(
"batch/coverage.html",
batch=batch,
x_axis=list(range(1, 23)),
data=data,
scatter_data=scatter_data,
box_data = box_data,
page_id="batches_cov",
)

Expand Down

0 comments on commit a7efddc

Please sign in to comment.