-
Notifications
You must be signed in to change notification settings - Fork 4
/
03_get_locus_ortholog_part2.py
executable file
·221 lines (167 loc) · 7.77 KB
/
03_get_locus_ortholog_part2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
##############################
##### DEF1 : Dico fasta #####
##############################
def dico(fasta_file_path):
F2 = open(fasta_file_path, "r")
dicoco = {}
while 1:
next2 = F2.readline()
if not next2:
break
if next2[0] == ">":
fasta_name_query = next2[:-1]
Sn = string.split(fasta_name_query, "||")
fasta_name_query = Sn[0]
next3 = F2.readline()
fasta_seq_query = next3[:-1]
# next3 = F2.readline() ## jump one empty line (if any after the sequence)
# #next3 = F2.readline()
# fasta_name_match = next3[:-1]
# Sn = string.split(fasta_name_match, "||")
# fasta_name_match = Sn[0]
# next3 = F2.readline()
# fasta_seq_match = next3[:-1]
#pairwise = [fasta_name_query,fasta_seq_query,fasta_name_match,fasta_seq_match]
dicoco[fasta_name_query]=fasta_seq_query
#dicoco[fasta_name_match]=fasta_seq_match
## ADD pairwise with condition
#list_pairwises.append(pairwise)
F2.close()
return(dicoco)
###################################################################################
##############################################################
##### DEF2 : Get sequences from list of names per locus #####
##############################################################
def get_seq_per_locus(list_locus, bash_seq, path_OUT,prefix_name):
print "\nGet seq per locus\n\n"
print "\tLIST LOCUS = %s\n\n" %list_locus
print "\tBASH SEQ KEYS = %s\n\n" %bash_seq.keys()
i=0
for locus in list_locus:
i = i+1
path_locus_OUT = "%s/%s_%d.fasta" %(path_OUT, prefix_name, i)
OUT = open(path_locus_OUT, "w")
for seq_name in locus:
sequence = bash_seq[seq_name]
OUT.write("%s\n" %seq_name)
OUT.write("%s\n" %sequence)
OUT.close()
###################################################################################
#######################
##### RUN RUN RUN #####
#######################
import string, os, sys, pickle
## 1. LOAD "PICKLE" LIST OF ALL POTENTIAL LOCUS (orthologuous and paraloguous)
## 2. SELECT ONLY ORTHOLOGUOUS (list of names of orthologuous sequences)
## 3. GET SEQUENCES CORRESPONDING TO THE ORTHOLOGUOUS NAMES (per locus)
############################################################
## 1. LOAD "PICKLE" LIST OF ALL POTENTIAL LOCUS
############################################################
file_LOCUS = open("02_backup_list_LOCUS")
list_LOCUS = pickle.load(file_LOCUS)
file_LOCUS.close()
LOG = open("04_ResultsParaloguesRemoval.log", "w")
######################################################
## 2. [1rst treatment INTRA LOCUS] SELECT ONLY ORTHOLOGUOUS: test if locus is orthologuous (i.e. when a same species appear with only 1 sequence in the locus)
######################################################
LOCUS_without_DUPLI = []
for locus in list_LOCUS:
## remove redondancy (i.e. same names present several times as it is expected)
l = []
for name in locus:
if name not in l:
if name[-2:] == "_S":
name = name[:-2]
l.append(name)
## Now test if only one name per species, if not ==> duplication
list_initials = []
D=0
for name in l:
initials = name[1:3]
if initials not in list_initials:
list_initials.append(initials)
else: # DUPLICATION DETECTED
D=1
if D==0: # means no duplication detected
LOCUS_without_DUPLI.append(l)
LOG.write("NUMBER OF REMAINING LOCUS AFTER 1RST TREATMENT [INTRA LOCUS] = %d\n" %len(LOCUS_without_DUPLI))
print LOCUS_without_DUPLI[1:10]
################################################################
## 3. [2nd treatment INTER LOCUS] In fact there are still some duplication in "LOCUS_without_DUPLI" ==> no duplication in each loci ... OK, but several loci sharing a same sequence is still possible, I will exclude this case now
################################################################
list_name_seq = []
bash_name_seq = {}
# 3.1. Get bash = key = sequence name // value = list of locus
for locus in LOCUS_without_DUPLI:
for sequence in locus:
if sequence not in list_name_seq:
list_name_seq.append(sequence)
list = [locus]
bash_name_seq[sequence] = list
else:
bash_name_seq[sequence] = bash_name_seq[sequence] + list
# 3.2. From the previous bash, test sequence name present in more than one loci (i.e. length of the list of loci > 1), record these paralogous loci
k = bash_name_seq.keys()
list_locus_paralogues = []
for sequence in k:
list = bash_name_seq[sequence]
if len(list) > 1:
for locus in list:
list_locus_paralogues.append(locus) ### list of list
print list_locus_paralogues[1:20]
# 3.3. Remove the paralogous loci from the "LOCUS_without_DUPLI" list
for locus in list_locus_paralogues:
#print "\t%s" %locus
if locus in LOCUS_without_DUPLI:
LOCUS_without_DUPLI.remove(locus)
LOG.write("NUMBER OF REMAINING LOCUS AFTER 2ND TREATMENT [INTER LOCUS] = %d\n\n" %len(LOCUS_without_DUPLI))
##################################
list_LOCUS_2sp = []
list_LOCUS_3sp = []
list_LOCUS_4sp = []
list_LOCUS_5sp = []
list_LOCUS_6sp = []
for locus in LOCUS_without_DUPLI:
if len(locus) == 2:
list_LOCUS_2sp.append(locus)
if len(locus) == 3:
list_LOCUS_3sp.append(locus)
if len(locus) == 4:
list_LOCUS_4sp.append(locus)
if len(locus) == 5:
list_LOCUS_5sp.append(locus)
if len(locus) == 6:
list_LOCUS_6sp.append(locus)
LOG.write("Number of locus with 2 species: %d\n" %len(list_LOCUS_2sp))
LOG.write("Number of locus with 3 species: %d\n" %len(list_LOCUS_3sp))
LOG.write("Number of locus with 4 species: %d\n" %len(list_LOCUS_4sp))
LOG.write("Number of locus with 5 species: %d\n\n" %len(list_LOCUS_5sp))
LOG.write("Number of locus with 6 species: %d\n\n" %len(list_LOCUS_6sp))
#########################################################################
## 4. GET SEQUENCES CORRESPONDING TO THE ORTHOLOGUOUS NAMES (per locus)
#########################################################################
path_TRANSCRIPTS = "../../tmp/01_formated_INPUT/"
L2 = os.listdir(path_TRANSCRIPTS)
BASH = {}
for subfile in L2:
fasta_file_path = "%s/%s" %(path_TRANSCRIPTS, subfile)
dicoco = dico(fasta_file_path)
BASH.update(dicoco)
LOG.write("TOTAL NUMBER OF LOCI PROCESSED = %d\n" %len(BASH.keys()))
#print BASH.keys()[1:10]
## list_LOCUS_2sp
get_seq_per_locus(list_LOCUS_2sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_perCLASS/LOCUS_2sp","locus_2sp") ### DEF 2 ###
get_seq_per_locus(list_LOCUS_2sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_ALL","locus_2sp") ### DEF 2 ###
## list_LOCUS_3sp
get_seq_per_locus(list_LOCUS_3sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_perCLASS/LOCUS_3sp","locus_3sp") ### DEF 2 ###
get_seq_per_locus(list_LOCUS_3sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_ALL","locus_3sp") ### DEF 2 ###
## list_LOCUS_4sp
get_seq_per_locus(list_LOCUS_4sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_perCLASS/LOCUS_4sp","locus_4sp") ### DEF 2 ###
get_seq_per_locus(list_LOCUS_4sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_ALL","locus_4sp") ### DEF 2 ###
## list_LOCUS_5sp
get_seq_per_locus(list_LOCUS_5sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_perCLASS/LOCUS_5sp","locus_5sp") ### DEF 2 ###
get_seq_per_locus(list_LOCUS_5sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_ALL","locus_5sp") ### DEF 2 ###
## list_LOCUS_6sp
get_seq_per_locus(list_LOCUS_6sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_perCLASS/LOCUS_6sp","locus_6sp") ### DEF 2 ###
get_seq_per_locus(list_LOCUS_6sp, BASH, "04_LOCUS_ORTHOLOGS_UNALIGNED_ALL","locus_6sp") ### DEF 2 ###