Skip to content
Snippets Groups Projects
Commit 7f0c3494 authored by nina.marthe_ird.fr's avatar nina.marthe_ird.fr
Browse files

changed the function to detect inversion to make it quicker and cleaner. added comments

parent 3b364c8e
Branches
Tags
No related merge requests found
......@@ -568,7 +568,6 @@ def get_old_new_pos_deletion(variation,feat_start,feature_path_target_genome,fea
return [pos_old,pos_new] # pos_old and pos_new are the base before the change
def init_new_var(variation,type,feature_path_source_genome,feature_path_target_genome,i,j,seg_seq,feature):
variation.type=type
variation.start_var=feature_path_source_genome[i]
......@@ -614,65 +613,62 @@ def continue_var(variation,seg_seq,feature_path_source_genome,feature_path_targe
variation.ref+=get_segment_sequence(seg_seq,feature_path_source_genome[i])
variation.seg_ref.append(feature_path_source_genome[i])
def get_common_segments(list1,list2):
list_output=[]
for elem in list1:
if elem in list2:
list_output.append(elem)
return list_output
def compare_strand(list_1,list_2,list_1_unstrand,list_2_unstrand):
# get the list of segments in common
seg_common=[]
for segment in list_1_unstrand:
if segment in list_2_unstrand:
seg_common.append(segment)
# for each segment in common, check if the strand is the same. check index in list unstranded to get the segment in list stranded
# gives the list of segments from list1 that are in list2
def get_common_segments(dict1,dict2):
list_common=[]
for segment in dict1:
if segment in dict2:
list_common.append(segment)
return list_common
# 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:
index_1=list_1_unstrand.index(segment)
index_2=list_2_unstrand.index(segment)
if list_1[index_1]==list_2[index_2]:
if dict1[segment]==dict2[segment]:
same_strand_count+=1
return [seg_common,same_strand_count]
return [len(seg_common),same_strand_count]
def detect_segment_order_inversion(list_1,list_2):
if (len(list_1)==1) | (len(list_2)==1):
return False
[cpt,i]=[0,0]
list_1_common=get_common_segments(list_1,list_2)
list_2_common=get_common_segments(list_2,list_1)
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 segments are on the same position when the lists are reversed, there is an inversion.
def detect_orient_inversion(list_1,list_2):
list_1_unstrand=[segment_stranded[1:] for segment_stranded in list_1]
list_2_unstrand=[segment_stranded[1:] for segment_stranded in list_2]
[seg_common,same_strand_count]=compare_strand(list_1,list_2,list_1_unstrand,list_2_unstrand)
def detect_orient_inversion(dict1,dict2):
[seg_common_count,same_strand_count]=compare_strand(dict1,dict2)
if same_strand_count>=len(seg_common)*0.9: # if more than 90% of segments shared have the same strand, no inversion
strand_inversion=False
if same_strand_count>=seg_common_count*0.9: # if more than 90% of segments shared have the same strand, no inversion
return [False,dict1,dict2]
else:
strand_inversion=True
return [strand_inversion,list_1_unstrand,list_2_unstrand]
return [True,dict1,dict2]
# 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):
# target_dict=dict.fromkeys(target_list,"")
# 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 we have an inversion of the orientation of the segments
[strand_inversion,list_1_unstrand,list_2_unstrand]=detect_orient_inversion(list_1,list_2)
[strand_inversion,dict1,dict2]=detect_orient_inversion(dict1,dict2)
# check if we have an inversion of the order of the segments
segment_order_inversion=detect_segment_order_inversion(list_1_unstrand,list_2_unstrand)
segment_order_inversion=detect_segment_order_inversion(dict1,dict2)
# if there we have both inversions, the gene is in an inverted region. reverse the second list for the comparison.
# if there we have both inversions, the gene is in an inverted region
if segment_order_inversion & strand_inversion:
return True
else :
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment