Skip to content

Commit

Permalink
Merge pull request #398 from pepkit/dev
Browse files Browse the repository at this point in the history
Release 0.14.1
  • Loading branch information
khoroshevskyi authored Oct 4, 2024
2 parents d59bd90 + 2307f39 commit 3a85a7f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pephub/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.14.0"
__version__ = "0.14.1"
12 changes: 6 additions & 6 deletions pephub/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@
]


SAMPLE_CONVERSION_FUNCTIONS = {
"json": lambda x: x.to_json(),
"csv": lambda x: x.to_csv(index=False),
"latex": lambda x: x.to_latex(),
"txt": lambda x: x.to_string(),
}
# SAMPLE_CONVERSION_FUNCTIONS = {
# "json": lambda x: x.to_json(),
# "csv": lambda x: x.to_csv(index=False),
# "latex": lambda x: x.to_latex(),
# "txt": lambda x: x.to_string(),
# }

VALID_UPDATE_KEYS = [
"name",
Expand Down
55 changes: 35 additions & 20 deletions pephub/routers/api/v1/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
from peppy.const import SAMPLE_DF_KEY, SAMPLE_RAW_DICT_KEY

from ....const import SAMPLE_CONVERSION_FUNCTIONS
# from ....const import SAMPLE_CONVERSION_FUNCTIONS
from ....dependencies import (
DEFAULT_TAG,
get_config,
Expand Down Expand Up @@ -224,7 +224,7 @@ async def delete_a_pep(
)


@project.get("/samples", response_model=SamplesResponseModel)
@project.get("/samples", response_model=Union[SamplesResponseModel, str, list, dict])
async def get_pep_samples(
proj: dict = Depends(get_project),
format: Optional[str] = None,
Expand All @@ -239,29 +239,44 @@ async def get_pep_samples(
project: example
namespace: databio
To convert project use format parameter. Available formats are: basic, csv, yaml, json
"""
if format is not None:
conversion_func: Callable = SAMPLE_CONVERSION_FUNCTIONS.get(format, None)
if conversion_func is not None:
return PlainTextResponse(content=conversion_func(proj[SAMPLE_DF_KEY]))
else:

AVALIABLE_FORMATS = ["basic", "csv", "yaml", "json"]

if format:

if format not in AVALIABLE_FORMATS:
raise HTTPException(
status_code=400,
detail=f"Invalid format '{format}'. Valid formats are: {list(SAMPLE_CONVERSION_FUNCTIONS.keys())}",
detail=f"Invalid format '{format}'. Valid formats are: {AVALIABLE_FORMATS}",
)
else:
if raw:
df = pd.DataFrame(proj[SAMPLE_RAW_DICT_KEY])
return SamplesResponseModel(
count=df.shape[0],
items=df.replace({np.nan: None}).to_dict(orient="records"),
)
else:

if isinstance(proj, dict):
proj = peppy.Project.from_dict(proj)
return SamplesResponseModel(
count=len(proj.samples),
items=[s.to_dict() for s in proj.samples],
)

if format == "json":
return {
"samples": [sample.to_dict() for sample in proj.samples],
}
elif format == "csv":
return eido.convert_project(proj, "csv")["samples"]
elif format == "yaml":
return eido.convert_project(proj, "yaml-samples")["samples"]
elif format == "basic":
return eido.convert_project(proj, "basic")

if raw:
df = pd.DataFrame(proj[SAMPLE_RAW_DICT_KEY])
return SamplesResponseModel(
count=df.shape[0],
items=df.replace({np.nan: None}).to_dict(orient="records"),
)
if isinstance(proj, dict):
proj = peppy.Project.from_dict(proj)
return [sample.to_dict() for sample in proj.samples]


@project.get("/config", summary="Get project configuration file")
Expand Down
4 changes: 2 additions & 2 deletions requirements/requirements-all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ fastapi>=0.108.0
psycopg>=3.1.15
pepdbagent>=0.11.1
# pepdbagent @ git+https://github.com/pepkit/pepdbagent.git@schams2.0#egg=pepdbagent
peppy>=0.40.6
eido>=0.2.3
peppy>=0.40.7
eido>=0.2.4
jinja2>=3.1.2
python-multipart>=0.0.5
uvicorn
Expand Down

0 comments on commit 3a85a7f

Please sign in to comment.