# Path: tools.py
# class Tracker(object):
# 	"""Tracks progress of task"""
# 	def __init__(self, name, size, currPercentage=0, count=0):
# 		self.name = name	#name of task
# 		self.size = size	#total size of task (e.g. number of lines in file)
# 		self.currPercentage = currPercentage	#current percentage of task complete
# 		self.count = count		#absolute amount of task complete (e.g. number of lines of file read)
# 	def increment(self):
# 		if self.size !=0 and self.size is not None:
# 			self.count += 1
# 			newPercentage = self.currPercentage + 1
# 			if float(self.count)/self.size >= float(newPercentage)/100:	#if at least X% of the file has been read, print percentage
# 				self.currPercentage = newPercentage
# 				print("{} {}% complete".format(self.name, self.currPercentage))	
# 
# Path: hic_oe.py
# def get_expected(mat):
# 	n = len(mat)
# 
# 	tots = np.zeros(n-1)
# 	counts = np.zeros(n-1)
# 
# 	for i in range(n):
# 		for j in range(i):
# 			observed = mat[i,j]
# 			if observed != 0:
# 				s = i - j
# 				tots[s - 1] += observed
# 				counts[s - 1] += 1
# 
# 	avgs = np.zeros(n-1)
# 	for i, count in enumerate(counts):
# 		if count != 0:
# 			avgs[i] = tots[i]/count
# 
# 	return avgs


import sys
import numpy as np
import array_tools as at
from tools import Tracker
from linear_algebra import *
from tad import *
from hic_oe import get_expected
	for structure in structures:
		new_points = np.zeros(structure.chrom.getLength(), dtype=object)
		for i, gen_coord in enumerate(consensus):
			abs_index = structure.chrom.getAbsoluteIndex(gen_coord)
			pos = structure.points[abs_index - structure.offset].pos
			new_points[abs_index - structure.offset] = Point(pos, structure.chrom, abs_index, i)
		structure.points = new_points

def transform(trueLow, highSubstructure, res_ratio):
	#approximate as low resolution
	inferredLow = highToLow(highSubstructure, res_ratio)

	scaling_factor = radius_of_gyration(trueLow)/radius_of_gyration(inferredLow)
	for i, point in enumerate(inferredLow.points):
		if point != 0:
			x, y, z = point.pos
			inferredLow.points[i].pos = (x*scaling_factor, y*scaling_factor, z*scaling_factor)
	
	#recover the transformation for inferred from true low structure
	r, t = getTransformation(inferredLow, trueLow)
	t /= scaling_factor

	#transform high structure
	highSubstructure.transform(r, t)

def distmat(path, structure, size=None, alpha=4, weight=0.05):
	contactMat = matFromBed(path, size, structure)

	assert len(structure.nonzero_abs_indices()) == len(contactMat)


# Path: tools.py
# class Tracker(object):
# 	"""Tracks progress of task"""
# 	def __init__(self, name, size, currPercentage=0, count=0):
# 		self.name = name	#name of task
# 		self.size = size	#total size of task (e.g. number of lines in file)
# 		self.currPercentage = currPercentage	#current percentage of task complete
# 		self.count = count		#absolute amount of task complete (e.g. number of lines of file read)
# 	def increment(self):
# 		if self.size !=0 and self.size is not None:
# 			self.count += 1
# 			newPercentage = self.currPercentage + 1
# 			if float(self.count)/self.size >= float(newPercentage)/100:	#if at least X% of the file has been read, print percentage
# 				self.currPercentage = newPercentage
# 				print("{} {}% complete".format(self.name, self.currPercentage))	
# 
# Path: hic_oe.py
# def get_expected(mat):
# 	n = len(mat)
# 
# 	tots = np.zeros(n-1)
# 	counts = np.zeros(n-1)
# 
# 	for i in range(n):
# 		for j in range(i):
# 			observed = mat[i,j]
# 			if observed != 0:
# 				s = i - j
# 				tots[s - 1] += observed
# 				counts[s - 1] += 1
# 
# 	avgs = np.zeros(n-1)
# 	for i, count in enumerate(counts):
# 		if count != 0:
# 			avgs[i] = tots[i]/count
# 
# 	return avgs


# Path: data_tools.py
import sys
import numpy as np
import array_tools as at
from tools import Tracker
from linear_algebra import *
from tad import *
from hic_oe import get_expected

	for structure in structures:
		new_points = np.zeros(structure.chrom.getLength(), dtype=object)
		for i, gen_coord in enumerate(consensus):
			abs_index = structure.chrom.getAbsoluteIndex(gen_coord)
			pos = structure.points[abs_index - structure.offset].pos
			new_points[abs_index - structure.offset] = Point(pos, structure.chrom, abs_index, i)
		structure.points = new_points

def transform(trueLow, highSubstructure, res_ratio):
	#approximate as low resolution
	inferredLow = highToLow(highSubstructure, res_ratio)

	scaling_factor = radius_of_gyration(trueLow)/radius_of_gyration(inferredLow)
	for i, point in enumerate(inferredLow.points):
		if point != 0:
			x, y, z = point.pos
			inferredLow.points[i].pos = (x*scaling_factor, y*scaling_factor, z*scaling_factor)
	
	#recover the transformation for inferred from true low structure
	r, t = getTransformation(inferredLow, trueLow)
	t /= scaling_factor

	#transform high structure
	highSubstructure.transform(r, t)

def distmat(path, structure, size=None, alpha=4, weight=0.05):
	contactMat = matFromBed(path, size, structure)

	assert len(structure.nonzero_abs_indices()) == len(contactMat)


	expected = get_expected(contactMat)