Skip to content
Snippets Groups Projects
detect_inversion.py 2.68 KiB
Newer Older
# takes two lists of segments for two genes, check if the first list is an inversion of the second one (if the segments in common are on the opposite strand)
def detect_feature_inversion(list_1,list_2):
    # convert list into dict with unstranded segment id as key and strand as value
    strand1=[seg[0] for seg in list_1]
    id1=[seg[1:] for seg in list_1]
    dict1 = {id1[i]: strand1[i] for i in range(len(strand1))}
    strand2=[seg[0] for seg in list_2]
    id2=[seg[1:] for seg in list_2]
    dict2 = {id2[i]: strand2[i] for i in range(len(strand2))}
 
    # check if there is an inversion of the orientation of the segments
    [strand_inversion,dict1,dict2]=detect_orient_inversion(dict1,dict2)
    # check if there is an inversion of the order of the segments
    segment_order_inversion=detect_segment_order_inversion(dict1,dict2)

    # if there both inversions are there, the gene is in an inverted region
    if segment_order_inversion and strand_inversion:
        return True
    else :
        return False
    

# check if the two dict have the same segments but in different orientation
def detect_orient_inversion(dict1,dict2):
    [seg_common_count,same_strand_count]=compare_strand(dict1,dict2)

    if same_strand_count>=seg_common_count*0.9: # if more than 90% of shared segments shared have the same strand, no inversion
        return [False,dict1,dict2]
    else:
        return [True,dict1,dict2]


# check if the two dict have their segments in the inverted order
def detect_segment_order_inversion(dict1,dict2):
    list_1_common=get_common_segments(dict1,dict2)
    list_2_common=get_common_segments(dict2,dict1) # same segments, different orders
    list_2_common_reversed=list(reversed(list_2_common))
    [cpt,i]=[0,0]
    while i<len(list_1_common):
        if list_2_common_reversed[i]==list_1_common[i]:
            cpt+=1
        i+=1
    return (cpt>len(list_1_common)*0.9) # if more than 90% of the shared segments are on the same position when the lists are reversed, there is an inversion. 
# check if common segments in the two dict have the same strand
def compare_strand(dict1,dict2): # dict1 and dict2 : [seg_id]->[seg_strand]
    seg_common=get_common_segments(dict1,dict2)
    # for each segment in common, check if the strand is the same
    same_strand_count=0
    for segment in seg_common:
        if dict1[segment]==dict2[segment]:
            same_strand_count+=1
    return [len(seg_common),same_strand_count]
# gives the list of segments from dict1 that are in dict2
def get_common_segments(dict1,dict2):
    list_common=[]
    for segment in dict1:
        if segment in dict2:
            list_common.append(segment)
    return list_common