-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.py
77 lines (66 loc) · 2.53 KB
/
io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pandas as pd
import numpy as np
import os
def parseGenes(
path: str | os.PathLike, genesOnRows: bool = False, delim: str = ","
) -> tuple[pd.DataFrame, pd.Series, pd.Series]:
"""
parse Wishbone flavored single cell expression matrix
@params path the path to the file.
@params genesOnRows if the genes are on the row indices.
@params delim the delimiter of the file.
@return a tuple of (expression, cell label, genes)
"""
df: pd.DataFrame = pd.read_csv(path, delimiter=delim)
df.rename_axis(columns=None, index=None, inplace=True)
if genesOnRows:
df = df.T
df, df.columns = df.iloc[1:], df.iloc[0] # row 0 as columns
else:
df, df.index = df.iloc[:, 1:], df.iloc[:, 0]
cells = df.index.to_series()
genes = df.columns.to_series()
return df, cells, genes
def parseCellSelect(path: str | os.PathLike) -> list[int]:
"""
parse in the TENET flavored cell select array.
@params `path` path to the cell select file.
@return boolean list of cell selection, 1 if selected.
"""
with open(path) as f:
return list(map(int, f))
def parseTrajectory(path: str | os.PathLike) -> list[float]:
"""
parse in the TENET flavored cell trajectory.
@params `path` path to the trajectory file.
@return list of cell trajectories
"""
with open(path) as f:
return list(map(float, f))
def writeTEmtx(path: str | os.PathLike, te: pd.DataFrame) -> None:
"""
Writes the TE matrix with the format alike TENET's output.
This is a wrapper to the pandas dataframe's `to_csv`.
@params `path` the output path.
@params `te` the TE matrix to write.
"""
with open(path, "w") as f:
f.write("TE")
te.to_csv(f, sep="\t")
def readTEmtx(path: str | os.PathLike) -> pd.DataFrame:
return pd.read_csv(path, sep="\t", index_col=0).rename_axis(index=None)
def locateJar(path: str|os.PathLike|None = None) -> str:
"""
locate the jar file
@params path: the path to the jar file or the directory containing the jar file.
@return the path to the jar file.
Default path is the submodule's jar.
"""
if not path:
return os.path.join(os.path.dirname(__file__), "TENET", "infodynamics.jar")
elif os.path.basename(path) == "infodynamics.jar" and os.path.exists(path):
return path
elif os.path.isdir(path) and os.path.exists(os.path.join(path, "infodynamics.jar")):
return os.path.join(path, "infodynamics.jar")
else:
raise FileNotFoundError(f"{path} not found")