-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneigh_graphic.py
409 lines (285 loc) · 9.99 KB
/
neigh_graphic.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
from __future__ import division
import json
from pymongo import MongoClient
from collections import Counter
import os,sys
import mongo_client
### connection to mongo client using mongo_client.py ###
coll_unigenes = mongo_client.mongo_connect()[0]
coll_clusters = mongo_client.mongo_connect()[1]
coll_e5 = mongo_client.mongo_connect()[2]
if len(sys.argv) == 2:
if sys.argv[1] == "-h" or sys.argv[1] == "-help":
sys.exit("\n"+"# unigenes cluster graphication #" +"\n" +
"\n" + "Usage: neigh_graphic.py <number_of_neighbours_genes_to_display> <unigene_cluster>"+"\n"+
"\n" + "Example: neigh_graphic.py 2 000_000_005"+"\n"
)
elif len(sys.argv) < 2:
sys.exit("\n"+"# unigenes cluster graphication #" +"\n" +
"\n" + "Error: few arguments" +
"\n" + "Usage: neigh_graphic.py <number_of_neighbours_genes_to_display> <unigene_cluster>"+"\n"+
"\n" + "Example: neigh_graphic.py 2 000_000_005"+"\n"
)
else:
try:
number_of_neigh = sys.argv[1]
unigene_cluster = sys.argv[2]
except:
sys.exit("\n"+"# unigenes cluster graphication #" +"\n" +
"\n" + "Error: many arguments" +
"\n" + "Usage: neigh_graphic.py <number_of_neighbours_genes_to_display> <unigene_cluster>"+"\n"+
"\n" + "Example: neigh_graphic.py 2 000_000_005"+"\n"
)
### kegg pathways containing file (KEGGs_pathways.txt)
### is parsed to generate KEEG=description hash
kegg_pathways = open("data/KEGGs_pathways.txt","r")
def make_kegg_dict(kegg_pathways):
"""generates kegg pathways hash which contains kegg descriptions"""
global kegg_dict
kegg_dict = {}
for line in kegg_pathways:
fields = line.strip("\n").split("\t")
kegg= fields[0]
description= " ".join(fields[1::]).rstrip(" ")
kegg_dict[kegg]= description
return kegg_dict
def get_kegg_description(kegg):
"""retrieve kegg description from kegg_dict"""
description = kegg_dict[kegg]
return description
def mongo_orf_find(GMGC):
""" retrieve information of an orf element (strand,locus,start,end) from a unigene cluster """
GMGC_cluster = coll_unigenes.find({"u":GMGC})
GMGC_dict = {}
for orf in GMGC_cluster:
orf = orf['o']
for a in orf:
gene = a['g']
Locus = a['s']
start = Locus[0]
end = Locus[1]
strand = Locus[2]
try:
#GMGC_dict[gene]=[contig,start,end,strand,sample]
GMGC_dict[gene]=[start,end,strand]
#print [contig,start,end,strand,sample]
except:
print("hay un problema")
return GMGC_dict
#gmgc_cluster = mongo_orf_find("000_000_000")
def mongo_functional_find(GMGC):
""" retrieve functional information from mongo.cluster database """
kegg_list = []
Egg_list = []
GMGC_function = coll_clusters.find({"u":GMGC})
GMGC_function_list =[]
for element in GMGC_function:
kegg = element['K_P']
kegg = kegg.split(",")
for n in kegg: #depuro la lista de kegg para quedarme solo con los kegg pathways
if n in kegg_dict and n not in kegg_list:
kegg_list.append(n)
Egg = element['OGs'].split(",")
#print(Egg)
for n in Egg:
if n.split("@")[1] == "1":
Egg.remove(n)
#print(Egg)
try:
#GMGC_function_list=[kegg_list,cog,eggnog,gene_symbol,desc]
GMGC_function_list=[kegg_list,Egg]
except:
print("hay un problema")
return GMGC_function_list
def retrieve_gmgc(gene):
""" retrieve the gmgc of an unigene orf in mongo database """
GMGC_function = coll_unigenes.find({ "o.g":gene},{"u":1}).limit(1)
selected = []
gmgc = ""
for n in GMGC_function:
try:
gmgc = n["u"]
except:
gmgc = ""
return gmgc
def retrieve_neighbours_data(gmgc_cluster,number):
""" retrieve a list with the four genes -2-1+1+2 which
sourround the query unigene orf """
neigh_dict = {}
for k,v in gmgc_cluster.items():
gene_ordered =[]
query_gene = k
start = v[0]
end = v[1]
strand = v[2]
orf = k.split("_")
gene = int(orf[3])
sample_contig =orf[0:3]
gene_list =range(-number,number+1,1)
for gene_pos in gene_list:
sample_cont = []
genes =str(int(gene)+int(gene_pos))+"#"+strand
sample_cont = sample_contig[0:3]
sample_cont.append(str(genes))
genes = "_".join(sample_cont)
gene_ordered.append(genes)
neigh_dict[query_gene]=gene_ordered
return neigh_dict
def get_kegg_description(Egg):
""" connect to mongo eggnog5 database
and retrieve description of Egnog """
Egg = Egg.split("@")[0]
e5_database = coll_e5.find({"e":Egg})
description = ""
for element in e5_database:
description = element["d"]
return description
def printing_genes(gmgc_list_func):
""" this function controls ouput gene graphication """
# draw elements for graphic output
positive=" [{}]>"
negative="<[{}] "
CRED = '\033[91m'
CYEW = '\033[33m'
CEND = '\033[0m'
CBLUE = '\033[44m'
CGRE = '\033[92m'
CGRE2 = '\033[42m'
CGREY = '\033[100m'
# variables
list_for_draw=[]
kegg_repeated=[]
keggs_for_draw=[]
egg_repeated=[]
egg_for_draw=[]
for n in gmgc_list_func:
if n !="":
keggs = n[0]
strand = n[1]
unigene = n[2]
eggnog_list = n[3]
final_kegg = unigene+":"
for kegg in keggs:
final_kegg = final_kegg+" "+kegg
if kegg in kegg_dict:
if kegg not in kegg_repeated:
kegg_repeated.append(kegg)
kegg_description=kegg_dict[kegg]
keggs_for_draw.append((CBLUE+str(kegg)+CEND)+" : "+kegg_description)
for egg in eggnog_list:
if egg not in egg_repeated:
egg_repeated.append(egg)
egg_description = get_kegg_description(egg)
egg_for_draw.append((CGRE2+str(egg)+CEND)+" : "+egg_description)
egg = " ".join(eggnog_list)
if strand == "+":
positive =" [{}]>".format(final_kegg+" "+(CGRE+egg+CEND))
list_for_draw.append(positive)
else:
negative="<[{}] ".format(final_kegg+" "+(CGRE+egg+CEND))
list_for_draw.append(negative)
else:
list_for_draw.append(CRED+"[X]"+CEND)
#printing the line
line=""
count = 0
for element in list_for_draw:
count +=1
if element != CRED+"[X]"+CEND:
if len(element.split(":")[1]) > 3:
if count == 3:
line+=(CGREY+" "+element+CEND)
#line+=(CRED+" "+element.replace("]","")+CEND+CRED+"]"+CEND)
else:
line+=(CYEW+" "+element+CEND)
else:
line+=" "+element
print(line)
for kegg in keggs_for_draw:
print(kegg)
for egg in egg_for_draw:
print(egg)
print("\n")
def get_unigene_functional_data(neigh_gene_list,strand):
# neigh_gene_list is a depured list (contains uniq syntenies)
# of ORFs and sourrounded neigh orfs
unigene_list_description = []
for unigene in neigh_gene_list:
unigene_cogs = mongo_functional_find(str(unigene)) # [[keggs][Eggnogs]]
unigene_description=[]
if unigene_cogs:
eggnog = unigene_cogs[1]
kegg = unigene_cogs[0]
# add kegg, strand, unigene and eggnogs to unigene description
unigene_description.append(kegg)
unigene_description.append(strand)
unigene_description.append(unigene)
unigene_description.append(eggnog)
else:
unigene_description=""
#unigene_list_description contains the 4 neihbours genes and the orfs with their strand and cog information
unigene_list_description.append(unigene_description)
#print(unigene_list_description)
return unigene_list_description
def synteny_based_on_keggs(neigh_gene_list):
""" generates a list of keggs that belong to neighbours genes to filter by kegg synteny"""
unigene_list_of_keggs = []
for unigene in neigh_gene_list:
unigene_cogs = mongo_functional_find(str(unigene)) # [[keggs][Eggnogs]]
unigene_description=[]
if unigene_cogs:
kegg = unigene_cogs[0]
kegg = "_".join(kegg)
#print unigene_description
else:
kegg=""
unigene_list_of_keggs.append(kegg)
unigene_list_of_keggs.sort()
return unigene_list_of_keggs
def main(unigene_cluster,number_of_neigh):
""" number of neigh is the number of genes you want to
show in the ouput, i.e ussing 2 you retrieve ,-2,-1,+1,+2"""
print("\n")
## VARIABLES AND LISTs
unique_neigh_syntenies=[]
keggs_synteny = []
kegg_list = []
gmgc_orf_dict = {}
number_neigh = 0
neigh_with_keggs = 0
analysed_orfs = 0
keggs_for_draw_list=[] #retrieve stats of genes with keggs
#retrieve orfs fromunigene cluster {"orf":[start,end,strand]...}
unigene_cluster = mongo_orf_find(unigene_cluster)
#retrieve neighbours for evey orfs and generate a dictionary orf, neighbours orfs
# {"orf":[unigene-2,unigene-1,unigen_from_query_cluster,unigene,unigen+1,unigene+2]}
unigene_cluster_neighbours = retrieve_neighbours_data(unigene_cluster,int(number_of_neigh))
#iteration over each orfs to analyze the synteny of Keggs
for query_gene, neighbour_gene_list in unigene_cluster_neighbours.items():
analysed_orfs += 1
gmgc_list_func = []
neigh_gene_list=[]
neigh_gene_list_for_synteny = []
#analyze each neighbour ORF
for ORF in neighbour_gene_list:
strand = ORF.split("#")[1] # unigenes strands
neigh_orf = ORF.split("#")[0] #unigene
unigene = retrieve_gmgc(neigh_orf)
neigh_gene_list.append(unigene)
neigh_gene_list_for_synteny.append(unigene)
# filtering by unigene synteny composition (first level)
neigh_gene_list_for_synteny.sort()
if neigh_gene_list_for_synteny not in unique_neigh_syntenies:
unique_neigh_syntenies.append(neigh_gene_list_for_synteny)
# filtering by keggs synteny composition (second level)
list_of_keggs = synteny_based_on_keggs(neigh_gene_list)
if list_of_keggs not in keggs_synteny:
keggs_synteny.append(list_of_keggs)
#almacenamos el resultado de los cogs de todas las sintenias para graficar
gmgc_list_func = get_unigene_functional_data(neigh_gene_list,strand)
#use printing_genes function to show funcy results in terminal
printing_genes(gmgc_list_func)
# we create kegg_pathways dictionary
make_kegg_dict (kegg_pathways)
if __name__ == main(unigene_cluster,2):
main(unigene_cluster)