示例#1
0
def get_single_cif(request: Request,
                   entry_id: str,
                   params: SingleEntryQueryParams = Depends()):
    from optimade.adapters import Structure

    response = get_single_entry(
        collection=structures_coll,
        entry_id=entry_id,
        request=request,
        params=params,
        response=StructureResponseOne,
    )

    adapter = Structure(response.data.dict())
    filename = entry_id.replace("/", "_") + ".cif"
    headers = {
        "Content-Type": "text/plain",
        "Content-Disposition": f"attachment;filename={filename};",
    }
    cif_list = adapter.as_cif.split("\n")
    num_lines = len(cif_list)
    cif_generator = (f"{line}\n" if ind != num_lines - 1 else line
                     for ind, line in enumerate(cif_list))
    return StreamingResponse(
        cif_generator,
        media_type="text/plain",
        headers=headers,
    )
def get_single_structure(request: Request,
                         entry_id: str,
                         params: SingleEntryQueryParams = Depends()):
    return get_single_entry(
        collection=structures_coll,
        entry_id=entry_id,
        response=StructureResponseOne,
        request=request,
        params=params,
    )
示例#3
0
def get_single_reference(
    request: Request,
    entry_id: str,
    params: SingleEntryQueryParams = Depends()
) -> ReferenceResponseOne:
    return get_single_entry(
        collection=references_coll,
        entry_id=entry_id,
        response=ReferenceResponseOne,
        request=request,
        params=params,
    )
示例#4
0
def get_single_structure(request: Request,
                         entry_id: str,
                         params: SingleEntryQueryParams = Depends()):

    response = get_single_entry(
        collection=structures_coll,
        entry_id=entry_id,
        request=request,
        params=params,
        response=StructureResponseOne,
    )

    context = {"request": request, "entry_id": entry_id}

    if response.meta.data_returned < 1:
        return TEMPLATES.TemplateResponse("structure_not_found.html", context)

    stoichiometry = get_stoich_from_formula(
        response.data.attributes.chemical_formula_descriptive)
    for ind, (elem, num) in enumerate(stoichiometry):
        if num - int(num) > 1e-5:
            raise RuntimeError("Unable to cast formula to correct format")
        stoichiometry[ind][1] = int(num)

    context.update({
        "odbx_title": "odbx",
        "odbx_blurb": "the open database of xtals",
        "odbx_about":
        'odbx is a public database of crystal structures from the group of <a href="https://ajm143.github.io">Dr Andrew Morris</a> at the University of Birmingham.',
        "odbx_cif_string": optimade_to_basic_cif(response.data),
        "structure_info": dict(response),
        "cif_link": str(request.url).replace("structures/", "cif/"),
        "stoichiometry": stoichiometry,
    })

    return TEMPLATES.TemplateResponse("structure.html", context)