-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(multithreading): isolate process data handling
Move data handling code from scripts.multithreading.process to scripts.data_handling.process. This allows for improved test coverage, by separating easy-to-test data handling from much harder-to-test concurrency.
- Loading branch information
1 parent
04648e7
commit 16d96e7
Showing
3 changed files
with
71 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Functions for handling data.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Data handling for *process* step.""" | ||
import logging | ||
from os import PathLike | ||
|
||
import pandas as pd | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def merge_data( | ||
gtex_path: PathLike, bm_path: PathLike, mane: pd.DataFrame | ||
) -> pd.DataFrame: | ||
"""Merge the data from previous pipeline queries. | ||
Parameters | ||
---------- | ||
gtex_path : PathLike | ||
Path to the file containing GTEx query data. | ||
bm_path : PathLike | ||
Path to the file containing BioMart query data. | ||
mane : pd.DataFrame | ||
A DataFrame containing MANE annotations | ||
Returns | ||
------- | ||
pd.DataFrame | ||
The merged DataFrame, containing the GTEx query, | ||
the BioMart query, and the MANE annotations. | ||
""" | ||
gtex = pd.read_csv(gtex_path, header=0, index_col=None) | ||
bm = pd.read_csv(bm_path, header=0, index_col=None) | ||
data = ( | ||
gtex.merge(bm, on=["geneSymbol", "gencodeId", "transcriptId"], how="outer") | ||
.merge( | ||
mane, | ||
on=["geneSymbol", "gencodeId", "transcriptId", "refseq"], | ||
how="left", | ||
) | ||
.sort_values(["median", "MANE_status"]) | ||
) | ||
return data | ||
|
||
|
||
def write_data(data: pd.DataFrame, writer: pd.ExcelWriter) -> None: | ||
"""Write a DataFrame to an Excel file. | ||
Note | ||
---- | ||
This function is best used within a ``with`` block, so that: | ||
#. The ``ExcelWriter`` is already open. | ||
#. It will be properly closed. | ||
Parameters | ||
---------- | ||
data : pd.DataFrame | ||
The DataFrame to be written. | ||
writer : pd.ExcelWriter | ||
An **open** pandas ExcelWriter. | ||
""" | ||
gene = data["geneSymbol"].unique()[0] | ||
data.to_excel(writer, index=False, sheet_name=gene) | ||
logger.info(f"{gene} add to output file.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters