Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
anilthanki committed Nov 3, 2023
1 parent 02bb8ad commit 9efe1e4
Showing 1 changed file with 16 additions and 47 deletions.
63 changes: 16 additions & 47 deletions tools/tertiary-analysis/decoupler/decoupler_pathway_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
)

Expand Down Expand Up @@ -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
Expand All @@ -109,34 +81,31 @@

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]
mlm_key = args.mlm_key

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:
Expand Down

0 comments on commit 9efe1e4

Please sign in to comment.