Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/resdk/tables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def _get_relations(self) -> pd.DataFrame:
for partition in relation.partitions:
value = ""
if partition["label"] and partition["position"]:
value = f'{partition["label"]} / {partition["position"]}'
value = f"{partition['label']} / {partition['position']}"
elif partition["label"]:
value = partition["label"]
elif partition["position"]:
Expand All @@ -371,6 +371,19 @@ def _get_orange_object(self) -> Data:
)

def _get_orange_data(self) -> pd.DataFrame:
def map_and_filter_samples(
df: pd.DataFrame, column_name: str, mapping: dict
) -> pd.DataFrame:
"""Map a predefined column name to a column named ``sample_id``, drop the original
and return the constructed DataFrame.
Omit samples for which mapping is not defined. These samples do not
have any data objects with the defined process type.
"""
df = df[df[column_name].isin(mapping.keys())]
df["sample_id"] = df[column_name].map(mapping)
df = df.drop(columns=[column_name])
return df

try:
orange_meta = self._get_orange_object()
except LookupError:
Expand Down Expand Up @@ -402,29 +415,32 @@ def _get_orange_data(self) -> pd.DataFrame:
df = df.rename(columns={"mS#Sample ID": "sample_id"})
elif "Sample slug" in df.columns:
mapping = {s.slug: s.id for s in self._samples}
df["sample_id"] = [mapping[value] for value in df["Sample slug"]]
df = df.drop(columns=["Sample slug"])
df = map_and_filter_samples(
df=df, column_name="Sample slug", mapping=mapping
)
elif "mS#Sample slug" in df.columns:
mapping = {s.slug: s.id for s in self._samples}
df["sample_id"] = [mapping[value] for value in df["mS#Sample slug"]]
df = df.drop(columns=["mS#Sample slug"])
df = map_and_filter_samples(
df=df, column_name="mS#Sample slug", mapping=mapping
)
elif "Sample name" in df.columns or "Sample name" in df.columns:
mapping = {s.name: s.id for s in self._samples}
if len(mapping) != len(self._samples):
raise ValueError(
"Duplicate sample names. Cannot map orange table data to other metadata"
)
df["sample_id"] = [mapping[value] for value in df["Sample name"]]
df = df.drop(columns=["Sample name"])
df = map_and_filter_samples(
df=df, column_name="Sample name", mapping=mapping
)
elif "mS#Sample name" in df.columns:
mapping = {s.name: s.id for s in self._samples}
if len(mapping) != len(self._samples):
raise ValueError(
"Duplicate sample names. Cannot map orange table data to other metadata"
)
df["sample_id"] = [mapping[value] for value in df["mS#Sample name"]]
df = df.drop(columns=["mS#Sample name"])

df = map_and_filter_samples(
df=df, column_name="mS#Sample name", mapping=mapping
)
return df.set_index("sample_id")

def _download_metadata(self) -> pd.DataFrame:
Expand Down
10 changes: 10 additions & 0 deletions test_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import resdk
from resdk.tables import QCTables

res = resdk.Resolwe(url="https://qa.genialis.io")
res.login()

collection = res.collection.get("validation-run-208")
qt = QCTables(collection=collection)

qt.meta
Loading