Skip to content

Commit 448627b

Browse files
committed
feat(dev): added ty.toml file for type hints
Signed-off-by: Josh Loecker <joshloecker@icloud.com>
1 parent 8d505f7 commit 448627b

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

main/como/proteomics_gen.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@ def process_proteomics_data(path: Path) -> pd.DataFrame:
2929
"""
3030
# Preprocess data, drop na, duplicate ';' in symbol,
3131
matrix: pd.DataFrame = pd.read_csv(path)
32-
if "gene_symbol" not in matrix.columns:
32+
gene_symbol_colname = [col for col in matrix.columns if "symbol" in col]
33+
if len(gene_symbol_colname) == 0:
3334
_log_and_raise_error(
3435
"No gene_symbol column found in proteomics data.",
3536
error=ValueError,
3637
level=LogLevel.ERROR,
3738
)
38-
39+
if len(gene_symbol_colname) > 1:
40+
_log_and_raise_error(
41+
"Multiple gene_symbol columns found in proteomics data.",
42+
error=ValueError,
43+
level=LogLevel.ERROR,
44+
)
45+
symbol_col = gene_symbol_colname[0]
46+
matrix = matrix.rename(columns={symbol_col: "gene_symbol"})
3947
matrix["gene_symbol"] = matrix["gene_symbol"].astype(str)
4048
matrix.dropna(subset=["gene_symbol"], inplace=True)
41-
matrix = matrix.assign(gene_symbol=matrix["gene_symbol"].str.split(";")).explode("gene_symbol")
49+
matrix["gene_symbol"] = matrix["gene_symbol"].str.split(":")
50+
matrix = matrix.explode(column=["gene_symbol"])
4251
return matrix
4352

4453

@@ -56,12 +65,23 @@ async def load_gene_symbol_map(gene_symbols: list[str], entrez_map: Path | None
5665
if entrez_map and entrez_map.exists():
5766
df = pd.read_csv(entrez_map, index_col="gene_symbol")
5867
else:
59-
biodbnet = BioDBNet()
60-
df = await biodbnet.async_db2db(
61-
values=gene_symbols,
62-
input_db=Input.GENE_SYMBOL,
63-
output_db=[Output.GENE_ID, Output.ENSEMBL_GENE_ID],
64-
)
68+
dataframes: list[pd.DataFrame] = []
69+
step_size: int = 300
70+
biodbnet = BioDBNet(cache=True)
71+
biodbnet.services.settings.TIMEOUT = 60
72+
for i in range(0, len(gene_symbols), step_size):
73+
# Operations: Goes from gene_symbols=["A", "B;C", "D"] -> gene.split=[["A"], ["B", "C"], ["D"]] -> chain.from_iterable["A", "B", "C", "D"]
74+
chunk: list[str] = list(itertools.chain.from_iterable(gene.split(";") for gene in gene_symbols[i : i + step_size]))
75+
dataframes.append(
76+
biodbnet.db2db(
77+
input_values=chunk,
78+
input_db=Input.GENE_SYMBOL.value,
79+
output_db=[Output.GENE_ID.value, Output.ENSEMBL_GENE_ID.value],
80+
taxon=9606,
81+
)
82+
)
83+
df = pd.concat(dataframes, axis="columns")
84+
print(df)
6585
df.loc[df["gene_id"].isna(), ["gene_id"]] = np.nan
6686
df.to_csv(entrez_map, index_label="gene_symbol")
6787

@@ -188,7 +208,7 @@ async def proteomics_gen(
188208
high_confidence_batch_ratio: float = 0.7,
189209
quantile: int = 25,
190210
log_level: LogLevel = LogLevel.INFO,
191-
log_location: str | TextIOWrapper = sys.stderr,
211+
log_location: str | TextIO = sys.stderr,
192212
):
193213
"""Generate proteomics data."""
194214
_set_up_logging(level=log_level, location=log_location)
@@ -229,13 +249,12 @@ async def proteomics_gen(
229249

230250
config_df = pd.read_excel(config_filepath, sheet_name=context_name)
231251
matrix: pd.DataFrame = process_proteomics_data(matrix_filepath)
232-
233252
groups = config_df["group"].unique().tolist()
234253

235254
for group in groups:
236255
indices = np.where([g == group for g in config_df["group"]])
237256
sample_columns = [*np.take(config_df["sample_name"].to_numpy(), indices).ravel().tolist(), "gene_symbol"]
238-
matrix = matrix.loc[:, sample_columns]
257+
matrix = cast(pd.DataFrame, matrix.loc[:, sample_columns])
239258

240259
symbols_to_gene_ids = await load_gene_symbol_map(
241260
gene_symbols=matrix["gene_symbol"].tolist(),

ty.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[environment]
2+
root = ["main"]
3+
4+
[rules]
5+
call-non-callable = "error"
6+
invalid-argument-type = "error"
7+
unresolved-reference = "error"
8+
unsupported-operator = "error"
9+
10+
ambiguous-protocol-member = "warn"
11+
deprecated = "warn"
12+
division-by-zero = "warn"
13+
possibly-missing-attribute = "warn"
14+
possibly-unresolved-reference = "warn"
15+
unsupported-base = "warn"
16+
unused-ignore-comment = "warn"

0 commit comments

Comments
 (0)