diff --git a/grnndata/GRNAnnData.py b/grnndata/GRNAnnData.py index f8740a1..0efe184 100644 --- a/grnndata/GRNAnnData.py +++ b/grnndata/GRNAnnData.py @@ -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") @@ -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=[ @@ -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"]) diff --git a/grnndata/utils.py b/grnndata/utils.py index e1a5088..54efb9c 100644 --- a/grnndata/utils.py +++ b/grnndata/utils.py @@ -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):