-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneigh_functions.py
421 lines (296 loc) · 12.9 KB
/
neigh_functions.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
410
411
412
413
414
415
416
417
418
419
420
421
import os
### Parseamos el fichero con las descripciones de los KEGGs pathways
### y generamos un diccionario para almacenarlos.
kegg_pathways = open("data/KEGGs_pathways.txt","r")
def make_kegg_dict(kegg_pathways):
"""generate kegg pathway dictionary containning 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 hash """
description = kegg_dict[kegg]
return description
def get_egg_description(Egg, coll_e5):
""" 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 mongo_orf_find(GMGC,maximum_gmgc_genes,coll_unigenes):
""" retrieve orf genomic information"""
GMGC_cluster = coll_unigenes.find({"u":GMGC})
GMGC_dict = {}
gene_count = 0
for orf in GMGC_cluster:
orf = orf['o']
for a in orf:
if gene_count < maximum_gmgc_genes:
gene_count +=1
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("something was wrong")
else:
break
return GMGC_dict
def retrieve_gmgc(gene, coll_unigenes):
""" retrieve gmgc for every orf from unigenes db """
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,neighbours_range):
""" get a list containning -2-1+1+2 neoghbours genes surrounding
the unigenes in every contig """
# generate a neighbour range to analyze 2,4,6,8 or any other desired value
# i.e. with 2 generate this gene list [-2,-1,1,2]
gene_list = []
for num in range(-neighbours_range,neighbours_range+1,1):
gene_list.append(num)
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]
for gene_pos in gene_list:
sample_cont = []
genes = int(gene)+int(gene_pos)
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 clean_unigene(gmgc):
'''in case GMGC nomeclature was used in the input example,
GMGC.100_000_123.UNKNOWN words after/before . are removed'''
gmgc_clean = gmgc.split(".")[1]
return gmgc_clean
def mongo_functional_find(GMGC, coll_clusters): #prueba al 350
""" get functional information from mongo.clusters db for gmgc element"""
kegg_list = [] # store the keggs
Egg = [] # store the Eggnogs cogs
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)
#gene_symbol = element['g_s']
#cog = element['COG']
#desc = element['desc']
Egg = element['OGs'].split(",")
GMGC_function_list=[kegg_list, Egg]
return GMGC_function_list
def compute_neigh_cogs_assignation(neighbours_genes,neighbours_range,coll_unigenes,coll_clusters,cog_category):
# VARIABLES
cogs_organization_list = [] # storage cogs assgination for every unigene
analysed_orfs = 0 # number of orfs (unigenes) that contains a GMGC cluster
number_neigh = 0 # number of neighbours genes present in that GMGC cluster
number_neigh_with_cogs= 0 # number of neighbours genes present in that gmgc cluster that contains cog assignation
unigenes_functions_list = [] # storage all the cogs retrieved from neighbours genes
for k,v in neighbours_genes.items():
analysed_orfs += 1
query_gene = k
gene_list = v
range_list = []
for num in range(0,neighbours_range*2,1):
range_list.append(num)
gmgc_list = [] # list for functional analysis developed below
for n in range_list:
gmgc_orf = retrieve_gmgc(gene_list[n],coll_unigenes)
gmgc_list.append(gmgc_orf)
#retrieve list of neigh orfs containing COGS instead of unigene code
cogs_organization = []
for unigene in gmgc_list: # unigene = cluster de GMGC al que pertenece el unigene
avoid_cog_repeated = [] # avoid repeated cogs in the same neigh orf
cog = ["NA"]
if unigene != '':
number_neigh +=1
try:
if cog_category == "kegg":
cog = mongo_functional_find(unigene,coll_clusters)[0]
else:
cog = mongo_functional_find(unigene,coll_clusters)[1]
except:
cog = ["NA"]
if cog == []:
cog=["NA"]
if cog != ["NA"] and cog[0] != '':
number_neigh_with_cogs +=1
if cog[0] =='':
cog= ["NA"]
cogs_organization.append(cog)
else:
cogs_organization.append(cog)
for n in cog: # clean the cogs to avoid future errors
if cog_category == "kegg":
if n in kegg_dict and n not in avoid_cog_repeated and n != '':
avoid_cog_repeated.append(n)
unigenes_functions_list.append(n)
else:
if n not in avoid_cog_repeated and n != '':
if n != 'NA':
avoid_cog_repeated.append(n)
unigenes_functions_list.append(n)
#print cogs_organization #this print show the synteny organization of the neighbours cogs
cogs_organization_list.append(cogs_organization)
return [cogs_organization_list,
analysed_orfs,
number_neigh,
number_neigh_with_cogs,
unigenes_functions_list
]
def neigh_scores(hits,num_query_cog,subject_cog_list,unique_cogs,count_of_cogs,neigh_orf_with_cogs):
""" most important neigh score is functional
conservation developed for GMGC nature """
#positve value (how many of preasignned keggs were retrieved keggs for that unigene)
try:
positive_value = ("{0:.2f}".format(int(hits)/num_query_cog))
except:
positive_value = 0
#accuracy value (proportion of how many retrieved keggs were correct)
try:
accuracy_value = ("{0:.2f}".format(hits/len(subject_cog_list)))
except:
accuracy_value = 0
#operon functional conservation proxy
try:
func_conservation = ("{0:.3f}".format(1-(unique_cogs/count_of_cogs)/neigh_orf_with_cogs)) # neigh of with cogs its the same that the number of ORF that enter in the neigh analysis
except:
func_conservation = 0
return [positive_value,
accuracy_value,
func_conservation
]
def get_neigh_orf_with_cogs(cogs_organizacion_list):
""" calculate number of unigenes with at least
one neigh genes with cog assignation"""
cog_depured_list =[]
for k in cogs_organizacion_list:
if k.count("NA") != 4:
cog_depured_list.append(k)
neigh_orf_with_cogs = len(cog_depured_list)
return neigh_orf_with_cogs
def print_results(argument, kegg_result, egg_result, list_of_neighbours_unigenes,input_unigene_file):
if argument == "-u":
print("#gmgc"+"\t"+
"query_cogs"+"\t"+
"subject_cogs"+"\t"+
"analysed_orfs"+"\t"+
"number_neigh_genes"+"\t"+
"number_neigh_with_cogs"+"\t"+
"unique_cogs"+"\t"+
"count_of_cogs"+"\t"+
"cog_conservation"+"\t"+
"hit_cog_percentage"+"\t"+
"cog_description"
)
print("\t".join(kegg_result[0][0]))
print("\t".join(egg_result[0][0]))
print("\t".join(list_of_neighbours_unigenes[0]))
if argument == "-f":
directory = input_unigene_file+"_result"
isdir = os.path.isdir(directory+"/")
if isdir == False:
os.mkdir(directory)
kegg_output = open(directory+"/kegg_prediction.tsv","w")
egg_output = open(directory+"/eggnog_prediction.tsv","w")
unigene_output = open(directory+"/neighbours_unigenes.tsv","w")
for line in kegg_result:
kegg_output.write("\t".join(line[0])+"\n")
for line in egg_result:
egg_output.write("\t".join(line[0])+"\n")
for line in list_of_neighbours_unigenes:
unigene_output.write("\t".join(line)+"\n")
def get_functional_data (subject_cog_dict,query_list,kegg_description_dict,cog_category):
hit_cog_percentage = [] # store percentage passed score of every COG i.e. COG@30.20
hits = 0 # cogs present in the query unigene that were predicted by the method
for k,v in subject_cog_dict.items():
cog = k
percentage = v
k_p = k+"@"+str(percentage)
hit_cog_percentage.append(k_p)
num_query_cog = len(query_list)
if cog in query_list:
hits +=1
try:
if query_list == [u'']:
query_list =['NA']
for cog in query_list:
if cog_category == "kegg":
kegg_description_dict[cog]=get_kegg_description(cog)
else:
kegg_description_dict[cog]=get_egg_description(cog,coll_e5)
except:
query_list= ["NA"]
try:
description_list = []
for cog, description in kegg_description_dict.items():
k_d= str(cog)+"@"+str(description)
description_list.append(k_d)
except:
description_list=[]
return [hits,
query_list,
description_list,
hit_cog_percentage,
num_query_cog
]
def create_unigenes_lists_for_print(gmgc,neighbours_genes,neighbours_range,coll_unigenes):
""" create a list containing the unigenes which
belong every neighbours for every orf in the unigene cluster separated by @
example: unigene_cluster \t unigene-2@unigene-1@unigene_cluster_orf@unigene+1@unigene+2"""
unigenes_list_for_print = [] # list of neighbours unigenes to be save as ouput for future analysis
analysed_orfs = 0
for k,v in neighbours_genes.items():
analysed_orfs += 1
query_gene = k
gene_list = v
range_list = [] # [0,1,2,3,4]
for num in range(0,(neighbours_range*2+1),1):
range_list.append(num)
gmgc_list = [] # list for functional analysis developed below
for n in range_list:
gmgc_orf = retrieve_gmgc(gene_list[n],coll_unigenes)
gmgc_list.append(gmgc_orf)
#create unigene_list_for_print, containing unigene assignation of every neigh orf
out_list = []
for n in gmgc_list:
if n == '':
n= 'NA'
else:
n=n
out_list.append(n)
out_line = "@".join(out_list)
unigenes_list_for_print.append(out_line)
line = gmgc+"\t"+",".join(unigenes_list_for_print)
return line