def read_case(case_id: str, db: MongoAdapter = Depends(database)): """Return a specific case given petname ID""" case = db.case({"case_id": case_id}) if not case: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Case {case_id} not found") return JSONResponse(jsonable_encoder(Case(**case)), status_code=status.HTTP_200_OK)
def build_case_object( adapter: MongoAdapter, case_id: str, profile_path: Union[Path, str], vcf_path: Union[Path, str], vcf_sv_path: Union[Path, str] = None, ) -> Case: """Build case document and insert into the database, return resulting document""" # Parse MAF profiles from profile files and save in the case object profiles: dict = get_profiles(adapter=adapter, vcf_file=profile_path) # Check if profiles have any duplicates in the database check_profile_duplicates(adapter=adapter, profiles=profiles) # CHeck that SNV file has GQ field check_vcf_gq_field(vcf_path=vcf_path) # CHeck that SNV file doesnt have SV variants check_snv_variant_types(vcf_path=vcf_path) individuals = { sample: Individual( ind_id=sample, case_id=case_id, ind_index=sample_index, profile=profile, ) for sample_index, (sample, profile) in enumerate(profiles.items()) } individuals_list: List = list(individuals.values()) case_object = Case( case_id=case_id, profile_path=profile_path, vcf_path=vcf_path, vcf_sv_path=vcf_sv_path, nr_sv_variants=0, nr_variants=get_vcf_variant_count(vcf_path=vcf_path), individuals=individuals_list, inds=individuals, id=case_id, ) if vcf_sv_path: case_object.nr_sv_variants = get_vcf_variant_count(vcf_path=vcf_sv_path) case_object.sv_individuals = individuals_list case_object.sv_inds = individuals adapter.add_case(case_object.dict(by_alias=True, exclude={"id"})) return Case(**adapter.case({"case_id": case_id}))
def delete_case(case_id: str, db: MongoAdapter = Depends(database)): """Delete a specific case given petname ID""" existing_case = db.case({"case_id": case_id}) if not existing_case: return JSONResponse(f"Case {case_id} does not exist", status_code=status.HTTP_404_NOT_FOUND) try: delete(adapter=db, case_obj=existing_case, genome_build=settings.genome_build) return JSONResponse(f"Case {case_id} had been deleted", status_code=status.HTTP_200_OK) except Exception as e: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail= f"Error {e.__class__.__name__}: {e}; Case may be partially deleted", )
def load_case( case_id: str, snv_file: str, profile_file: str, sv_file: Optional[str] = None, db: MongoAdapter = Depends(database), ): """Upload a case to loqusdb""" if db.case({"case_id": case_id}): return JSONResponse(f"Case {case_id} already exists", status_code=status.HTTP_409_CONFLICT) if ((sv_file and not Path(sv_file).exists()) or not Path(snv_file).exists() or not Path(profile_file).exists()): raise HTTPException( detail="Input file path does not exist", status_code=status.HTTP_406_NOT_ACCEPTABLE, ) try: case_object: Case = build_case_object( case_id=case_id, vcf_path=snv_file, vcf_sv_path=sv_file, profile_path=profile_file, adapter=db, ) insert_case_variants(adapter=db, case_obj=case_object) return JSONResponse(jsonable_encoder(case_object), status_code=status.HTTP_200_OK) except LoqusdbAPIError as e: LOG.error(e) raise HTTPException( detail=f"Exception {e.__class__.__name__}: {e.message}", status_code=status.HTTP_400_BAD_REQUEST, ) except Exception as e: LOG.error(e) raise HTTPException( detail=f"Exception {e.__class__.__name__} {e}", status_code=status.HTTP_400_BAD_REQUEST, )