Skip to content

Commit

Permalink
working on improving grnndata
Browse files Browse the repository at this point in the history
  • Loading branch information
jkobject committed Jan 11, 2024
1 parent a8957b1 commit 1059448
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
44 changes: 13 additions & 31 deletions grnndata/GRNAnnData.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, *args, grn, **kwargs):
super().__init__(*args, **kwargs)
self.varp["GRN"] = grn

## add concat
# add concat
def concat(self, other):
if not isinstance(other, GRNAnnData):
raise ValueError("Can only concatenate with another GRNAnnData object")
Expand All @@ -31,44 +31,22 @@ def concat(self, other):
grn=scipy.sparse.vstack([self.varp["GRN"], other.varp["GRN"]]),
)

## add slice
# add slice
def __getitem__(self, *args):
sub = super().__getitem__(*args)
if sub.shape[1] < self.shape[1]:
locs = np.where(self.var_names == sub.var_names)[0]
sub.varm["All_Targets"] = self.varp["GRN"][locs]
sub.varm["all_targets"] = self.varp["GRN"][
self.var_names.isin(sub.var_names)
]
return sub

# import pdb
# pdb.set_trace()
# if isinstance(name, str):
# index = self.var_names.tolist().index(name)
# return GRNAnnData(
# self[:, name],
# grn=self.varp["GRN"][index],
# )
# # need to put it in varm
# if isinstance(name, list):
# index = [self.var_names.tolist().index(i) for i in name]
# return GRNAnnData(grn=self.varp["GRN"][index], X=self.X[index])
# # need to put it in varm too
# if isinstance(name, np.ndarray):
# return GRNAnnData(grn=self.varp["GRN"][name], X=self.X[name])
# # need to put it in varm too
# if isinstance(name, slice):
# return GRNAnnData(
# grn=self.varp["GRN"][name, name],
# X=self.X[name],
# )
# # need to put it in varm too

@property
def grn(self):
return pd.DataFrame(
data=self.varp["GRN"], index=self.var_names, columns=self.var_names
)

## add return list of genes and corresponding weights
# add return list of genes and corresponding weights
def extract_links(
adata, # AnnData object
columns=[
Expand All @@ -95,8 +73,12 @@ def extract_links(
columns=columns,
).sort_values(by=columns[2], ascending=False)

@classmethod
def read(cls, *args, **kwargs):
return from_anndata(AnnData.read(*args, **kwargs))


def from_anndata(adata):
if "GRN" not in adata.obsp:
raise ValueError("GRN not found in adata.obsp")
return GRNAnnData(adata, grn=adata.obsp["GRN"])
if "GRN" not in adata.varp:
raise ValueError("GRN not found in adata.varp")
return GRNAnnData(adata, grn=adata.varp["GRN"])
2 changes: 1 addition & 1 deletion grnndata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def fileToList(filename, strconv=lambda x: x):
return [strconv(val[:-1]) for val in f.readlines()]


TF = fileToList("./TF.txt")
file_dir = os.path.dirname(os.path.realpath(__file__))
TF = fileToList(file_dir + "/TF.txt")


def get_centrality(grn, top_k=30):
Expand Down

0 comments on commit 1059448

Please sign in to comment.