def findOverlapCoords(coordTree, coord_list, this_coord): this_tree = coordTree try: this_key = this_tree.getKey() except: return thisIsOverlap = False if coordsOverlap(this_coord[0], this_coord[1], this_key[0], this_key[1]): coord_list.append(this_tree.getKey()) thisIsOverlap = True if not this_tree.getIsLeaf(): if this_coord < this_key or thisIsOverlap: if this_tree.getLeft().isEmpty: return return findOverlapCoords(this_tree.getLeft(), coord_list, this_coord) if this_coord > this_key or thisIsOverlap: if this_tree.getRight().isEmpty: return return findOverlapCoords(this_tree.getRight(), coord_list, this_coord)
def hasOverlap(coordTree, this_coord): this_tree = coordTree try: # Make sure that the tree is nonEmpty tree_coord = this_tree.getKey() except: return False if coordsOverlap(this_coord[0], this_coord[1], tree_coord[0], tree_coord[1]): return True if not this_tree.getIsLeaf(): if this_coord < this_tree.getKey(): if this_tree.getLeft().isEmpty: return False return hasOverlap(this_tree.getLeft(), this_coord) if this_coord > this_tree.getKey(): if this_tree.getRight().isEmpty: return False return hasOverlap(this_tree.getRight(), this_coord) return False
def mergeRedundantEvents(as_type2chr2strand2redundantGroup2event): """ Because of the construction of the dictionary, some of the redundant groups are overlapping. """ for as_type in as_type2chr2strand2redundantGroup2event: for chr in as_type2chr2strand2redundantGroup2event[as_type]: for strand in as_type2chr2strand2redundantGroup2event[as_type][ chr]: for first_group in as_type2chr2strand2redundantGroup2event[ as_type][chr][strand]: for second_group in as_type2chr2strand2redundantGroup2event[ as_type][chr][strand]: if first_group == second_group: continue if coordsOverlap(first_group[2], first_group[3], second_group[2], second_group[3]): # Add sets together new_set = as_type2chr2strand2redundantGroup2event[ as_type][chr][strand][first_group].union( as_type2chr2strand2redundantGroup2event[ as_type][chr][strand][second_group]) new_start = min(first_group[2], second_group[2]) new_end = max(first_group[3], second_group[3]) new_region = (first_group[0], first_group[1], new_start, new_end) if new_region in as_type2chr2strand2redundantGroup2event[ as_type][chr][strand]: as_type2chr2strand2redundantGroup2event[ as_type][chr][strand][new_region].update( new_set) else: as_type2chr2strand2redundantGroup2event[ as_type][chr][strand][new_region] = new_set # Remove old sets and return with flag if new_region != first_group: del as_type2chr2strand2redundantGroup2event[ as_type][chr][strand][first_group] if new_region != second_group: del as_type2chr2strand2redundantGroup2event[ as_type][chr][strand][second_group] return True # If not returned from within the loop, then no change occurred return False
def updateRedundantDictionary(as_type2chr2strand2redundantGroup2event, as_type, redundantRegion, event): this_chr = redundantRegion[0] this_strand = redundantRegion[1] if as_type in as_type2chr2strand2redundantGroup2event: if not this_chr in as_type2chr2strand2redundantGroup2event[as_type]: as_type2chr2strand2redundantGroup2event[as_type][this_chr] = {this_strand: {redundantRegion: set([event])}} return if not this_strand in as_type2chr2strand2redundantGroup2event[as_type][this_chr]: as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand] = {redundantRegion: set([event])} return foundOverlap = False for redundantGroup in as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand]: if coordsOverlap(redundantGroup[2], redundantGroup[3], redundantRegion[2], redundantRegion[3]): foundOverlap = True # Pick longest region as the key region_start = min(redundantGroup[2], redundantRegion[2]) region_end = max(redundantGroup[3], redundantRegion[3]) # Copy the existing set of exon_names cur_set = set(as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand][redundantGroup]) # Add the current event cur_set.add(event) del as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand][redundantGroup] # Check for existing new group new_group = (redundantGroup[0], redundantGroup[1], region_start, region_end) if new_group in as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand]: as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand][new_group].update(cur_set) else: as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand][new_group] = cur_set break if not foundOverlap: # Add this non overlapping region on its own as_type2chr2strand2redundantGroup2event[as_type][this_chr][this_strand][redundantRegion] = set([event]) else: as_type2chr2strand2redundantGroup2event[as_type] = {this_chr: {this_strand: {redundantRegion: set([event])}}}
def mergeRedundantEvents(as_type2chr2strand2redundantGroup2event): """ Because of the construction of the dictionary, some of the redundant groups are overlapping. """ for as_type in as_type2chr2strand2redundantGroup2event: for chr in as_type2chr2strand2redundantGroup2event[as_type]: for strand in as_type2chr2strand2redundantGroup2event[as_type][chr]: for first_group in as_type2chr2strand2redundantGroup2event[as_type][chr][strand]: for second_group in as_type2chr2strand2redundantGroup2event[as_type][chr][strand]: if first_group == second_group: continue if coordsOverlap(first_group[2], first_group[3], second_group[2], second_group[3]): # Add sets together new_set = as_type2chr2strand2redundantGroup2event[as_type][chr][strand][first_group].union( as_type2chr2strand2redundantGroup2event[as_type][chr][strand][second_group] ) new_start = min(first_group[2], second_group[2]) new_end = max(first_group[3], second_group[3]) new_region = (first_group[0], first_group[1], new_start, new_end) if new_region in as_type2chr2strand2redundantGroup2event[as_type][chr][strand]: as_type2chr2strand2redundantGroup2event[as_type][chr][strand][new_region].update( new_set ) else: as_type2chr2strand2redundantGroup2event[as_type][chr][strand][new_region] = new_set # Remove old sets and return with flag if new_region != first_group: del as_type2chr2strand2redundantGroup2event[as_type][chr][strand][first_group] if new_region != second_group: del as_type2chr2strand2redundantGroup2event[as_type][chr][strand][second_group] return True # If not returned from within the loop, then no change occurred return False
def updateRedundantDictionary(as_type2chr2strand2redundantGroup2event, as_type, redundantRegion, event): this_chr = redundantRegion[0] this_strand = redundantRegion[1] if as_type in as_type2chr2strand2redundantGroup2event: if not this_chr in as_type2chr2strand2redundantGroup2event[as_type]: as_type2chr2strand2redundantGroup2event[as_type][this_chr] = { this_strand: { redundantRegion: set([event]) } } return if not this_strand in as_type2chr2strand2redundantGroup2event[as_type][ this_chr]: as_type2chr2strand2redundantGroup2event[as_type][this_chr][ this_strand] = { redundantRegion: set([event]) } return foundOverlap = False for redundantGroup in as_type2chr2strand2redundantGroup2event[as_type][ this_chr][this_strand]: if coordsOverlap(redundantGroup[2], redundantGroup[3], redundantRegion[2], redundantRegion[3]): foundOverlap = True # Pick longest region as the key region_start = min(redundantGroup[2], redundantRegion[2]) region_end = max(redundantGroup[3], redundantRegion[3]) # Copy the existing set of exon_names cur_set = set(as_type2chr2strand2redundantGroup2event[as_type] [this_chr][this_strand][redundantGroup]) # Add the current event cur_set.add(event) del as_type2chr2strand2redundantGroup2event[as_type][this_chr][ this_strand][redundantGroup] # Check for existing new group new_group = (redundantGroup[0], redundantGroup[1], region_start, region_end) if new_group in as_type2chr2strand2redundantGroup2event[ as_type][this_chr][this_strand]: as_type2chr2strand2redundantGroup2event[as_type][this_chr][ this_strand][new_group].update(cur_set) else: as_type2chr2strand2redundantGroup2event[as_type][this_chr][ this_strand][new_group] = cur_set break if not foundOverlap: # Add this non overlapping region on its own as_type2chr2strand2redundantGroup2event[as_type][this_chr][ this_strand][redundantRegion] = set([event]) else: as_type2chr2strand2redundantGroup2event[as_type] = { this_chr: { this_strand: { redundantRegion: set([event]) } } }