Exemple #1
0
def interMDS(names, prefix, inter_res, intra_res, full, args):
	inter_res_string = tools.get_res_string(inter_res)
	intra_res_string = tools.get_res_string(intra_res)

	#get low-res structures from intra files
	low_structures = []
	for name in names:
		path = "{}_{}_{}.bed".format(prefix, name, intra_res_string)
		chrom = dt.chromFromBed(path)
		#reduce res
		chrom.res = inter_res
		chrom.minPos = int(np.floor(float(chrom.minPos)/chrom.res)) * chrom.res	#round
		chrom.maxPos = int(np.ceil(float(chrom.maxPos)/chrom.res)) * chrom.res
		low_structures.append(dt.structureFromBed(path, chrom))

	#for correct indexing
	n = len(names)
	offsets = np.zeros(n, dtype=int)
	for i in range(1, n):
		offsets[i] = offsets[i-1] + len(low_structures[i-1].getPoints())

	inter_mat = get_inter_mat(prefix, inter_res_string, intra_res_string, low_structures, offsets)

	#perform MDS at low resolution on all chroms
	infer_structures(inter_mat, low_structures, offsets, args[3], args[4])

	#perform MDS at high resolution on each chrom
	high_structures = []
	inferred_low_structures = []
	ts = []
	for true_low, name in zip(low_structures, names):
		path = "{}_{}_{}.bed".format(prefix, name, intra_res_string)
		if full:
			high_structure = mm.fullMDS(path, False, args[4], args[3])
		else:
			high_structure = mm.partitionedMDS(path, args)
		high_structures.append(high_structure)
		inferred_low = dt.highToLow(high_structure, true_low.chrom.res/high_structure.chrom.res)
		inferred_low_structures.append(inferred_low)

		#rescale
		rescaling_factor = la.radius_of_gyration(true_low)/la.radius_of_gyration(inferred_low)
		rescaled_coords = [rescaling_factor * coord for coord in inferred_low.getCoords()]
		for i, point in enumerate(inferred_low.getPoints()):
			point.pos = rescaled_coords[i]

		r, t = la.getTransformation(inferred_low, true_low)
		high_structure.transform(r, None)	#do not translate now (need to rescale)
		ts.append(t)	

	#translate (with rescaling)
	low_rgs = np.array([la.radius_of_gyration(structure) for structure in low_structures])
	high_rgs = np.array([la.radius_of_gyration(structure) for structure in high_structures])
	scaling_factor = np.mean(high_rgs/low_rgs)
	for high_structure, t in zip(high_structures, ts):
		high_structure.transform(None, scaling_factor*t)	#rescale translation

	return high_structures
Exemple #2
0
def interMDS(names, inter_prefix, intra_prefix, inter_res, intra_res, intra_low_res, args):
	inter_res_string = tools.get_res_string(inter_res)
	intra_res_string = tools.get_res_string(intra_res)
	if intra_low_res is None:
		intra_low_res_string = None
	else:
		intra_low_res_string = tools.get_res_string(intra_low_res)

	#get low-res clusters from intra files
	low_clusters = [dt.clusterFromBed("{}_{}_{}.bed".format(intra_prefix, name, inter_res_string), None, None) for name in names]

	#for correct indexing
	n = len(names)
	offsets = np.zeros(n, dtype=np.int32)
	for i in range(1, n):
		offsets[i] = offsets[i-1] + len(low_clusters[i-1].getPoints())

	inter_mat = get_inter_mat(intra_prefix, inter_prefix, inter_res, low_clusters, offsets)

	#perform MDS at low resolution on all chroms
	mm.infer_clusters(inter_mat, low_clusters, offsets, args[4])

	#perform MDS at high resolution on each chrom
	high_clusters = []
	inferred_low_clusters = []
	ts = []
	for true_low, name in zip(low_clusters, names):
		path = "{}_{}_{}.bed".format(intra_prefix, name, intra_res_string)
		if intra_low_res_string is None:
			high_cluster = mm.fullMDS(path, False, args[4])
		else:
			low_path = "{}_{}_{}.bed".format(intra_prefix, name, intra_low_res_string)
			high_cluster = mm.partitionedMDS(path, low_path, args)
		high_clusters.append(high_cluster)
		inferred_low = dt.highToLow(high_cluster, true_low.chrom.res/high_cluster.chrom.res)
		inferred_low_clusters.append(inferred_low)

		#rescale
		rescaling_factor = la.radius_of_gyration(true_low)/la.radius_of_gyration(inferred_low)
		rescaled_coords = [rescaling_factor * coord for coord in inferred_low.getCoords()]
		for i, point in enumerate(inferred_low.getPoints()):
			point.pos = rescaled_coords[i]

		r, t = la.getTransformation(inferred_low, true_low)
		high_cluster.transform(r, None)	#do not translate now (need to rescale)
		ts.append(t)	

	#translate (with rescaling)
	low_rgs = np.array([la.radius_of_gyration(cluster) for cluster in low_clusters])
	high_rgs = np.array([la.radius_of_gyration(cluster) for cluster in high_clusters])
	scaling_factor = np.mean(high_rgs/low_rgs)
	for high_cluster, t in zip(high_clusters, ts):
		high_cluster.transform(None, scaling_factor*t)	#rescale translation

	return high_clusters