## network property ###
## output network properties ##
## number of nodes
## input network file should not have headers ##

import networkx as nx 
import numpy as np 
import pandas as pd 
import os

def NetPro(fn1, outName, path):
	f = pd.read_csv(fn1, header = None, sep = '\t')
	f = f.iloc[:, 0:3]
	f.columns = ['from', 'to', 'weight']

	col1 = f['from']
	col2 = f['to']

	node = col1.append(col2)
	node1 = node.drop_duplicates().tolist()

	print ("# of nodes: " + str(len(node1)))


	nodeID = range(1, len(node1)+1)
	nodelist = pd.DataFrame(columns=['ID', 'Label'])
	nodelist.iloc[:, 0] = nodeID
	nodelist.iloc[:, 1] = node1

	#### Rename the node to ID in the edge list ### 
	f.columns = ['Label', 'to', 'weight']

	f = pd.merge(f, nodelist, how='left', on = ['Label'])
	f = f.iloc[:, 1:4]
	f.columns = ['Label', 'weight', 'from']
	
	f = pd.merge(f, nodelist, how='left', on = ['Label'])
	f = f.iloc[:, 1:4]
	f.columns = ['weight', 'from', 'to']
	edgelist = f[['from', 'to', 'weight']]

	print ("# of edges: " + str(edgelist.shape[0]))

	density = edgelist.shape[0]*1.0/(len(node1)*(len(node1)-1)*0.5)

	print ("Density of the network: " + str(round(density, 5)))

	nodelist.to_csv(os.path.join(path, outName+"_nodelist.txt"), sep='\t', index=False, header = None)

	edgelist.to_csv(os.path.join(path, outName+"_edgelist.txt"), sep='\t', index=False, header = None)

def NetDeg(fn1, outName, path):
	net = nx.read_weighted_edgelist(fn1, delimiter = '\t')

	deg = list(net.degree)

	degree = []
	for i in range(len(deg)):
		degree.append(deg[i][1])

	outF = open(os.path.join(path, outName+"_degree.txt"), "w")
	outF.write(','.join(map(str, degree)))
	outF.close()

