Skip to content

Commit

Permalink
Merge pull request #22 from Clinical-Genomics/tresholds
Browse files Browse the repository at this point in the history
inserting tresholds mm
  • Loading branch information
mayabrandi committed Nov 20, 2020
2 parents 6100f9a + aca092c commit cd2b8b4
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 142 deletions.
6 changes: 3 additions & 3 deletions NIPTool/server/templates/batch/header.html
Expand Up @@ -79,13 +79,13 @@ <h5>Batch Warnings </h5>
href="{{ url_for('server.batch', batch_id = batch._id) }}">NCV Table</a>
</li>
<li {% if page_id =='batches_NCV13' %} class="active" {% endif %}><a
href="{{ url_for('server.NCV13', batch_id = batch._id) }}">NCV13</a>
href="{{ url_for('server.NCV', batch_id = batch._id, ncv = '13') }}">NCV13</a>
</li>
<li {% if page_id =='batches_NCV18' %} class="active" {% endif %}><a
href="{{ url_for('server.NCV18', batch_id = batch._id) }}">NCV18</a>
href="{{ url_for('server.NCV', batch_id = batch._id, ncv = '18') }}">NCV18</a>
</li>
<li {% if page_id =='batches_NCV21' %} class="active" {% endif %}><a
href="{{ url_for('server.NCV21', batch_id = batch._id) }}">NCV21</a>
href="{{ url_for('server.NCV', batch_id = batch._id, ncv = '21') }}">NCV21</a>
</li>
<li {% if page_id =='batches_FF' %} class="active" {% endif %}><a
href="{{ url_for('server.fetal_fraction', batch_id = batch._id) }}">FF</a>
Expand Down
167 changes: 95 additions & 72 deletions NIPTool/server/templates/batch/plots/NCV.html
@@ -1,84 +1,107 @@
{% block scripts %}
<script>
{% for chrom, cases in ncv_chrom_data.items() %}
$(function chrom_plot() {
var data = []
var layout = {
legend:{hovermode:'closest' },
hovermode:'closest',
annotations : [],
xaxis :{showline: true,
ticktext : {{cases['names']|tojson}},
tickvals : {{cases['x_axis']}},
tickangle : 20,
linecolor: '#636363',
linewidth: 5,
showgrid: true,
gridcolor: '#bdbdbd'
},

yaxis :{range: [-10,],
zeroline: false,
showline: true,
showgrid: false,
linecolor: '#636363',
linewidth: 5,
title : 'NCV{{chrom}}'
}};
<script>
{% for chrom, cases in ncv_chrom_data.items() %}
$(function chrom_plot() {
var data = []
var layout = {
legend: {hovermode: 'closest'},
hovermode: 'closest',
annotations: [],
xaxis: {
showline: true,
ticktext: {{cases['names']|tojson}},
tickvals: {{cases['x_axis']}},
tickangle: 20,
linecolor: '#636363',
linewidth: 5,
showgrid: true,
gridcolor: '#bdbdbd'
},

var cases = {name: "Current batch (N ="+ "{{cases['count']}}" +")" ,
y: {{cases['values']}},
x: {{cases['x_axis']}},
text: {{cases['names']|tojson}},
mode: 'markers',
type: 'scatter',
yaxis: {
range: [-10,],
zeroline: false,
showline: true,
showgrid: false,
linecolor: '#636363',
linewidth: 5,
title: 'NCV{{chrom}}'
}
};
data.push(cases)

var box = {
y: {{ normal_data['values'] }},
pointpos: 30,
type: 'box',
marker: {
color: '#ccccb3'},
name: 'Negative (N='+{{normal_data['count']}}+')',
hoverinfo:'none'}
data.push(box)
var cases = {
name: "Current batch (N =" + "{{cases['count']}}" + ")",
y: {{cases['values']}},
x: {{cases['x_axis']}},
text: {{cases['names']|tojson}},
mode: 'markers',
type: 'scatter',
};
data.push(cases)

var box = {
y: {{ normal_data['values'] }},
pointpos: 30,
type: 'box',
marker: {
color: '#ccccb3'
},
name: 'Negative (N='+{{normal_data['count']}}+')',
hoverinfo: 'none'
}
data.push(box)


{% for line in tris_thresholds %}
var line = {
x: [{{cases['x_axis'][0]}}, {{cases['x_axis'][-1]}}],
y: [{{tris_thresholds[line]['NCV']}}, {{tris_thresholds[line]['NCV']}}],
mode: 'lines',
text: "{{tris_thresholds[line]['text']}}",
showlegend: false,
line: {
dash: "dot",
color: "{{tris_thresholds[line]["color"]}}",
width: 1
},
name: "{{line}}"
};
data.push(line);
{% endfor %}


{% for status in abnormal_data.keys() %}
var verif = {
name: "{{status}} T{{chrom}} (N="+{{abnormal_data[status]['count']}}+")",
y: {{abnormal_data[status]['values']}},
x: {{abnormal_data[status]['x_axis']}},
text: {{abnormal_data[status]['names']|tojson}},
mode: 'markers',
type: 'scatter'
};
data.push(verif);
{% endfor %}

{% for status in abnormal_data.keys() %}
var verif = {
name: "{{status}} T{{chrom}} (N="+{{abnormal_data[status]['count']}}+")",
y: {{abnormal_data[status]['values']}},
x: {{abnormal_data[status]['x_axis']}} ,
text: {{abnormal_data[status]['names']|tojson}},
mode: 'markers',
type: 'scatter'};
data.push(verif);
{% endfor %}


Plotly.newPlot('NCV{{chrom}}_plot', data, layout);
});
{% endfor %}
</script>
Plotly.newPlot('NCV{{chrom}}_plot', data, layout);
});
{% endfor %}
</script>


<script>
$(document).ready(function() {
var table = $('#batch_table').DataTable( {
lengthChange: false,
paging: false,
info: false,
searching: false
} );
} );
</script>
<script>
$(document).ready(function () {
var table = $('#batch_table').DataTable({
lengthChange: false,
paging: false,
info: false,
searching: false
});
});
</script>


<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
{% endblock %}
2 changes: 2 additions & 0 deletions NIPTool/server/templates/batch/report.html
Expand Up @@ -54,7 +54,9 @@ <h2 class="page-header"><strong>NIPT Results </strong><br><small>Batch:
</table>
</footer>
<footer>
{% if coverage=="1" %}
<div id="cov_plot" style="min-width:400px; height:800px; "></div>
{% endif %}
</footer>
<footer>
<div id="NCV13_plot" style="min-width:400px; height:800px; "></div>
Expand Down
56 changes: 28 additions & 28 deletions NIPTool/server/utils.py
Expand Up @@ -12,17 +12,17 @@ def get_sample_info(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)
if isinstance(val,(float,int)):
if isinstance(val, (float, int)):
sample[key] = round(val, 2)
else:
sample[key] = ""

return {
"sample_id": sample.get("_id"),
"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",},
"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")},
Expand All @@ -47,32 +47,31 @@ def _get_status(sample):

