-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paralogs.py
166 lines (140 loc) · 6.71 KB
/
Paralogs.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
import simplejson
import globals
filehash = globals.json('Paralogsfilehash', './dbase/')
class paralogs:
def __init__(self, orfs):
self.orfs = orfs
def getalignindex(self):
if hasattr(self, 'alignment') and getattr(self, 'alignment').keys().__len__() == 2:
print 'alignindex:' + str(self.orfs[0].orfname) + str(self.orfs[1].orfname)
alignindex = {}
dashes = 0
i = 0
alignseq = getattr(self, 'alignment')
alignindex[self.orfs[0].orfname] = {}
while i < len(self.orfs[0].seq):
if alignseq[self.orfs[0].orfname][i+dashes] == '-':
dashes = dashes + 1
else:
alignindex[self.orfs[0].orfname][i] = dashes+i
i = i+1
alignindex[self.orfs[1].orfname] = {}
i = 0
dashes = 0
while i < len(self.orfs[1].seq):
if alignseq[self.orfs[1].orfname][i+dashes] == '-':
dashes = dashes + 1
else:
alignindex[self.orfs[1].orfname][i] = dashes+i
i = i+1
setattr(self, 'alignindex', alignindex)
def getphosphosites(self):
setattr(self, 'phosphosites', {})
print 'phosphosite:'+ str(self.orfs[0].orfname)
print 'phosphosite:' + self.orfs[1].orfname
for i in range(0,len(self.orfs)):
if hasattr(self.orfs[i], 'phosphosite') and hasattr(self,
'alignindex'):
self.phosphosites[self.orfs[i].orfname] = map(lambda x: self.alignindex[self.orfs[i].orfname][x], self.orfs[i].phosphosite)
def getdisorderedpsites(self):
setattr(self, 'disorderedpsites', {})
for i in range(0, len(self.orfs)):
if hasattr(self.orfs[i], 'disorderedpsites') and hasattr(self,
'alignindex'):
self.disorderedpsites[self.orfs[i].orfname] = map(lambda x:
self.alignindex[self.orfs[i].orfname][x], self.orfs[i].disorderedpsites)
def getorderedpsites(self):
setattr(self, 'orderedpsites', {})
for i in range(0, len(self.orfs)):
if hasattr(self.orfs[i], 'orderedpsites') and hasattr(self,
'alignindex'):
self.orderedpsites[self.orfs[i].orfname] = map(lambda x:
self.alignindex[self.orfs[i].orfname][x], self.orfs[i].orderedpsites)
def getsyt(self):
if hasattr(self, 'alignindex'):
sytpos = {}
setattr(self, 'sytpos', sytpos)
for i in self.orfs:
self.sytpos[i.orfname] = (map(lambda x: self.alignindex[i.orfname][x], i.sytpos))
def psitedivfun(self):
if hasattr(self, 'phosphosites'):
if len(self.phosphosites.values()) == 1:
setattr(self, 'psitediv', 1)
print self.orfs[0].orfname
print self.orfs[1].orfname
if len(self.phosphosites.values()) == 2:
l = self.phosphosites.values()[0].__len__() + self.phosphosites.values()[1].__len__()
incommon = list(set(self.phosphosites.values()[0]) & set(self.phosphosites.values()[1]))
print incommon
print l-2*len(incommon)
print float(l-len(incommon))
setattr(self, 'psitediv', (l-2*len(incommon))/float(l-len(incommon)))
def alignmentstats(self):
if hasattr(self, 'alignment'):
o1 = self.orfs[0]
o2 = self.orfs[1]
seqcons = map(lambda x,y: x == y, self.alignment[o1.orfname], self.alignment[o2.orfname])
sytconswithinSYT = list(set(self.sytpos[o1.orfname]) & set(self.sytpos[o2.orfname]))
sytcons1 = ([x for x in self.sytpos[o1.orfname] if self.alignment[o1.orfname][x] == self.alignment[o2.orfname][x]])
sytcons2 = ([x for x in self.sytpos[o2.orfname] if self.alignment[o1.orfname][x] == self.alignment[o2.orfname][x]])
setattr(self, 'sytconswithinsyt', sytconswithinSYT)
setattr(self, 'seqcons', seqcons)
setattr(self, 'sytcons', sytcons1 + sytcons2)
setattr(self, 'numseqcons', sum(seqcons))
setattr(self, 'sumseqlength', len(o1.seq)+len(o2.seq))
setattr(self, 'seqdiff', 1-(self.numseqcons/float(self.sumseqlength/2)))
setattr(self, 'numsytcons', len(list(set(self.sytcons))))
setattr(self, 'numsytconswithinsyt', len(list(set(sytconswithinSYT))))
setattr(self, 'avgsyt', (len(self.sytpos[o1.orfname])+len(self.sytpos[o2.orfname])/2))
setattr(self, 'sytdiff', 1-(float(self.numsytcons)/self.avgsyt))
def phosphostats(self):
o1 = self.orfs[0]
o2 = self.orfs[1]
if hasattr(self, 'phosphosites'):
if len(self.phosphosites.keys()) != 2:
setattr(self, 'phosphositecons', 0)
else:
phosphositecons = len(list(set(self.phosphosites[o1.orfname]) &
set(self.phosphosites[o2.orfname])))
(setattr(self, 'phosphositecons', phosphositecons/
(float((len(self.phosphosites[o1.orfname]) +
len(self.phosphosites[o2.orfname])))/2)))
def getevents(self, data):
key = self.orfs[0].orfname + '_' + self.orfs[1].orfname
setattr(self, 'Wevent', data[key])
def initall(objects, orfs, parset):
plist = globals.json(filehash[parset])
for par in plist:
objects[(par[0], par[1])] = paralogs((orfs[par[0]], orfs[par[1]]))
def calcdivergence(objects, seqcons, sytcons, psitecons):
for p in objects.keys():
par = objects[p]
o1 = par.orfs[0]
o2 = par.orfs[1]
if o1.orthos['Kwal'] == 'NONE' or o2.orthos['Kwal'] == 'NONE':
continue
setattr(par, 'minorfseqdiv', min(1-getattr(o1,seqcons), 1-getattr(o2,seqcons)))
setattr(par, 'maxorfseqdiv', max(1-getattr(o1,seqcons), 1-getattr(o2,seqcons)))
setattr(par, 'minorfsytdiv', min(1-getattr(o1,sytcons), 1-getattr(o2,sytcons)))
setattr(par, 'maxorfsytdiv', max(1-getattr(o1,sytcons), 1-getattr(o2,sytcons)))
if hasattr(o1, psitecons) and hasattr(o2, psitecons):
setattr(par, 'minorfphosphodiv', min(1-getattr(o1,psitecons), 1-getattr(o2,psitecons)))
setattr(par, 'maxorfphosphodiv', max(1-getattr(o1,psitecons),1-getattr(o2,psitecons)))
def getalign(par, alignments):
alignment = {}
name1 = par.orfs[0].orfname + '_' + par.orfs[1].orfname
name2 = par.orfs[1].orfname + '_' + par.orfs[0].orfname
if name1 in alignments.keys():
name = name1
elif name2 in alignments.keys():
name = name2
else:
print par.orfs[0].orfname
print par.orfs[1].orfname
return
alignment = alignments[name]
setattr(par, 'alignment', alignment)
def reminstances(objects, attr):
for p in objects.keys():
if not hasattr(objects[p], attr):
del objects[p]