-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpmanalysis.py
301 lines (205 loc) · 8.73 KB
/
pmanalysis.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
#######################################################################
#
# PairMaP object and related tools for post-analysis of PAIR-MaP data
#
# Anthony Mustoe
# Copyright 2018
#
# This file is licensed under the terms of the MIT license
#
# Change-log
#
#
#######################################################################
class PairMap(object):
"""This class is a container for correlations yielded by a RING experiment"""
def __init__(self, corrfile=None):
if corrfile is not None:
self.readPairFile(corrfile)
def readPairFile(self, corrfile):
self.sourcename = corrfile
self.primary = []
self.secondary = []
self.remainder = []
with open(corrfile) as inp:
header = inp.readline().split()
self.molsize = int(header[0])
self.window = int(header[1].split('=')[1])
# pop off the second header line
inp.readline()
for line in inp:
spl = line.split()
ptype = int(spl[3])
c = (int(spl[0]), int(spl[1]), float(spl[2]), float(spl[4]))
if ptype == 1:
self.primary.append(c)
elif ptype == 2:
self.secondary.append(c)
else:
self.remainder.append(c)
#def ppvsens(self, corrlist):
#
# exit('ppvsens not working')
#
# if self.knownstructure is None:
# raise AttributeError('knownstructure has not been initialized!')
#
# # calculate how many non-repetitive pairs match known pairs
# totalknown = 0.0
# for c in corrs_nr:
# if self.knownstructure.ct[c[0]-1] == c[1]:
# totalknown += 1
#
# knownpairs = len(self.knownstructure.pairList())
#
# sens = totalknown / knownpairs
# ppv = totalknown / len(corrs_nr)
#
# return ppv, sens
#
def ppvsens_duplex(self, ctobj, ptype=1, exact=False, profile=None, mask=False, verbal=False, printFP=False):
"""Compute ppv&sens from a duplex perspective relative to reference ct
ptype = 1/2/3/0
1 -> only primary
2 -> only secondary
3 -> 1+2
0 -> everything (including remainder)
exact = True/False
if False, allow duplex to be shifted +/-1 from correct register
profile = if provided, reactivity profile is used to define regions with
no data that are then excluded from ppv/sens calcs
mask = True will exclude masked regions in ctobj from ppv/sens calcs
"""
if mask:
print(len(ctobj.mask), len(ctobj.mask)-sum(ctobj.mask))
if exact:
allowedoffset = 0
else:
allowedoffset = 1
corrlist = []
if ptype == 1:
corrlist = self.primary
elif ptype == 2:
corrlist = self.secondary
elif ptype == 3:
corrlist = self.primary + self.secondary
elif ptype == 0:
corrlist = self.primary + self.secondary + self.remainder
else:
raise ValueError('ptype={} is not supported. Please select 1/2/3/0'.format(ptype))
predpairs = []
for c in corrlist:
pair = (c[0], c[1])
if not (mask and masked(pair, ctobj.mask, self.window)):
predpairs.append(pair)
elif verbal:
print('Skipped {0}'.format(pair))
predpairs = set(predpairs)
# for sensitivity calculation, get list of helices
helices = ctobj.extractHelices(fillPairs=False)
# need to shift helix indices to match window-shifting in PairMap
shifted_helices = {}
knowndup = []
for h,pairs in helices.items():
temphelix = []
for p in pairs[:-(self.window-1)]:
shiftedpair = (p[0], p[1]-self.window+1)
# skip if masked
if mask and masked(shiftedpair, ctobj.mask, self.window):
continue
if profile is None or hasdata(shiftedpair, profile, self.window):
temphelix.append(shiftedpair)
# this deals with helices smaller than the window
# for calculations, add in "dummies" that starts and end 1 bp upstream/downstream
if len(pairs) < self.window:
shiftedpair = (pairs[0][0]-1, pairs[0][1]-self.window+2)
# skip if masked
if mask and masked(shiftedpair, ctobj.mask, self.window):
pass
elif profile is None or hasdata(shiftedpair, profile, self.window):
temphelix.append(shiftedpair)
shiftedpair = (pairs[0][0], pairs[0][1]-self.window+1)
if mask and masked(shiftedpair, ctobj.mask, self.window):
pass
elif profile is None or hasdata(shiftedpair, profile, self.window):
temphelix.append(shiftedpair)
if len(temphelix) > 0:
knowndup.extend( temphelix )
shifted_helices[h] = temphelix
if verbal:
print(h, temphelix)
elif verbal:
print(h, 'skipped', pairs)
# print "WARNING: Helix starting at pair {0} skipped because of no data".format(pairs[0])
knowndup = set(knowndup)
# compute sens
senset = set()
for h in shifted_helices:
for c in shifted_helices[h]:
if c in predpairs or nonexactMatch(c, predpairs, allowedoffset):
senset.add(h)
break
# compute ppv
ppvset = set()
for c in sorted(predpairs):
if c in knowndup or nonexactMatch(c, knowndup, allowedoffset):
ppvset.add(c)
elif printFP:
print('FP: {}'.format(c))
if len(predpairs)==0:
ppv = 0.0
else:
ppv = float(len(ppvset))/len(predpairs)
if len(shifted_helices)==0:
sens = 0.0
else:
sens = float(len(senset))/len(shifted_helices)
return ppv, sens
def nonexactMatch(corr, corrlist, allowedoffset=0):
for x in range(-allowedoffset, allowedoffset+1):
for y in range(-allowedoffset, allowedoffset+1):
if (corr[0]+x,corr[1]+y) in corrlist:
return True
return False
def hasdata(pair, profile, window):
missing = [0,0]
for i in range(window):
r0 = profile[pair[0]-1+i] # pair is 1-indexed, profile isn't
r1 = profile[pair[1]-1+i]
if r0!=r0 or r0<-10:
missing[0] += 1
if r1!=r1 or r1<-10:
missing[1] += 1
if max(missing) == window:
return False
else:
return True
def masked(pair, mask, window):
s1 = sum(mask[pair[0]-1:pair[0]-1+window])
s2 = sum(mask[pair[1]-1:pair[1]-1+window])
if max(s1,s2) > window/2:
return True
else:
return False
if __name__ == '__main__':
import argparse
import RNAtools2 as RNAtools
prs = argparse.ArgumentParser()
prs.add_argument('pmfile', help='path of pair mapper file')
prs.add_argument('ctfile', help='path of reference ct file')
prs.add_argument('--ptype', default=1, type=int, help='Which correlations to compute Sens/PPV for. Options are 1 (primary only;default), 2 (secondary only), 3 (primary and secondary), 0 (everything; not recommended)')
prs.add_argument('--dms', help='path of dms reactivity file')
prs.add_argument('--mask', action='store_true',help='Mask out CT-specified regions when computing ppv/sens')
prs.add_argument('--verbal', action='store_true',help='Print each correlation and its status')
args = prs.parse_args()
pm = PairMap(args.pmfile)
ct = RNAtools.CT(args.ctfile, filterNC=True, filterSingle=True)
if args.dms:
profile,seq = RNAtools.readSHAPE(args.dms)
if len(profile)!=len(ct.ct):
raise IndexError('Profile file not the same length as the CT!')
else:
profile = None
p,s = pm.ppvsens_duplex(ct, ptype=args.ptype, exact=False, profile=profile, mask=args.mask, verbal=args.verbal)
print("PPV={0:.0f} Sens={1:.0f}".format(p*100, s*100))
#print pm.ppvsens_duplex(ct, ptype=1, exact=True, profile=profile)