def _get_ff_warning(fetal_fraction):
"""Get fetal fraction warning based on preset treshold"""
if fetal_fraction is "":
if fetal_fraction == "":
return "default"

try:
fetal_fraction = fetal_fraction
except:
fetal_fraction = None
if fetal_fraction and fetal_fraction <= FF_TRESHOLD:
return "danger"
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

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 None or z_score is None:
return "default"

Expand Down Expand Up @@ -112,6 +111,7 @@ def get_scatter_data_for_coverage_plot(samples):
data[sample_id]["x"].append(chr)
return data


def get_box_data_for_coverage_plot(samples):
"""Coverage Ratio data for Coverage Plot."""
data = {}
Expand Down Expand Up @@ -202,8 +202,12 @@ def get_ff_cases(adapter, batch_id):
}
},
]
data = list(adapter.sample_aggregate(pipe))[0]
aggregation = list(adapter.sample_aggregate(pipe))

massaged_data = {}
if not aggregation:
return massaged_data
data = aggregation[0]
for i, sample_id in enumerate(data["names"]):
massaged_data[sample_id] = {
"FFY": data["FFY"][i],
Expand Down Expand Up @@ -345,14 +349,14 @@ def get_last_batches(adapter, nr: int) -> list:
batch_sort_aggregation = [{'$sort': {'SequencingDate': -1}}]
sorted_batches = list(adapter.batch_aggregate(batch_sort_aggregation))
if len(sorted_batches) > nr:
sorted_batches = sorted_batches[0:nr]
sorted_batches = sorted_batches[0:nr]

return(sorted_batches)
return (sorted_batches)


# Statistics

def get_statistics_for_scatter_plot(batches: list, fields: list)-> dict:
def get_statistics_for_scatter_plot(batches: list, fields: list) -> dict:
"""Formating data for scatter plot"""

scatter_plot_data = {}
Expand All @@ -371,24 +375,20 @@ def get_statistics_for_box_plot(adapter, batches: list, fields: list):

match = {'$match': {'SampleProject': {'$in': batches}}}
lookup = {'$lookup': {
'from': 'batch',
'localField': 'SampleProject',
'foreignField': '_id',
'as': 'batch'}}
'from': 'batch',
'localField': 'SampleProject',
'foreignField': '_id',
'as': 'batch'}}
unwind = {'$unwind': {'path': '$batch'}}
group = {'$group': {'_id': {
'batch': '$SampleProject',
'date': '$batch.SequencingDate'}}}
'batch': '$SampleProject',
'date': '$batch.SequencingDate'}}}

for field in fields:
group['$group'][field] = {'$push': f"${field}"}

pipe = [match, lookup, unwind, group]
#maybe add a fina sort to the pipe
# maybe add a fina sort to the pipe
box_plot_data = list(adapter.sample_aggregate(pipe))

return box_plot_data




return box_plot_data

0 comments on commit cd2b8b4

Please sign in to comment.