def get_ranks(line):
	##INPUT
	filename = line.strip().split()[0]
	chain_names = line.strip().split()[1].strip().split(':')

	##GET INTERFACE
	chain1, chain2 = first.get_residues(filename, chain_names) 
	interface_nodes_1, interface_nodes_2, edges = first.calc_interface(chain1, chain2)

	##GET NODES CONNECTED TO INTERFACE
	within1, within2 = first.calc_within(interface_nodes_1, interface_nodes_2, chain1, chain2)

	##ADD NODES AND EDGES (with weights) TO GRAPH
	G0 = first.make_graph(chain1, chain2, edges, within1, within2) #add nodes and edges to the graph

	##FIND NODE RANKS
	functions = [find_weights, find_changes, find_seq_changes,find_centrality]
	for i in range(num):
		if i>fixed:
			j = fixed
		else:
			j = i 
		G0 = find_ranks(interface_nodes_1+interface_nodes_2, G0, functions[j], names[i])
	maxrank = len(interface_nodes_1 + interface_nodes_2)
	return G0, maxrank, interface_nodes_1+interface_nodes_2
def get_changes(line):
	##INPUT
	filename = line.strip().split()[0]
	chain_names = line.strip().split()[1].strip().split(':')
	'''
	chain_choices = []
	with open(filename+'.pdb') as f:
		for line in f:
			line = line.strip().split()
			if line[0]=='COMPND' and line[2]=='CHAIN:':
				chain_choices.append(line[3][0])
	print "Choose 2 chains/sets of chains from the following: "
	print chain_choices
	chain_names = raw_input("Enter chain names separated by space: ")
	chain_names = chain_names.strip().split()
	'''

	##GET INTERFACE
	chain1, chain2 = first.get_residues(filename, chain_names) 
	interface_nodes_1, interface_nodes_2, edges = first.calc_interface(chain1, chain2)

	##GET NODES CONNECTED TO INTERFACE
	within1, within2 = first.calc_within(interface_nodes_1, interface_nodes_2, chain1, chain2)

	##ADD NODES AND EDGES (with weights) TO GRAPH
	G0 = first.make_graph(chain1, chain2, edges, within1, within2) #add nodes and edges to the graph

	##FIND NODE RANKS
	maxrank = len(interface_nodes_1)+len(interface_nodes_2)+1
	for node in nx.nodes(G0):
		G0.node[node]['rank']= maxrank #Initialise all ranks to worst. Non-interface residues retain this rank.
	G0 = find_node_ranks(interface_nodes_1+interface_nodes_2, G0) #find node rank by sorting node weights 
	''' For changing edge width based on weight.
	widths = np.array([edge[2]['weight'] for edge in edges]) 
	widths = widths / float(max(widths)) * 2
	'''
	ranks = nx.get_node_attributes(G0,"rank")
	ranks_keys = {y:x for x,y in ranks.iteritems()}

	##FIND SUM OF SQUARE CHANGES WHEN NODE OF RANK 'rank' IS REMOVED
	errors =[]
	for rank in range(1,maxrank):
		G1, interface_nodes_new = delete_node_by_rank(interface_nodes_1+interface_nodes_2, G0, rank)
		G1 = find_node_ranks(interface_nodes_new, G1)
		weights_original = nx.get_node_attributes(G0, "weight_sum")
		weights_new = nx.get_node_attributes(G1, "weight_sum")
		error =0.0
		for node in weights_new:
			error += np.square(weights_original[node] - weights_new[node])
		errors.append((ranks_keys[rank], round(error,2)))
	return errors