From 9efe1e46cc2704a9b51d4cdf54214d2df2cc49f6 Mon Sep 17 00:00:00 2001 From: Anil Thanki Date: Fri, 3 Nov 2023 10:37:53 +0000 Subject: [PATCH] code refactoring --- .../decoupler/decoupler_pathway_inference.py | 63 +++++-------------- 1 file changed, 16 insertions(+), 47 deletions(-) diff --git a/tools/tertiary-analysis/decoupler/decoupler_pathway_inference.py b/tools/tertiary-analysis/decoupler/decoupler_pathway_inference.py index 7e24e502..5caca6f9 100644 --- a/tools/tertiary-analysis/decoupler/decoupler_pathway_inference.py +++ b/tools/tertiary-analysis/decoupler/decoupler_pathway_inference.py @@ -5,7 +5,6 @@ import decoupler as dc import pandas as pd import hdf5plugin -# import scanpy as sc # define arguments for the script parser = argparse.ArgumentParser() @@ -16,17 +15,10 @@ # add network input file option parser.add_argument("-n", "--input_network", help="Network input file", required=True) -# optional network formatted output file option +# output file prefix parser.add_argument( - "--output_network", - help="network formatted output files prefix", - default=None, -) - -# optional network formatted output file option -parser.add_argument( - "--output_anndata", - help="anndata formatted output files prefix", + "--output", + help="output files prefix", default=None, ) @@ -64,38 +56,18 @@ ) - args = parser.parse_args() -# read the input file -# check that either -o or -on is specified -if args.output_anndata is None and args.output_network is None: - raise ValueError("Please specify either -o or -on") +# check that either -o or --output is specified +if args.output is None: + raise ValueError("Please specify either -o or --output") # read in the AnnData input file adata = ad.read_h5ad(args.input_anndata) -# network = dc.load_network(args.input_network) +# read in the input file network input file network = pd.read_csv(args.input_network, sep='\t') -# d = dc.Decoupler(network) - -# pathway_activity = d.infer_pathway_activity(adata) - -# # Save the pathway activity to a file -# pathway_activity.to_csv(args.output_network, index=False) - -# # read in the network and check that source, target, and weight columns are present -# network = pd.read_csv(args.input_network, index_col=0) - -print(network.columns) - -print(args.source) - -print(args.target) - -print(args.weight) - if ( args.source not in network.columns or args.target not in network.columns @@ -109,18 +81,15 @@ dc.run_mlm( mat=adata, - net=network + net=network, + source=args.source, + target=args.target, + weight=args.weight, + verbose=True ) -# , -# source=args.source, -# target=args.target, -# weight=args.weight, -# verbose=True -# ) mlm_key = "mlm_estimate" if args.mlm_key is not None and args.mlm_p_key is not mlm_key: - print("here mlm_key") adata.obsm[args.mlm_key] = adata.obsm[mlm_key].copy() # delete adata.obsm[mlm_key] del adata.obsm[mlm_key] @@ -128,15 +97,15 @@ mlm_pvals_key = "mlm_pvals" if args.mlm_p_key is not None and args.mlm_p_key is not mlm_pvals_key: - print("here mlm_pvals_key") adata.obsm[args.mlm_p_key] = adata.obsm[mlm_pvals_key].copy() + # delete adata.obsm[mlm_pvals_key] del adata.obsm[mlm_pvals_key] mlm_pvals_key = args.mlm_p_key -if args.output_network is not None: +if args.output is not None: # write adata.obsm[ulm_key] and adata.obsm[ulm_pvals_key] to the output network files - adata.obsm[mlm_key].to_csv(args.output_network + "_mlm.tsv", sep="\t") - adata.obsm[mlm_pvals_key].to_csv(args.output_network + "_mlm_pvals.tsv", sep="\t") + adata.obsm[mlm_key].to_csv(args.output + "_mlm.tsv", sep="\t") + adata.obsm[mlm_pvals_key].to_csv(args.output + "_mlm_pvals.tsv", sep="\t") # if args.activities_path is specified, generate the activities AnnData and save the AnnData object to the specified path if args.activities_path is not None: