From 8daa138bd5e69a369dd207ea263441561081c116 Mon Sep 17 00:00:00 2001 From: yhoogstrate Date: Wed, 8 Feb 2017 14:12:39 +0100 Subject: [PATCH] Permanent removal of unused Circos code --- drdisco/CircosController.py | 133 --------------------------------- drdisco/IntronDecomposition.py | 57 ++------------ 2 files changed, 5 insertions(+), 185 deletions(-) delete mode 100644 drdisco/CircosController.py diff --git a/drdisco/CircosController.py b/drdisco/CircosController.py deleted file mode 100644 index 4a8f2d5f..00000000 --- a/drdisco/CircosController.py +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/env python -# *- coding: utf-8 -*- -# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 textwidth=79: - -from subprocess import Popen, PIPE -import os - - -class CircosController: - circos_chr_name = 'hs' # make this argumentable - smoothing_offset = 15000 - smoothing_prec = 3 - - def __init__(self, sid, data, main_config_file, coordinate_config_file, data_file): - self.sid = sid - self.data = data - # self.main_config_file = main_config_file - self.coordinate_config_file = coordinate_config_file - self.data_file = data_file - - def draw_network(self, png_file, svg_file): - self.write_configs() - - def write_configs(self): - # Chooses the chromosomes - coordinates = self.estimate_coordinates() - self.write_coordinate_config(coordinates) # "tmp/select-coordinates.conf") - - self.write_data() - - self.run() - - def write_coordinate_config(self, coordinates): - """Generates a config file similar to this: -chromosomes_display_default = no -chromosomes = hs21[a]:0-39.7;hs21[b]:39.7-39.9;hs21[c]:39.9-42.7;hs21[d]:42.7-42.9;hs21[e]:42.9-50 -#chromosomes_reverse = /hs[]/ -#chromosomes_scale = hs1=0.5r,/hs[234]/=1.0rn -chromosomes_scale = a:1.0;b:25.0;c:1.0;d:25.0;e:1.0 -chromosomes_radius = a=0.4r, b=0.99r, c=0.4r, d=0.99r, e=0.4r - """ - - scales = {'small': '1.0', 'large': '35.0'} - radius = {'small': '0.55r', 'large': '0.99r'} - - fh = open(self.coordinate_config_file, "w") - fh.write("chromosomes_display_default = no\n") - fh.write("chromosomes = " + ";".join([c[0] + "[" + c[4] + "]:" + (("%." + str(self.smoothing_prec) + "f") % c[1]) + "-" + (("%." + str(self.smoothing_prec) + "f") % c[2]) for c in coordinates]) + "\n") - fh.write("chromosome_scale = " + ";".join([str(c[4]) + ":" + scales[c[3]] for c in coordinates]) + "\n") - fh.write("chromosome_radius = " + ",".join([str(c[4]) + "=" + radius[c[3]] for c in coordinates]) + "\n") - - fh.close() - - def estimate_coordinates(self): - """Returns chunks with a certain offset (usually 10kb) - """ - - idx = {} - for dp in self.data: - for i in [0, 1]: - _chr = dp[i]._target.position._chr.replace('chr', self.circos_chr_name) - if _chr not in idx: - idx[_chr] = {} - - idx[_chr][dp[i]._target.position.pos] = True - - vec = [] - for _chr in idx.keys(): - previous = None - vec_large = [] - - for pos in sorted(idx[_chr].keys()): - chunk = [pos - self.smoothing_offset, pos + self.smoothing_offset] - chunk[0] = float(chunk[0]) / float(pow(10, 6)) # a million -> Mb - chunk[1] = float(chunk[1]) / float(pow(10, 6)) # a million -> Mb - chunk = [round(chunk[0], self.smoothing_prec), round(chunk[1], self.smoothing_prec)] - - # Look what to do with previous chunk, if there is any - if previous is not None: - # if overlap, extend previous chunk - if chunk[0] <= previous[1]: - chunk[0] = previous[0] - else: - vec_large.append((_chr, max(0.0, previous[0]), previous[1], 'large')) - - previous = chunk - - vec_large.append((_chr, max(0.0, previous[0]), previous[1], 'large')) - - i = 0 - if vec_large[0][0] > 0.0: - vec.append((_chr, 0.0, vec_large[0][1], 'small', _chr + "_" + str(i))) - i += 1 - - for k in xrange(len(vec_large) - 1): - vec.append((_chr, vec_large[k][1], vec_large[k][2], vec_large[k][3], _chr + "_" + str(i))) - i += 1 - vec.append((_chr, vec_large[k][2], vec_large[k + 1][1], 'small', _chr + "_" + str(i))) - i += 1 - - vec.append((_chr, vec_large[k + 1][1], vec_large[k + 1][2], vec_large[k + 1][3], _chr + "_" + str(i))) - vec.append((_chr, vec_large[k + 1][2], 1000.0, 'small', _chr + "_" + str(i + 1))) - - return vec - - def write_data(self): - k = 1 - fh = open(self.data_file, "w") - for dp in self.data: - fh.write("fusion_event_" + str(self.sid) + "_dp_" + str(k) + " " + dp[0]._origin.position._chr.replace('chr', 'hs') + " " + str(dp[0]._origin.position.pos) + " " + str(dp[0]._origin.position.pos + 1) + "\n") - fh.write("fusion_event_" + str(self.sid) + "_dp_" + str(k) + " " + dp[0]._target.position._chr.replace('chr', 'hs') + " " + str(dp[0]._target.position.pos) + " " + str(dp[0]._target.position.pos + 1) + "\n") - fh.write("\n") - k += 1 - - fh.close() - - def run(self): - # 1. remove existing circos.png and circos.svg - for _file in ["circos.png", "circos.svg"]: - if os.path.exists(_file): - os.remove(_file) - - # 2. - p = Popen([os.getenv("CIRCOS_DIR") + "/bin/circos", "-conf", "share/circos/circos.conf", "-debug_group", "summary, timer"], stdin=PIPE, stdout=PIPE, stderr=PIPE) - output, err = p.communicate() - # rc = p.returncode - - # 3. move circos.png and circos.svg into tmp/fusion_event_$id/circos.png etc - if not os.path.exists("tmp/circos"): - os.mkdir("tmp/circos") - - for _file in ["circos.png", "circos.svg"]: - os.rename(_file, "tmp/circos/fusion_event_" + str(self.sid) + "_" + _file) diff --git a/drdisco/IntronDecomposition.py b/drdisco/IntronDecomposition.py index 83f11acd..119784a8 100644 --- a/drdisco/IntronDecomposition.py +++ b/drdisco/IntronDecomposition.py @@ -18,7 +18,6 @@ import HTSeq from .CigarAlignment import cigar_to_cigartuple -# from .CircosController import CircosController from fuma.Fusion import STRAND_FORWARD, STRAND_REVERSE, STRAND_UNDETERMINED strand_tt = {STRAND_FORWARD: '+', STRAND_REVERSE: '-', STRAND_UNDETERMINED: '?'} @@ -228,15 +227,12 @@ def remove_edge(self, edge): except: del(self.edges[edge._target.position._hash]) - def __iter__(self): - for k in sorted(self.edges): - yield self.edges[k] - def __str__(self): # pragma: no cover out = str(self.position) a = 0 - for sedge in self.edges: + for k in sorted(self.edges): + sedge = self.edges[k] edge = self.edges[sedge] filtered_edges = {JunctionTypeUtils.str(x): edge._types[x] for x in sorted(edge._types)} # if x not in ['cigar_soft_clip','cigar_hard_clip'] @@ -330,9 +326,9 @@ def str(enumcode): if value == enumcode: return key - @staticmethod - def get_score(junction_type): - return JunctionTypeUtils.scoring_table[junction_type] +# @staticmethod +# def get_score(junction_type): +# return JunctionTypeUtils.scoring_table[junction_type] @staticmethod def is_fusion_junction(junction_type): @@ -542,42 +538,6 @@ def insert_edge(self, pos1, pos2, _type, cigarstrs): if node1 == edge._origin: # Avoid double insertion of all keys :) only do it if the positions don't get swapped edge.add_alignment_key(cigarstrs) - def insert_splice_edge(self, pos1, pos2, _type, cigarstrs): - """ - Checks if Node exists at pos1, otherwise creates one - - Checks if Node exists at pos2, otherwise creates one - - Checks if Edge exists between them, otherwise inserts it into the Nodes - - cigarstrs must be something like ("126M","126M") or ("25S50M2000N","25M50S") - """ - self.create_node(pos1) - self.create_node(pos2) - - node1 = self.get_node_reference(pos1) - node2 = self.get_node_reference(pos2) - - if cigarstrs is not None: - # Hexadec saves more mem - short_pos1 = "%0.2X" % pos1.pos # str(pos1.pos) - short_pos2 = "%0.2X" % pos2.pos # str(pos2.pos) - - cigarstrs = short_pos1 + strand_tt[pos1.strand] + cigarstrs[0] + "|" + short_pos2 + strand_tt[pos2.strand] + cigarstrs[1] - - edge1 = node1.get_edge_to_node(node2) - - if edge1 is None: - edge1 = Edge(node1, node2) - edge2 = Edge(node2, node1) - - node1.insert_edge(edge1) - node2.insert_edge(edge2) - else: - edge2 = node2.get_edge_to_node(node1) - - edge1.add_type(_type, 1) - edge2.add_type(_type, 1) - # if node1 == edge._origin: # Avoid double insertion of all keys :) only do it if the positions don't get swapped - # edge.add_alignment_key(cigarstrs) - def reinsert_edges(self, edges): """Only works for Edges of which the _origin and _target Node still exists @@ -1415,13 +1375,6 @@ def decompose(self, MIN_SCORE_FOR_EXTRACTING_SUBGRAPHS): subnet.classify_intronic_exonic(splice_junctions) del(splice_junctions) - # If circos: - # s = 1 - # for subnet in self.results: - # c = CircosController(str(s), subnet, "tmp /circos.conf","tmp /select-coordinates.conf", "tmp /circos-data.txt") - # c.draw_network("tmp /test.png","tmp /test.svg") - # s += 1 - return len(self.results) def export(self, fh):