
## Author Qi Li (qli8@nd.edu)  ###
## If use this framework, please cite the following papers ##
# “Margolin, Adam A., etal. “ARACNE: an algorithm for the reconstruction of gene regulatory networks in a mammalian cellular context.” BMC bioinformatics. Vol. 7. No. 1. 

#######################################################################################################
import numpy as np 
import pandas as pd 
import sys
import random
from scipy import stats as st 
import networkx as nx 
import os
import argparse
from os import path
import threading
from function import *
#######################################################################################################

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--output directory", dest = 'd', type = str)
parser.add_argument("-k", "--number of samples", dest = 'k', type = int) ## number of columns in gene expression data
parser.add_argument("-b0", "--start bootstraps ID", dest = 'b0', type = int)
parser.add_argument("-b1", "--end bootstraps ID", dest = 'b1', type = int)
parser.add_argument("-f", "--input file", dest = "f", type = str )
parser.add_argument("-t", "--manual threshold", dest = "t", type = float, default = 0.6)
args=parser.parse_args()

print (len(sys.argv))
if len(sys.argv) < 11:
	print ("ERROR MESSAGE: ")
	print ("comand should be: python absPCC.py -d <output directory> -k < number of columns you want to select each bootstrap[> -b0 <start bootstrap ID> -b1 <end bootstrap ID> -f <input gene expression file> -t <extra threshold>")
	print ("example would be: python absPCC.py -d out-absPCC -k 183 -b0 1 -b1 100 -f ./examples/expression-data/Hu_data_renamed_nocontrol.txt -t 0.6")
	sys.exit()

### create output directory  ###
dir_path = os.getcwd()
out_dir = dir_path + '/' + args.d
if not os.path.exists(out_dir):
	os.makedirs(out_dir)

#######################################################################################################

## read in expression data
df = pd.read_csv(args.f, sep = "\t")
print (df.shape)

#######################################################################################################
######## calculate the thresholds #########
###########################################

print ("Threshold process start")


## check if file exists:
if path.exists("./" + args.d +"/threshold_0.txt"):
	f = open("./" + args.d +"/threshold_0.txt", "r")
	thre = f.readline()
	thre = float(thre)
	print (thre)
else:
	## one random case 
	df_rand = Rand_EXP(df, axis=0)
	edgelist_rand = absPCC(df_rand, 0, args.d, "threshold")
	thre = threshold(0, args.d, "threshold")

	print (thre)

print ("Threshold process finished")
#######################################################################################################
########       100 bootstrap      #########
###########################################

for x in range(args.b0, args.b1):
	print ("bootstraping start")
	df_boot = Bootstrap(df, args.k, x)
	print ("bootstraping data ready: " + str(x))

	absPCC(df_boot, x, args.d, "bootstrapNetwork")
	print ("all pair calculation finished")

	if args.t == 0:
		edgelist = threshold_cut(x, args.d, "bootstrapNetwork", thre)
		print (edgelist.shape)
	else:
		## add a manual step of threshold ##
		edgelist = threshold_cut(x, args.d, "bootstrapNetwork", args.t)
		print (edgelist.shape)

	edgelist_DPI = DPI(edgelist)
	edgelist_DPI.to_csv("./" + args.d + "/bootstrapNetwork_"+str(x)+".txt", index = False, sep = "\t")







