Skip to content

Commit

Permalink
Fixed an issue with getconsensusnetwork.py when input adjmat path arg…
Browse files Browse the repository at this point in the history
…ument is a single directory (e.g. outputsjaracne_foo_out_/); use built-in Python path-joining functionality
  • Loading branch information
khughitt committed Feb 15, 2019
1 parent ae82bf3 commit 1a921b5
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions SJARACNe/get_consensus_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
mu = 0
sigma = 0

# Command-line arguments
adjmat_dir = sys.argv[1]
p_thresh_arg = sys.argv[2]
output_dir = sys.argv[3]

# Processing all bootstrap networks, summarizing them into corresponding variables
for adj_file in os.listdir(sys.argv[1]):
for adj_file in os.listdir(adjmat_dir):
total_edge.append(0)
with open(sys.argv[1] + adj_file, "r") as f: # Opening each bootstrap file
with open(adjmat_dir + adj_file, "r") as f: # Opening each bootstrap file
for line in f:
if line[0] == ">" and run_num == 0:
parameters += line
Expand Down Expand Up @@ -65,26 +70,24 @@
run_num += 1 # Increment the bootstrap file index

# Network name
path_tokens = sys.argv[1].split("/")
name = path_tokens[len(path_tokens) - 2]
path = "/".join(path_tokens[0 : len(path_tokens) - 2]) + "/"
name = os.path.basename(os.path.dirname(adjmat_dir))

# Archiving the bootstrap networks
current_path = os.getcwd()
os.chdir(path)

# Parent directory of adjacency matrix output
path = os.path.dirname(os.path.dirname(adjmat_dir))

# If we aren't already in the parent directory of the adjacency matrix output dir, change to it
if path is not "":
os.chdir(path)
with closing(tarfile.open(name + ".tar.gz", "w:gz")) as tar:
for f in os.listdir(name):
tar.add(name + "/" + f)
os.chdir(current_path)
shutil.rmtree(sys.argv[1])
for f in os.listdir(name):
tar.add(os.path.join(name, f))

# Writing out the summary of all bootstrap files into bootstrap_info.txt file
info_file = open(
sys.argv[3] + "bootstrap_info_.txt"
if sys.argv[3].endswith("/")
else sys.argv[3] + "/bootstrap_info_.txt",
"w",
)
info_file = open( os.path.join(output_dir, "bootstrap_info_.txt"), "w")

info_file.write("Total edge tested: " + str(len(total_support)) + "\n")
info_file.write(
"Bonferroni corrected (0.05) alpha: " + str(0.05 / len(total_support)) + "\n"
Expand All @@ -102,36 +105,19 @@

# Setting p_threshold if given to the given value, if not to Bonferroni corrected value
p_threshold = 0.05 / len(total_support)
if sys.argv[2] != None:
p_threshold = float(sys.argv[2])
if p_thresh_arg != None:
p_threshold = float(p_thresh_arg)

# Writing out the parameters that the bootstrap networks are constructed with plus other parameters that is used to create consensus network
parameter_file = open(
sys.argv[3] + "parameter_info_.txt"
if sys.argv[3].endswith("/")
else sys.argv[3] + "/parameter_info_.txt",
"w",
)
parameter_file = open(os.path.join(output_dir, "parameter_info_.txt"), "w")
parameters += "> Bootstrap No: " + str(run_num) + "\n"
parameters += "> Source: sjaracne2\n"
parameters += (
"> Output network: "
+ (
sys.argv[3] + "consensus_network_3col_.txt"
if sys.argv[3].endswith("/")
else sys.argv[3] + "/consensus_network_3col_.txt"
)
+ "\n"
)
parameters += "> Output network: " + os.path.join(output_dir, "consensus_network_3col_.txt") + "\n"
parameter_file.write(parameters)

# Writing out the consensus network preserving edges with statistically significant support
consensus_network = open(
sys.argv[3] + "consensus_network_3col_.txt"
if sys.argv[3].endswith("/")
else sys.argv[3] + "/consensus_network_3col_.txt",
"w",
)
consensus_network = open(os.path.join(output_dir, "consensus_network_3col_.txt"), "w")

header += "source\ttarget\tMI\n"
consensus_network.write(header)
current_gene = "none"
Expand Down

0 comments on commit 1a921b5

Please sign in to comment.