def coverage( request: Request, batch_id: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Batch view with coverage plot""" batch: Batch = find.batch(batch_id=batch_id, adapter=adapter) db_samples: List[DataBaseSample] = find.batch_samples(batch_id=batch_id, adapter=adapter) samples: List[Sample] = [ Sample(**db_sample.dict()) for db_sample in db_samples ] scatter_data: Dict[ str, CoveragePlotSampleData] = get_scatter_data_for_coverage_plot(samples) box_data: Dict[int, List[float]] = get_box_data_for_coverage_plot(samples) return templates.TemplateResponse( "batch/tabs/coverage.html", context=dict( request=request, current_user=user, batch=batch.dict(), x_axis=list(range(1, 23)), scatter_data=scatter_data, box_data=box_data, page_id="batches_cov", ), )
def Zscore( request: Request, batch_id: str, ncv: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Batch view with with Zscore plot""" batch: Batch = find.batch(batch_id=batch_id, adapter=adapter) return templates.TemplateResponse( "batch/tabs/Zscore.html", context=dict( request=request, tris_thresholds=TRISOMI_TRESHOLDS, batch=batch.dict(), chromosomes=[ncv], ncv_chrom_data={ ncv: get_tris_samples(adapter=adapter, chr=ncv, batch_id=batch_id) }, normal_data={ncv: get_tris_control_normal(adapter, ncv)}, abnormal_data={ncv: get_tris_control_abnormal(adapter, ncv, 0)}, page_id=f"batches_NCV{ncv}", current_user=user, ), )
def sample_tris( request: Request, sample_id: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Sample view with trisomi plot.""" sample: DataBaseSample = find.sample(sample_id=sample_id, adapter=adapter) batch: Batch = find.batch(batch_id=sample.batch_id, adapter=adapter) abnormal_data: Dict[str, ZscoreSamples] = get_abn_for_samp_tris_plot( adapter=adapter) normal_data: Zscore131821 = get_normal_for_samp_tris_plot(adapter=adapter) sample_data: ZscoreSamples = get_sample_for_samp_tris_plot(sample) return templates.TemplateResponse( "sample/sample_tris.html", context=dict( request=request, current_user=user, normal_data=normal_data.dict(exclude_none=True, by_alias=True), abnormal_data=abnormal_data, sample_data=sample_data, sample=Sample(**sample.dict()), batch=batch, status_colors=STATUS_COLORS, page_id="sample_tris", ), )
def fetal_fraction_XY( request: Request, batch_id: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Batch view with fetal fraction (X against Y) plot""" batch: Batch = find.batch(batch_id=batch_id, adapter=adapter) cases = get_fetal_fraction.samples(adapter=adapter, batch_id=batch_id) control: FetalFractionSamples = get_fetal_fraction.samples( batch_id=batch_id, adapter=adapter, control_samples=True) abnormal: FetalFractionControlAbNormal = get_fetal_fraction.control_abnormal( adapter) abnormal_dict = abnormal.dict( exclude_none=True, exclude={ "X0": {"status_data_"}, "XXX": {"status_data_"}, "XXY": {"status_data_"}, "XYY": {"status_data_"}, }, ) x_max = max(control.FFX + cases.FFX) + 1 x_min = min(control.FFX + cases.FFX) - 1 sex_thresholds = SexChromosomeThresholds(x_min=x_min, x_max=x_max) return templates.TemplateResponse( "batch/tabs/FF_XY.html", context=dict( sex_thresholds={ "XY_fetal_fraction_y": sex_thresholds.XY_fetal_fraction_y(), "XX_lower": sex_thresholds.XX_lower(), "XX_upper": sex_thresholds.XX_upper(), "XY_upper": sex_thresholds.XY_upper(), "XY_lower": sex_thresholds.XY_lower(), "XXY": sex_thresholds.XXY(), }, request=request, current_user=user, colors=COLORS, control=control, abnormal=abnormal_dict, cases=cases, max_x=x_max, min_x=x_min, batch=batch.dict(), page_id="batches_FF_XY", ), )
async def batch_comment( request: Request, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Update batch comment""" form = await request.form() if user.role not in ["RW", "admin"]: return RedirectResponse(request.headers.get("referer")) batch_id: str = form["batch_id"] batch: Batch = find.batch(batch_id=batch_id, adapter=adapter) comment: str = form.get("comment") if comment != batch.comment: batch.comment = comment update.update_batch(adapter=adapter, batch=batch) return RedirectResponse(request.headers.get("referer"))
def batch( request: Request, batch_id: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Batch view with table of all samples in the batch.""" samples: List[DataBaseSample] = find.batch_samples(batch_id=batch_id, adapter=adapter) return templates.TemplateResponse( "batch/tabs/table.html", context={ "request": request, "batch": find.batch(batch_id=batch_id, adapter=adapter), "sample_info": [Sample(**sample.dict()) for sample in samples], "page_id": "batches", "current_user": user, }, )
def batch( response: Response, batch_files: BatchRequestBody, adapter: StatinaAdapter = Depends(get_nipt_adapter), ): """Function to load batch data into the database with rest""" nipt_results = Path(batch_files.result_file) if not nipt_results.exists(): response.status_code = status.HTTP_422_UNPROCESSABLE_ENTITY return {"message": "Results file missing."} samples: List[DataBaseSample] = get_samples(nipt_results) batch: Batch = get_batch(nipt_results) if find.batch(adapter=adapter, batch_id=batch.batch_id): return "batch already in database" insert_batch(adapter=adapter, batch=batch, batch_files=batch_files) insert_samples(adapter=adapter, samples=samples, segmental_calls=batch_files.segmental_calls) response.status_code = status.HTTP_200_OK return {"message": f"Batch {batch.batch_id} inserted to the database"}
def batch_download( request: Request, batch_id: str, file_id: str, file_name: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """View for batch downloads""" batch: dict = find.batch(adapter=adapter, batch_id=batch_id).dict() file_path = batch.get(file_id) if not validate_file_path(file_path): return RedirectResponse(request.headers.get("referer")) path = Path(file_path) if path.is_dir(): file_obj = zip_dir(source_dir=file_path) return StreamingResponse(file_obj, media_type="application/text") return FileResponse(str(path.absolute()), media_type="application/octet-stream", filename=path.name)
def sample( request: Request, sample_id: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Post sample with id""" sample: DataBaseSample = find.sample(sample_id=sample_id, adapter=adapter) batch: Batch = find.batch(batch_id=sample.batch_id, adapter=adapter) return templates.TemplateResponse( "sample/sample.html", context=dict( request=request, current_user=user, chrom_abnorm=CHROM_ABNORM, sample=Sample(**sample.dict()), status_classes=STATUS_CLASSES, batch=batch, page_id="sample", ), )
def fetal_fraction( request: Request, batch_id: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Batch view with fetal fraction plot""" batch: Batch = find.batch(batch_id=batch_id, adapter=adapter) return templates.TemplateResponse( "batch/tabs/FF.html", context=dict( request=request, current_user=user, colors=COLORS, control=get_fetal_fraction.samples(adapter=adapter, batch_id=batch_id, control_samples=True), cases=get_fetal_fraction.samples(adapter=adapter, batch_id=batch_id), batch=batch.dict(), page_id="batches_FF", ), )
def report( request: Request, batch_id: str, file_name: str, adapter: StatinaAdapter = Depends(get_nipt_adapter), user: User = Depends(get_current_user), ): """Report view, collecting all tables and plots from one batch.""" samples: List[DataBaseSample] = find.batch_samples(batch_id=batch_id, adapter=adapter) batch: Batch = find.batch(batch_id=batch_id, adapter=adapter) cases = get_fetal_fraction.samples(adapter=adapter, batch_id=batch_id) control: FetalFractionSamples = get_fetal_fraction.samples( batch_id=batch_id, adapter=adapter, control_samples=True) abnormal: FetalFractionControlAbNormal = get_fetal_fraction.control_abnormal( adapter) abnormal_dict = abnormal.dict( exclude_none=True, exclude={ "X0": {"status_data_"}, "XXX": {"status_data_"}, "XXY": {"status_data_"}, "XYY": {"status_data_"}, }, ) x_max = max(control.FFX + cases.FFX) + 1 x_min = min(control.FFX + cases.FFX) - 1 sex_thresholds = SexChromosomeThresholds(x_min=x_min, x_max=x_max) template = templates.get_template("batch/report.html") output_from_parsed_template = template.render( # common request=request, current_user=user, batch=find.batch(batch_id=batch_id, adapter=adapter), # Fetal Fraction XY sex_thresholds={ "XY_fetal_fraction_y": sex_thresholds.XY_fetal_fraction_y(), "XX_lower": sex_thresholds.XX_lower(), "XX_upper": sex_thresholds.XX_upper(), "XY_upper": sex_thresholds.XY_upper(), "XY_lower": sex_thresholds.XY_lower(), "XXY": sex_thresholds.XXY(), }, control=control, abnormal=abnormal_dict, cases=cases, colors=COLORS, max_x=x_max, min_x=x_min, # table sample_info=[Sample(**sample.dict()) for sample in samples], ) in_mem_file = io.StringIO() in_mem_file.seek(0) in_mem_file.write(output_from_parsed_template) in_mem_file.seek(0) return StreamingResponse(in_mem_file, media_type="application/text")