def add_telomeres(blocks, spe1, spe2, ACS): markers_to_ch = {spe1: {}, spe2: {}} # map from marker to its chromosome for ch in blocks[spe1]: for m in ch: markers_to_ch[spe1][m._id] = ch #endif #endfor for ch in blocks[spe2]: for m in ch: markers_to_ch[spe2][m._id] = ch #endif #endfor telomere_rows = [] # syntenies with telomeres for row in ACS: s = row._set # row's set of 1's x = iter(s).next() # arbitrary marker from s ch1 = markers_to_ch[spe1][x] # marker's chromosome in spe1 ch2 = markers_to_ch[spe2][x] # marker's chromosome in spe2 if (ch1[0]._id in s or ch1[len(ch1) - 1]._id in s) and (ch2[0]._id in s or ch2[len(ch2) - 1]._id in s): telomere_rows.append( bm.Row(s, row._id + "T", 0, [spe1, spe2], True)) #endif #endfor for t in telomere_rows: ACS.add_row_info(t)
def make_PQCR_tree(matrix): # print "debug 1" # create overlap graphs og = c1p.create_overlap_graph(matrix) # overlap graph (as an array of spanning trees) # print "debug 2" pq = tree.PQRTree() # pqr tree # print "debug 3" S = matrix.get_support() # set of all columns (support of the matrix) # print "debug 4" # sort components of the overlap graph by size of support og.sort() # print len(og) # add head node og.append(c1p.OverlapComponent(bm.Row(S, 'head', 0, [], False))) # delete empty lines (will have to reverse) while len(og[0]._support) == 0: del og[0] #endwhile # delete overlap graphs with the same support i = 0 # component iterator while i < len(og) - 2: if og[i]._support == og[i + 1]._support: if len(og[i]._rows) == 0: del og[i] elif len(og[i + 1]._rows) == 0: del og[i + 1] else: i += 1 #endif else: i += 1 #endif #endwhile # make S sorted S = [x for x in S] S.sort() # create PQR tree pq._head = make_PQCR_tree_from_graph(S, og) # head of PQ-tree return pq
def make_PQR_tree_from_graph(support, og): # add leaves for x in support: og.insert(0, c1p.OverlapComponent(bm.Row(set([x]), 'leaf', 0, [], False))) #endfor # create PQR-tree nodes nodes = [tree.TreeNode(g._support) for g in og] # the nodes of the PQR-tree # for each overlap graph make a node in the PQR-tree for g in xrange(len(og)): head = og[g]._head # the head of the current overlap graph # if there is only one node in the tree the component is a P node otherwise: if len(og[g]._rows) > 0: # refine to determine order or R'ness is_R = False # True if node is an R-node p = part.Partition(head) # partition of the # current overlap graph for row in og[g]._rows: if not p.refine(row): is_R = True break #endif #endfor if is_R: nodes[g]._value = 'R' else: nodes[g]._value = 'Q' nodes[g]._child = p._part #endif else: # P node if len(og[g]._support) == 1: nodes[g]._value = og[g]._support.__iter__().next() else: nodes[g]._value = 'P' #endif #endif #endfor return make_PQR_tree_from_partitions(support, nodes, og)
def join_ACS(ACSs): ACS_sort = [] for acs in ACSs: for syn in acs: if syn._isX == True: # is bm.XRow: S = [x for x in syn._set] S.sort() val = copy.copy(S) val.append('X') X = [x for x in syn._Xs] X.sort() val += X val.append(syn._isT) val += syn._sp val.append(syn._weight) val.append(len(X)) val.append(len(S)) else: S = [x for x in syn._set] S.sort() val = copy.copy(S) val.append(syn._isT) val += syn._sp val.append(syn._weight) val.append(len(S)) #endif ACS_sort.append(val) #endfor del acs #endfor del ACSs ACS_sort.sort() joined_ACS = bm.BinaryMatrix() r = 0 while len(ACS_sort) > 0: i = 0 current_row = ACS_sort[i] if 'X' in current_row: isX = True a = current_row[-1] b = current_row[-2] row = bm.XRow(set(current_row[0:a]), set(current_row[a + 1:a + b + 1]), str(r), current_row[-3], set(current_row[a + b + 2:-3]), current_row[a + b + 1]) else: isX = False row = bm.Row(set(current_row[0:current_row[-1]]), str(r), current_row[-3], set(current_row[current_row[-1] + 1:-2]), current_row[current_row[-1]]) #endif while i < len(ACS_sort): if isX: if a == ACS_sort[i][-1] and b == ACS_sort[i][-2]: if current_row[0:a + b + 1] == ACS_sort[ i][0:a + b + 1] and current_row[a + b + 1] == ACS_sort[i][a + b + 1]: row._sp |= set(ACS_sort[i][a + b + 2:-3]) row._weight = max(current_row[-3], ACS_sort[i][-3]) else: break #endif else: break #endif else: if current_row[-1] == ACS_sort[i][-1]: if current_row[0:current_row[-1]] == ACS_sort[i][ 0:current_row[-1]] and current_row[current_row[ -1]] == ACS_sort[i][current_row[-1]]: row._sp |= set(ACS_sort[i][current_row[-1] + 1:-2]) row._weight = max(current_row[-2], ACS_sort[i][-2]) else: break #endif else: break #endif #endif i = i + 1 #endwhile for j in xrange(i): del ACS_sort[0] #endif row._sp = [x for x in row._sp] row._sp.sort() joined_ACS.add_row_info(row) r += 1 #endwhile return joined_ACS
def make_PQCR_tree_from_graph(support, og): # add leaves for x in support: og.insert(0, c1p.OverlapComponent(bm.Row(set([x]), 'leaf', 0, [], False))) #endfor # create PQR-tree nodes nodes = [tree.TreeNode(g._support) for g in og] # the nodes of the PQR-tree # for each overlap graph make a node in the PQR-tree for g in xrange(len(og)): head = og[g]._head # the head of the current overlap graph # if there is only one node in the tree the component is a P node otherwise: if len(og[g]._rows) > 0: # refine to determine order or R'ness is_R = False # True if node is an R-node p = part.Partition(head) # partition of the current overlap graph # print str(p) for row in og[g]._rows: if not p.refine(row): is_R = True break #endif #endfor if is_R: # check if node is circC1P for n in xrange(g+1, len(og) - 1): if og[g]._support < og[n]._support: if n != len(og) - 1: nodes[g]._value = 'R' #endif #endif #endfor if nodes[g]._value != 'R': m = bm.BinaryMatrix() for row in og[g]._all_rows: m.add_row_info(row) #endfor if cc1p.check_circC1P(m): p_comp = part.Partition(head) non_c1p = [] for row in og[g]._rows: if p_comp.test_refine(row._set) < 0: p_comp.refine(row) else: non_c1p.append(row) #endif #endif for row in non_c1p: p_comp.left_refine(row) p_comp.right_refine(row) if len(row._set - p_comp._support) > 0: p_comp.insert_after(tree.TreeNode(row._set - p_comp._support), p_comp._end) p_comp._support = p_comp._support | row._set #endif #endfor nodes[g]._value = 'C' nodes[g]._child = p_comp._part else: nodes[g]._value = 'R' #endfor else: nodes[g]._value = 'R' #endif else: nodes[g]._value = 'Q' nodes[g]._child = p._part #endif else: # P node if len(og[g]._support) == 1: nodes[g]._value = og[g]._support.__iter__().next() else: nodes[g]._value = 'P' #endif #endif #endfor return make_PQR_tree_from_partitions(support, nodes, og)
def make_PQR_tree(matrix): # seperate telomere rows telomere_rows = [] i = 0 remove_rows = [] while i < matrix._height: row = matrix.get_row_info(i) if row._isT: ad = True # True if we will add the telomere row # to the list of minimal telomere rows # ensure list of telomere rows is minimal for j in xrange(len(telomere_rows) - 1, -1, -1): tRow = telomere_rows[j] # current row in telomere list if tRow[0] < row._set: ad = False break #endif if row._set < tRow[0]: remove_rows.append(tRow[1]) del telomere_rows[j] #endif #endfor if ad: # add telomere to row telomere_rows.append([row._set, i]) row._set = row._set | set([c1p.Telomere()]) else: remove_rows.append(i) #endif #endif i += 1 #endfor remove_rows.sort() for i in xrange(len(remove_rows) - 1, -1, -1): matrix.remove_row(remove_rows[i]) #endfor # create overlap graphs og = c1p.create_overlap_graph(matrix) # overlap graph (as an array of spanning trees) pq = tree.PQRTree() # pqr tree S = matrix.get_support() # set of all columns (support of the matrix) # sort components of the overlap graph by size of support og.sort() # add head node og.append(c1p.OverlapComponent(bm.Row(S, 'head', 0, [], False))) # delete empty lines (will have to reverse) while len(og[0]._support) == 0: del og[0] #endwhile # delete overlap graphs with the same support i = 0 # component iterator while i < len(og) - 2: if og[i]._support == og[i + 1]._support: if len(og[i]._rows) == 0: del og[i] elif len(og[i + 1]._rows) == 0: del og[i + 1] else: i += 1 #endif else: i += 1 #endif #endwhile # make S sorted S = [x for x in S] S.sort() # create PQR tree pq._head = make_PQR_tree_from_graph(S, og) # head of PQ-tree return pq