diff --git a/netZooPy/condor/condor.py b/netZooPy/condor/condor.py index bc5e95c5..92f7d9a9 100644 --- a/netZooPy/condor/condor.py +++ b/netZooPy/condor/condor.py @@ -304,9 +304,8 @@ def matrices(self, c,resolution): # Computes weighted biadjacency matrix. A = np.matrix(np.zeros((p, q))) - for edge in self.net.iterrows(): - row = edge[1] - A[gn[row.iloc[1]], rg[row.iloc[0]]] = row.iloc[2] + for edge in self.net.itertuples(index=False): + A[gn[edge[1]], rg[edge[0]]] = edge[2] # Computes node degrees for the nodesets. ki = A.sum(1) diff --git a/netZooPy/panda/__init__.py b/netZooPy/panda/__init__.py index 4c994d56..d093c4d7 100755 --- a/netZooPy/panda/__init__.py +++ b/netZooPy/panda/__init__.py @@ -3,4 +3,4 @@ from .panda import Panda from .analyze_panda import AnalyzePanda from .calculations import compute_panda -from .io import load_motif, load_expression +from .io import load_motif, load_expression, load_ppi diff --git a/netZooPy/panda/io.py b/netZooPy/panda/io.py index c399fa1c..da3bdcb9 100644 --- a/netZooPy/panda/io.py +++ b/netZooPy/panda/io.py @@ -74,4 +74,38 @@ def load_expression(expression_file, with_header = False, start = 1, end = None) return(expression_data, expression_genes, expression_samples) -#def load_ppi(ppi_file): +def load_ppi(ppi_file): + """Read PPI data from a file path or DataFrame. + + Parameters + ---------- + ppi_file : str or pd.DataFrame or None + Path to file containing the PPI data or a pandas DataFrame. + The PPI can be symmetrical; if not, it will be transformed into + a symmetrical adjacency matrix. + If None, returns (None, []). + + Returns + ------- + ppi_data : pd.DataFrame or None + PPI data as a DataFrame. + ppi_tfs : list + Sorted list of unique TFs in the PPI. + """ + if type(ppi_file) is str: + ppi_data = pd.read_csv(ppi_file, sep="\t", header=None) + ppi_tfs = sorted( + set(pd.concat([ppi_data[0], ppi_data[1]])) + ) + elif type(ppi_file) is not str: + if ppi_file is not None: + if not isinstance(ppi_file, pd.DataFrame): + raise Exception("Please provide a pandas dataframe for PPI data.") + ppi_data = ppi_file + ppi_tfs = sorted( + set(pd.concat([ppi_data[0], ppi_data[1]])) + ) + else: + ppi_data = None + ppi_tfs = [] + return (ppi_data, ppi_tfs) diff --git a/netZooPy/panda/panda.py b/netZooPy/panda/panda.py index bfc65c5c..6d947c7e 100755 --- a/netZooPy/panda/panda.py +++ b/netZooPy/panda/panda.py @@ -356,31 +356,17 @@ def processData( ) ### Loading the PPI - # #TODO: move this to io - if isinstance(ppi_file, (str, os.PathLike)): - with Timer("Loading PPI data ..."): - self.ppi_data = pd.read_csv(ppi_file, sep="\t", header=None) - self.ppi_tfs = sorted( - set(pd.concat([self.ppi_data[0], self.ppi_data[1]])) - ) - print("Number of PPIs:", self.ppi_data.shape[0]) - else: - if ppi_file is not None: - if not isinstance(ppi_file, pd.DataFrame): - raise Exception("Please provide a pandas dataframe for PPI data.") - self.ppi_data = ppi_file # pd.read_csv(ppi_file, sep='\t', header=None) - self.ppi_tfs = sorted( - set(pd.concat([self.ppi_data[0], self.ppi_data[1]])) - ) - print("Number of PPIs:", self.ppi_data.shape[0]) - else: - # TODO: marouen, here we do not have an identiy matrix - print( - "No PPI data given: ppi matrix will be an identity matrix of size", - len(self.motif_tfs), - ) - self.ppi_data = None - self.ppi_tfs = self.motif_tfs + with Timer("Loading PPI data ..."): + self.ppi_data, self.ppi_tfs = io.load_ppi(ppi_file) + + if self.ppi_data is not None: + print("Number of PPIs:", self.ppi_data.shape[0]) + elif ppi_file is None: + print( + "No PPI data given: ppi matrix will be an identity matrix of size", + len(self.motif_tfs), + ) + self.ppi_tfs = self.motif_tfs ### Data combination