forked from Paramecium-aurelia/20170214-Gene-Deletion-WGD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20170515_htsc.holes.domain.splitting.py
535 lines (504 loc) · 24.1 KB
/
20170515_htsc.holes.domain.splitting.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
from __future__ import division
import re, sys, math, operator, copy,collections
import numpy as np
from itertools import groupby
def ProcessCLI(args):
scaf_pb=readgff(args[1])
bestHits= readpsl(args[2],scaf_pb)
#for k in bestHits:
# print ' \t'.join([str(i) for i in list(k)])
def getInt(pb):
return [int(i) for i in re.split(r'(\d+)',pb) if len(i) >0 and i.isdigit()][0]
def getdup1(lst):
d={}
for key, value in lst:
d[key] = d.get(key, []) + [value]
for k in d:
if len(d[k])==1:
print k, ','.join(d[k])
else:
print 'dup:', k, '-->', ' '.join(d[k])
#return pdup(d)
def domainsplit_tandemdup_ohno(htscList,en_psl,gff):#if a single protein is hit by multiple other proteins when we compare pairs
#of scaffolds, we infer that there is a tandem duplication, or a domain splitting of one large protein, and choose a single
#best mate to form the ohnologan
bestmultL=[]; noscorebestmultL=[]; doublesD=dict(); casesL=[]
for htsc in htscList:
doubles0=[]; doubles1=[]; mult=set(); mult0=set(); mult1=set()
for tup in htsc:
if tup[0] not in doubles0:
doubles0+=[tup[0]]
else:
mult.add(tup[0]); mult0.add(tup[0)])
if tup[1] not in doubles1:
doubles1+=[tup[1]]
else:
mult.add(tup[1]); mult1.add(tup[1])
for tup in htsc:
if tup[0] in mult0:
if tup[0] not in doublesD.keys():
doublesD[tup[0]]=set()
doublesD[tup[0]].add(tup[1])
else:
doublesD[tup[0]].add(tup[1])
else:
continue
if tup[1] in mult1:
if tup[1] not in doublesD.keys():
doublesD[tup[1]]=set()
doublesD[tup[1]].add(tup[0])
else:
doublesD[tup[1]].add(tup[0])
else:
continue
for k in doublesD.keys():
klen=0
vlentot=0
with open(gff,'r') as gfile:
for line in gfile:
if k in re.split('\W+',line[8]):
klen+=float((math.fabs(float(line[4]) - float(line[3]))))
for v in doublesD[k]:
if v in re.split('\W+',line[8]):
vlentot+=float((math.fabs(float(line[4]) - float(line[3]))))
if klen/vlentot > 1.5:
casesL+=[(k,doublesD[k],"tandem duplication")]
else:
casesL+=[(k,doublesD[k]),"domain split"]
#dummy=[]
with open(en_psl,'r') as psl:
for line in psl:
if ((line[0] == k and line[2] in doublesD[k]) or (line[0] in doublesD[k] and line[2]==k)) and line[1]!=line[3]:
bestmultL.append((line[0],line[2],(float(line[-3]),float(line[-2]))))#we now have a tuple with the ohnologan, & its bitscore&ev
noscorebestmultL.append((line[0],line[2]))
#bestmultL+=bestdoubles; #noscorebestmultL+=
return bestmultL, noscorebestmultL, casesL
##################################################################################################
#what we need to do now:
#1) the items in bestmultL are (p1, p2, (bscore,escore)) triples
#so, let's make 2-part tuples: for t in bmL: twomultL.append((t{0], t[1]))
#then: for htsc in htscList:
#build a list of mults
#for t in htsc:
#if t has a mult and t not in bmL and (t[1], t[0]) not in bmL:
#delete the tuple from htsc---it was a tandem duplication or a split domain, so that isn't an ohnolog
#if we need to know which was the case, we also have a list somewhere saying that (t[0], t[1], t[2]) was one of those cases
#where t[0] is 1 protein,t[1] is the set of other proteins in the gff that it hits, and t[2] is a string indicated which case
def remove_ds_td_extras(htscList, bmL):#see comments above
singly=htscList.copy()
for htsc in singly:
doubles0=[]; doubles1=[]; mult=set(); mult0=set(); mult1=set()
for tup in htsc:
if tup[0] not in doubles0:
doubles0+=[tup[0]]
else:
mult0.add(tup[0])
if tup[1] not in doubles1:
doubles1+=[tup[1]]
else:
mult1.add(tup[1])
for tup in htsc:
for p in tup:
if p in mult:
if tup or (tup[1],tup[0]) in bmL:
continue
else:
#################################################
#DELETE THE TUPLE FROM THE htsc!!!!!!!!!!!!!!!!!!!!!!!!!
#But will this f**k up the indexing by which the for loop works? I hope not
#we will have to delete by value, not by index, that is for sure
#maybe use:
htsc.remove(tup)
return singly
def fillht(htscList, en_pslout_file):#infer synteny if hits between pairs of scaffolds are uniformly increasing/incrasing
#or uniformly increasing; if there are gaps in the synteny find appropriate hits that fill the holes and mark the pairs as
#ohnologans
holesList=[]
l2hL=[]
l2thhL=[]
twohitsholesList=[]
hole1way=[]
for htsc in htscList:
if len(htsc)>2:
for i in range(1,len(htsc)-1):
if getInt(htsc[i-1][0])< getInt(htsc[i][0]) and getInt(htsc[i][0])< getInt(htsc[i+1][0]):
if getInt(htsc[i-1][1])< getInt(htsc[i][1]) and getInt(htsc[i][1])< getInt(htsc[i+1][1]):
if getInt(htsc[i-1][0])<(getInt(htsc[i][0])-1) and getInt(htsc[i-1][1])<(getInt(htsc[i][1])-1):#if there is a hole on BOTH scafs
#there was a gap on the scaffold. now we want to fill it in.
'''if getInt(htsc[i-1][0])-(getInt(htsc[i][0])-1) == getInt(htsc[i-1][1])-getInt(htsc[i][1]):
#if the gaps are the same length on both scaffolds, we try to fill in the holes one at a time
for index,protein_int in enumerate(range((getInt(htsc[i-1][0])+1),getInt(htsc[i][0]))):
for line in en_pslout:
line=line.rstrip().split()
if (getInt(line[0]==protein_int) and getInt(line[2])==(getInt(htsc[i-1][1])+(index+1))\#if the pair exists at all in the
or getInt(line[2]==protein_int) and getInt(line[0])==(getInt(htsc[i-1][1])+(index+1))):#enriched psl output
##ADD THAT PAIR TO THE LIST OF OHNOLOGANS
##BUT IT DON'T KNOW WHAT THAT LIST IS CALLED---HELP ME, ETIENNE
elif getInt(htsc[i-1][0])-(getInt(htsc[i][0])-1) != getInt(htsc[i-1][1])-getInt(htsc[i][1]):'''
indivcol1=set(range(getInt(htsc[i-1][0])+1),getInt(htsc[i][0]))
indivcol2=set(range(getInt(htsc[i-1][1])+1),getInt(htsc[i][1]))
mycol=indivcol1.union(indivcol2)
dummy=[]
with open(en_pslout_file,'r') as en_pslout:
for line in en_pslout:
if getInt(line[0]) in mycol and getInt(line[2]) in mycol and line[1]!=line[3]:
dummy.append((line[0],line[2],(float(line[-3]),float(line[-2]))))#we now have a tuple with the ohnologan, & its bitscore&ev
dummy_s=sorted(dummy,key=lambda tup:(tup[2][0]),reverse=True)
allowed=set()#this code makes sure that we don't keep duplicates of a reciprocal hit
kill_it=[]
for tup in dummy_s:
if tup[0] not in allowed and tup[1] not in allowed:
allowed.add(tup[0]);allowed.add(tup[1])
else:
kill_it.append(tup)
for tupk in kill_it:
dummy_s.remove(tupk)
dummy_top=dummy_s[0:(min(len(indivcol1),len(indivcol2))+1)].
length2=[]
for trip in dummy_top:
length2.append((trip[0],trip[1]))
l2hL+=[length2]
holesList+=[dummy_top]
elif getInt(htsc[i-1][1])> getInt(htsc[i][1]) and getInt(htsc[i][1])> getInt(htsc[i+1][1]):#case: scafs in reverse order
if getInt(htsc[i-1][0])<(getInt(htsc[i][0])-1) and getInt(htsc[i-1][1])>(getInt(htsc[i][1])+1):#if there is a hole on BOTH scafs
indivcol1=set(range(getInt(htsc[i-1][0])+1),getInt(htsc[i][0]))
indivcol2=set(range(getInt(htsc[i-1][1])-1),getInt(htsc[i][1]),-1)
mycol=indivcol1.union(indivcol2)
dummy=[]
with open(en_pslout_file,'r') as en_pslout:
for line in en_pslout:
if getInt(line[0]) in mycol and getInt(line[2]) in mycol and line[1]!=line[3]:
dummy.append((line[0],line[2],(float(line[-3]),float(line[-2]))))#we now have a tuple with the ohnologan, & its bitscore&ev
dummy_s=sorted(dummy,key=lambda tup:(tup[2][0]),reverse=True)
allowed=set()
kill_it=[]
for tup in dummy_s:
if tup[0] not in allowed and tup[1] not in allowed:
allowed.add(tup[0]);allowed.add(tup[1])
else:
kill_it.append(tup)
for tupk in kill_it:
dummy_s.remove(tupk)
dummy_top=dummy_s[0:(min(len(indivcol1),len(indivcol2))+1)].
length2=[]
for trip in dummy_top:
length2.append((trip[0],trip[1]))
l2hL+=[length2]
holesList+=[dummy_top]
elif len(htsc)==2:
for i in range(1,2):
if getInt(htsc[i-1][0])< getInt(htsc[i][0]):
if getInt(htsc[i-1][1])< getInt(htsc[i][1]):
if getInt(htsc[i-1][0])<(getInt(htsc[i][0])-1) and getInt(htsc[i-1][1])<(getInt(htsc[i][1])-1):#if there is a hole on BOTH scafs
#there was a gap on the scaffold. now we want to fill it in.
indivcol1=set(range(getInt(htsc[i-1][0])+1),getInt(htsc[i][0]))
indivcol2=set(range(getInt(htsc[i-1][1])+1),getInt(htsc[i][1]))
mycol=indivcol1.union(indivcol2)
dummy=[]
with open(en_pslout_file,'r') as en_pslout:
for line in en_pslout:
if getInt(line[0]) in mycol and getInt(line[2]) in mycol and line[1]!=line[3]:
dummy.append((line[0],line[2],(float(line[-3]),float(line[-2]))))#we now have a tuple with the ohnologan, & its bitscore&ev
dummy_s=sorted(dummy,key=lambda tup:(tup[2][0]),reverse=True)
allowed=set()
kill_it=[]
for tup in dummy_s:
if tup[0] not in allowed and tup[1] not in allowed:
allowed.add(tup[0]);allowed.add(tup[1])
else:
kill_it.append(tup)
for tupk in kill_it:
dummy_s.remove(tupk)
dummy_top=dummy_s[0:(min(len(indivcol1),len(indivcol2))+1)].
length2=[]
for trip in dummy_top:
length2.append((trip[0],trip[1]))
l2thhL+=[length2]
twohitsholesList+=[dummy_top]
elif getInt(htsc[i-1][1])> getInt(htsc[i][1]) and getInt(htsc[i][1])> getInt(htsc[i+1][1]):#case: scafs in reverse order
if getInt(htsc[i-1][0])<(getInt(htsc[i][0])-1) and getInt(htsc[i-1][1])>(getInt(htsc[i][1])+1):#if there is a hole on BOTH scafs
indivcol1=set(range(getInt(htsc[i-1][0])+1),getInt(htsc[i][0]))
indivcol2=set(range(getInt(htsc[i-1][1])-1),getInt(htsc[i][1]),-1)
mycol=indivcol1.union(indivcol2)
dummy=[]
with open(en_pslout_file,'r') as en_pslout:
for line in en_pslout:
if getInt(line[0]) in mycol and getInt(line[2]) in mycol and line[1]!=line[3]:
dummy.append((line[0],line[2],(float(line[-3]),float(line[-2]))))#we now have a tuple with the ohnologan, & its bitscore&ev
dummy_s=sorted(dummy,key=lambda tup:(tup[2][0]),reverse=True)
allowed=set()
kill_it=[]
for tup in dummy_s:
if tup[0] not in allowed and tup[1] not in allowed:
allowed.add(tup[0]);allowed.add(tup[1])
else:
kill_it.append(tup)
for tupk in kill_it:
dummy_s.remove(tupk)
dummy_top=dummy_s[0:(min(len(indivcol1),len(indivcol2))+1)].
length2=[]
for trip in dummy_top:
length2.append((trip[0],trip[1]))
l2thhL+=[length2]
twohitsholesList+=[dummy_top] #i think this will make thhL a list of lists, one list for each sca-sca pair
#which may be good, or bad, but it is easy to change
#twohitsholesList+=sorted(dummy,key=lambda tup:(tup[2][0]),reverse=True)[0:(min(len(indivcol1),len(indivcol2))+1)]
return holesList, l2hL, twohitsholesList, l2thhL
def printht(htsc):
md=[]
ml=[]
if len(htsc)>2:
for i in range(1,len(htsc)-1):
if getInt(htsc[i-1][0])< getInt(htsc[i][0]) and getInt(htsc[i][0])< getInt(htsc[i+1][0]):
if getInt(htsc[i-1][1])< getInt(htsc[i][1]) and getInt(htsc[i][1])< getInt(htsc[i+1][1]):
if i==1:
ml+=[htsc[i-1],htsc[i],htsc[i+1]]
elif i>1:
ml+=[htsc[i+1]]
elif getInt(htsc[i-1][1])> getInt(htsc[i][1]) and getInt(htsc[i][1])> getInt(htsc[i+1][1]):
if i==1:
ml+=[htsc[i-1],htsc[i],htsc[i+1]]
elif i>1:
ml+=[htsc[i+1]]
else:
print 'failed:', htsc[i]
print ml
elif len(htsc)==2:
if getInt(htsc[0][0])< getInt(htsc[1][0]):
if getInt(htsc[0][1])> getInt(htsc[1][1]):
md+=[htsc[0]]
md+=[htsc[1]]
elif getInt(htsc[0][1])<getInt(htsc[1][1]):
md+=[htsc[0]]
md+=[htsc[1]]
print md
else:
print 'single : ', htsc
def getdup2(lst):
d={}
dd={}
for key, value in lst:
d[value] = d.get(value, []) + [key]
for k in d.keys():
if len(d[k])==1:
print ','.join(d[k]), k
else:
print 'dup: ',','.join( d[k]),"-->", k
def getdupboth(lst):
d1={}
d2={}
for k,v in lst:
if k in d1:
d1[k]+=[v]
else:
d1[k]=[v]
if v in d2 :
d2[v]+=[k]
else:
d2[v]+=[k]
for k1 in d1:
print k1,':',d1[k1]
for k2 in d2:
#if len(d2[k2])==1:
print d2[k2],':',k2
def pdup(d):
for k in d:
if len(d[k])==1:
print (k,d[k])
else:
print 'dup:',(k,d[k])
def prTp(lst):
for k in lst:
print k[0],k[1]
def getCts(lst):
recip=[]
recipC=0
oneway=[]
onewayC=0
clst=copy.copy(lst)
recip_seen=[]
for (k,v) in clst:
if (v,k) in clst:
if (v,k) not in recip_seen:
recip_seen.append((k,v))
recip_seen.append((v,k))
recipC+=1
elif (v,k) not in clst:
oneway.append((k,v))
onewayC+=1
# print 'Reciprocal hits : ', len(recip_seen),recipC
# print 'One way Hits : ', len(oneway), onewayC
def getDistrib(lst,diPL):
d=[]
seen=[]
for k,v in lst:
if (v,k) not in seen:
t=float(diPL[k]) / float(diPL[v])
if t > 1.0:
t=1/t
d.append(t)
seen.append((k,v))
return d
#Read in and update the psl output and find hits
def readpsl(pslout,pbidict):
bhits=[]
hits=collections.OrderedDict()
phits=collections.OrderedDict()
scaffolds={}
Lengthprot={}
ps=collections.OrderedDict()
with open(pslout,'r') as file:
for line in file:
line=line.rstrip().split()
ov=max([ int(i) for i in re.split(r'(\d+)',line[-1]) if len(i)>0 and i.isdigit()])
if float(line[4])>40.0 and ov/min(float(line[5]),float(line[6]))>=0.12:
if line[0]!=line[2]:
if line[1] not in hits.keys():
hits[line[1]]=[line[3]]
else:
hits[line[1]].append(line[3])
if line[0] not in phits.keys():
phits[line[0]]={line[2]:line[-2]}
else:
phits[line[0]][line[2]]=line[-2]
if line[1] not in scaffolds.keys():
scaffolds[line[1]]=[line[0]]
else:
scaffolds[line[1]].append(line[0])
if line[3] not in scaffolds.keys():
scaffolds[line[3]]=[line[2]]
else:
scaffolds[line[3]].append(line[2])
if line[1] in ps:
if line[3] in ps[line[1]].keys():
ps[line[1]][line[3]]+=[(line[0],line[2],line[-2])]
else:
ps[line[1]][line[3]]=[(line[0],line[2],line[-2])]
else:
ps[line[1]]={}
ps[line[1]][line[3]]=[(line[0],line[2],line[-2])]
if line[0] not in Lengthprot:
Lengthprot[line[0]]=int(line[5])
if line[2] not in Lengthprot:
Lengthprot[line[2]]=int(line[6])
for key in hits:
kval=collections.Counter(hits[key])
hits[key]=dict([a,-math.log(float(x)/sum(kval.values()))] for a, x in kval.iteritems())
for k1 in hits:
for k2 in hits[k1]:
score1=0
score2=0
try:
score1=hits[k1][k2]
except:
score1=10
try:
score2=hits[k2][k1]
except:
score2=10
score=score1+score2
bhits.append((k1,k2,score))
ghits=collections.OrderedDict()
for p1 in phits.keys():
minval=min([float(phits[p1][p2]) for p2 in phits[p1].keys()])
uplimit=float(minval*10**10)
check=0
for p2 in phits[p1]:
if float(phits[p1][p2]) <=uplimit:
if p1 not in ghits.keys():
ghits[p1]={}
ghits[p1][p2]=float(phits[p1][p2])
else:
ghits[p1][p2]=float(phits[p1][p2])
if float(phits[p1][p2])==minval and check==0:
print p1,p2
check=1
betterHits=collections.OrderedDict()
pairs=[]
# scaffolds_score=sorted(bhits.items(),key=lambda kv: kv[1], reverse=True)
# for k in bhits:
# print k, bhits[k]
return
for g in ghits.keys():
score=15.0
bh=None
if pbidict[g] not in betterHits.keys():
betterHits[pbidict[g]]={}
# print 'P1: ', g
for g2 in ghits[g].keys():
# print 'P2',g2, ghits[g][g2],hits[pbidict[g]][pbidict[g2]]
if hits[pbidict[g]][pbidict[g2]]<score:
bh=g2
score=hits[pbidict[g]][pbidict[g2]]
#print 'Best Hit: ',g,bh, Scaffolds: ,pbidict[g],pbidict[bh]
# print g,bh, 'Scaffolds: ',pbidict[g],pbidict[bh]
pairs.append((g,bh))
# print ' ',pbidict[g],pbidict[bh]
if pbidict[bh] not in betterHits[pbidict[g]].keys():
betterHits[pbidict[g]][pbidict[bh]]=[(g,bh)]
else:
betterHits[pbidict[g]][pbidict[bh]].append((g,bh))
# return
getCts(pairs)
# print getDistrib(pairs,Lengthprot)
for k in betterHits.keys():
for g in betterHits[k].keys():
#for g in ghits[k].keys():
htsc=sorted(betterHits[k][g],key=lambda tup: (tup[0]))
gh=[]
# print k,g#,'\n'
#print htsc
'''
s1=[i[0] for i in htsc]
s2=[i[1] for i in htsc]
if len(htsc)==len(set(s1)) and len(htsc)==len(set(s2)):
# print len(htsc), len(set(s1)), len(set(s2))
#print ' '.join(list(htsc))
#prTp(htsc)
# print 'case 1'
printht(htsc)
#getscaf(htsc)
# continue
# if len(set(s1))==1 and len(set(s2))==1:
# print 'singleton: ', htsc
# if len(set(s1))==2 and len(set(s2))==2:
# print ' duo: ', htsc
elif len(htsc)!=len(set(s1)) and len(htsc)!=len(set(s2)):
# print 'case 2'
getdupboth(htsc)
else:
if len(htsc)==len(set(s1)):
# print 'case 3'
getdup2(htsc)
elif len(htsc)==len(set(s2)):
# print 'case 4'
getdup1(htsc)
#print len(htsc), len(set(s1)), len(set(s2))
#print htsc
'''
# print sorted(betterHits[k][g],key=lambda tup: (tup[0]))
# print betterHits[g][k]
#return
# return sorted(bhits, key=lambda tup: (tup[2]) )#bhits
## get scaffolds dictionary
def readScaf(psl):
scaffolds={}
filename=open(psl,"r")
for line in filename:
line=line.rstrip().split()
if line[1] in scaffolds and line[3] in scaffolds[line[1]]:
scaffolds[line[1]][line[3]]+=[(line[0],line[2],line[-2])]
else:
scaffolds[line[1]]={line[3]:[(line[0],line[2],line[-2])]}
#Read he gff3 files and create a dictionary: scaffolds: list of proteins
def readgff(f):
filename = open(f, "r")
scaffolds={}
pbi={}
for line in filename:
line=line.strip().split()
if line[0][:8]=='scaffold' and line[2]=='mRNA':
pbi[re.split(r'[=;\s]',line[8])[1]]=line[0]
return pbi
#if __name__ == '__main__':
ProcessCLI(sys.argv)