Skip to content

Commit

Permalink
Handle metadata rows in crosswalk tables
Browse files Browse the repository at this point in the history
  • Loading branch information
axdanbol committed Feb 6, 2024
1 parent 9b236f0 commit cad838c
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion containers/crosswalking/context/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
import csv
import re
from pathlib import Path
import typing as t

import anndata
import pandas as pd
Expand Down Expand Up @@ -124,6 +126,22 @@ def _get_empty_table(args: argparse.Namespace) -> pd.DataFrame:
)


def _read_table(path: str) -> t.Optional[pd.DataFrame]:
"""Read a crosswalking table. Metadata rows before the header are skipped.
Args:
path (str): Path to the csv file
Returns:
pd.DataFrame: A data frame with the table data
"""
with open(path) as file:
for row in csv.reader(file):
if row[0].lower() == 'organ_level':
return pd.read_csv(file, names=row)
return None


def main(args: argparse.Namespace):
"""Crosswalks a h5ad file and saves the result to another h5ad file.
Expand Down Expand Up @@ -155,7 +173,7 @@ def _get_arg_parser() -> argparse.ArgumentParser:
parser.add_argument("matrix", type=anndata.read_h5ad, help="h5ad data file")
parser.add_argument(
"--crosswalk-table",
type=pd.read_csv,
type=_read_table,
help="crosswalking csv file path",
)
parser.add_argument(
Expand Down

0 comments on commit cad838c

Please sign in to comment.