Skip to content

Commit 581acef

Browse files
authored
Merge branch 'devel' into master
2 parents 9102a6d + bc5db61 commit 581acef

4 files changed

Lines changed: 77 additions & 6 deletions

File tree

netZooPy/command_line.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def panda(expression, motif, ppi, output, computing='cpu', precision='double',wi
158158
help='If true, the final PANDA is saved as an adjacency matrix. Works only when save_memory is false')
159159
@click.option('--old_compatible', is_flag=True, show_default=True,
160160
help='If true, PANDA is saved without headers. Pass this if you want the same results of netzoopy before v0.9.11')
161-
def lioness(expression, motif, ppi, output_panda, output_lioness, el, fmt, computing, precision, ncores, save_memory, save_tmp, rm_missing, mode_process,output_type, alpha, panda_start, panda_end, start, end, subset_numbers='', subset_names='',with_header=False, save_single_lioness=False,ignore_final=False, as_adjacency=False, old_compatible=False):
161+
def lioness(expression, motif, ppi, output_panda, output_lioness, el, fmt, computing, precision, ncores, save_tmp, rm_missing, mode_process,output_type, alpha, panda_start, panda_end, start, end, subset_numbers='', subset_names='',with_header=False, save_single_lioness=False,ignore_final=False, as_adjacency=False, old_compatible=False):
162162
"""Run Lioness to extract single-sample networks.
163163
First runs panda using expression, motif and ppi data.
164164
Then runs lioness and puts results in the output_lioness folder.

netZooPy/panda/panda.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ def processData(
539539
commind1 = ~np.isnan(idx_tfs) & ~np.isnan(idx_genes)
540540
idx_tfs = [i for (i, v) in zip(idx_tfs, commind1) if v]
541541
idx_genes = [i for (i, v) in zip(idx_genes, commind1) if v]
542+
if (len(idx_genes) == 0) or (len(idx_tfs) == 0):
543+
raise Exception('Error when creating the motif network!'
544+
' Typically this exception is raised if your'
545+
' expression matrix genes and motif priors'
546+
' not have any intersection.')
542547
idx = np.ravel_multi_index(
543548
(idx_tfs, idx_genes), self.motif_matrix_unnormalized.shape
544549
)

tests/test_lioness.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,16 @@ def test_lioness():
9494
assert result.returncode == 0
9595

9696
# 1. Test command line
97-
#positional: expression, motif, ppi, output_panda, output_lioness, el, fmt, computing, precision, ncores, save_memory, save_tmp, rm_missing, mode_process,output_type, alpha, panda_start, panda_end, start, end, subset_numbers, subset_names,with_header, save_single_lioness):
98-
# expression, motif, ppi, output_panda, output_lioness, el, fmt, computing, precision, ncores, save_memory, save_tmp, rm_missing, mode_process,output_type, alpha, panda_start, panda_end, start, end, subset_numbers='', subset_names='',with_header=False, save_single_lioness=False,ignore_final=False, as_adjacency=False, old_compatible=False):
99-
100-
cmd.lioness.callback(expression_data, motif, ppi, 'panda.txt','lioness_output_cmd',None,'npy','cpu','double',1,False,True,rm_missing,'legacy','network',0.1,1,4,1,None,save_single_lioness=True)
97+
#positional: expression, motif, ppi, output_panda, output_lioness, el, fmt, computing, precision, ncores, save_tmp, rm_missing, mode_process,output_type, alpha, panda_start, panda_end, start, end, subset_numbers, subset_names,with_header, save_single_lioness):
98+
# expression, motif, ppi, output_panda, output_lioness, el, fmt, computing, precision, ncores, save_tmp,
99+
# rm_missing, mode_process,output_type, alpha, panda_start, panda_end, start, end, subset_numbers='',
100+
# subset_names='',with_header=False, save_single_lioness=False,ignore_final=False, as_adjacency=False,
101+
# old_compatible=False):
102+
103+
cmd.lioness.callback(expression_data, motif, ppi, 'panda.txt','lioness_output_cmd',None,'npy','cpu','double',1,True,rm_missing,'legacy','network',0.1,1,4,1,None,save_single_lioness=True)
101104
# Now lioness is saved with lioness.sample_name.sample_index
102105
res = np.load("lioness_output/lioness.1.0.npy")
103-
gt = res = np.load("lioness_output_cmd/lioness.1.0.npy")
106+
gt = np.load("lioness_output_cmd/lioness.1.0.npy")
104107
assert np.allclose(gt, res)
105108

106109
# 2. Testing Lioness with motif set to None to compute Lioness on coexpression networks

tests/test_panda.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,66 @@ def test_panda():
343343
assert(np.allclose(W_gt, W_res, rtol=1e-05, atol=1e-08))
344344

345345

346+
def test_incorrect_gene_ids_raises_exception():
347+
'''
348+
This test checks that an appropriately descriptive exception
349+
is raised if one attempts to run PANDA on an expression matrix
350+
that does not have genes intersecting with those in the motif
351+
prior matrix
352+
'''
353+
354+
# these numbers are arbitrary but don't need to be large
355+
# to trigger the exception since it would be raised prior
356+
# to any Panda iterations.
357+
num_genes = 20 # number of genes in the expression mtx
358+
num_samples = 30 # number of samples in the expression mtx
359+
num_tfs = 10 # number of transcription factors in the motif prior
360+
num_motif_genes = 10 # number of genes in the motif prior
361+
362+
# create a dataframe of mock expression data
363+
exp_mtx_genes = [f'g{_}' for _ in range(num_genes)]
364+
exp_mtx_samples = [f's{_}' for _ in range(num_samples)]
365+
expression_data = pd.DataFrame(
366+
np.random.randint(0, 100, size=(num_genes, num_samples)),
367+
index=exp_mtx_genes,
368+
columns=exp_mtx_samples
369+
)
370+
371+
# mock motif prior. Note that the genes (2nd col) start with 'G'
372+
# instead of 'g' in the expression matrix
373+
tf_list = [f'tf{_}' for _ in range(num_tfs)]
374+
motif_gene_list = [f'G{_}' for _ in range(num_motif_genes)]
375+
motif_data = pd.DataFrame(
376+
{
377+
0: np.repeat(tf_list, num_motif_genes),
378+
1: np.tile(motif_gene_list, num_tfs),
379+
2: np.random.random(num_tfs*num_motif_genes)
380+
}
381+
)
382+
383+
# mock ppi prior
384+
ppi_data = pd.DataFrame(
385+
{
386+
0: np.repeat(tf_list, num_tfs),
387+
1: np.tile(tf_list, num_tfs),
388+
2: np.random.random(num_tfs**2)
389+
}
390+
)
391+
392+
with pytest.raises(Exception,
393+
match='Error when creating the motif network!.*'):
394+
panda_obj1 = Panda(
395+
expression_data,
396+
motif_data,
397+
ppi_data,
398+
modeProcess='legacy'
399+
)
400+
401+
with pytest.raises(Exception,
402+
match='Error when creating the motif network!.*'):
403+
panda_obj2 = Panda(
404+
expression_data,
405+
motif_data,
406+
ppi_data,
407+
modeProcess='intersection'
408+
)

0 commit comments

Comments
 (0)